January 5, 2023

Inheritance

Posted on January 5, 2023  •  4 minutes  • 758 words

C++ supports inheritance as a key feature of Object Oriented Programming (OOP). It is a method for creating new classes (derived classes) from existing classes (base classes).

Base class and Derived class

A derived class inherits the features of a base class as well as its own features.

output

Each shape has two properties, for example area and perimeter. And child classes should be able to access these properties, so we have given them the protected access specifier.

So we can create a class Shape as

class Shape
{
public:
    double getArea()
    {
        return area;
    }

    double getPerimeter()
    {
        return perimeter;
    }

protected:
    double area;
    double perimeter;
};

where getArea() and getPerimeter() are public functions to access area and perimeter.

In the next section, we will learn how to define a derived class from a base class

Syntax

class BaseClassName
{
    // properties
};


class ChildClassName : access_specifier BaseClassName
{
    // properties

    //1. Inherited Properties

    //2. Its own Properties
};

Example

For now we consider a Circle shape

  1. Circle has a radius
  2. Circle has perimeter
  3. Circle has area

Area and perimeter are inherited properties of Circles, while radius is its own property.

So we can inherit Circle class from Shape class.

class Circle : public Shape
{
public:

    Circle(double rad)
    {
        radius = rad;
    }


private:
    double radius;

};

But here we have to calculate area and perimeter from circle equations.

In that case, we need to create two functions as private in Circle class.

  1. calculateArea
double calculateArea()
{
    return (3.14 * radius * radius);
}
  1. calculatePerimeter
double calculatePerimeter()
{
    return (2 * 3.14 * radius);
}

Next we can access the protected members of Shape class from Circle class. so we can set the values of area and perimeter from the Circle class.

class Circle : public Shape
{
public:
    Circle(double rad)
    {
        radius = rad;
        area = calculateArea();
        perimeter = calculatePerimeter();
    }

private:
    double calculateArea()
    {
        return (3.14 * radius * radius);
    }

    double calculatePerimeter()
    {
        return (2 * 3.14 * radius);
    }

    double radius;

};

Similarly we can create Rectangle and Triangle classes

  1. Rectangle Class
class Rectangle : public Shape
{
public:
    Rectangle(double len, double wid)
    {
        length = len;
        width = wid;
        area = calculateArea();
        perimeter = calculatePerimeter();
    }

private:
    double calculateArea()
    {
        return (length * width);
    }

    double calculatePerimeter()
    {
        return (2 * (length + width));
    }

    double length;
    double width;
};
  1. Triangle Class
class Triangle : public Shape
{
public:
    Triangle(double s1, double s2, double s3)
    {
        side1 = s1;
        side2 = s2;
        side3 = s3;
        area = calculateArea();
        perimeter = calculatePerimeter();
    }

private:
    double calculateArea()
    {
        double s = (side1 + side2 + side3) / 2;
        double area = sqrt(s * (s-side1) * (s-side2) * (s-side3));
        return area;
    }

    double calculatePerimeter()
    {
        return (side1 + side2 + side3);
    }

    double side1;
    double side2;
    double side3;

};

where sqrt() function is used to calculate square root

#include <cmath>

Full program

#include <iostream>
#include <cmath>

class Shape
{
public:
    double getArea()
    {
        return area;
    }

    double getPerimeter()
    {
        return perimeter;
    }

protected:
    double area;
    double perimeter;
};

class Circle : public Shape
{
public:
    Circle(double rad)
    {
        radius = rad;
        area = calculateArea();
        perimeter = calculatePerimeter();
    }

private:
    double calculateArea()
    {
        return (3.14 * radius * radius);
    }

    double calculatePerimeter()
    {
        return (2 * 3.14 * radius);
    }

    double radius;
};


class Rectangle : public Shape
{
public:
    Rectangle(double len, double wid)
    {
        length = len;
        width = wid;
        area = calculateArea();
        perimeter = calculatePerimeter();
    }

private:
    double calculateArea()
    {
        return (length * width);
    }

    double calculatePerimeter()
    {
        return (2 * (length + width));
    }

    double length;
    double width;
};

class Triangle : public Shape
{
public:
    Triangle(double s1, double s2, double s3)
    {
        side1 = s1;
        side2 = s2;
        side3 = s3;
        area = calculateArea();
        perimeter = calculatePerimeter();
    }

private:
    double calculateArea()
    {
        double s = (side1 + side2 + side3) / 2;
        double area = sqrt(s * (s-side1) * (s-side2) * (s-side3));
        return area;
    }

    double calculatePerimeter()
    {
        return (side1 + side2 + side3);
    }

    double side1;
    double side2;
    double side3;
};


int main(int argc, char const *argv[])
{
    Circle c1(10);
    std::cout << "Circle Area : " << c1.getArea() << std::endl;
    std::cout << "Circle Perimeter : " << c1.getPerimeter() << std::endl << std::endl;

    Rectangle r1(10,20);
    std::cout << "Rectangle Area : " << r1.getArea() << std::endl;
    std::cout << "Rectangle Perimeter : " << r1.getPerimeter() << std::endl << std::endl;

    Triangle t1(3,4,5);
    std::cout << "Triangle Area : " << t1.getArea() << std::endl;
    std::cout << "Triangle Perimeter : " << t1.getPerimeter() << std::endl << std::endl;
    
    return 0;
}

Follow me

I work on everything coding and share developer memes