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

Inheritance in C++

Inheritance in C++ allows one class to inherit attributes and methods from another class, known as the base or parent class, to create a new class called the derived or child class. A derived class inherits all non-private members of the base class and can access them directly or define new behaviors. Access specifiers like public, private, and protected determine whether members can be accessed directly or indirectly from derived classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Inheritance in C++

Inheritance in C++ allows one class to inherit attributes and methods from another class, known as the base or parent class, to create a new class called the derived or child class. A derived class inherits all non-private members of the base class and can access them directly or define new behaviors. Access specifiers like public, private, and protected determine whether members can be accessed directly or indirectly from derived classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Inheritance

• In C++, it is possible to inherit attributes and


methods from one class to another. We group
the "inheritance concept" into two categories:
• derived class (child) - the class that inherits
from another class
• base class (parent) - the class being inherited
from
• To inherit from a class, use the : symbol.
// Base class
class Vehicle {
  public:
    string brand = "Ford";
    void honk() {
      cout << "Tuut, tuut! \n" ;
    }
};

// Derived class
class Car: public Vehicle {
  public:
    string model = "Mustang";
};

int main() {
  Car myCar;
  myCar.honk();
  cout << myCar.brand + " " + myCar.model;
  return 0;
}
Why And When To Use "Inheritance"?

It is useful for code reusability: reuse attributes


and methods of an existing class when you
create a new class.
Multilevel Inheritance

A class can also be derived from one class, which


is already derived from another class.

In the following example, MyGrandChild is


derived from class MyChild (which is derived
from MyClass).
// Base class (parent)
class MyClass {
  public:
    void myFunction() {
      cout << "Some content in parent class." ;
    }
};

// Derived class (child)


class MyChild: public MyClass {
};

// Derived class (grandchild)


class MyGrandChild: public MyChild {
};

int main() {
  MyGrandChild myObj;
  myObj.myFunction();
  return 0;
}
• A class can also be derived from more than one base class, using a comma-separated list:
• // Base class
class MyClass {
  public:
    void myFunction() {
      cout << "Some content in parent class." ;
    }
};

// Another base class


class MyOtherClass {
  public:
    void myOtherFunction() {
      cout << "Some content in another class." ;
    }
};

// Derived class
class MyChildClass: public MyClass, public MyOtherClass {
};

int main() {
  MyChildClass myObj;
  myObj.myFunction();
  myObj.myOtherFunction();
  return 0;
}
Access Specifiers

• The third specifier, protected, is similar to


private, but it can also be accessed in the
inherited class
• // Base class
class Employee {
  protected: // Protected access specifier
    int salary;
};

// Derived class
class Programmer: public Employee {
  public:
    int bonus;
    void setSalary(int s) {
      salary = s;
    }
    int getSalary() {
      return salary;
    }
};

int main() {
  Programmer myObj;
  myObj.setSalary(50000);
  myObj.bonus = 15000;
  cout << "Salary: " << myObj.getSalary() << "\n";
  cout << "Bonus: " << myObj.bonus << "\n";
  return 0;
}

You might also like