C++ Class
>Abstraction
>Encapsulation /Access Modifiers
>Inheritance
- #include <iostream>
- using namespace std;
- class vehicle
- {
- public:
- vehicle()
- {
- cout<<"this is a constructor of vehicle"<<endl;
- }
- ~vehicle()
- {
- cout<<"this is a deconstructor of vehicle"<<endl;
- }
- };
- class fourwheeler
- {
- public:
- fourwheeler()
- {
- cout<<"this is the constructor of fourwheeler"<<endl;
- }
- fourwheeler(string x)
- {
- cout<<"This is another constructor which has "<<x<<" with it"<<endl;
- }
- ~fourwheeler()
- {
- cout<<"this is deconstructor of fourwheeler"<<endl;
- }
- };
- //Single Inheritance
- class Car: public vehicle
- {
- public:
- Car()
- {
- cout<<"this is a derrived child class from vehicle 's constructor"<<endl;
- }
- ~Car()
- {
- cout<<"this is a deconstructor of Car which is derrived from vehicle"<<endl;
- }
- };
- //multiple inheritance
- class nuhash: public vehicle,public fourwheeler
- {
- public:
- nuhash()
- {
- cout<<"this is a constructor of nuhash"<<endl;
- }
- nuhash(string x)
- {
- cout<<"this is a constructor of nuhash with "<<x<<"with it"<<endl;
- }
- ~nuhash()
- {
- cout<<"this is a deconstructor of nuhash"<<endl;
- }
- };
- //multilevel inheritance
- class nuhan:public nuhash
- {
- public:
- nuhan()
- {
- cout<<"constructor of nuhan"<<endl;
- }
- ~nuhan()
- {
- cout<<"de constructor of nuhan"<<endl;
- }
- };
- //multilevel inheritance//derived from nuhan and nuhan is derived from nuhash when nuhash is derived from vehicle and four wheeler
- class ira:public nuhan
- {
- public:
- ira()
- {
- cout<<"constructor of ira"<<endl;
- }
- ~ira()
- {
- cout<<"de constructor of ira"<<endl;
- }
- };
- // car and nuhash is example of tree inheritance + hybrid as they are both derrived from vehicle
- int main()
- {
- ira obj;
- return 0;
- }
Frined Class and Friend Function
- Accessing Class's object by another class/class's fucntion/user define functions
- #include <iostream>
- using namespace std;
- //class -> class friend
- class nuhash
- {
- private:
- int password;
- public:
- nuhash()
- {
- password=12131313;
- }
- friend class Noman;
- };
- class Noman
- {
- private:
- int gf_count;
- public:
- Noman()
- {
- gf_count=1;
- }
- void showNoman(nuhash &x)
- {
- cout<<"nuhash's password "<<x.password<<endl;
- }
- void gf()
- {
- cout<<gf_count<<endl;
- }
- };
- //class -> function friend
- class CF;
- class UVA
- {
- private:
- int submission;
- public:
- void show(CF& x);
- } ;
- class CF
- {
- private:
- int user;
- public:
- CF()
- {
- user=100;
- }
- friend void UVA::show(CF& x);
- //class->normal function
- friend int pow(CF &x);
- };
- void UVA::show(CF &x)
- {
- cout<<x.user<<endl;
- }
- int pow(CF& x)
- {
- cout<<x.user*23<<endl;
- }
- int main()
- {
- CF x;
- UVA y;
- y.show(x);
- pow(x);
- }
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
মন্তব্যসমূহ
একটি মন্তব্য পোস্ট করুন