阅读程序,请写出这些程序的运行结果
(1)阅读程序,写出执行结果
#include <iostream>
using namespace std;
class Data
{
public:
Data(int i):x(i){cout<<"A";}
~Data(){ cout<<"B";}
private:
int x;
};
class Base
{
public:
Base(int i):b1(i){cout<<"C";}
~Base(){ cout<<"D";}
private:
int b1;
};
class Derived:public Base
{
public:
Derived (int i,int j):Base(i),d1(j){
cout<<"E";
}
~Derived(){cout<<"F";}
private:
Data d1;
};
int main()
{
Derived obj(1,2);
return 0;
}
(2)阅读程序,写出执行结果
#include <iostream>
using namespace std;
class A
{
public:
A(){a=0;}
A (int i){a=i;}
void print(){cout<<a<<" ";}
private:
int a;
};
class B: public A
{
public:
B(){b=0;}
B(int i, int j, int k): A(i),aa(j){b=k;}
//思考:这3处出现的print,有何区别
void print() //(1)
{
A::print(); //(2)
aa.print(); //(3)
cout<<b<<endl;
}
private:
int b;
A aa;
};
int main()
{
B test[2];
test[0]=B(1,4,7);
test[1]=B(2,5,8);
for(int i=0; i<2; i++)
test[i].print();
return 0;
}
(3)阅读程序,写出执行结果
#include <iostream>
using namespace std;
class A
{
public:
A(char *s){cout<<s<<endl;}
};
class B:public A
{
public:
B(char *s1, char *s2):A(s1){
cout<<s2<<endl;
}
};
class C:public A
{
public:
C(char *s1,char *s2):A(s1){
cout<<s2<<endl;
}
};
class D:public B, C
{
public:
D(char *s1,char *s2,char *s3,char *s4):
B(s1,s2),C(s3,s4){cout<<s4<<endl;}
};
int main()
{
D d("class A","class B","class C","class D");
return 0;
}
(4)阅读程序,写出执行结果
#include <iostream>
using namespace std;
class Base
{
public:
Base(char i) { cout<<"Base constructor. --"<<i<<endl; }
};
class Derived1:virtual public Base
{
public:
Derived1(char i,char j):Base(i){
cout<<"Derived1 constructor. --"<<j<<endl;
}
};
class Derived2:virtual public Base
{
public:
Derived2(char i,char j):Base(i){
cout<<"Derived2 constructor. --"<<j<<endl;
}
};
class MyDerived: public Derived1,public Derived2
{
public:
MyDerived(char i,char j,char k,char l,char m,char n,char x):
Derived2(i,j), Derived1(k,l), Base(m), d(n){
cout<<"MyDerived constructor. --"<<x<<endl;
}
private:
Base d;
};
int main()
{
MyDerived obj('A','B','C','D','E','F','G');
return 0;
}
(5)阅读下面类的定义,请说出在测试函数中不同情况的调用产生的结果
#include <iostream>
using namespace std;
class A
{
protected:
int a,b;
public:
A(int aa, int bb):a(aa), b(bb) {}
void printA(){
cout<<"a: "<<a<<"\tb: "<<b<<endl;
}
};
class B: public A
{
int c;
public:
B(int aa, int bb, int cc):A(aa,bb),c(cc) {}
void printB()
{
cout<<"a: "<<a<<"\tb: "<<b<<"\tc: "<<c<<endl;
}
};
int main()
{
A a(1,1);
B b(2,3,4);
//此处加入下面各小题中的代码
return 0;
}
(a)
a=b;
a.printA();
b.printA();
b.printB();
你认为输出结果是:____________
运行程序的结果是:____________
(b)
b=a;
a.printA();
b.printA();
b.printB();
程序会发生编译错误,原因是:____
记录下IDE中提示的错误并理解:___
(c)
A &r1=a;
A &r2=b;
r1.printA();
r2.printA();
r2.printB();
将会发生错误的一行删除;
对余下的程序,你认为输出是:____
实际运行的输出是:__________
那一行的错误原因是:________
(d)
A *p=&a;
p->printA();
p=&b;
p->printA();
p->printB();
将会发生错误的一行删除;
对余下的程序,你认为输出是:____
实际运行的输出是:__________
那一行的错误原因是:________
(e)
在class A中增加成员函数:int getA(){return a;}
在main函数前增加一般函数:
void f(A x)
{
cout<<"aaaaah, my a: "<<x.getA()<<endl;
}
main函数中指定部分为:
f(a);
f(b);
你认为输出结果是:____________
运行程序的结果是:____________