/*派生类的构造函数和析构函数
*/
#include<iostream>
using namespace std;
class base
{
float x,y;
public:
base(float x1,float y1);//基类的一般构造函数
~base();
void set_base(float x1,float y1){x=x1,y=y1;}
float get_x(){return x;}
float get_y(){return y;}
void print(){cout<<"基类对象或派生类对象继承的x值="<<x<<"\ty的值="<<y<<endl;}
};
base::base(float x1,float y1)
{
static int i=0;
cout<<"第"<<++i<<"次调用基类构造函数"<<endl;
x=x1;y=y1;
}
base::~base()
{
static int j=0;
cout<<"第"<<++j<<"次调用基类析构函数"<<endl;
}
class derived:public base
{
float z;
base a;//基类的对象作为派生类的私有数据
public:
derived(float x1,float y1,float a1,float a2,float z1);//派生类的构造函数的声明,仅需要一个整体的参数列表
~derived();
void set_derived(float x1,float y1,float a1,float a2,float z1);//设置派生的值
float get_z(){return z;}
void print();//基类中也有print函数的。注意两者的调用!
};
derived::derived(float x1,float y1,float a1,float a2,float z1):base(x1,y1),a(a1,a2)
{
static int j=0;
cout<<"第"<<++j<<"次调用派生类的构造函数"<<endl;
z=z1;
}
void derived::set_derived(float x1,float y1,float a1,float a2,float z1)//设置派生中各成员的值
{
base::set_base(x1,y1);//设置从基类中继承下来的值
a.set_base(a1,a2);//设置派生类的数据成员(基类对象a)
z=z1;
};
derived::~derived()
{
static int i=0;
cout<<"第"<<++i<<"次调用派生类的析构函数"<<endl;
}
void derived::print()
{
cout<<"派生类新增的数据成员z="<<z<<endl;
cout<<"从基类继承的数据成员x="<<base::get_x()<<",y="<<base::get_y()<<endl;
//cout<<"从基类继承的数据成员"<<base::print()<<endl;//这语句是不行的,因为木有返回值撒!!
//cout<<"派生类中的基类对象成员a="<<a.print()<<endl;
cout<<"另一种的显式方法:";
base::print();
cout<<"派生类中的基类对象成员a=";
a.print();
}
void main()
{
cout<<"(1)对基类base的测试:\n";
base b1(1,2);
b1.print();
cout<<"x="<<b1.get_x()<<",y="<<b1.get_y()<<endl;
cout<<"(2)对于派生类的测试:"<<endl;
derived d1(1,2,3,4,5);
/*d1.print();*/
d1.set_derived(6,7,8,9,10);
d1.print();
}
*/
#include<iostream>
using namespace std;
class base
{
float x,y;
public:
base(float x1,float y1);//基类的一般构造函数
~base();
void set_base(float x1,float y1){x=x1,y=y1;}
float get_x(){return x;}
float get_y(){return y;}
void print(){cout<<"基类对象或派生类对象继承的x值="<<x<<"\ty的值="<<y<<endl;}
};
base::base(float x1,float y1)
{
static int i=0;
cout<<"第"<<++i<<"次调用基类构造函数"<<endl;
x=x1;y=y1;
}
base::~base()
{
static int j=0;
cout<<"第"<<++j<<"次调用基类析构函数"<<endl;
}
class derived:public base
{
float z;
base a;//基类的对象作为派生类的私有数据
public:
derived(float x1,float y1,float a1,float a2,float z1);//派生类的构造函数的声明,仅需要一个整体的参数列表
~derived();
void set_derived(float x1,float y1,float a1,float a2,float z1);//设置派生的值
float get_z(){return z;}
void print();//基类中也有print函数的。注意两者的调用!
};
derived::derived(float x1,float y1,float a1,float a2,float z1):base(x1,y1),a(a1,a2)
{
static int j=0;
cout<<"第"<<++j<<"次调用派生类的构造函数"<<endl;
z=z1;
}
void derived::set_derived(float x1,float y1,float a1,float a2,float z1)//设置派生中各成员的值
{
base::set_base(x1,y1);//设置从基类中继承下来的值
a.set_base(a1,a2);//设置派生类的数据成员(基类对象a)
z=z1;
};
derived::~derived()
{
static int i=0;
cout<<"第"<<++i<<"次调用派生类的析构函数"<<endl;
}
void derived::print()
{
cout<<"派生类新增的数据成员z="<<z<<endl;
cout<<"从基类继承的数据成员x="<<base::get_x()<<",y="<<base::get_y()<<endl;
//cout<<"从基类继承的数据成员"<<base::print()<<endl;//这语句是不行的,因为木有返回值撒!!
//cout<<"派生类中的基类对象成员a="<<a.print()<<endl;
cout<<"另一种的显式方法:";
base::print();
cout<<"派生类中的基类对象成员a=";
a.print();
}
void main()
{
cout<<"(1)对基类base的测试:\n";
base b1(1,2);
b1.print();
cout<<"x="<<b1.get_x()<<",y="<<b1.get_y()<<endl;
cout<<"(2)对于派生类的测试:"<<endl;
derived d1(1,2,3,4,5);
/*d1.print();*/
d1.set_derived(6,7,8,9,10);
d1.print();
}