Home Uncategorized Write C++ Programs and incorporating various forms of Inheritance

Write C++ Programs and incorporating various forms of Inheritance

SHARE

C++ Programs and incorporating various forms of Inheritance

Reusability is one of the important features in “OOP”. It is always nice, if we could reuse something that already exists rather than trying to create the same allover again. C++ strongly supports the concept of reusability.

C++ classes can be reused in several ways. Once a class been written and tested it can be adapted by other programmers to suit their requirements. This is basically done by creating new class reuse of the properties of existing class. The mechanism of deriving a new class from an existing one is called INHERITANCE.

The old class is refer to as “BASE Class” and the new one is called “Derived Class”.

Syntax:

class derived_class: (visibility mode) Base_class
{
     Coding of Derived_class;
};

The colon ( : ) indicates that the derived class is derived from base class. the visibility mode is optional. If it is present may be either private. The default visibility mode is private. The visibility mode is specifies whether the features of the base class are privately derived or publicly derived.

When a base class is privately inherited by a base class, “public members of the base class become “Private members of the derived class.

When a base class is publicly inherits by a derived class, “Public members “ of the base class become “public members” of the derived class.

Types of inheritance:

The c++ classes can be derived in several ways. Based on that the inheritance can be divided in to Five types.

  1. Single Inheritance.
  2. Hierarchical Inheritance.
  3. Multiple Inheritances.
  4. Multi Level Inheritance.
  5. Hybrid or Multi-path Inheritance.

i) Single Inheritance

AIM: Write a C++ program to illustrate Single Inheritance.

THEORY:

Single inheritance enables a derived class to inherit properties and behavior from a single parent class. It allows a derived class to inherit the properties and behavior of a base class, thus enabling code reusability as well as adding new features to the existing code. This makes the code much more elegant and less repetitive. Inheritance is one of the key features of object-oriented programming (OOP).

Single inheritance is safer than multiple inheritance if it is approached in the right way. It also enables a derived class to call the parent class implementation for a specific method if this method is overridden in the derived class or the parent class constructor.

The inheritance concept is used in many programming languages, including C++, Java, PHP, C#, and Visual Basic. To implement inheritance, C++ uses the “:” operator, while Java and PHP use the “extend” keyword, and Visual Basic uses the keyword “inherits.” Java and C# enable single inheritance only, while other languages like C++ support multiple inheritance.

Image of Single or Simple Inheritance
Single or Simple Inheritance

SOURCE CODE:

#include<iostream>
using namespace std;
class A
{
    protected:
    char name[10];
    int age;
};
class B:public A
{
    public:
    float h;
    int w;
    void get_data()
    {
        cout<<“Enter name and age:\n”;
        cin>>name>>age;
        cout<<“\n Enter weight and height:\n”;
        cin>>w>>h;
    }
    void show()
    {
        cout<<“Name: “<<name<<endl;
        cout<<“Age: “<<age<<endl;
        cout<<“Weight: “<<w<<endl;
        cout<<“Height: “<<h<<endl;
    }
};
int main()
{
    B C;
    C.get_data();
    C.show();
}

OUTPUT:

Enter name and age:
Radhika       52
Enter weight and height:
72           5.5
Name: Radhika
Age: 52
Weight: 72
Height: 5.5

ii) Hierarchical Inheritance.

AIM: Write a C++ program to incorporate Hierarchical Inheritance.

THEORY:

In Object Oriented Programming, the root meaning of inheritance is to establish a relationship between objects. In Inheritance, classes can inherit behavior and attributes from pre-existing classes, called Base Classes or Parent Classes. The resulting classes are known as derived classes or child classes.

So in Hierarchical Inheritance, we have 1 Parent Class and Multiple Child Classes, as shown in the pictorial representation given on this page, Inheritance. Many programming problems can be cast into a hierarchy where certain features of one level are shared by many others below that level. One example could be classification of accounts in a commercial bank or classification of students in a university.

In C++, such problems can be easily converted into hierarchies. The base class will include all the features that are common to the subclasses. A subclass can be constructed by inheriting the properties of the base class. A subclass can serve as a base class for the lower level classes and so on.

A derived class with hierarchical inheritance is declared as follows:

 

class A {…..};            // Base class
class B: public A {…..};     // B derived from A
class C: public A {…..};     // C derived from A

Image of Hierarchical Inheritance
Hierarchical Inheritance

This process can be extended to any number of levels. Let us understand this concept by a simple C++ program:

SOURCE CODE:

#include<iostream>
using namespace std;
class A
{
    protected:
    char name[20];
    int age;
};
class B:public A
{
    public:
    float h;
    int w;
    void get_data1()
    {
        cout<<“Enter name:”;
        cin>>name;
        cout<<“Enter weight and height:”;
        cin>>w>>h;
    }
    void show()
    {
        cout<<“This is class B and it is inherited from Class A\n”;
        cout<<“Name: “<<name<<endl;
        cout<<“Weight: “<<w<<endl;
        cout<<“Height: “<<h<<endl;
    }
};
class C:public A
{
    public:
    char gender;
    void get_data2()
    {
        cout<<“Enter age:”;
        cin>>age;
        cout<<“Enter gender:”;
        cin>>gender;
    }
    void show()
    {
        cout<<“This is class C and it is inherited from class A\n”;
        cout<<“Age: “<<age<<endl;
        cout<<“Gender: “<<gender<<endl;
    }
};
int main()
{
    B ob;
    C ob1;
    ob.get_data1();
    ob1.get_data2();
    ob.show();
    ob1.show();
}

OUTPUT:

Enter name: Ramu
Enter weight and height: 63    5.8
Enter age: 26
Enter gender: M
This is class B and it is inherited form class A
Name: Ramu
Weight: 63
Height: 5.8
This is class C and it is inherited form class A
Age: 26
Gender: M

iii) Multiple Inheritances.

AIM: Write a C+ Inheritance+ program to incorporate Multiple Inheritance

THEORY:

Multiple inheritance is a feature of some computer programming languages in which an object or class can inherit characteristics and features from more than one parent object or parent class. It is distinct from single inheritance, where an object or class may only inherit from one particular object or class.

Multiple Inheritance has been a sensitive issue for many years, with opponents pointing to its increased complexity and ambiguity in situations such as the “diamond problem”, where it may be ambiguous as to which parent class a particular feature is inherited from if more than one parent class implements said feature. This can be addressed in various ways, including using virtual inheritance. Alternate methods of object composition not based on inheritance such as mixins and traits have also been proposed to address the ambiguity.

Image of Multiple Inheritance
Multiple Inheritance

SOURCE CODE:

#include<iostream>
using namespace std;
class A
{
    protected:
    char name[20];
    int age;
};
class B
{
    protected:
    int w;
    float h;
};
class C:public A,B
{
    public:
    char g;
    void get_data()
    {
        cout<<“Enter name and age:\n”;
        cin>>name>>age;
        cout<<“Enter weight and height:\n”;
        cin>>w>>h;
        cout<<“Enter gender: “;
        cin>>g;
    }
    void show()
    {
        cout<<“\nName: “<<name<<endl<<“Age: “<<age<<endl;
        cout<<“Weight: “<<w<<endl<<“Height: “<<h<<endl;
        cout<<“Gender: “<<g<<endl;
    }
};
int main()
{
    C ob;
    ob.get_data();
    ob.show();
}

OUTPUT:

Enter name and age:
Mukesh         31
Enter weight and height:
64           5.9
Enter gender: M
Name: Mukesh
Age: 31
Weight: 64
Height: 5.9
Gender: M

iv) Multi-level inheritance.

AIM: Write a C++ program for incorporating multi-level inheritance.

THEORY:

Inheritance is the process of inheriting properties of objects of one class by objects of another class. The class which inherits the properties of another class is called Derived or Child or Sub class and the class whose properties are inherited is called Base or Parent or Super class. When a class is derived from a class which is also derived from another class, i.e. a class having more than one parent classes, such inheritance is called Multilevel Inheritance. The level of inheritance can be extended to any number of level depending upon the relation. Multilevel inheritance is similar to relation between grandfather, father and child.

Image of Multi Level Inheritance
Multi-Level Inheritance

SOURCE CODE:

#include<iostream>
using namespace std;
class A
{
    protected:
    char name[20];
    int age;
};
class B:public A
{
    protected:
    int w;
    float h;
};
class C:public B
{
    public:
    char g;
    void get_data()
    {
        cout<<“Enter name and age:\n”;
        cin>>name>>age;
        cout<<“Enter weight and height:\n”;
        cin>>w>>h;
        cout<<“Enter gender:”;
        cin>>g;
    }
    void show()
    {
        cout<<“\nName: “<<name<<endl<<“Age: “<<age<<endl;
        cout<<“Weight: “<<w<<endl<<“Height: “<<h<<endl;
        cout<<“Gender: “<<g<<endl;
    }
};
int main()
{
    C ob;
    ob.get_data();
    ob.show();
}

OUTPUT:

Enter name and age:
Madhu     35
Enter weight and height:
58           5.6
Enter gender: F
Name: Madhu
Age: 35
Weight: 58
Height: 5.6
Gender: F

v) Hybrid inheritance.

AIM: Write a C++ program for incorporating Hybrid inheritance.

THEORY:

Hybrid inheritance is combination of two or more types of inheritance. It can also be called multi path inheritance.

For example, below diagram shows both Hierarchical Inheritance and multi level inheritance.

Image of Hybrid or Multi-Path Inheritance
Hybrid or Multi-Path Inheritance

SOURCE CODE:

#include<iostream>
using namespace std;
class A   //Base class
{
    public:
    int l;
    void len()
    {
        cout<<“Lenght: “;
        cin>>l;           //Lenght is enter by user
    }
};
class B :public A //Inherits property of class A
{
    public:
    int b,c;
    void l_into_b()
    {
        len();
        cout<<“Breadth: “;
        cin>>b;         //Breadth is enter by user
        c=b*l;            //c stores value of lenght * Breadth i.e. (l*b).
    }
};
class C
{
    public:
    int h;
    void height()
    {
        cout<<“Height: “;
        cin>>h;         //Height is enter by user
    }
};
class D:public B,public C  //Hybrid Inheritance Level
{
    public:
    int res;
    void volume()
    {
        l_into_b();
        height();
        res=h*c;
        cout<<“Volume is (l*b*h): “<<res<<endl;
    }
    void area()
    {
        l_into_b();
        cout<<“Area is (l*b): “<<c<<endl;
    }
};
int main()
{
    D d1;
    cout<<“Enter dimensions of object to get Area:\n”;
    d1.area();
    cout<<“Enter values of object to get Volume:\n”;
    d1.volume();
    return 0;
}

OUTPUT:

Enter dimensions of object to get Area:
Length: 63
Breadth: 23
Area is (l * b): 1449
Enter dimensions of object to get Volume:
Length: 12
Breadth: 27
Height: 14
Volume is (l * b * h): 4536

Back to Programs.