C++考试的2个题目,求教?(麻烦不要只给个答案,给点解析,谢谢)

2025-12-18 04:04:51
推荐回答(3个)
回答1:

#include
using namespace std;
void main()
{ int num = 50, &ref = num; //ref引用num所存的数,即两者批向的数值是一样的
ref = ref + 10;//即num值 50+10
cout << "num = " << num << endl; //输出60
num = num + 40; //60+40
cout << "ref = " << ref << endl; //输出100
}
#include
using namespace std;
class A
{ public:
virtual void print()=0;//定义一个虚函数.不存在实际输出,由子类的同名函数决定输出什么东东来
};
class B : public A
{ public:
void print(){ cout<<”In B”<};
class C : public B
{ public:
void print(){ cout<<”In C”<};
void main()
{ A *pa;
B b;
C c;
pa = &b;//指针指向B对象
pa->print();//调用B对像的输出函数输出In B
pa = &c;//指向C对象
pa->print();//调用C对象的输出函数输出In C
}

回答2:

第一题结果:
num = 60
ref = 100

第二题结果:
In B
In C

注:你的分号,改为半角;不能为全角;否则会报很多错误。。双引号也是如此。

回答3:

gt;
#include

class person
{
private:
char name[20];
char sex;
int age;
public:
void SetName(char *n)
void SetSex(char s)
void SetAge(int a)
char *GetName()
char GetSex()
char GetAge()
};

void main()
{ person p;
char Name[20],Sex;
int Age;
cout<<"请输入姓名、性别、年龄:";
cin>>Name>>Sex>>Age;
p.SetName(Name);
p.SetSex(Sex);
p.SetAge(Age);
cout<<"姓名:"<}