oop2
oop2
oop2
Example:
#include<iostream>
int main()
int x=10;
int &y=x; // y is reference variable
cout<<x<<endl;
++y;
cout<<x;
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
Question:
#include<iostream>
#include<conio.h>
{
int temp=a;
a=b;
b=temp;
Syntax:
// function
inline declaration
return-type function-name(parameters);
#include<iostream>
int main()
int a,b;
cin>>a>>b;
int sum=add(a,b);
cout<<" "<<sum;
return x+y;
Example :
#include<iostream>
using namespace std;
int add(int,int,int=0);
int main()
int a,b,c;
cin>>a>>b;
cout<<endl<<add(a,b)<<endl;
cin>>a>>b>>c;
cout<<endl<<"sum is "<<add(a,b,c)<<endl;
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:
int add(int a)
float add(double a)
Example 2:
#include <iostream>
cout << " and double number: " << var2 << endl;
}
void display(int var) {
int main() {
int a = 5;
double b = 5.5;
display(a);
display(b);
display(a, b);
return 0;
}
this Pointer
#include<iostream>
class Test
private:
int x;
public:
this->x = x;
};
int main()
{
Test obj;
int x = 20;
obj.setX(x);
obj.print();
return 0;