You are on page 1of 17

Module 25

Partha Pratim
Das
Module 25: Programming in C++
Objectives &
Outline
Inheritance: Part 5
Inheritance in
C++

private Partha Pratim Das


Inheritance

protected
Inheritance
Department of Computer Science and Engineering
Indian Institute of Technology, Kharagpur
Visibility
ppd@cse.iitkgp.ernet.in
Use &
Examples

Summary
Tanwi Mallick
Srijoni Majumdar
Himadri B G S Bhuyan

NPTEL MOOCs Programming in C++ Partha Pratim Das 1


Module Objectives

Module 25

Partha Pratim Explore restricted forms of inheritance (private and


Das
protected) in C++ and their semantic implications
Objectives &
Outline

Inheritance in
C++

private
Inheritance

protected
Inheritance

Visibility

Use &
Examples

Summary

NPTEL MOOCs Programming in C++ Partha Pratim Das 2


Module Outline

Module 25

Partha Pratim ISA Relationship


Das
Inheritance in C++
Objectives &
Outline
Semantics
Inheritance in
Data Members and Object Layout
C++ Member Functions
private Overriding
Inheritance
Overloading
protected
Inheritance protected Access
Visibility Constructor & Destructor
Use & Object Lifetime
Examples
Example – Phone Hierarchy
Summary
Inheritance in C++ (private)
Implemented-As Semantics

NPTEL MOOCs Programming in C++ Partha Pratim Das 3


Inheritance in C++: Semantics

Module 25 Derived ISA Base


Partha Pratim
Das

Objectives & class Base; // Base Class = Base


Outline class Derived: public Base; // Derived Class = Derived
Inheritance in
C++ Use keyword public after class name to denote
private inheritance
Inheritance

protected
Name of the Base class follow the keyword
Inheritance

Visibility

Use &
Examples

Summary

NPTEL MOOCs Programming in C++ Partha Pratim Das 4


Inheritance Exercise: What is the output?

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 };

Visibility int main() {


D d;
Use &
Examples return 0;
}
Summary

NPTEL MOOCs Programming in C++ Partha Pratim Das 5


Inheritance Exercise: What is the output?

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 };

Visibility int main() {


D d;
Use &
Examples return 0;
}
Summary

Output:

B C D
~D ~C ~B

NPTEL MOOCs Programming in C++ Partha Pratim Das 6


private Inheritance

Module 25 private Inheritance


Definition
Partha Pratim
Das class Base;
class Derived: private Base;
Objectives & Use keyword private after class name
Outline
Name of the Base class follow the keyword
Inheritance in private inheritance does not mean generalization / specialization
C++

private
Inheritance • Private inheritance means nothing during software design, only during
protected software implementation
Inheritance

Visibility • Private inheritance means is-implemented-in-terms of. It’s usually in-


ferior to composition, but it makes sense when a derived class needs access
Use &
Examples
to protected base class members or needs to redefine inherited virtual functions

Summary – Scott Meyers in Item 32, Effective C++ (3rd. Edition)

NPTEL MOOCs Programming in C++ Partha Pratim Das 7


private Inheritance
public Inheritance private Inheritance
Module 25
class Person {...}; class Person { ... };
Partha Pratim
Das class Student: class Student: // inheritance is now private
public Person {...}; private Person { ... };

Objectives & // anyone can eat


// anyone can eat
Outline void eat(const Person& p);
void eat(const Person& p);
Inheritance in // only students study
C++ // only students study
void study(const Student& s); void study(const Student& s);
private
Person p; // p is a Person Person p; // p is a Person
Inheritance

protected Student s; // s is a Student Student s; // s is a Student


Inheritance
eat(p); // fine, p is a Person eat(p); // fine, p is a Person
Visibility
eat(s); // fine, s is a Student, eat(s); // error! a Student isn’t a Person
Use &
// and a Student is-a Person
Examples

Summary study(s); // fine

study(p); // error! p isn’t a Student

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

NPTEL MOOCs Programming in C++ Partha Pratim Das 8


protected Inheritance

Module 25 protected Inheritance


Definition
Partha Pratim
Das class Base;
class Derived: protected Base;
Objectives & Use keyword protected after class name
Outline
Name of the Base class follow the keyword
Inheritance in protected inheritance does not mean generalization / specialization
C++

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

NPTEL MOOCs Programming in C++ Partha Pratim Das 9


Visibility across Access and Inheritance

Module 25 Visibility Matrix


Partha Pratim Inheritance
Das
public protected private
Objectives &
Visibility
Outline
public public protected private
Inheritance in
protected protected protected private
C++
private private private private
private
Inheritance

protected
Inheritance

Visibility

Use &
Examples

Summary

NPTEL MOOCs Programming in C++ Partha Pratim Das 10


Inheritance Exercise: What is the output?

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 };

Visibility int main() {


D d;
Use &
Examples return 0;
}
Summary

NPTEL MOOCs Programming in C++ Partha Pratim Das 11


Inheritance Exercise: What is the output?

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 };

Visibility int main() {


D d;
Use &
Examples return 0;
}
Summary

Output:

B C B C D
~D ~C ~B ~C ~B

NPTEL MOOCs Programming in C++ Partha Pratim Das 12


Inheritance Exercise: Access Rights
Inaccessible Members Accessible Members
Module 25 class A { void f(A& a,
private: int x; B& b, C& c, D& d,
Partha Pratim protected: int y; E& e, F& f, G& g) {
Das public: int z; a.z;
};
class B : public A { b.z;
Objectives & private: int u; b.w;
Outline protected: int v;
public: int w; void f() { x; } c.w;
Inheritance in };
C++ class C: protected A { d.w;
private: int u;
private
protected: int v; e.z;
Inheritance
public: int w; void f() { x; } e.w;
protected };
Inheritance class D: private A { f.w;
private: int u;
Visibility protected: int v; g.w;
public: int w; void f() { x; } }
Use & };
Examples class E : public B {
public: void f() { x; u; }
Summary };
class F : public C {
public: void f() { x; u; }
};
class G : public D {
public: void f() { x; y; z; u; }
};

NPTEL MOOCs Programming in C++ Partha Pratim Das 13


Car HAS–A Engine:
Composition OR private Inheritance?
Simple Composition private Inheritance
Module 25
#include <iostream> #include <iostream>
using namespace std; using namespace std;
Partha Pratim
Das
class Engine { class Engine {
public: public:
Objectives & Engine(int numCylinders) { } Engine(int numCylinders) { }
Outline // Starts this Engine // Starts this Engine
void start() { } void start() { }
Inheritance in }; };
C++
class Car { class Car : private Engine { // Car has-a Engine
private public: public:
Inheritance // Initializes this Car with 8 cylinders // Initializes this Car with 8 cylinders
protected Car() : e_(8) { } Car() : Engine(8) { }
Inheritance
// Start this Car by starting its Engine // Start this Car by starting its Engine
Visibility void start() { e_.start(); } using Engine::start;
private:
Use & Engine e_; // Car has-a Engine
Examples }; };

Summary int main() { int main() {


Car c; Car c;

c.start(); c.start();

return 0; return 0;
} }

NPTEL MOOCs Programming in C++ Partha Pratim Das 14


private Inheritance

Module 25 Use composition when you can, private inheritance when


Partha Pratim you have to
Das

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)

NPTEL MOOCs Programming in C++ Partha Pratim Das 15


Module Summary

Module 25

Partha Pratim Introduced restricted forms of inheritance and protected


Das
specifier
Objectives &
Outline Discussed how private inheritance is used for
Inheritance in Implemented-As Semantics
C++

private
Inheritance

protected
Inheritance

Visibility

Use &
Examples

Summary

NPTEL MOOCs Programming in C++ Partha Pratim Das 16


Instructor and TAs

Module 25

Partha Pratim
Das Name Mail Mobile
Objectives &
Partha Pratim Das, Instructor ppd@cse.iitkgp.ernet.in 9830030880
Outline Tanwi Mallick, TA tanwimallick@gmail.com 9674277774
Inheritance in Srijoni Majumdar, TA majumdarsrijoni@gmail.com 9674474267
C++ Himadri B G S Bhuyan, TA himadribhuyan@gmail.com 9438911655
private
Inheritance

protected
Inheritance

Visibility

Use &
Examples

Summary

NPTEL MOOCs Programming in C++ Partha Pratim Das 17

You might also like