假设有如下程序,该程序输出为____ class Base { //基类 int x, y; public: Base(int a, int b) { x=a; y=b; } ~ Base(){ cout<<"destruct base } }; derived: public { 派生类 public: derived(int z):base(z,z) ~ derived() derived int main(int argc, char* argv[]) od(8); return 0;> A、destruct Base
B、destruct Base destruct Derived
C、destruct Derived destruct Base
D、destruct Derived
第1题
(1)定义基类base的保护数据成员x和y用于记录边的长度,它们都是int型的数据。请在注释“//**1**”之后添加适当的语句。
(2)完成基类base默认构造函数的定义,把数据成员x和y分别初始化为参数a和b的值,要求使用作用域符“::”。请在注释“//**2**”之后添加适当的语句。
(3)完成派生类triangle函数disp的定义,使其以“三角形面积:”的格式将三角形的面积输出到屏幕上。请在注释“//**3**”之后添加适当的语句。
(4)添写派生类square的默认构造函数的定义,使其调用基类的构造函数,记录下正方形的边长信息a,其缺省值为0。请在注释“//** 4 **”之后添加适当的语句。
输出结果如下:
三角形面积:200
正方形面积:400
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
源程序文件test3_3.cpp清单如下:
include<iostream.h>
class base
{
protected:
//** 1 **
public:
base(int a,int b)
{
//** 2 **
}
virtual void disp(){cout<<"调用base基类"<<endl;)
};
class triangle:public base
{
public:
triangle(int a,int b):base(a,b){};
void disp()
{
//** 3 **
}
};
class square:public base
{
public:
//** 4 **
void disp(){cout<<"正方形面积:"<<x*y<<endl;}
};
void main()
{
base *p;
triangle t(20,20);
square s(20);
p=&t;
p—>disp();
p=&S;
p->disp();
}
第2题
A.10,20
B.30,40
C.20,30
D.50,50
第3题
A.10,20
B.30,40
C.20,30
D.50,50
第4题
请按要求完成下列操作,将类的定义补充完成:
①类Inherit是类Base的公有派生类。请在注释“//* *1* *”之后添加适当的语句。
②完成类Inherit成员函数setvalue(int a,int b,int c,int d)的定义,此函数实现的功能是将基类成员x、y、z和派生类的数据成员m的值分别设置成a、b、c和d。请在注释“//* *2* *”之后添加适当的语句。
③完成类Inherit成员函数display()的类体外定义,此函数实现的功能是以“,,,,”的格式将x、y、z和m的值输出到屏幕上。请在注释“//* *3* *”之后添加适当的语句。
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
源程序文件MyMain15.cpp清单如下:
//Mymain15.cpp
include<iostream>
using namespace std;
class Base
{
private:
int x;
protected:
int y;
public:
int z;
void setx(int i)
{
x=i;
}
int getx()const
{
return x;
}
};
//* * *1* * *
{
private:
int m;
public:
void setvalue(int a,int b,int c,int d)
{
//* * *2* * *
}
void display()const;
};
//* * *3* * *
int main()
{
Inherit A;
A.setvalue(1,2,3,4);
A.display();
return 0;
}
第5题
#include
using namespace std;
class Base{
int a,b;
public:
Bace ( int x ,int y ) { a=x; b=y;}
Void show () { cout<<A<< ’ , ’ <<B<<ENDL;}
}
clsaa Derived:public Base {
int c ,d;
public:
Derived ( int x,int y,int z,int m ) :Base ( x,y ) {c=z; d=m;}
Void show () { cout<<C<< ’ , ’ D<<ENDL;}
};
int main ()
{
Base B1 ( 50,50 ) ,*pb;
Derived DI ( 10,20,30,40 ) ;
Pb= & D1;
Pb->show () ;
Return 0;
}
运行时输出的结果是
A ) 10 , 20
B ) 30 , 40
C ) 20 , 30
D ) 50 , 50
第7题
B.cout<< y;
C.cout<< z;
D.cout<< a;
为了保护您的账号安全,请在“上学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!