You are on page 1of 18

01

02

LECTURE 30
04
Today’s Agenda
01 Introduction to Inheritance

02 Types of Inheritance

03 Syntax of inheriting a class and an example

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).

What is the benefit of inheriting a 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()

Student Teacher Player


takeclass() takeexam() match()
Types of Inheritance

1. Single level Inheritance 2. Multilevel Inheritance

A A

B
B

C
Types of Inheritance

3. Multiple Inheritance 4. Hierarchical Inheritance

A B A

C B C
Types of Inheritance

3. Hybrid Inheritance
A

B C

D
Syntax of Inheriting a Class

 Single Level Inheritance

class <Class_name> Base Class


{
....
....
};
Derived Class Base Class

class <class_name>:<public|private|protected> <class_name>


{
....
....
}; Modes of Inheritance
Example of Single Inheritance in "public" Mode

class Box class Carton:public Box int main()


{ { {
private: private: Carton obj;
int l, b, h; char material[20]; obj.get();
public: public: obj.set();
void get() void set() obj.show();
{ { obj.display();
cout<<"Enter l, b, h: "; cout<<"Enter Material: "; return 0;
cin>>l>>b>>h; cin>>material; }
} }
void show() void display()
{ { ? ”Wood” mat
cout<<l<<", "<<b<<", "<<h<<endl; cout<<material;
} } ? 30 h
}; }; ? 20 b
? 10 l
32 bytes
Output

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!!!

 Why did the previous code generate 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.

 What is the SOLUTION?

 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.

2. The keyword "protected" is same as "private" unless inheritance is done.

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

class Box class Carton: public Box int main()


{ { {
protected: private: Carton obj;
int l, b, h; char material[20]; obj.get();
public: public: obj.set();
void get() void set() obj.show();
{ { obj.display();
cout<<"Enter l, b, h: "; cout<<"Enter Material: "; obj.volume();
cin>>l>>b>>h; cin>>material; return 0;
} } }
void show() void display()
{ {
cout<<l<<", "<<b<<", "<<h<<endl; cout<<material<<endl;
} }
}; void volume()
{
cout<<"Volume: "<<l * b * h<<endl;
}
};
Output

Enter l, b, h: 10 20 30
Enter Material: Wood
10, 20, 30
Wood
Volume: 6000

Process returned 0 (0x0) execution time : 5.996 s


Press any key to continue.
End of Lecture 30
For any queries mail us @: scalive4u@gmail.com
Call us @ : 0755-4271659, 7879165533

Thank you

You might also like