我在我的程序中使用了多重继承,但我无法获得所需的输出 [英] I have used multiple inheritance in my program but I cant get the desired output

查看:76
本文介绍了我在我的程序中使用了多重继承,但我无法获得所需的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>

using namespace std;

class add
{
   protected:
        int x;int y;
public:
    int c;
    void getvalueforadd(int a, int b)
{
    x=a;y=b;
}
void display()
{
    cout<<"the value of x and y are "<<x<<"\t"<<y;
}
void sum()
{
    c=(x+y);
    cout<<"Sum="<<c<<"\n";
}
};
class sub
{
    protected:
    int p,q;
public:
    int d;
    void getvalueforsub(int a,int b)
{
    p=a;q=b;
}
void show()
{
    cout<<"the value of p and q are "<<p<<"\t"<<q;
}
void diff()
{
    d=(p-q);
    cout<<"Diff="<<d<<"\n";
}
};

class mul:public add,public sub
{
protected:
    int k;
public:

    int getk()
    {
        return (c*d);
    }
};
<b><b></b></b>
int main()
{
    int m,n;
    add A;
    sub B;
    mul D;

    A.getvalueforadd(7,6);
    B.getvalueforsub(6,3);

    A.display();
    B.show();

    A.sum();
    B.diff();

    m=D.getk();
    cout<<m;
    return (0);
}

What I have tried:

<pre lang="C#">I want to divide the sum of numbers from first class 'add' and difference of numbers from second class 'sub' and then multiply both the results ..

推荐答案

您有三个类但没有使用它们。您需要编写创建对象的代码并调用各种方法来获得所需的结果。不要忘记 mul :: getk 将无效,除非 add :: sum sub: :diff 已被调用。
You have three classes but nothing that is using them. You need to write the code that creates an object and calls the various methods to get the results you want. Not forgetting that mul::getk will not work unless add::sum and sub::diff have already been called.


这篇关于我在我的程序中使用了多重继承,但我无法获得所需的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆