Inheritance Concept
class Rectangle{
Polygon private:
int numVertices;
float *xCoord, *yCoord;
public:
Rectangle void set(float *x, float *y, int nV);
Triangle
float area();
};
class Polygon{ class Triangle{
private: private:
int numVertices; int numVertices;
float *xCoord, *yCoord; float *xCoord, *yCoord;
public: public:
void set(float *x, float *y, int nV); void set(float *x, float *y, int nV);
float area();
}; 1
};
Inheritance Concept
class Polygon{
Polygon protected:
int numVertices;
float *xCoord, float *yCoord;
public:
void set(float *x, float *y, int nV);
Rectangle };
Triangle
class Rectangle{
protected:
class Rectangle : public Polygon{ int numVertices;
public: float *xCoord, float *yCoord;
float area(); public:
void set(float *x, float *y, int nV);
};
float area(); 2
};
Inheritance Concept
class Polygon{
protected:
Polygon
int numVertices;
float *xCoord, float *yCoord;
public:
void set(float *x, float *y, int nV);
Rectangle };
Triangle
class Triangle{
protected:
class Triangle : public Polygon{ int numVertices;
public: float *xCoord, float *yCoord;
float area(); public:
void set(float *x, float *y, int nV);
};
float area(); 3
};
Inheritance Concept
x
class Point{
Point y protected:
int x, y;
public:
Circle 3D-Point void set (int a, int b);
x x };
y y
r z
class Circle : public Point{ class 3D-Point: public Point{
private: private:
double r; int z;
}; };
4
Inheritance Concept
• Augmenting the original class
Polygon Point
Rectangle
Triangle Circle 3D-Point
• Specializing the original class
ComplexNumber real
imag
RealNumber ImaginaryNumber imag
real 5
Why Inheritance ?
Inheritance is a mechanism for
• building class types from existing class types
• defining new class types to be a
– specialization
– augmentation
of existing types
6
Define a Class Hierarchy
• Syntax:
class DerivedClassName : access-level BaseClassName
where:
– access-level specifies the type of derivation
• private by default, or
• public
• Any class can serve as a base class
– Thus a derived class can also be a base class
7
Class Derivation
Point class Point{
protected:
int x, y;
3D-Point public:
void set (int a, int b);
};
Sphere
class 3D-Point : public Point{ class Sphere : public 3D-Point{
private: private:
double z; double r;
…… ……
}; };
Point is the base class of 3D-Point, while 3D-Point is the base class of Sphere
8
What to inherit?
• In principle, every member of a base class is
inherited by a derived class
– just with different access permission
9
Access Control Over the Members
• Two levels of access control
base class/ superclass/ over class members
parent class – class definition
– inheritance type
members goes to
derive from
class Point{
protected: int x, y;
public: void set(int a, int b);
};
derived class/ subclass/
child class class Circle : public Point{
……
}; 10
Access Specifiers Recap
1. Private:
• Members are only accessible within the class
itself.
• External objects cannot access private members
directly.
2. Public:
• Members are accessible from anywhere the
object is visible.
• Allows interaction with the outside world
using functions.
11
What is protected:
• A new access specifier used in class
• Members declared as protected can be
accessed:
– By member functions of the same class.
– By derived (subclass) classes.
• Unlike private members, protected allows
sharing with derived classes while
maintaining some level of encapsulation.
12
Inheritance and Accessibility
• The type of inheritance defines the access level for the
members of derived class that are inherited from the base
class
13
Class Derivation
class mother{ class grandDaughter : public daughter {
protected: int mProc; private: double gPriv;
public: int mPubl; public: void gFoo ( );
private: int mPriv; };
};
private/protected/public
int main() {
class daughter : --------- mother{
/*….*/
private: double dPriv;
}
public: void dFoo
mFoo((););
};
void daughter :: dFoo ( ){
mPriv = 10; //error
mProc = 20;
14
};
What to inherit?
• In principle, every member of a base class is
inherited by a derived class
– just with different access permission
• However, there are exceptions for
– constructor and destructor
– operator=() member
– friends
Since all these functions are class-specific
15
Constructor Rules for Derived Classes
The default constructor and the destructor of the
base class are always called when a new object
of a derived class is created or destroyed.
class A { class B : public A
public: {
A() public:
{cout<< “A:default”<<endl;} B (int a)
A (int a) {cout<<“B”<<endl;}
{cout<<“A:parameter”<<endl;} };
};
output: A:default
B test(1); B 16
Constructor Rules for Derived Classes
You can also specify an constructor of the
base class other than the default constructor
DerivedClassCon ( derivedClass args ) : BaseClassCon ( baseClass
args )
{ DerivedClass constructor body }
class A { class C : public A {
public: public:
A() C (int a) : A(a)
{cout<< “A:default”<<endl;} {cout<<“C”<<endl;}
A (int a) };
{cout<<“A:parameter”<<endl;}
};
output: A:parameter
C test(1); C 17
Define its Own Members
The derived class can also define class Point{
its own members, in addition to protected:
the members inherited from the int x, y;
base class
public:
x void set(int a, int b);
Point y
};
x
y Circle class Circle{
r protected:
int x, y;
class Circle : public Point{ private:
private: double r;
double r; public:
public: void set(int a, int b);
void set_r(double c); void set_r(double c);
18
}; };
Even more …
• A derived class can override methods defined in its parent
class. With overriding,
– the method in the subclass has the identical signature to the method
in the base class.
– a subclass implements its own version of a base class method.
class A {
class B : public A {
protected:
public:
int x, y;
void print ()
public:
{cout<<“From B”<<endl;}
void print ()
};
{cout<<“From A”<<endl;}
};
19
Access a Method
class Point{
class Circle : public Point{
protected:
private: double r;
int x, y;
public:
public:
void set (int a, int b, double c) {
void set(int a, int b)
Point :: set(a, b); //same name function call
{x=a; y=b;}
r = c;
void foo ();
}
void print();
void print(); };
};
Circle C;
Point A; C.set(10,10,100); // from class Circle
A.set(30,50); // from base class Point C.foo (); // from base class Point
A.print(); // from base class Point C.print(); // from class Circle
20
Let’s Try!!!
21
Take Home Message
• Inheritance is a mechanism for defining new
class types to be a specialization or an
augmentation of existing types.
• In principle, every member of a base class is
inherited by a derived class with different
access permissions, except for the constructors
22
Single Inheritance
23
“Abstract” Base Class
24
Access Combinations
25
Access Combinations
26
Levels of Inheritance
27
Levels of Inheritance
28
Levels of Inheritance
As a more concrete example, suppose that we decided to add
a special kind of laborer called a foreman to the Employee
program. Since a foreman is a kind of laborer, the foreman
class is derived from the laborer class,
29