Module 25: Programming in C++: Partha Pratim Das
Module 25: Programming in C++: Partha Pratim Das
Module 25: Programming in C++: Partha Pratim Das
Partha Pratim
Das
Module 25: Programming in C++
Objectives &
Outline
Inheritance: Part 5
Inheritance in
C++
protected
Inheritance
Department of Computer Science and Engineering
Indian Institute of Technology, Kharagpur
Visibility
[email protected]
Use &
Examples
Summary
Tanwi Mallick
Srijoni Majumdar
Himadri B G S Bhuyan
Module 25
Inheritance in
C++
private
Inheritance
protected
Inheritance
Visibility
Use &
Examples
Summary
Module 25
protected
Name of the Base class follow the keyword
Inheritance
Visibility
Use &
Examples
Summary
Module 25 class B {
public:
Partha Pratim B() { cout << "B "; }
Das ~B() { cout << "~B "; } };
class C {
Objectives & public:
Outline C() { cout << "C "; }
~C() { cout << "~C "; } };
Inheritance in
C++
class D : public B {
private C data_;
Inheritance public:
D() { cout << "D " << endl; }
protected ~D() { cout << "~D "; }
Inheritance };
Module 25 class B {
public:
Partha Pratim B() { cout << "B "; }
Das ~B() { cout << "~B "; } };
class C {
Objectives & public:
Outline C() { cout << "C "; }
~C() { cout << "~C "; } };
Inheritance in
C++
class D : public B {
private C data_;
Inheritance public:
D() { cout << "D " << endl; }
protected ~D() { cout << "~D "; }
Inheritance };
Output:
B C D
~D ~C ~B
private
Inheritance • Private inheritance means nothing during software design, only during
protected software implementation
Inheritance
Compilers converts a derived class object (Stu- Compilers will not convert a derived class object
dent) into a base class object (Person) if the in- (Student) into a base class object (Person) if the
heritance relationship is public inheritance relationship is private
private
Inheritance • Private inheritance means something entirely different (from public inheri-
protected tance), and protected inheritance is something whose meaning eludes me to
Inheritance this day
Visibility
– Scott Meyers in Item 32, Effective C++ (3rd. Edition)
Use &
Examples
Summary
protected
Inheritance
Visibility
Use &
Examples
Summary
Module 25 class B {
protected:
Partha Pratim B() { cout << "B "; }
Das ~B() { cout << "~B "; }
};
class C : public B {
Objectives & protected:
Outline C() { cout << "C "; }
~C() { cout << "~C "; }
Inheritance in
};
C++
class D : private C {
private C data_;
Inheritance public:
D() { cout << "D " << endl; }
protected ~D() { cout << "~D "; }
Inheritance };
Module 25 class B {
protected:
Partha Pratim B() { cout << "B "; }
Das ~B() { cout << "~B "; }
};
class C : public B {
Objectives & protected:
Outline C() { cout << "C "; }
~C() { cout << "~C "; }
Inheritance in
};
C++
class D : private C {
private C data_;
Inheritance public:
D() { cout << "D " << endl; }
protected ~D() { cout << "~D "; }
Inheritance };
Output:
B C B C D
~D ~C ~B ~C ~B
c.start(); c.start();
return 0; return 0;
} }
Objectives &
Outline • Private inheritance means nothing during software
Inheritance in design, only during software implementation
C++
private
Inheritance • Private inheritance means is-implemented-in-terms of.
protected
Inheritance
It’s usually inferior to composition, but it makes sense
Visibility
when a derived class needs access to protected base class
Use &
members or needs to redefine inherited virtual functions
Examples
Summary
– Scott Meyers in Item 32, Effective C++ (3rd. Edition)
Module 25
private
Inheritance
protected
Inheritance
Visibility
Use &
Examples
Summary
Module 25
Partha Pratim
Das Name Mail Mobile
Objectives &
Partha Pratim Das, Instructor [email protected] 9830030880
Outline Tanwi Mallick, TA [email protected] 9674277774
Inheritance in Srijoni Majumdar, TA [email protected] 9674474267
C++ Himadri B G S Bhuyan, TA [email protected] 9438911655
private
Inheritance
protected
Inheritance
Visibility
Use &
Examples
Summary