A-A+

有如下程序: include<iostream> using namespace std

2022-08-06 04:20:01 问答库 阅读 175 次

问题详情

有如下程序:
include<iostream>
using namespace std;
class Monitor{
public:
Monitor(char t):type(t){ }
void Print()const
{cout<<"The type of monitor is"<<type< private:
char type;
};
class Computer{
public:
Computer(int i,char C) :______{}
void Print()const
{eout<<"The computer is"<<id<<endl;mort.Print();}
private:
int id;
Monitor mon;
};
int main(){
const Computer myComputer(101,"B");
myComputer.Print();
return 0;
}
请将程序补充完整,使程序在运行时输出:
The computer is 101
The type of monitor is B请帮忙给出正确答案和分析,谢谢!

参考答案

正确答案:id(I)mon(C)
id(I),mon(C) 解析:带参构造函数的定义格式(在类外部声明)为:
类名::构造函数名([参数表]):数据成员名1(初始值1),数据成员名2(初始值2)……在类中声明为:
构造函数名([参数表]):数据成员名1(初始值1),数据成员名2(初始值2)……
在compute中有两个数据成员,所以在构造函数中应该对这两个数据成员id和mon初始化,初始化mon创建一个对象,参数为构造函数的形参c。

考点:程序