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

Inheritance

The document provides an overview of inheritance in object-oriented programming, defining it as the process of extending an existing class (base class) into a new class (derived class). It explains what is inherited from the base class, the syntax for implementing inheritance, and the different access control levels (public, protected, private) in inheritance. Several examples illustrate the concepts of inheritance using classes such as Vehicle, Polygon, Car, and Rectangle.

Uploaded by

hfzarj
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)
3 views

Inheritance

The document provides an overview of inheritance in object-oriented programming, defining it as the process of extending an existing class (base class) into a new class (derived class). It explains what is inherited from the base class, the syntax for implementing inheritance, and the different access control levels (public, protected, private) in inheritance. Several examples illustrate the concepts of inheritance using classes such as Vehicle, Polygon, Car, and Rectangle.

Uploaded by

hfzarj
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/ 16

Object Oriented Programming

Lecture
Inheritance
Inheritance

Definition:
“Process of extending existing class into new class is known as
inheritance”
• Existing class is known as Base Class (or Parent Class)
• New class is known as Derived Class (or Child Class)

What is inherited from the base class?


Every member of a base class except:
• constructors and its destructor
• assignment operator members (operator=)
• friends
• private members (Hidden)
UML Notation

Parent Class Base Class

Child Class Derived Class


Inheritance (example)

Vehicle Polygon
class class

Car Truck Rectangle Triangle


class class class class
Inheritance (Syntax and Example)

Syntax
class Child : public Parent {
class ChildClass: public BaseClass public:
{ int multiply()
... {
}; return x * y;
}
#include <iostream> };
using namespace std;

class Parent int main()


{ {
public: Child var1;
var1.set_values(4, 5);
int x, y; cout << var1.multiply();
void set_values(int a, int b) return 0;
{ }
x = a; y = b;
}
};
Access in Inheritance

• There are three different access control in inheritance

• Public
• Protected
• Private

• Use keyword public, private or protected to define access level in


inheritance
Public Inheritance

class Child: public Parent {…};

Member access in
Base Class Derived Class
Public Public
Protected Protected
Private Hidden
Protected Inheritance

class Child: protected Parent {…};

Member access in
Base Class Derived Class
Public Protected
Protected Protected
Private Hidden
Private Inheritance

class Child: private Parent {…};

Member access in
Base Class Derived Class
Public Private
Protected Private
Private Hidden
Inheritance (example)
#include <iostream>
using namespace std;

class Polygon {
private:
int width, height;
public:
void set_values(int a, int b)
{
width = a; height = b;
}
int get_width()
{
return width;
}
int get_height()
{
return height;
}

};
Inheritance (example)
#include <iostream>
using namespace std; class Rectangle : public Polygon {
public:
class Polygon { int area()
private: {
int width, height; return get_width() * get_height();
public: }
void set_values(int a, int b) };
{
width = a; height = b;
}
int get_width()
{
return width;
}
int get_height()
{
return height;
}

};
Inheritance (example)
#include <iostream>
using namespace std; class Rectangle : public Polygon {
public:
class Polygon { int area()
private: {
int width, height; return get_width() * get_height();
public: }
void set_values(int a, int b) };
{
width = a; height = b;
}
int get_width() int main()
{ {
return width; Rectangle rec1;
} rec1.set_values(4, 5);
int get_height() cout << rec1.area();
{ return 0;
return height; }
}

};
Inheritance (example)
#include <iostream>
using namespace std; class Rectangle : protected Polygon {
public:
class Polygon { int area()
private: {
int width, height; return get_width() * get_height();
public: }
void set_values(int a, int b) };
{
width = a; height = b;
}
int get_width() int main()
{ {
return width; Rectangle rec1;
} rec1.set_values(4, 5); Error
int get_height() cout << rec1.area();
{ return 0;
return height; }
}

};
Inheritance (example)
#include <iostream>
using namespace std; class Rectangle : private Polygon {
public:
class Polygon { int area()
private: {
int width, height; return get_width() * get_height();
public: }
void set_values(int a, int b) };
{
width = a; height = b;
}
int get_width() int main()
{ {
return width; Rectangle rec1;
} rec1.set_values(4, 5); Error
int get_height() cout << rec1.area();
{ return 0;
return height; }
}

};
Thanks a lot

You might also like