记录学习日常 代码可能有错 大家多多包涵 有好的建议提出的话 我会开心接纳 初学阶段
有一个类One及其公有派生类Two、Three、Four。类One和main函数的代码已经完成,请根据已有的代码给出上述三个派生类的设计。(只需要提交这三个类的代码)
程序的开始部分代码如下:
#include <iostream>
using namespace std;
class One{
int a,b,c;
public:
One(int a,int b,int c):a(a),b(b),c(c){}
int getA()const{return a;}
int getB()const{return b;}
int getC()const{return c;}
virtual void getResult()=0;
};
main函数的代码如下:
int main(){
One *p;
Two o1(1,2,3);
Three o2(4,5,6);
Four o3(7,8,9);
p=&o1;
p->getResult();
p=&o2;
p->getResult();
p=&o3;
p->getResult();
return 0;
}
输出描述
三个数的和是6
三个数的积是120
前两个数的和与第三个数的积是135
class Two:public One{
int x,y,z;
public:
Two()=default;
Two(int x,int y,int z):x(x),y(y),z(z), One(x,y,z){}
void getResult(){
cout<<"三个数的和是"<<x+y+z<<endl;
}
};
class Three:public One{
int x,y,z;
public:
Three()=default;
Three(int x,int y,int z):x(x),y(y),z(z), One(x,y,z){}
void getResult(){
cout<<"三个数的积是"<<x*y*z<<endl;
}
};
class Four:public One{
int x,y,z;
public:
Four()=default;
Four(int x,int y,int z):x(x),y(y),z(z), One(x,y,z){}
void getResult(){
cout<<"前两个数的和与第三个数的积是"<<(x+y)*z<<endl;
}
};