0% found this document useful (0 votes)
3 views3 pages

Inheritance

The document explains different types of inheritance in object-oriented programming: single inheritance, multiple inheritance, multilevel inheritance, hierarchical inheritance, and hybrid inheritance. Each type is illustrated with code examples demonstrating how derived classes can inherit characteristics from base classes. The document highlights the relationships and structures formed through these inheritance types.
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 views3 pages

Inheritance

The document explains different types of inheritance in object-oriented programming: single inheritance, multiple inheritance, multilevel inheritance, hierarchical inheritance, and hybrid inheritance. Each type is illustrated with code examples demonstrating how derived classes can inherit characteristics from base classes. The document highlights the relationships and structures formed through these inheritance types.
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

INHERITANCE

Single Inheritance: In single inheritance, a derived class inherits


from only one base class.

class Base {
public:
void display() {
cout << "Base class" << endl;
}
};

class Derived : public Base {


public:
void show() {
cout << "Derived class" << endl;
}
};

Multiple Inheritance: In multiple inheritance, a derived class


inherits from more than one base class. This allows the derived
class to have characteristics of multiple base classes.

class Base1 {
public:
void display1() {
cout << "Base1 class" << endl;
}
};

class Base2 {
public:
void display2() {
cout << "Base2 class" << endl;
}
};

class Derived : public Base1, public Base2 {


public:
void show() {
cout << "Derived class" << endl;
}
};
Multilevel Inheritance: In multilevel inheritance, a derived class is
created from another derived class. This forms a chain of
inheritance.
class Base {
public:
void display() {
cout << "Base class" << endl;
}
};

class Derived1 : public Base {


public:
void show1() {
cout << "Derived1 class" << endl;
}
};

class Derived2 : public Derived1 {


public:
void show2() {
cout << "Derived2 class" << endl;
}
};

Hierarchical Inheritance: In hierarchical inheritance, multiple


classes are derived from a single base class.

class Base {
public:
void display() {
cout << "Base class" << endl;
}
};

class Derived1 : public Base {


public:
void show1() {
cout << "Derived1 class" << endl;
}
};

class Derived2 : public Base {


public:
void show2() {
cout << "Derived2 class" << endl;
}
};

Hybrid Inheritance: Hybrid inheritance is a combination of multiple


and multilevel inheritance. It involves multiple inheritance paths.

class Base1 {
public:
void display1() {
cout << "Base1 class" << endl;
}
};

class Base2 {
public:
void display2() {
cout << "Base2 class" << endl;
}
};

class Derived1 : public Base1, public Base2 {


public:
void show1() {
cout << "Derived1 class" << endl;
}
};

class Derived2 : public Derived1 {


public:
void show2() {
cout << "Derived2 class" << endl;
}
};

You might also like