C++ Class

>Abstraction

>Encapsulation /Access Modifiers


>Inheritance


  1. #include <iostream>
  2. using namespace std;

  3. class vehicle
  4. {
  5. public:
  6.     vehicle()
  7.     {
  8.         cout<<"this is a constructor of vehicle"<<endl;
  9.     }
  10.     ~vehicle()
  11.     {
  12.         cout<<"this is a deconstructor of vehicle"<<endl;
  13.     }
  14. };

  15. class fourwheeler
  16. {
  17. public:
  18.     fourwheeler()
  19.     {
  20.         cout<<"this is the constructor of fourwheeler"<<endl;
  21.     }
  22.     fourwheeler(string x)
  23.     {
  24.         cout<<"This is another constructor which has "<<x<<" with it"<<endl;
  25.     }
  26.     ~fourwheeler()
  27.     {
  28.         cout<<"this is deconstructor of fourwheeler"<<endl;
  29.     }
  30. };

  31. //Single Inheritance

  32. class Car: public vehicle
  33. {
  34.     public:
  35.     Car()
  36.     {
  37.         cout<<"this is a derrived child class from vehicle 's constructor"<<endl;
  38.     }
  39.     ~Car()
  40.     {
  41.         cout<<"this is a deconstructor of Car which is derrived from vehicle"<<endl;
  42.     }
  43. };

  44. //multiple inheritance

  45. class nuhash: public vehicle,public fourwheeler
  46. {
  47.     public:
  48.     nuhash()
  49.     {
  50.         cout<<"this is  a constructor of nuhash"<<endl;
  51.     }
  52.     nuhash(string x)
  53.     {
  54.         cout<<"this is a constructor of nuhash with "<<x<<"with it"<<endl;
  55.     }
  56.     ~nuhash()
  57.     {
  58.         cout<<"this is a deconstructor of nuhash"<<endl;
  59.     }
  60. };

  61. //multilevel inheritance

  62. class nuhan:public nuhash
  63. {
  64. public:
  65.     nuhan()
  66.     {
  67.         cout<<"constructor of nuhan"<<endl;
  68.     }
  69.     ~nuhan()
  70.     {
  71.         cout<<"de constructor of nuhan"<<endl;
  72.     }
  73. };

  74. //multilevel inheritance//derived from nuhan and nuhan is derived from nuhash when nuhash is derived from vehicle and four wheeler

  75. class ira:public nuhan
  76. {
  77. public:
  78.     ira()
  79.     {
  80.         cout<<"constructor of ira"<<endl;
  81.     }
  82.     ~ira()
  83.     {
  84.         cout<<"de constructor of ira"<<endl;
  85.     }
  86. };

  87. // car and nuhash is example of tree inheritance + hybrid as they are both derrived from vehicle

  88. int main()
  89. {
  90.     ira obj;
  91.     return 0;
  92. }



Frined Class and Friend Function


  • Accessing Class's object by another class/class's fucntion/user define functions

  1. #include <iostream>
  2. using namespace std;


  3. //class -> class friend
  4. class nuhash
  5. {
  6. private:
  7.     int password;
  8. public:
  9.     nuhash()
  10.     {
  11.         password=12131313;
  12.     }
  13.     friend class Noman;
  14. };

  15. class Noman
  16. {
  17. private:
  18.     int gf_count;
  19. public:
  20.     Noman()
  21.     {
  22.         gf_count=1;
  23.     }
  24.     void showNoman(nuhash &x)
  25.     {
  26.         cout<<"nuhash's password "<<x.password<<endl;
  27.     }
  28.     void gf()
  29.     {
  30.         cout<<gf_count<<endl;
  31.     }
  32. };

  33. //class -> function friend
  34. class CF;

  35. class UVA
  36. {
  37. private:
  38.     int submission;
  39. public:
  40.     void show(CF& x);
  41. } ;

  42. class CF
  43. {
  44. private:
  45.     int user;
  46. public:
  47.     CF()
  48.     {
  49.         user=100;
  50.     }
  51.     friend void UVA::show(CF& x);
  52.     //class->normal function
  53.     friend int pow(CF &x);
  54. };

  55. void UVA::show(CF &x)
  56. {
  57.     cout<<x.user<<endl;
  58. }

  59. int pow(CF& x)
  60. {
  61.     cout<<x.user*23<<endl;
  62. }

  63. int main()
  64. {
  65.     CF x;
  66.     UVA y;
  67.     y.show(x);
  68.     pow(x);
  69. }


Note:

  • In C++ friend function or class is not inherited
  • Like CR's friend wont be his child's friend and his child's friend will not be his friend
  • Derived class wont have the  friend function the parent had
  • To use the friend function in derived class we have to declare it in derived function again 




মন্তব্যসমূহ

এই ব্লগটি থেকে জনপ্রিয় পোস্টগুলি

lightOJ-1072 Calm Down