Inheritance_Concept
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
Rectangle
Triangle Circle 3D-Point
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?
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
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((););
};
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 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.
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
29