Provides a way to create a new class from an existing class.
New class is a specialized version of the existing class.
inherited
by child class.inherits
from base class.class Student{ //base class
//body
};
class UnderGrad : public Student{ //derived class
//body
};
Inheritance establishes an “is a” relationship between classes.
An object of the derived class “is a” object of the base class. For example:
A derived object has all characteristics of the base class.
An object of child class has:
An object of child class can use:
public
members defined in the child class.public
members defined in the parent class.Protected Members
protected
member access specification: similar to private
, but accessible by objects of derived class.Class Access Specifiers
public
: the object of the derived class can be treated as an object of the base class.protected
: more restrictive than public, but allows derived classes to know details of parents.private
: prevents objects of the derived class to be treated as objects of base class.#include <iostream>
using namespace std;
// Base class
class Shape {
public:
Shape(){length = 0; width = 0;} //default constructor
void setlength(int l, int w) {length = l; width = w;}
protected:
int length, width;
};
// Derived class
class Square: public Shape {
public:
Square() : Shape() {} //declaring and initializing derived class constructor
int get_Area(){ return (length * width); }
};
int main(void) {
Square sq; //making object of child class Square
sq.setlength(5, 5); //setting length equal to 5
// Print the area of the object.
cout << "Total area of square is: " << sq.get_Area() << endl;
return 0;
}
As you can see in the example above,
Try the following example in the editor below.
With reference to above example, create a derived class Rectangle inherited from class Shape, your task is to create a public function in it named get_Perimeter
which return the perimeter of rectange.