Showing posts with label OOP. Show all posts
Showing posts with label OOP. Show all posts

Friend function in C++

Friend function in C++



  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. class Shape;///Create forward reference of Class Shape  
  5.   
  6. class Area{///Create the class Area which member function be friend of class Shape  
  7. public:  
  8.     void getArea(Shape obj);  
  9. };  
  10.   
  11. class Shape{///Create class Shape  
  12. private:  
  13.     int length;  
  14.     int width;  
  15.     int height;  
  16. public:  
  17.     Shape(int a,int b,int c){  
  18.         length=a;  
  19.         width=b;  
  20.         height=c;  
  21.     }  
  22.     friend void Area::getArea(Shape obj);  
  23. };  
  24. ///Define friend function  
  25. void Area::getArea(Shape obj){  
  26.     cout<<"Area: "<<obj.length*obj.width<<endl;  
  27. }  
  28.   
  29. int main()  
  30. {  
  31.     Shape shape(3,4,5);  
  32.     Area area;  
  33.     area.getArea(shape);  
  34.     return 0;  
  35. }  
Share:

Friend class in C++

Friend class in C++



  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. class Shape{  
  5. private:  
  6.     int length;  
  7.     int width;  
  8.     int height;  
  9. public:  
  10.     Shape(int a,int b,int c){  
  11.         length=a;  
  12.         width=b;  
  13.         height=c;  
  14.     }  
  15.     friend class Area;///Friend class Area  
  16. };  
  17.   
  18. class Area{  
  19. public:  
  20.     void getArea(Shape obj){  
  21.         cout<<"Area: "<<obj.length*obj.width<<endl;   ///Since Area is friend of Shape, it can access private members of Shape.  
  22.     }  
  23. };  
  24.   
  25. int main()  
  26. {  
  27.     Shape shape(2,4,6);  
  28.     Area area;  
  29.     area.getArea(shape);  
  30.     return 0;  
  31. }  
Share:

Polymorphism in c++

Polymorphism in c++


Function overloading in c++:


  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. class Shape{  
  5. public:  
  6.     void add(int a,int b){  
  7.         cout<<"Sum: "<<a+b<<endl;  
  8.     }  
  9.     void add(double a,double b){  
  10.         cout<<"Sum: "<<a+b<<endl;  
  11.     }  
  12.     void add(string a,string b){  
  13.         cout<<"Sum: "<<a+b<<endl;  
  14.     }  
  15. };  
  16.   
  17. int main()  
  18. {  
  19.     int a=5,b=10;  
  20.     double x=3.5,y=6.7;  
  21.     string s1="hello",s2=" world!";  
  22.   
  23.     Shape obj;  
  24.     obj.add(a,b);  
  25.     obj.add(x,y);  
  26.     obj.add(s1,s2);  
  27.     return 0;  
  28. }  


Operator Overloading in C++:


  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. class Volume{  
  5. private:  
  6.     int length;  
  7.     int width;  
  8.     int height;  
  9. public:  
  10.     Volume(){  
  11.         length=0;  
  12.         width=0;  
  13.         height=0;  
  14.     }  
  15.     Volume(int a,int b,int c){  
  16.         length=a;  
  17.         width=b;  
  18.         height=c;  
  19.     }  
  20.     Volume operator +(Volume vol2){/// Operator overloading  
  21.         Volume temp;  
  22.         temp.length=length+vol2.length;  
  23.         temp.width=width+vol2.width;  
  24.         temp.height=height+vol2.height;  
  25.         return temp;  
  26.     }  
  27.     Volume operator +(int x){/// Operator overloading  
  28.         Volume temp;  
  29.         temp.length=length+x;  
  30.         temp.width=width+x;  
  31.         temp.height=height+x;  
  32.         return temp;  
  33.     }  
  34.     void getVolume(){  
  35.         cout<<"Length: "<<length<<endl;  
  36.         cout<<"Width: "<<width<<endl;  
  37.         cout<<"Height: "<<height<<endl;  
  38.     }  
  39. };  
  40.   
  41.   
  42. int main()  
  43. {  
  44.     int n=5;  
  45.     Volume vol1(2,3,4);  
  46.     Volume vol2(1,2,3);  
  47.     Volume vol3;  
  48.     vol3=vol1+vol2;///Add two object  
  49.     //vol3=vol1+n;///Add variable with object  
  50.     vol3.getVolume();  
  51.     return 0;  
  52. }  

Share:

Inheritance With Base Class Parameterized Constructor in C++

Inheritance With Base Class Parameterized Constructor in C++


  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. class Area  
  5. {  
  6. private:  
  7.     int length;  
  8.     int width;  
  9. public:  
  10.     Area(int a,int b){  
  11.         length=a;  
  12.         width=b;  
  13.     }  
  14.     int getArea(){  
  15.         return length*width;  
  16.     }  
  17. };  
  18.   
  19. class Volume:public Area  
  20. {  
  21. private:  
  22.     int height;  
  23. public:  
  24.     Volume(int a,int b,int c):Area(a,b){ /// Call constructor of the base class with parameters from constructor of the derived class  
  25.         height=c;  
  26.     }  
  27.     void getVolume(){  
  28.         cout<<"Volume : "<<height*getArea()<<endl;  
  29.     }  
  30. };  
  31.   
  32.   
  33. int main()  
  34. {  
  35.     Volume volume(3,5,7);  
  36.     volume.getVolume();  
  37.     return 0;  
  38. }  
Share: