0% found this document useful (0 votes)
12 views13 pages

Understanding Inheritance in C++

The document summarizes key concepts related to derived classes in C++, including: 1) Inheritance allows a class to inherit properties and behaviors from a base class. There are different types like single, multiple, and multilevel inheritance. 2) Friend classes are granted special access privileges to private and protected members of another class. 3) A virtual base class ensures only a single instance is shared among derived classes in multiple inheritance scenarios to avoid ambiguity issues.

Uploaded by

Lilia Bournane
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)
12 views13 pages

Understanding Inheritance in C++

The document summarizes key concepts related to derived classes in C++, including: 1) Inheritance allows a class to inherit properties and behaviors from a base class. There are different types like single, multiple, and multilevel inheritance. 2) Friend classes are granted special access privileges to private and protected members of another class. 3) A virtual base class ensures only a single instance is shared among derived classes in multiple inheritance scenarios to avoid ambiguity issues.

Uploaded by

Lilia Bournane
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

Chapter 5.

Derived classes (1 week)

- Inheritance and instantiation


- Friend « classes »
- Virtual Base Class
- Multiple inheritance
Inheritance
• Inheritance is a mechanism that allows a class
to inherit properties and behaviors from
another class.
• The class that is being inherited from is called
the base class or parent class, and the class
that inherits is called the derived class or child
class.
• Inheritance facilitates code reuse and the
creation of a hierarchy of classes.
• There are several types of inheritance in C++:
Single Inheritance: A class can inherit from only one base class.
class Base
{ // Base class members};

class Derived : public Base


{ // Derived class members};

Multiple Inheritance: A class can inherit from more than one base
class.
class Base1
{ // Base1 class members};
class Base2
{ // Base2 class members};

class Derived : public Base1, public Base2


{ // Derived class members};
Multilevel Inheritance: A class can be derived from another
derived class, creating a hierarchy of classes.
class Base
{ // Base class members};

class Derived1 : public Base


{ // Derived1 class members};

class Derived2 : public Derived1


{ // Derived2 class members};

It's worth noting that C++ supports different access specifiers


(public, private, protected) for class members, which control the
visibility and accessibility of those members in derived classes and
client code.
// base class int main() {
class Animal { // Create object of the Dog class
public: Dog dog1;
void eat() {
cout << "I can eat!" << endl; // Calling members of the base class
} [Link]();
[Link]();
void sleep() {
cout << "I can sleep!" << endl; // Calling member of the derived class
} [Link]();
};
return 0;
// derived class }
class Dog : public Animal {
public: Output:
void bark() { I can eat!
cout << "I can bark! Woof woof!!" << I can sleep!
endl; I can bark! Woof woof!!
}
};
#include <iostream> class Dog : public Animal {// derived class
#include <string> public:
using namespace std; void setType(string tp) {
type = tp; }
class Animal { // base class
private: void displayInfo(string c) {
string color; cout << "I am a " << type << endl;
protected: cout << "My color is " << c << endl;}
string type;
public: void bark() {
void eat() { << "I can bark! Woof woof!!" << endl; }
cout << "I can eat!" << endl; } };

void sleep() {
cout << "I can sleep!" << endl; }

void setColor(string clr) { color = clr; }

string getColor() { return color;}


};
int main() { Output:
Dog dog1; // Create object of the Dog class I can eat!
I can sleep!
// Calling members of the base class I can bark! Woof woof!!
[Link](); I am a mammal
[Link](); My color is black
[Link]("black");
IMPORTANT:
// Calling member of the derived class 1. the variable type is protected and is
[Link](); thus accessible from the derived
[Link]("mammal"); class Dog. We can see this as we
have initialized type in the Dog class
// Using getColor() of dog1 as argument using the function setType().
// getColor() returns string data
[Link]([Link]()); 2. the private variable color cannot be
initialized in Dog
return 0;
} 3. since the protected keyword hides
data, we cannot access type directly
from an object of Dog or Animal
class.
Friend « classes »
• The friend keyword is used to designate a
function or an entire class as a friend of
another class.
• When a class or function is declared as a
friend of another class, it is granted special
access privileges to the private and protected
members of that class.
class FriendClass; // Forward declaration of FriendClass
class MyClass
{private:
int privateData;
public:
MyClass(int data) : privateData(data)
{}

friend class FriendClass; // Declaration of FriendClass as a friend


};

class FriendClass // Definition of FriendClass


{public:
void accessPrivateData(MyClass& obj)
{ // FriendClass can access private members of MyClass
cout << "Accessing private data: " << [Link] << endl; }
};
int main()
{ MyClass obj(42); // FriendClass can access private members of MyClass
FriendClass friendObj;
[Link](obj);
return 0;}
In this example, FriendClass is declared as a friend of MyClass
using the friend keyword.

As a result, FriendClass can access the private member


privateData of MyClass directly.

The main function demonstrates how an instance of FriendClass


can access and print the private data of an object of MyClass.
Virtual Base Class
• A virtual base class is a class that is intended
to be used as a common base class for
multiple derived classes.
• It helps to avoid certain problems that can
arise in multiple inheritance scenarios.
• When a base class is declared as virtual, it
ensures that only a single instance of the base
class is shared among the derived classes.
class Base class MultipleDerived : public Derived1,
{public: public Derived2
int data; {public: MultipleDerived(int value) :
Base(int value) : data(value) {} Base(value), Derived1(value),
void display() const Derived2(value) {}
{ cout << "Base::display() - data: " << data };
<< std::endl; }
}; int main()
{ MultipleDerived obj(42);
class Derived1 : public virtual Base // Access data member from the base class
{public: [Link]();
Derived1(int value) : Base(value) {} return 0;
}; }

class Derived2 : public virtual Base


{public: Derived2(int value) : Base(value)
{}
};
In this example, both Derived1 and Derived2 virtually inherit from
the Base class, and MultipleDerived inherits from both Derived1
and Derived2.

By using the virtual keyword in the inheritance, you ensure that


there is only one instance of the Base class within MultipleDerived.

This helps to avoid a problem that can occur in multiple


inheritance, where ambiguity arises due to the presence of multiple
instances of a common base class.

The display function from the Base class can be called through an
instance of MultipleDerived, and it will work correctly without
ambiguity.

You might also like