Comparison between C and C++
example :
int main()
{
int x =5;
int &y = x;
y++;
cout<<x;
}
Output:
6
Note:
Destructor:
Example:
class A
{
private:
int a;
public:
void setdata(int x)
{a =x;}
A operator+(A x)
{
A c;
c.a = a+x.a;
return c;
}
void showdata()
{cout<<a<<endl;}
};
int main()
{
A obj1,obj2,obj3;
obj1.setdata(2);
obj2.setdata(3);
obj3 = obj1 + obj2; // now two non primitive type are been added
obj3.showdata();
return 0;
}
Note :
obj1 + obj2 can also be written as obj1.operator+(obj2)
Example:
class comp
{
private:
int a,b;
public:
comp(){a=0;b=0;}
void setdata(int x,int y)
{a = x; b = y;}
friend comp add(comp, comp);
void showdata()
{cout<<a<<"+j"<<b;}
};
comp add(comp x,comp y)
{
comp o;
o.a = x.a + y.a;
o.b = y.b + y.b;
return o;
}
int main()
{
comp obj1,obj2,obj3;
obj1.setdata(5,6);
obj2.setdata(6,7);
obj3 = add(obj1,obj2);
obj3.showdata();
return 0;
}
It looks like you're new here. If you want to get involved, click one of these buttons!