1class base
2{
3 public:
4 int x;
5 protected:
6 int y;
7 private:
8 int z;
9};
10
11class publicDerived: public base
12{
13 // x is public
14 // y is protected
15 // z is not accessible from publicDerived
16};
17
18class protectedDerived: protected base
19{
20 // x is protected
21 // y is protected
22 // z is not accessible from protectedDerived
23};
24
25class privateDerived: private base
26{
27 // x is private
28 // y is private
29 // z is not accessible from privateDerived
30}
31
1Inheritance is one of the key features of Object-oriented programming in C++.
2It allows us to create a new class (derived class) from an existing class (base class).
3
4The derived class inherits the features from the base class and can have additional features of its own.
5 For example,
6
7class Animal {
8 // eat() function
9 // sleep() function
10};
11
12class Dog : public Animal {
13 // bark() function
14};
15Here, the Dog class is derived from the Animal class.
16Since Dog is derived from Animal, members of Animal are accessible to Dog.
17
18Notice the use of the keyword public while inheriting Dog from Animal.
19
20class Dog : public Animal {...};
21We can also use the keywords private and protected instead of public
22
23Example:
24// C++ program to demonstrate inheritance
25
26#include <iostream>
27using namespace std;
28
29// base class
30class Animal {
31
32 public:
33 void eat() {
34 cout << "I can eat!" << endl;
35 }
36
37 void sleep() {
38 cout << "I can sleep!" << endl;
39 }
40};
41
42// derived class
43class Dog : public Animal {
44
45 public:
46 void bark() {
47 cout << "I can bark! Woof woof!!" << endl;
48 }
49};
50
51int main() {
52 // Create object of the Dog class
53 Dog dog1;
54
55 // Calling members of the base class
56 dog1.eat();
57 dog1.sleep();
58
59 // Calling member of the derived class
60 dog1.bark();
61
62 return 0;
63}
64Output
65
66I can eat!
67I can sleep!
68I can bark! Woof woof!!
69Here, dog1 (the object of derived class Dog) can access members of the base class Animal.
70It's because Dog is inherited from Animal.
1#include <iostream>
2
3// Example in a game we have multiple entities so we put commom functionality and variables in base class Entity and Create Sub Classes Of the base class
4class Entity {
5 //This is a base class of all entities
6public:
7 float x =0 , y = 0;//this is the position of entity
8 void Move(float xa, float ya) {
9 x += xa;
10 y += ya;
11 //this function moves entity
12 }
13};
14// in this example Player inherits from public entity
15class Player:public Entity// inhertiting From Entity class
16{
17 // Player class is a Sub class of Entity
18 //Player Class ha all the functions and var of public entity + some additional functionality and variables it is a superset of Entity
19
20
21public :
22 const char* name = nullptr;
23 void Print() {
24 std::cout << name << std::endl;
25 }
26 //Player class has type of palyer and type of entity
27 //Because it has additional method Print and var name
28 //We can create entity from palyer because player has everything of entity but we can't create an Entity from player because it has additional things
29};
30int main()
31{
32 Player D;
33 D.x = 5.5f;//initializing inherited variable
34 D.y = 4.4f;//initializing inherited variable
35 D.Move(1.1f,2.2f);//Calling inherited method
36 D.name = "Caleb";//initializing variable owned by player class
37 D.Print();//calling method owned by Player class
38 //Now looking at the size of each class
39 std::cout <<"Size of Entity was : " << sizeof(Entity) << std::endl;
40 std::cout <<"Size of Player was : "<< sizeof(Player) << std::endl;
41 //size of Entity output => 8
42 //size of Player output => 12
43 //because Entity has 2 floats = 4bytes +4 bytes =8 bytes
44 //Class Player has 2floats and const char ptr which is 4 bytes for 32 bit application = (4 +4 + 4)bytes = 12bytes
45 //Note:At the end inheretance is just a way to prevent code duplication
46 std::cin.get();
47}