高考知识网 时间:2023-08-17 00:03:43
1.考察虚继承内存体系
class A
{
public:
A() { cout<<"Construct A"<
~A() { cout<<"Destruct A"<
void speak() { cout<<"A is speaking!"<
};
class B:public virtual A
{
public:
B() { cout<<"Construct B"<
~B() { cout<<"Destruct B"<
};
class C:public virtual A
{
public:
C() { cout<<"Constuct C"<
~C() { cout<<"Destruct C"<
};
class D:public B, public C
{
public:
D(){ cout<<"Constsruct D"<
~D(){ cout<<"Destruct D"<
};
int main()
{
D *p = new D();
p->speak();
delete p;
}
输出:
Construct A
Construct B
Constuct C
Constsruct D
A is speaking!
Destruct D
Destruct C
Destruct B
Destruct A
2.考察非虚析构函数这道题
class Parent
{
public:
Parent(){cout<<"Parent construct"<
~Parent(){ cout<<"Parent destruct "<
};
class Child : public Parent
{
public:
Child() { cout<<"Child construct "<
~Child() {cout<<"child destruct"<
};
int main()
{
Parent *p;
Child *c = new Child();
p = c;
delete p; // 因为析构函数是非 virtual 的,故析构的时候按照指针的类型进行析构
}
输出:
Parent construct
Child Construct
Parent destruct
3.考察初始化列表的写法
class A
{
public:
A(int x, int y, int z):a=x,b=y,c=z {} (1)
A(int x, int y, int z):a(x),b(y),c(z){} (2)
private:
int a;
int b;
int c;
};
int main()
{
A a(1,2,3);
}
第 1 种写法是错误的,第 2 种正确。
4.考察拷贝构造函数和赋值的区别。
class A
{
public:
A() { cout<<"Construct A by default"<
A(const A& a) { cout<<"consttuct A by copy"<
A& operator =(const A& a) { cout<<"cosnt A by operator ="<
~A() { cout<<"Destruct A"<
};
int main()
{
A a;
A b=a; //调用拷贝构造函数
A c(a); //调用拷贝构造
A d;
d=a; //赋值
}
输出:
Construct A by default //构造对象 a
consttuct A by copy //拷贝构造 b
consttuct A by copy //拷贝构造 c
Construct A by default //构造 a
cosnt A by operator = //赋值 d=a
Destruct A
Destruct A
Destruct A
Destruct A
5.考察函数指针
voidfunc(char* a)
{
cout<
}
int main()
{
void (*fp)(char*); //填空处
fp = func; //函数名func相当于函数的地址,将其赋给函数指针fp
char* s="helloc";
fp(s);
}
中国点击率最高的一篇文章 !
2023-08-13 03:45:29山东大学在福建高考招生计划人数和专业代码(参考)
2024-06-08 08:59:56广州华南商贸职业学院在福建高考招生计划人数和专业代码(参考)
2024-06-08 08:56:34陕西高考610至620分左右理科可以上什么大学
2024-06-08 08:53:10湖南上东北师范大学多少分 分数线及排名
2024-06-08 08:49:38天津艺术职业学院对比安徽国防科技职业学院哪个好 附分数线排名
2024-06-08 08:46:04保定学院网络与新媒体专业怎么样?录取分数线多少分
2024-06-08 08:42:14河北新闻网两学一做知识竞赛(试题+答案完整版)
2023-08-13 23:55:25河北新闻网两学一做知识竞赛活动试题答案
2023-08-14 04:06:25两学一做学习教育知识竞赛活动10篇
2023-08-21 12:22:13安捷伦笔试真题
2023-08-24 23:53:05江森自控笔试真题
2023-08-14 13:17:14金融行业通用笔试题和面试题答案目
2023-08-18 11:41:02