Professional Documents
Culture Documents
C++ Lecture-30
C++ Lecture-30
02
LECTURE 30
04
Today’s Agenda
01 Introduction to Inheritance
02 Types of Inheritance
04 Assignment Question
05
05 Introduction to keyword “protected”
05
Inheritance
Inheritance is the capability of extending the features of an existing class(Base class) into another newly
created class(Derived Class).
The most important benefit offered by inheritance is CODE REUSABILITY i.e., the derived class
programmer can directly reuse the function as well as the data provided by the base class
programmer in his class. This saves the derive class programmer from rewriting the same code
again and again.
Yes, all of them
For Example: are humans
1. Student
2. Teacher Is there any common thing between these
3. Player entities!!!
Inheritance
So, we can create a class called person, and within this Person class we can put all common things in this
class which is required to all these entities like get(), show()
With the help of this we have to code get() and show() one time and will be available to all the 3 classes with
there existing functionality.
Person
For example:
get()
1. Student: takeclass() show()
2. Teacher: takeexam()
3. Player: match()
A A
B
B
C
Types of Inheritance
A B A
C B C
Types of Inheritance
3. Hybrid Inheritance
A
B C
D
Syntax of Inheriting a Class
Enter l, b, h: 10 20 30
Enter Material: Wood
10, 20, 30
Wood
Process returned 0 (0x0) execution time : 7.570 s
Press any key to continue.
Assignment
Now, suppose you are asked to add one more function in your
code and using that function you have to calculate the volume
of the Carton object.
Solution
To achieve this we will have to design a member function called volume() inside the "Carton"
class class Carton:public Box int main()
class Box { {
{ private: Carton obj;
private: char material[20]; obj.get();
int l, b, h; public: obj.set();
public: void set() obj.show();
void get() { obj.display();
{ cout<<"Enter Material: "; return 0;
cout<<"Enter l, b, h: "; cin>>material; }
cin>>l>>b>>h; }
} void display()
void show() {
{ cout<<material<<endl;
cout<<l<<", "<<b<<", "<<h<<endl; }
} void volume()
}; { This code will
cout<<"Volume: "<<l * b * h;
} generate error
};
Reason behind the error!!!
This is because the members l, b, h are PRIVATE MEBERS of the BOX class and being private
they are NOT AT ALL accessible from outside their own class. Not even from the member
functions of the derived class.
This means that even after inheritance the private members of the base class stick to their
original rule which is that PRIVATE CAN ONLY BE ACCESSED FROM THE MEMBER FUNCTION
OF THE SAME CLASS.
To solve this issue, C++ recommends that we must declare the data members of the "base"
class "Box" as "protected"
What is "protected"?
1. Like "private" and "public", "protected" is also a keyword as well as an access specifier.
3. This means that if we are not performing "Inheritance" in our program then like "private"
members, the "protected" of the class also are not accessible from outside the class.
4. But when we apply "Inheritance" in our programs, then "protected" behaves differently as
compared to "private"
5. The "private" members of the base class are still not accessible from outside the base class
i.e., form member functions of the derived class as well as from the main() function
What is "protected"?
6. But if we declare members of the base class as "protected", then although they too are NOT
ACCESSIBLE from the main() function, but they are ACCESSIBLE from the member functions
of the derived class.
7. That's the reason we use "protected" in Inheritance so that, the derive class can perform
additional operations on them. Also protected members are known as "SEMI PRIVATE" or
"INHERITANCE READY MEMBERS"
Improved version of the previous code
Enter l, b, h: 10 20 30
Enter Material: Wood
10, 20, 30
Wood
Volume: 6000
Thank you