Prepared By :
Abdullah Salih
Fatima Muhammed karim
Sawsan Jasim Muhammed
Rebin Faisal Wasman
Rozhgar Muhsin
Supervised By :
Dr. Chiman Haidar
Erbil Polytechnic University
ICTE Department
Programming Language ll
2023-2024
Structure Of Presentation
• WhatisInheritance?
• WhatisProtected?
• TypesOfInheritance
• Example
• ConstructorinInheritance
What is Inheritance?
• Inheritance is one of the key features of Object-oriented
programming in C++. It allows us to create a
new class (derived class) from an existing class (base
class).
• Base Class A base class is a class that is created
with the intention of deriving other classes from it.
Syntax & Example 1
• Syntax :
class nameclass1
{
};
class nameclass2 : public
nameclass2
{
};
protected Members
• The access modifier protected is especially relevant when it
comes to C++ inheritance.
• Like private members, protected members are inaccessible
outside of the class. However, they can be accessed
by derived classes and friend classes/functions.
• We need protected members if we want to hide the data of a
class, but still want that data to be inherited by its derived
classes.
protected Members
Types Of Inheritance
Single
Inheritance
Multilevel
Inheritance
Multiple
Inheritance
Hierarchical
Inheritance
Hybird
Inheritance
1. Single Inheritance
• In a single inheritance the derived class is derived from a single base
class.
class Animal
{
// eat() function
// sleep() function
};
class Dog : public Animal
{
// bark() function
};
Example:
2. Multilevel Inheritance
• In C++ programming, a class be can derived from a derived class
which is known as multilevel inheritance.
class A
{ .... ... .... };
class B : public A
{ .... ... .... };
class C : public B
{ .... ... .... };
Example:
3. Multiple Inheritance
• It is the inheritance hierarchy wherein one derived class inherits
from multiple base class(es).
class A
{ .... ... .... };
class B
{ .... ... .... };
class C : public A ,public B
{ .... ... .... };
Example:
4. Hierarchical Inheritance
• In hierarchical inheritance several classes can be derived
from a single base class.
class A
{ .... ... .... };
class B :public A
{ .... ... .... };
class C : public A
{ .... ... .... };
class D : public A
{ .... ... .... };
Example:
5. Hybrid Inheritance
• The inheritance hierarchy that reflects any legal combination of
other four types of inheritance.
class A
{ .... ... .... };
class B :public A
{ .... ... .... };
class C : public A
{ .... ... .... };
class D : public B , public C
{ .... ... .... };
Example:
Constructer in Inheritance
class Base
{
public:
Base ()
{ cout << "Default of Base" << endl;
}
Base (int x)
{ cout << "Param of Base " << x <<
endl;}
};
Example:
class Derived : public Base
{
public:
Derived ()
{
cout << "Default of Derived" << endl;
}
Derived (int a)
{
cout << "Param of Derived : " << a <<
endl;
}
};
int main()
{
Derived d(5);
}
Constructer in Inheritance
class Base
{ public:
Base ()
{ cout << "Default of Base" << endl; }
Base (int x) { cout << "Param of Base " << x << endl; }
};
class Derived : public Base {
public:
Derived ()
{ cout << "Default of Derived" << endl; }
Derived (int a)
{ cout << "Param of Derived : " << a << endl; }
Derived(int x, int a) : Base(x) { cout << "Param of Derived " << a; } };
Example:
int main()
{ Derived d(25, 15);
}
inheritance in C++ programming Language.pptx

inheritance in C++ programming Language.pptx

  • 1.
    Prepared By : AbdullahSalih Fatima Muhammed karim Sawsan Jasim Muhammed Rebin Faisal Wasman Rozhgar Muhsin Supervised By : Dr. Chiman Haidar Erbil Polytechnic University ICTE Department Programming Language ll 2023-2024
  • 2.
    Structure Of Presentation •WhatisInheritance? • WhatisProtected? • TypesOfInheritance • Example • ConstructorinInheritance
  • 3.
    What is Inheritance? •Inheritance is one of the key features of Object-oriented programming in C++. It allows us to create a new class (derived class) from an existing class (base class). • Base Class A base class is a class that is created with the intention of deriving other classes from it.
  • 4.
    Syntax & Example1 • Syntax : class nameclass1 { }; class nameclass2 : public nameclass2 { };
  • 5.
    protected Members • Theaccess modifier protected is especially relevant when it comes to C++ inheritance. • Like private members, protected members are inaccessible outside of the class. However, they can be accessed by derived classes and friend classes/functions. • We need protected members if we want to hide the data of a class, but still want that data to be inherited by its derived classes.
  • 6.
  • 7.
  • 8.
    1. Single Inheritance •In a single inheritance the derived class is derived from a single base class. class Animal { // eat() function // sleep() function }; class Dog : public Animal { // bark() function }; Example:
  • 9.
    2. Multilevel Inheritance •In C++ programming, a class be can derived from a derived class which is known as multilevel inheritance. class A { .... ... .... }; class B : public A { .... ... .... }; class C : public B { .... ... .... }; Example:
  • 10.
    3. Multiple Inheritance •It is the inheritance hierarchy wherein one derived class inherits from multiple base class(es). class A { .... ... .... }; class B { .... ... .... }; class C : public A ,public B { .... ... .... }; Example:
  • 11.
    4. Hierarchical Inheritance •In hierarchical inheritance several classes can be derived from a single base class. class A { .... ... .... }; class B :public A { .... ... .... }; class C : public A { .... ... .... }; class D : public A { .... ... .... }; Example:
  • 12.
    5. Hybrid Inheritance •The inheritance hierarchy that reflects any legal combination of other four types of inheritance. class A { .... ... .... }; class B :public A { .... ... .... }; class C : public A { .... ... .... }; class D : public B , public C { .... ... .... }; Example:
  • 13.
    Constructer in Inheritance classBase { public: Base () { cout << "Default of Base" << endl; } Base (int x) { cout << "Param of Base " << x << endl;} }; Example: class Derived : public Base { public: Derived () { cout << "Default of Derived" << endl; } Derived (int a) { cout << "Param of Derived : " << a << endl; } }; int main() { Derived d(5); }
  • 14.
    Constructer in Inheritance classBase { public: Base () { cout << "Default of Base" << endl; } Base (int x) { cout << "Param of Base " << x << endl; } }; class Derived : public Base { public: Derived () { cout << "Default of Derived" << endl; } Derived (int a) { cout << "Param of Derived : " << a << endl; } Derived(int x, int a) : Base(x) { cout << "Param of Derived " << a; } }; Example: int main() { Derived d(25, 15); }