Module M25
L
Partha Pratim
Das
Programming in Modern C++
E
Objectives &
Outlines
Module M25: Inheritance: Part 5: private & protected Inheritance
T
Inheritance in
C++
P
private
Inheritance
Uncopyable
Partha Pratim Das
N
HAS–A
protected
Inheritance Department of Computer Science and Engineering
Visibility Indian Institute of Technology, Kharagpur
Examples
[email protected]
Module Summary
All url’s in this module have been accessed in September, 2021 and found to be functional
Programming in Modern C++ Partha Pratim Das M25.1
Module Recap
Module M25
• Using the Phone Hierarchy as an example analyzed the design process with inheritance
L
Partha Pratim
Das
E
Objectives &
Outlines
T
Inheritance in
C++
P
private
Inheritance
Uncopyable
N
HAS–A
protected
Inheritance
Visibility
Examples
Module Summary
Programming in Modern C++ Partha Pratim Das M25.2
Module Objectives
Module M25
• Explore restricted forms of inheritance (private and protected) in C++ and their
L
Partha Pratim
Das
semantic implications
E
Objectives &
Outlines
T
Inheritance in
C++
P
private
Inheritance
Uncopyable
N
HAS–A
protected
Inheritance
Visibility
Examples
Module Summary
Programming in Modern C++ Partha Pratim Das M25.3
Module Outline
Module M25
Objectives & Outlines
L
Partha Pratim 1
Das
E
Objectives &
Outlines
2 Inheritance in C++
T
Inheritance in
C++ 3 private Inheritance
P
private
Inheritance Uncopyable
Uncopyable
HAS–A
N
HAS–A
protected
Inheritance 4 protected Inheritance
Visibility
Examples 5 Visibility
Module Summary
6 Examples
7 Module Summary
Programming in Modern C++ Partha Pratim Das M25.4
Inheritance in C++
Module M25
L
Partha Pratim
Das
E
Objectives &
Outlines
T
Inheritance in
C++
P
private
Inheritance
Uncopyable
N
HAS–A
protected
Inheritance
Visibility
Examples
Module Summary Inheritance in C++
Programming in Modern C++ Partha Pratim Das M25.5
Inheritance in C++: Semantics
Module M25
• Derived ISA Base
L
Partha Pratim
Das
E
Objectives &
Outlines
T
Inheritance in class Base; // Base Class = Base
C++
class Derived: public Base; // Derived Class = Derived
P
private
Inheritance
Uncopyable
• Use keyword public after class name to denote inheritance
N
HAS–A
• Name of the Base class follow the keyword
protected
Inheritance
Visibility
Examples
Module Summary
Programming in Modern C++ Partha Pratim Das M25.6
Inheritance Exercise: What is the output?
Module M25 class B {
public:
L
Partha Pratim
Das
B() { cout << "B "; }
~B() { cout << "~B "; }
};
E
Objectives &
Outlines
class C {
T
Inheritance in
C++ public:
C() { cout << "C "; }
P
private
Inheritance ~C() { cout << "~C "; }
Uncopyable };
N
HAS–A
class D : public B {
protected
Inheritance C data_; // Embedded Object
public:
Visibility
D() { cout << "D " << endl; } // Intrinsic Base Class Object
Examples ~D() { cout << "~D "; }
Module Summary };
int main() {
D d;
}
Programming in Modern C++ Partha Pratim Das M25.7
Inheritance Exercise: What is the output?
Module M25 class B {
public:
L
Partha Pratim
Das B() { cout << "B "; }
~B() { cout << "~B "; }
E
Objectives & };
Outlines
class C {
T
Inheritance in public:
C++ C() { cout << "C "; }
~C() { cout << "~C "; }
P
private
Inheritance };
Uncopyable class D : public B {
N
HAS–A C data_; // Embedded Object
protected public:
Inheritance D() { cout << "D " << endl; } // Intrinsic Base Class Object
Visibility ~D() { cout << "~D "; }
};
Examples
int main() {
Module Summary D d;
}
Output:
B C D // First base class object, then embedded object, finally self
~D ~C ~B
Programming in Modern C++ Partha Pratim Das M25.8
private Inheritance
Module M25
L
Partha Pratim
Das
E
Objectives &
Outlines
T
Inheritance in
C++
P
private
Inheritance
Uncopyable
N
HAS–A
protected
Inheritance
Visibility
Examples
Module Summary private Inheritance
Programming in Modern C++ Partha Pratim Das M25.9
private Inheritance
Module M25
• private Inheritance
L
Partha Pratim
Das
◦ Definition
E
Objectives &
Outlines class Base;
T
Inheritance in
C++
class Derived: private Base;
◦ Use keyword private after class name
P
private
Inheritance
Uncopyable
◦ Name of the Base class follow the keyword
◦ private inheritance does not mean generalization / specialization
N
HAS–A
protected
Inheritance
Visibility
Examples
Module Summary
Programming in Modern C++ Partha Pratim Das M25.10
private Inheritance
public Inheritance private Inheritance
Module M25
class Person { ... }; class Person { ... };
L
Partha Pratim
Das
class Student: class Student: // inheritance is now private
E
Objectives & public Person { ... }; private Person { ... };
Outlines
T
Inheritance in void eat(const Person& p); // anyone can eat void eat(const Person& p); // anyone can eat
C++ void study(const Student& s); // only students study void study(const Student& s); // only students study
P
private
Inheritance Person p; // p is a Person Person p; // p is a Person
Uncopyable Student s; // s is a Student Student s; // s is a Student
N
HAS–A
protected eat(p); // fine, p is a Person eat(p); // fine, p is a Person
Inheritance eat(s); // fine, s is a Student, eat(s); // error! a Student isn’t a Person
Visibility // and a Student is-a Person
Examples
study(s); // fine
Module Summary study(p); // error! p isn’t a Student
• Compilers converts a derived class object (Student) • Compilers will not convert a derived class object (Stu-
into a base class object (Person) if the inheritance rela- dent) into a base class object (Person) if the inheritance
tionship is public relationship is private
Programming in Modern C++ Partha Pratim Das M25.11
Uncopyable Class
Module M25 • Suppose we want to design a MyClass every object must be unique. That is instance objects must not be copied (the
class needs to be Uncopyable)
L
Partha Pratim
Das MyClass m1;
MyClass m2;
E
Objectives &
Outlines MyClass m3(m1); // attempt to copy m1 - should not compile!
T
Inheritance in m1 = m2; // attempt to copy m2 - should not compile!
C++
• Naturally, we do not want to provide copy constructor or copy assignments operator. But that does not work as the
P
private compiler will provide free versions of these functions. How to stop that?
Inheritance
Uncopyable
class MyClass {
public:
N
HAS–A
...
protected
Inheritance
private:
...
Visibility MyClass(const MyClass&); // declarations only
Examples MyClass& operator=(const MyClass&);
Module Summary
};
The last trick is not to provide the implementations (bodies) of these copy functions.
• With the above any global function or other class would not be able to copy - there will be compilation error; and any
member function of MyClass will get linker error on missing implementation
• This is, of course, not an elegant solution and has to depend on the programmer to do things right for every such
Uncopyable class. private inheritance helps out
Programming in Modern C++ Partha Pratim Das M25.12
Uncopyable
Module M25
• class Uncopyable is designed as a root class that can make copy functions of any child class private
class Uncopyable { protected: // allow construction and destruction of derived objects ...
L
Partha Pratim Uncopyable() { }
Das
~Uncopyable() { }
private:
E
Objectives &
Outlines Uncopyable(const Uncopyable&); // ... but prevent copying
Uncopyable& operator=(const Uncopyable&);
T
Inheritance in
C++ };
• Any class that inherits from class Uncopyable, will not have copy functionality:
P
private
Inheritance class MyClass: private Uncopyable { // class no longer declares copy ctor or copy assign. operator
Uncopyable // ...
N
HAS–A
void ProhibitiveCopy() { MyClass test1, test2; // Member functions cannot perform copy
protected MyClass test3(test1); // Error 1: ’Uncopyable::Uncopyable’ : cannot access private member
Inheritance test2 = test1; // Error 2: ’Uncopyable::operator =’ : cannot access private member
Visibility }
};
Examples
int main() { MyClass test1, test2; // Global functions cannot perform copy
Module Summary MyClass test3(test1); // Error 1: ’Uncopyable::Uncopyable’ : cannot access private member
test2 = test1; // Error 2: ’Uncopyable::operator =’ : cannot access private member
}
• The inheritance from Uncopyable need not be public (though it will work), hence using private we express that it
is purely for implementation - not for modeling ISA that actually does not exist
• You may also use the boost::noncopyable
• C++11 provides an explicit support by delete to stop compilers from providing free functions
Programming in Modern C++ Partha Pratim Das M25.13
Car HAS–A Engine: Composition OR private Inheritance?
Simple Composition private Inheritance
Module M25
#include <iostream> #include <iostream>
L
Partha Pratim
Das using namespace std; using namespace std;
E
Objectives & class Engine { class Engine {
Outlines public: public:
T
Inheritance in Engine(int numCylinders) { } Engine(int numCylinders) { }
C++ void start() { } // Starts this Engine void start() { } // Starts this Engine
}; };
P
private
Inheritance class Car { class Car : private Engine { // Car has-a Engine
Uncopyable public: public:
N
HAS–A // Initializes this Car with 8 cylinders // Initializes this Car with 8 cylinders
protected Car() : e_(8) { } Car() : Engine(8) { }
Inheritance
Visibility // Start this Car by starting its Engine // Start this Car by starting its Engine
void start() { e_.start(); } using Engine::start;
Examples
private:
Module Summary Engine e_; // Car has-a Engine
}; };
int main() { int main() {
Car c; Car c;
c.start(); c.start();
} }
Programming in Modern C++ Partha Pratim Das M25.14
private Inheritance
Module M25
• For HAS-A: Use composition when you can, private inheritance when you have to
L
Partha Pratim
Das
E
Objectives &
Outlines • Private inheritance means nothing during software design, only during software
implementation
T
Inheritance in
C++
P
private
Inheritance • Private inheritance means is-implemented-in-terms of. It is usually infe-
Uncopyable
rior to composition, but it makes sense when a derived class needs access to
N
HAS–A
protected protected base class members or needs to redefine inherited virtual functions
Inheritance
Visibility
Examples
– Scott Meyers in Item 32, Effective C++ (3rd. Edition)
Module Summary
Programming in Modern C++ Partha Pratim Das M25.15
protected Inheritance
Module M25
L
Partha Pratim
Das
E
Objectives &
Outlines
T
Inheritance in
C++
P
private
Inheritance
Uncopyable
N
HAS–A
protected
Inheritance
Visibility
Examples
Module Summary protected Inheritance
Programming in Modern C++ Partha Pratim Das M25.16
protected Inheritance
Module M25
• protected Inheritance
L
Partha Pratim
Das
◦ Definition
E
Objectives &
Outlines class Base;
T
Inheritance in
C++
class Derived: protected Base;
◦ Use keyword protected after class name
P
private
Inheritance
Uncopyable
◦ Name of the Base class follow the keyword
◦ protected inheritance does not mean generalization / specialization
N
HAS–A
protected
Inheritance
Visibility • Private inheritance means something entirely different (from public inheritance),
Examples and protected inheritance is something whose meaning eludes me to this day
Module Summary
– Scott Meyers in Item 32, Effective C++ (3rd. Edition)
Programming in Modern C++ Partha Pratim Das M25.17
Visibility
Module M25
L
Partha Pratim
Das
E
Objectives &
Outlines
T
Inheritance in
C++
P
private
Inheritance
Uncopyable
N
HAS–A
protected
Inheritance
Visibility
Examples
Module Summary Visibility
Programming in Modern C++ Partha Pratim Das M25.18
Visibility across Access and Inheritance
Module M25 • Visibility Matrix
Inheritance
L
Partha Pratim
Das
public protected private
E
Visibility
Objectives &
Outlines
public public protected private
protected protected protected private
T
Inheritance in
C++
private private private private
P
private
Inheritance
Uncopyable
N
HAS–A
protected
Inheritance
Visibility
Examples
Module Summary
Programming in Modern C++ Partha Pratim Das M25.19
Use and Examples
Module M25
L
Partha Pratim
Das
E
Objectives &
Outlines
T
Inheritance in
C++
P
private
Inheritance
Uncopyable
N
HAS–A
protected
Inheritance
Visibility
Examples
Module Summary Use and Examples
Programming in Modern C++ Partha Pratim Das M25.20
Inheritance Exercise: What is the output?
Module M25
class B {
L
Partha Pratim
Das
protected:
B() { cout << "B "; }
~B() { cout << "~B "; }
E
Objectives &
Outlines };
class C : public B {
T
Inheritance in
C++ protected:
C() { cout << "C "; }
P
private
Inheritance ~C() { cout << "~C "; }
Uncopyable };
class D : private C {
N
HAS–A
protected
C data_;
Inheritance public:
D() { cout << "D " << endl; }
Visibility
~D() { cout << "~D "; }
Examples };
Module Summary
int main() {
D d;
}
Programming in Modern C++ Partha Pratim Das M25.21
Inheritance Exercise: What is the output?
Module M25 class B {
protected:
L
Partha Pratim
Das
B() { cout << "B "; }
~B() { cout << "~B "; }
};
E
Objectives &
Outlines class C : public B {
protected:
T
Inheritance in
C++ C() { cout << "C "; }
~C() { cout << "~C "; }
P
private
Inheritance };
Uncopyable class D : private C {
N
HAS–A C data_;
public:
protected
Inheritance D() { cout << "D " << endl; }
~D() { cout << "~D "; }
Visibility
};
Examples
Module Summary int main() {
D d;
}
Output:
B C B C D
~D ~C ~B ~C ~B
Programming in Modern C++ Partha Pratim Das M25.22
Inheritance Exercise: Access Rights
Module M25 Inaccessible Members Accessible Members
class A { private:
L
Partha Pratim int x; void f(A& a,
Das
protected: int y; B& b, C& c, D& d,
public: int z; E& e, F& f, G& g) {
E
Objectives &
Outlines }; a.z;
class B : public A { private: int u;
T
Inheritance in
C++
protected: int v; b.z;
public: int w; void f() { x; } b.w;
P
private };
Inheritance
class C: protected A { private: int u; c.w;
Uncopyable
protected: int v;
N
HAS–A
public: int w; void f() { x; } d.w;
protected };
Inheritance
class D: private A { private: int u; e.z;
Visibility protected: int v; e.w;
Examples public: int w; void f() { x; }
}; f.w;
Module Summary
class E : public B { public: void f() { x; u; }
}; g.w;
class F : public C { public: void f() { x; u; } }
};
class G : public D { public: void f() { x; y; z; u; }
};
Programming in Modern C++ Partha Pratim Das M25.23
Module Summary
Module M25
• Introduced restricted forms of inheritance and protected specifier
L
Partha Pratim
Das
• Discussed how private inheritance is used for Implemented-As Semantics
E
Objectives &
Outlines
T
Inheritance in
C++
P
private
Inheritance
Uncopyable
N
HAS–A
protected
Inheritance
Visibility
Examples
Module Summary
Programming in Modern C++ Partha Pratim Das M25.24