实验
5
继承与派生
5.3
实验内容
(1)
定义一个基类
Animal
,有私有整型成员变量
age
,构造其派生类
Dog
,在其成
员函数
setAge(int n)
中直接给
age
赋值,看看会有什么问题,把
age
改为公有
成员变量,还会有问题吗?
:基类的private成员变量无论以哪种继承方式,在子类中都是不可见的。
class Animal
{
protected:
int age;
};
class Dog :public Animal
{
public:
void setAge(int n)
{
age = n;//由于age是Animala的私有成员变量 无论以何种方式继承对于子类都是不可见的 所以需要将基类改为protected
}
};
(2)
编写一个程序计算出球、圆柱和圆锥的表面积和体积,要求如下:
(a)
定义一个基类圆,至少含有一个数据成员半径;
(b)
定义基类的派生类球、圆柱、圆锥,都含有求表面积和体积的成员函数和
输出函数;
(c)
定义主函数,求球、圆柱、圆锥的体积;
#include<iostream>
#include<vector>
#include<string>
#include<list>
#include<algorithm>
#include<math.h>
using namespace std;
#define pi acos(-1)
class Round
{
public:
Round(int rr=0)
:r(rr)
{}
protected:
int r;
};
class Globe:public Round
{
public:
Globe(int r = 0)
:Round(r)
{}
double GetS()
{
return pow(r, 2) * 4 * pi;
}
double GetV()
{
return (4 * pi * pow(r, 3)) / 3.0;
}
void Print()
{
cout << "The Globe's r is " << r << endl;
}
};
class Cylinder :public Round//柱
{
public:
Cylinder(int rr=0,int hh=0)
:Round(rr),h(hh)//在子类的构造中需调用父类的构造函数
{}
double GetS()
{
return 2 * pi * r * h + pow(r, 2) * pi * 2;
}
double GetV()
{
return pi * pow(r, 2) * h;
}
void Print()
{
cout << "The Cylinder's r is " << r <<"and h is "<< h << endl;
}
private:
int h;
};
class Cone :public Round// 锥
{
public:
Cone(int rr=0,int hh=0)
:Round(rr),h(hh)
{}
double GetS()
{
return pi * r * h + pow(r, 2) * pi * 2;
}
double GetV()
{
return (pi * pow(r, 2) * h)/3.0;
}
void Print()
{
cout << "The Cone's r is " << r << "and h is " << h << endl;
}
private:
int h;
};
(3)
定义一个日期
(
年、月、日
)
的类和一个时间
(
时、分、秒
)
的类,并由这两个
类派生出日期和时间类。主函数完成基类和派生类的测试工作。
class Date
{
public:
Date(int y=0,int m=0,int d=0)
:year(y),month(m),day(d)
{}
Date(const Date& d)
{
year = d.year;
month = d.month;
day = d.day;
}
bool IsLeapYear()
{
if (year % 4 == 0 && year & 4 || !year % 400)
{
return true;
}
return false;
}
Date& operator++()
{
int a[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
if (IsLeapYear())a[1]++;
day++;
if (a[month - 1] < day)
{
day = 1;
month++;
}
if (month == 13)
{
year++;
month = 1;
}
return *this;
}
void Print()
{
cout << year << '/' << month << '/' << day << endl;
}
protected:
int year, month, day;
};
class Time
{
public:
Time& operator++()
{
second++;
if (second >= 60)
{
second = 0;
minute++;
}
if (minute >= 60)
{
minute = 0;
hour++;
hour %= 24;//这里采用二十四小时制
}
return *this;
}
Time(int h = 0, int m = 0, int s = 0)
:hour(h),minute(m),second(s)
{}
Time(const Time& t)
{
hour = t.hour;
minute = t.minute;
second = t.second;
}
void Print()
{
printf("%02d:%02d:%02d\n", hour, minute, second);
}
protected:
int hour, minute, second;
};
class DT:public Date,public Time
{
public:
DT(int y = 0, int m = 0, int d = 0, int h = 0, int min = 0, int s = 0)
:Date(y,m,d),Time(h,min,s)
{}
DT(Date d,Time t)
:Date(d),Time(t)
{}
DT& operator++()
{
Date nd(*this);
Time nt(*this);
++nd;//可以复用Date类的++
++nt;
DT ndt(nd, nt);//构造一个临时对象
swap(*this, ndt);//把这个临时对象当作垃圾桶,调用完++其生命周期就结束了
return *this;
}
void Print()
{
cout << year << '/' << month << '/' << day << ' ';
printf("%02d:%02d:%02d\n", hour, minute, second);//控制输出格式
}
(4)
设计一个描述二维平面内一点的
CPoint
类,成员包括:
x
,
y
。需要实现的功能
(
成员函数
)
:构造函数、设置坐标、获取坐标、获取极坐标、求两点之间的距
离。然后再设计一个描叙三维平面内一点的
C3DPoint
类,该类为
CPoint
类的
派生类,新添加的成员包括:
z
。需新添加或重新定义的功能包括:构造函数、
设置坐标、获取坐标、求两点之间的距离。请在
main()
函数中测试之。
class CPoint
{
public:
CPoint(int xx=0,int yy=0)
:x(xx),y(yy)
{}
void SetX(const int& xx)
{
x = xx;
}
void SetY(const int& yy)
{
y = yy;
}
pair<int, int> Get()
{
return make_pair(x, y);
}
double Distance(const CPoint& p)
{
return sqrt(pow(abs(x - p.x), 2) + pow(abs(y - p.y), 2));
}
pair<double, double> Polar()//first是模长 second是角度
{
return make_pair(sqrt(pow(x, 2) + pow(y, 2)), atan(y*1.0 / (1.0*x)));
}
protected:
int x, y;
};
class C3DPoint :public CPoint
{
public:
C3DPoint(int xx=0,int yy=0,int zz=0)
:CPoint(xx,yy),z(zz)//这里要调用基类的构造函数
{}
void SetX(const int& xx)
{
x = xx;
}
void SetY(const int& yy)
{
y = yy;
}
void SetZ(const int& zz)
{
z = zz;
}
void Get()
{
cout << "x=" << x << "y=" << y << "z=" << z << endl;
}
double Distance(const C3DPoint& p)
{
return sqrt(pow(abs(x - p.x), 2) + pow(abs(y - p.y), 2)+pow(abs(z-p.z),2));
}
private:
int z;
};
//测试
int main()
{
CPoint p2d(1, 1);
CPoint p2d1(2, 2);
pair<double,double>xy=p2d.Polar();
cout << xy.first << xy.second<<endl;
cout<<p2d.Get().first<<p2d.Get().second<<endl;
cout << p2d.Distance(p2d1)<<endl;
p2d.SetX(2);
p2d.SetY(2);
C3DPoint d1(1, 1, 1);
C3DPoint d2(2, 2, 2);
d1.Get();
cout << d1.Distance(d2)<<endl;
return 0;
}