0% found this document useful (0 votes)
84 views

Inheritance PDF

Inheritance allows classes to inherit attributes and behaviors from other existing classes, known as base classes, to help reuse code. A derived class inherits from a base class and can override or extend the base class's methods and properties. Inheritance is useful for modeling real-world hierarchical relationships and promotes code reuse through polymorphism.

Uploaded by

Yassir Ibrahim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

Inheritance PDF

Inheritance allows classes to inherit attributes and behaviors from other existing classes, known as base classes, to help reuse code. A derived class inherits from a base class and can override or extend the base class's methods and properties. Inheritance is useful for modeling real-world hierarchical relationships and promotes code reuse through polymorphism.

Uploaded by

Yassir Ibrahim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Inheritance

Jussi Pohjolainen
TAMK University of Applied Sciences
Introduction to Inheritance
• Inheritance is a relationship between two or
more classes where derived class inherites
behaviour and attributes of pre-existing (base)
classes
• Intended to help reuse of existing code with
little or no modification
Inheritance
• Inheritance can be continous
– Derived class can inherit another class, which
inherits another class and so on
– When changing the base class all the derived
classes changes also
• Example:
– Mammal <– Human <– Worker <- Programmer
• Could mammal be a derived class? If so, what
would be the base class?
Picture about Inheritance
features: a,b
a
b

Class A

Features: a,b,c Features: a,b,d,e


c d
e

Class B Class C

Features: a,b,d,e,f
f

Class D
Multiple Inheritance
• In multiple inheritance a derived class has
multiple base classes
• C++ supports multiple base classes, Java don't
Driver Conductor
- license - Account number
House Boat - Year of approval

Taxi Driver
Houseboat - area
Inheritance and Capsulation
• private
– Is accessible only via the base class
• public
– Is accessible everywhere (base class, derived class,
othe classes)
• protected
– Is accessible by the base class and derived classes
Basic example
• What are Programmer's attributes and
methods?

Human
Programmer
string name
int salary
void sleep()
void implementApps()
void drink()
void beNerd()
void eat()
Overriding?
• What about now?

Human
Programmer
string name
int salary
void sleep()
void drink() void implementApps()
void eat() void beNerd()
void drink()
void eat()
Overriding
• Since programmer eats and drinks differently
than humans (only Coke and Pizza) the eat and
drink methods are overriden in Programmer!
Abstract Class
• Abstract class is a class which you cannot
instantiate (create objects)
• You can inherit abstract class and create
objects from the inherited class, if it is concrete
one
• Abstract class in C++ has abstract methods,
that do not have implementations
• These methods forces derived classes to
implement those methods
Example
<<abstract>>
Mammal

string name

void makesound() {abstract}

Elephant

int trunkLength

makesound()
Example
<<abstract>>
Figure

int x, y

double calculateArea() {abstract}

Circle Rect

double radius double length, height

double calculateArea() double calculateArea()


Exercises
INHERITANCE IN C++
Declaring Inheritance
class Circle : public Figure
{

}
Declaring Inheritance
class Figure
{
public:
int x, y;
};

class Circle : public Figure


{
public:
int radius;
};

int main()
{
Circle a;
a.x = 0;
a.y = 0;
a.radius = 10;
}
Encapsulation
class Figure
{
protected:
int x, y;
};

class Circle : public Figure


{
public:
int radius;
};
example.cpp: In function ‘int main()’:
int main()
{ example.cpp:5: error: ‘int Figure::x’ is protected
Circle a; example.cpp:17: error: within this context
a.x = 0; example.cpp:5: error: ‘int Figure::y’ is protected
a.y = 0; example.cpp:18: error: within this context
a.radius = 10;
}
Encapsulation
class Figure Circle::Circle(int x, int y, int
{ radius)
protected: {
int x_, y_; x_ = x;
}; y_ = y;
radius_ = radius;
class Circle : public Figure }
{
private: int main()
int radius_; {
public: Circle a(0,0,10);
Circle(int x, int y, int }
radius);
};
Encapsulation
class Figure Circle::Circle(int x, int y, int
{ radius)
private: {
int x_, y_; x_ = x;
}; y_ = y;
radius_ = radius;
class Circle : public Figure }
{
private: int main()
int radius_; {
public: Circle a(0,0,10);
Circle(int x, int y, int }
radius);
}; example.cpp: In constructor ‘Circle::Circle(int, int, int)’:
example.cpp:5: error: ‘int Figure::x_’ is private
example.cpp:18: error: within this context
example.cpp:5: error: ‘int Figure::y_’ is private
example.cpp:19: error: within this context
Encapsulation
class Figure class Circle : public Figure
{ {
private: private:
int radius_;
int x_, y_;
public:
public: Circle(int x, int y, int
void SetX(int x); radius);
void SetY(int y); };
}; Circle::Circle(int x, int y, int
radius)
void Figure::SetX(int x) {
{ SetX(x);
x_ = x; SetY(y);
} this->radius_ = radius;
}
void Figure::SetY(int y)
int main()
{
{
y_ = y; Circle a(0,0,10);
} }
What is the result?
class Figure class Circle : public Figure
{ {
public: public:
Figure() { Circle() {
cout << "Figure cout << "Circle
Constructor\n"; Constructor\n";
} }
~Figure() { ~Circle() {
cout << "Figure cout << "Circle
Destructor\n"; Destructor\n";
} }
}; };

int main()
{
Circle a;
}
Inheritance and Constructors
• When creating a object from derived class, also
the member values of the base class must be
initialized
• Base constructor is called before the derived
classes constructor
• Destructors vice versa.
Calling the Base Classes constructor
class Figure class Circle : public Figure
{ {
public: public:
Figure() { Circle() : Figure() {
cout << "Figure cout << "Circle
Constructor\n"; Constructor\n";
} }
~Figure() { ~Circle() {
cout << "Figure cout << "Circle
Destructor\n"; Destructor\n";
} }
}; };

int main()
{
Circle a;
}
Calling the Base Classes constructor
class Figure
{
private:
int x_, y_;
public:
Figure(int x, int y) : x_(x), y_(y) {
cout << "Figure Constructor\n";
}
~Figure() {
cout << "Figure Destructor\n";
}
};
Calling the Base Classes constructor
class Circle : public Figure
{
private:
double radius_;
public:
Circle(int x, int y, int radius) : Figure(x, y),
radius_(radius)
{
cout << "Circle Constructor\n";
}
~Circle() {
cout << "Circle Destructor\n";
}
};

int main()
{
Circle a(0,0,5);
}
Abstract Class
• In C++, Abstract class is a class that has one
abstract method
• Abstract method is a method without
implementation.
• Abstract method is created by reserverd word
"virtual"
Example of Abstract class
class Figure
{
private:
int x_, y_;
public:
Figure(int x, int y) : x_(x), y_(y) {
cout << "Figure Constructor\n";
}
~Figure() {
cout << "Figure Destructor\n";
}

virtual double calculateArea() = 0;


};
Example of Abstract class
class Circle : public Figure
{
private:
double radius_;
public:
Circle(int x, int y, int radius) : Figure(x, y),
radius_(radius)
{
cout << "Circle Constructor\n";
}
~Circle() {
cout << "Circle Destructor\n";
}
double calculateArea() {
return 3.14 * radius_ * radius_;
}
};
Example of Abstract class
int main()
{
Circle a(0,0,5);
cout << a.calculateArea() << endl;

// This Does not work, since figure is abstract:


// Figure f(0,0);
}

You might also like