C++ 9 继承与派生2
题目1
运行下列程序,写出运行结果,注意程序行中的注释。(目的:理解多重继承中基类构造函数和析构函数的调用顺序)
#include <iostream>
using namespace std;
class Base1
{
protected:
int data1;
public:
Base1(int a=0)
{ data1 = a;
cout<<"Base Constructor1\n";
}
~Base1( )
{ cout<<"Base Destructor1\n"; }
};
class Base2
{
protected:
int data2;
public:
Base2(int a=0)
{ data2 = a;
cout<<"Base Constructor2\n";
}
~Base2( )
{ cout<<"Base Destructor2\n"; }
};
class Derived: public Base2, public Base1 //A
{
int d;
public:
Derived(int x, int y, int z): Base1(y) ,Base2(x)//B
{ d=z; cout<<"Derived Constructor\n"; } //派生类的构造函数,问题:调用次序由A行还是B行决定?
//改动A行派生次序,观察结果?再改动B行,观察结果
~Derived( )
{ cout<<"Derived Destructor\n"; }
void Show( )
{ cout<<data1<<','<<data2<<','<<d<<endl; }
};
void main( )
{ Derived c(1, 2, 3);
c.Show( );
}
运行结果:
Base Constructor2
Base Constructor1
Derived Constructor
2,1,3
Derived Destructor
Base Destructor1
Base Destructor2
题目2
更正下列程序中的错误,写出正确的运行结果,(目的:理解二义性问题并提出解决方案。
#include
using namespace std;
class A
{
protected:
int x;
public:
void Show( )
{ cout <<“A类中的x=” << x << ‘\n’ ; }
};
程序
#include <iostream>
using namespace std;
class A
{
protected:
int x;
public:
void Show()
{
cout << "A类中的x=" << x << '\n';
}
};
class B
{
protected:
int x;
public:
void Show()
{
cout << "B类中的x=" << x << '\n';
}
};
class C :public A, public B
{
int y;
public:
void SetAx(int a)
{
A::x = a;
}
void SetBx(int a)
{
B::x = a;
}
void Sety(int b)
{
y = b;
}
int Gety()
{
return y;
}
};
void main(void)
{
C c;
c.SetAx(10);
c.SetBx(10);
c.Sety(20);
c.A::Show();
c.B::Show();
}
题目3
(提高题,目的,熟悉构造函数的写法,成员函数的调用)
下面程序中已定义了Date日期类和Time时间类,请在填空处完善程序。
1)定义Date类带缺省值的构造函数,缺省日期为2011/12/13;
2) 定义Time类带缺省值的构造函数,缺省时间为11:12:13
3) 定义DateTime类公有继承以上两个类。
#include <iostream>
#include <string.h>
using namespace std;
class Date
{
private:
int year, month, day;
public:
//(1) 完成带缺省值的构造函数的实现
void SetDate(int y, int m, int d)
{ year=y; month=m; day=d; }
void ShowDate()
{ cout<<year<<"-"<<month<<"-"<<day<<endl;}
};
class Time
{
private:
int hour, minute, second;
public:
//(2) 完成带缺省值的构造函数的实现
void SetTime(int h=0, int m=0, int s=0)
{ hour=h; minute=m; second=s; }
void ShowTime()
{ cout<<hour<<":"<<minute<<":"<<second<<endl;}
};
class DateTime : //( 3)补充完成代码
{
public:
DateTime(int yy=1900, int mm=1, int dd=1, int h=0, int m=0, int s=0): // (4)补充完成代码
{ }
void SetDateTime(int yy, int mm, int dd, int h, int m, int s)
{
// (5) 调用基类的成员函数设置日期时间
}
void ShowDateTime( )
{
//(6) 调用基类的成员函数显示日期时间
}
};
void main( )
{
DateTime dt(2004,5,8,12,30,10);
dt.ShowDateTime( );
// (7) 补充完成代码 ; //重新将dt的日期时间设置为2015-05-19 12:30:30
dt.ShowDateTime( );
}
修改`
//题目6(提高题,目的,熟悉构造函数的写法,成员函数的调用)
//
//下面程序中已定义了Date日期类和Time时间类,请在填空处完善程序。
//1)定义Date类带缺省值的构造函数,缺省日期为2011 / 12 / 13;
//2) 定义Time类带缺省值的构造函数,缺省时间为11:12 : 13
//3) 定义DateTime类公有继承以上两个类。
#include <iostream>
#include <string.h>
using namespace std;
class Date
{
private:
int year, month, day;
public:
//(1) 完成带缺省值的构造函数的实现
Date(int y = 2001, int m = 12, int d = 13)
{
year = y;
month = m;
day = d;
}
void SetDate(int y, int m, int d)
{
year = y; month = m; day = d;
}
void ShowDate()
{
cout << year << "-" << month << "-" << day << endl;
}
};
class Time
{
private:
int hour, minute, second;
public:
//(2) 完成带缺省值的构造函数的实现
Time(int h = 11, int m = 12, int s = 13)
{
hour = h;
minute = m;
second = s;
}
void SetTime(int h = 0, int m = 0, int s = 0)
{
hour = h; minute = m; second = s;
}
void ShowTime()
{
cout << hour << ":" << minute << ":" << second << endl;
}
};
class DateTime :public Date,Time//( 3)补充完成代码
{
public:
DateTime(int yy = 1900, int mm = 1, int dd = 1, int h = 0, int m = 0, int s = 0) :Date(yy,mm,dd),Time(h,m,s) // (4)补充完成代码
{ }
void SetDateTime(int yy, int mm, int dd, int h, int m, int s)
{
// (5) 调用基类的成员函数设置日期时间
SetDate(yy, mm, dd);
SetTime(h, m, s);
}
void ShowDateTime()
{
//(6) 调用基类的成员函数显示日期时间
ShowDate();
ShowTime();
}
};
void main()
{
DateTime dt(2004, 5, 8, 12, 30, 10);
dt.ShowDateTime();
// (7) 补充完成代码 ; //重新将dt的日期时间设置为2015-05-19 12:30:30
dt.SetDateTime(2015, 5, 19, 12, 30, 30);
dt.ShowDateTime();
}