Himanshu Raghav
Himanshu Raghav

Reputation: 31

Why is this program of hybrid inheritance giving wrong output?

In this program, I am trying to inculcate hybrid inheritance in this program but this is giving wrong output. I have taken arithmetic as base class and add,sub,mul,div as its derived classes.

And then I have derived a class result using add,sub,mul,div as base classes. I have tried all the data types but with all it gives wrong or zero output.

#include "iostream"
using namespace std;

class arithmetic
{
public:
    float var1,var2;
    void introduce()
    {
        cout<<"This program will perform arithmetic on two variables"<<endl
        <<"Enter the first variable: ";
        cin>>var1;
        cout<<"Enter the second variable: ";
        cin>>var2;
    }
};

class add:public arithmetic
{
protected:
    float res_add;
public:
    void show_add()
    {
        res_add=var1+var2;

        cout<<"Addition of those variables gives "<<res_add<<endl;
    }
};
class sub:public arithmetic
{
protected:
    float res_sub;
public:
    void show_sub()
    {
        res_sub=var1-var2;

        cout<<"Subtraction of those variables gives "<<res_sub<<endl;
    }
};
class mul:public arithmetic
{
protected:
    float res_mul;
public:
    void show_mul()
    {
        res_mul=var1*var2;

        cout<<"Multiplication of those variables gives "<<res_mul<<endl;
    }
};
class div:public arithmetic
{
protected:
    float res_div;
public:
    void show_div()
    {
        res_div=var1/var2;

        cout<<"Divison of those variables gives "<<res_div<<endl;
    }
};
class result:public add, public sub,public mul,public div
{
public:
    void showres()
    {
        cout<<"Arithmetic on the given two varibales gives us the following result:"<<endl;
    }
};

int main()
{
    result example;
    arithmetic var;
    var.introduce();
    example.showres();
    example.show_add();
    example.show_sub();
    example.show_mul();
    example.show_div();
    return 0;
}

Upvotes: 2

Views: 191

Answers (2)

Sanjeev
Sanjeev

Reputation: 358

You are creating two separate object var (base class object) and example ( derived class object). Initializing the var1 and var2 by calling the var.introduce() and calling the methods show_add(), show_sub() etc. in the example object, in example object var1 and var2 is not initialized. so whatever arithmetic operation you are calling is getting applied in the uninitialized var1 and var2 member variable of example object.

you don't need to create the base class object(var). call the intruduce() method from the example then it will start working fine.

Please go through the below sample code to understand the concept of virtual base class.

#include <iostream>

class A 
{ 
    public: 
        int i; 
};

class B : virtual public A 
{ 
    public: 
        int j; 
};

class C: virtual public A 
{ 
    public: 
       int k; 
};

class D: public B, public C 
{ 
    public: 
        int sum; 
};

int main() 
{ 
    D ob; 
    ob.i = 10; //unambiguous since only one copy of i is inherited. 
    ob.j = 20; 
    ob.k = 30; 
    ob.sum = ob.i + ob.j + ob.k; 
    std::cout << "Value of i is : "<< ob.i<<"\n"; 
    std::cout << "Value of j is : "<< ob.j<<"\n";
    std::cout << "Value of k is : "<< ob.k<<"\n"; 
    std::cout << "Sum is : "<< ob.sum <<"\n"; 

    return 0; 
}

Output:

Value of i is : 10
Value of j is : 20
Value of k is : 30
Sum is : 60

Live Demo on coliru

Upvotes: 1

P.W
P.W

Reputation: 26800

If you want to avoid virtual inheritance and want something simpler, use composition.
For this, class result should contain objects of add, sub, mul and div.

The resulting code for result will look something like this:

class result:public arithmetic
{
public:

    mul m;
    add a;
    sub s;
    div d;  
    void assignvals() 
    {
        m.var1 = var1; m.var2 = var2;
        a.var1 = var1; a.var2 = var2;
        s.var1 = var1; s.var2 = var2;
        d.var1 = var1; d.var2 = var2;
    }
    void showres()
    {               
        cout<<"Arithmetic on the given two variables gives us the following result:"<<endl;
    }
};

The resulting code for main will look something like this:

int main()
{
    result example;    
    example.introduce();
    example.assignvals();
    example.showres();
    example.a.show_add();
    example.s.show_sub();
    example.m.show_mul();
    example.d.show_div();
    return 0;
}

Note: Some compilers will complain if div is used as a class name because it is also the name of a library function in C++. So you will have to change the name of this class.

Upvotes: 0

Related Questions