Inheritance in C++
Composition:-
- A Composition is similar as an inheritance.
- A Composition means writing an object of some another class inside some another class.
- This gives a same effect as an inheritance but there are some functional differences in an inheritance and a composition.
e.g.
class demo
{
public:
int i;
void fun()
{
cout<<”inside the fun of a demo class.”
}
};
class hello
{
public:
int x;
demo d;
void gun()
{
cout<<”inside the gun of a hello class.”;
}
};
int main()
{
demo d1;
hello d2;
d2.d.fun();
d1.fun();
return 0;
}
O/P:-
inside the fun of a demo class.
inside the fun of a demo class.
- In a composition also effect of all access specifiers are applicable for the composite class.
- Memory for the base class is allocated always inside a derived class without considering the type if an inheritance and access specifiers inside the base class.
Basic access specifiers:-
- There are three types of access specifiers namely as,
1) Public
2) Private
3) Protected
- We can write those access specifiers in a class definition in multiple times.
Public:-
- If a members are declared inside the public section that can be accessible outside the class by creating an object, inside the member function of a class , inside the other class, to a friend function or a friend class.
Private:-
- If the members are declared inside a private section then that member is accessible only inside a class.
- Outside any class or a main function cannot access that member.
- A friend function or a friend class has permission to access those members.
Protected:-
- Protected is valid only when there is an inheritance.
- Protected members are accessible inside the class.
- But we cannot access protected members by creating a object of that class.
- Protected members are only accessible inside a derived class.
Combined example of all access specifiers:-
class demo
{
int i; // if there is no access specifier then default access specifier is private.
public:
int j;
base()
{
i=10;
j=20;
k=30;
l=40;
z=60;
} // constructor must be in a public section.
int fun() // this function is in public section.
{
cout<<”Inside the fun.”;
cout<<i; // it is allowed, because, fun is member function and it can access all the
cout<<j; // members of a class without considering the access specifiers.
cout<<k;
cout<<l;
cout<<z;
}
private:
int k;
int gun()
{
cout<<I; // Allowed, because, those are member function.
cout<<j;
cout<<k;
cout<<l;
cout<<z;
}
protected:
int l;
int sun()
{
// code
}
public:
int z; // in this way we can write access specifiers in the same class.
};
class derived:public base
{
public:
int x;
private:
int y;
protected:
int m;
public:
derived()
{
cout<<I; // error
cout<<j; // allowed
cout<<k; // error
cout<<l; // allowed
cout<<z; // allowed
cout<<x; // allowed
cout<<y; // allowed
cout<<m; // allowed. X,y and m are member of a same class.
}
};
int main()
{
cout<<i; // error. We cannot access member of the class without creating object of that class.
base b;
cout<<b.i; // error, because, it is private section.
cout<<b.j; // 20, because, it is in public section
cout<<b.k; // error, because, it is in private section.
cout<<b.l; // error, because, it cannot access protected member outside the class.
cout<<b.z; // 50.
c.fun(); // allowed, because, fun is public.
b.gun(); // error, because, gun is private, we can call gun() by calling it from fun() function but we cannot call directly.
b.sun(); // error, because, it is in protected section and we cannot call protected member outside the class.
derived d;
d.fun(); // allowed, because, it is public in a base class.
d.gun(); // error, because, it is private in a base class.
d.sun(); // allowed, because, it is protected in a base class means public for derived and private for other classes.
}
Types of an inheritance depend on access specifiers:-
- In this case we are deriving a base class into the derived class publically.
e.g.
class base
{
public:
int i;
private:
int j;
protected:
int k;
};
class derived:public base
{
public:
int x;
private:
int y;
protected:
int z;
};
int main()
{
base b;
derived d;
cout<<sizeof(b); // 12
cout<<sizeof(d); // 24
d.i=100;
d.i=10; // allowed
- i is a member which is in public section of the base class and we are deriving a base class using public inheritance that’s why member of base class become public member of derived class.
- j is a private member of a base class and we cannot access private member of a base class in any type of an inheritance.
- k is protected member of a base class and we can access that k is only inside the derived class.
- Protected members are public for derived class and private for all other classes.
d.j=20; // error.
d.k=30; // error, because, we cannot access protected members by creating an object so it gives an error.
b.x=40; // allowed.
b.y=50; // error.
b.z=60; // error.
return 0;
}
- Memory layout of a base class is as follows,
i = 4 bytes
j = 4 bytes
k = 4 bytes
total = 12 bytes
- Memory layout of a derived class is as follows,
i = 4 bytes // base class members
j = 4 bytes // base class members
k = 4 bytes // base class members
x = 4 bytes // derived class members
y = 4 bytes // derived class members
z = 4 bytes // derived class members
total = 24 bytes
- According to above, it is clear that memory allocation of the derived class is not depend on the type of an inheritance and access specifiers inside a base class.
- But we have to consider an access specifiers and the type of an inheritance while accessing a particular member.
Important points about the public inheritance:-
- A public member of a base class becomes public in a derived class.
- A private member of a base class becomes private in a derived class.
- A protected member of a base class becomes protected in a derived class.
- In this case, we are using single inheritance and if we derive some another class then that class can access member of grandparent class which is depend on the type of an inheritance used by the last class in an inheritance hierarchy.
Private inheritance:-
- Public member of a base class becomes private in a derived class.
- Private member of a base class becomes private in a derived class.
- Protected member of a base class becomes private in a derived class.
- According to above, when we inherit the class by private inheritance we cannot access all the members of a base class by creating an object of a derived class then what is the use of private inheritance.
Protected inheritance:-
- Public member of a base class becomes protected in a derived class.
- Protected member of a base class becomes protected in a derived class.
- Private member of a base class becomes private in a derived class.
Object slicing:-
- When we initialize a base class object by the derived class then a base class copies only the contents which are in a base class itself and the remaining contents which are the part of a derived class are not copied inside a base class, because, there is no memory for a derived class members of a base class and this members are sliced from a base class layout that’s why this concept is called as an object slicing.
- Consider an example in public inheritance previously taken,
base b;
derived d;
b=d; // contents of base class are copied but derived class contents are not copied, because, derived class did not get the memory. For more reading about technology news in singapore and seo to online marketing do view more about other pages.