oop2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Reference Variable

1. A reference variable is one that refers to the reference of another


variable.
2. The variable reference is nothing but an alternate name for the already
existing variable.
3. When a variable is declared as a reference, it becomes an alternative
name for an existing variable.
4. A variable can be declared as a reference by putting ‘&’ in the
declaration.
5. Reference variables cannot be updated.
6. Reference variable is an internal pointer.
7. Declaration of a Reference variable is preceded with the ‘&’ symbol ( but
do not read it as “address of”).
8. A reference must be initialized when declared.
9. References cannot be NULL.
10. Once a reference is created, it cannot be later made to
reference another object; it cannot be reset.
Syntax: data_type &ref = variable;

Example:

#include<iostream>

using namespace std;

int main()

int x=10;
int &y=x; // y is reference variable
cout<<x<<endl;

++y;

cout<<x;

Difference beetween Pointer and Reference Variable


A pointer is a variable that holds the memory address of another
variable.

A reference variable is an alias, that is, another name for an already


existing variable.
A reference, like a pointer, is also implemented by storing the
address of variable.

1. A pointer can be initialized in this way:


int a = 10;
int *p = &a;
// OR
int *p;
p = &a;

We can declare and initialize pointer at same step or in multiple line.

While in references,
int a = 10;
int &p = a; // It is correct
// but
int &p;
p = a; // It is incorrect as we should declare and initialize
references at single step

2. A pointer can be re-assigned.


example:
int a = 5;
int b = 6;
int *p;
p = &a;
p = &b;

On the other hand,


a reference cannot be re-assigned, and must be assigned at
initialization.
int a = 5;
int b = 6;
int &p = a;
int &p = b; // error

3. A pointer can be assigned NULL directly, whereas a reference


cannot be.

Question:

Write a program to swap two number using call by reference and


Call by Adress

#include<iostream>

using namespace std;

#include<conio.h>

void swap(int &a,int &b) // Call by reference

{
int temp=a;

a=b;

b=temp;

void swap(int *a,int *b) // Call by Adress


{
int temp=*a;
*a=*b;
*b=temp;
}
int main()
{
int x,y;
cout<<"Enter Two numbers:\n";
cin>>x>>y;
cout<<endl<<"Before Swapping:"<<endl;
cout<<"x="<<x<<" y="<<y;
swap(x,y);
cout<<endl<<"After Swapping:"<<endl;
cout<<"x="<<x<<" y="<<y;
getch();
}
inline function

1. inline is a request not a command.


2. inline is a keyword.
3. An inline function is a function that is expanded in line when it is called.
4. When the inline function is called whole code of the inline function gets
inserted or substituted at the point of the inline function call.
5. This substitution is performed by the C++ compiler at compile time.
6. An inline function may increase efficiency if it is small.

Syntax:

// function
inline declaration
return-type function-name(parameters);

The compiler may not perform inlining in such circumstances


as:

1. If a function contains a loop. (for, while and do-while)


2. If a function contains static variables.
3. If a function is recursive.
4. If a function contains a switch or goto statement.

Example for inline function :

#include<iostream>

using namespace std;


inline int add(int , int);

int main()

int a,b;

cout<<"Enter two number:\n";

cin>>a>>b;

int sum=add(a,b);

cout<<" "<<sum;

int add(int x, int y)

return x+y;

Default Arguments (Parameters)


1. In C++ programming, we can provide default values
function parameters.
2. If a function with default arguments is called without passing arguments,
then the default parameters are used.
3. However, if arguments are passed while calling the function, the default
arguments are ignored.

Example :

#include<iostream>
using namespace std;

int add(int,int,int=0);

int main()

int a,b,c;

cout<<"Enter two numbers:"<<endl;

cin>>a>>b;

cout<<endl<<add(a,b)<<endl;

cout<<" Enter three numbers:"<<endl;

cin>>a>>b>>c;

cout<<endl<<"sum is "<<add(a,b,c)<<endl;

int add(int x,int y,int z){

return x+y+z;

Function Overloading
 In C++, two functions can have the same name if the number
of arguments or type of arguments passed is different.
 These functions having the same name but different
arguments are known as overloaded functions.
For example:

// same name different arguments


int add()

int add(int a)

float add(double a)

int add(int a, double b)

Example 2:
#include <iostream>

using namespace std;

void display(int var1, double var2) {

cout << "Integer number: " << var1;

cout << " and double number: " << var2 << endl;

void display(double var) {

cout << "Double number: " << var << endl;

}
void display(int var) {

cout << "Integer number: " << var << endl;

int main() {

int a = 5;

double b = 5.5;

// call function with int type parameter

display(a);

// call function with double type parameter

display(b);

// call function with 2 parameters

display(a, b);

return 0;

}
this Pointer

 this is a keyword that refers to the current instance of the class.


 It can be used to pass current object as a parameter to another method.
 It can be used to refer current class instance variable.
 It can be used to declare indexers.

#include<iostream>

using namespace std;

class Test

private:

int x;

public:

void setX (int x)

this->x = x;

void print() { cout << "x = " << x << endl; }

};

int main()

{
Test obj;

int x = 20;

obj.setX(x);

obj.print();

return 0;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy