长沙半岛蓝湾:请高手帮忙分析C++的运行结果题

来源:百度文库 编辑:查人人中国名人网 时间:2024/05/09 06:43:28
7.给出程序运行的结果。
#include <iostream.h>
class A{
public:
A( ){ }
virtual void func( ){cout<<"Destructor A"<<endl;}
~A( ) {func();}
};
class B:public A{
public:
B( ){ }
void func(){cout<<"Destructor B"<<endl;}
~B( ) {func();}
};
void main( )
{
B b;
A&c=b;
}
8.给出程序运行的结果。
#include <iostream.h>
class Base {
private:
float x,y;
public:
Base(float x1,float y1);
~Base();
void SetBase(float x1,float y1)
{x=x1;y=y1;}
float GetX() {return x;}
float GetY() {return y;}
void Print()
{
cout <<"基类对象或派生类对象继承的x值="<<x<<"\t y值="
<<y<<endl;
}
};
class Derived:public Base {
float z;
Base a;
public:
Derived(float x1,float y1,float x2,float y2,float z1);
~Derived();
void SetDerived(float x1,float y1,float x2,float y2,float z1) {
Base::SetBase(x1,y1);
a.SetBase(x2,y2); z=z1;
}
float GetZ() {
return z;
}
void Print() {
Base::Print(); a.Print();
cout <<"派生类对象的z值=" <<z <<endl;
}
};
Base::Base(float x1,float y1) {
static int i=0;
x=x1;y=y1;
cout << "第" <<++i <<"次调用基类构造函数!\n";
}
Base::~Base() {
static int i=0;
cout << "第" <<++i <<"次调用基类析构函数!\n";
}
Derived::Derived(float x1,float y1,float x2,float y2,float z1)
:Base(x1,y1),a(x2,y2)
{
static int j=0;
z=z1;
cout <<"第" <<++j <<"次调用派生类构造函数!\n";
}
Derived::~Derived() {
static int j=0;
cout <<"第" <<++j <<"次调用派生类析构函数!\n";
}
void main() {
Base b(30,20);
Derived d(11,12,13,14,15);
}