1.请采用继承设计点类、圆类及圆柱体类,构建各类的成员函数并用对象测试。程序 输出圆的面积、圆柱体的表面积及体积。
【问题描述】按以下提示信息,由基类的设计和测试开始,逐渐地完成各个类的设计,并且完成要求的功能。
①设计一个Point(点)类,包含数据成员x、y(坐标点)。
②以Point为基类,派生出一个Circle(圆)类,增加数据成员r(半径)。
③以Circle类为直接基类,派生出一个Cylinder(圆柱体)类,再增加数据成员h(高)。
请设计出各类中基本的成员函数,包括构造函数、设置数据成员和获取数据成员的函数,以及计算圆的周长和面积、计算圆柱体的表面积和体积的函数,使程序正确运行。
pi=3.1415926
#include<iostream>
#include<iomanip>
using namespace std;
const double pi = 3.1415926;
//点类相关实现
class Point
{
protected:
double x, y;
public:
Point(double _x, double _y);
Point SetPoint(double new_x, double new_y);
void Show();
double GetX();
double GetY();
};
Point::Point(double _x, double _y)
{
x = _x;
y = _y;
}
Point Point::SetPoint(double new_x, double new_y)
{
x = new_x;
y = new_y;
Point p(x, y);
return p;
}
double Point::GetX()
{
return x;
}
double Point::GetY()
{
return y;
}
void Point::Show()
{
cout << x << "," << y << endl;
}
//点类相关实现结束
//圆类相关实现
class Circle :public Point
{
protected:
double r;
public:
Circle(double,double,double);
double Girth();
double Area();
double GetR();
void Show();
};
Circle::Circle(double _x, double _y, double _r):Point( _x, _y)
{
r = _r;
}
double Circle::Girth()
{
return 2 * pi * r;
}
double Circle::Area()
{
return pi * r * r;
}
double Circle::GetR()
{
return r;
}
void Circle::Show()
{
Point::Show();
cout << r<< endl;
}
//圆类相关实现结束
//圆柱类相关实现
class Cylinder:public Circle
{
protected:
double h;
public:
Cylinder();
void SetCylinder(Circle,double);
double Area();
double Volumn();
void Show();
double GetH();
};
Cylinder::Cylinder():Circle(0,0,0)
{
h = 0;
}
void Cylinder::SetCylinder(Circle _c, double _h)
{
x = _c.GetX();
y = _c.GetY();
r = _c.GetR();
h = _h;
}
double Cylinder::Area()
{
return 2 * Circle::Area() + Girth() * h;
}
double Cylinder::Volumn()
{
return Circle::Area() * h;
}
double Cylinder::GetH()
{
return h;
}
void Cylinder::Show()
{
Circle::Show();
cout << GetH() << endl;
}
int main()
{
double x,y;
cin>>x>>y;//若输入:10 20
Point p1(x,y);
p1.SetPoint(x+10,y+5);
cout<<fixed<<setprecision(1);
p1.Show();//单独占一行输出:10,20
Circle c1(p1.GetX(),p1.GetY(),20);
c1.Show();//输出两行:圆心一行,半径一行
cout<<"c1的周长是:"<<c1.Girth()<<endl;//一个数据一行
cout<<"c1的面积是:"<<c1.Area()<<endl;//一个数据一行
Cylinder cy1;
cy1.SetCylinder(c1,10.5);
cy1.Show();//输出三行:圆心一行,半径一行,高一行
cout<<"cy1的表面积是:"<<cy1.Area()<<endl;//一个数据一行
cout<<"cy1的体积是:"<<cy1.Volumn()<<endl;//一个数据一行
return 0;
}
2.以学校职工为基类,派生出教师类和管理人员类,又从教师类和管理人员类共同派生 出教师管理人员类。虚基类
【问题描述】以学校职工为基类,派生出教师类和管理人员类,又从教师类和管理人员类共同派生出教师管理人员类。
【输入形式】
【输出形式】
Zhao is a Staff 20 years old
Zhang is a Teacher 30 years old, Lecture
Wang is a management 50 years old, dean
Li is a Teacher management 40 years old, Peofessor, department head
#include <iostream>
using namespace std;
#include<cstring>
class CStaff
{ protected:
int number;
char name[10];
int age;
public:
CStaff(int num, const char *na, int a)
{ number = num;
age = a;
strcpy(name, na);
}
void Display()
{ cout << name << " is a Staff " << age << " yeas old" << endl; }
};
class CTeacher:virtual public CStaff
{
protected:
char zch[20]; //职称
public:
CTeacher(int num, const char *na, int a,const char *zc);
void Display()
{
cout << name << " is a Teacher " << age << " yeas old, " << zch << endl;
}
};
CTeacher::CTeacher(int num, const char *na, int a,const char *zc):CStaff(num,na,a)
{
strcpy(zch, zc);
}
class CManagement:virtual public CStaff
{
protected:
char zw[50]; //职务
public:
CManagement(int num,const char *na, int a,const char *z);
void Display()
{
cout << name << " is a management " << age << " yeas old, " << zw << endl;
}
};
CManagement::CManagement(int num,const char *na, int a,const char *z):CStaff(num,na, a)
{
strcpy(zw, z);
}
class CTeacherManagement:public CTeacher,CManagement
{
public:
CTeacherManagement(int num,const char *na, int a, const char *zc,const char *z);
void Display()
{
cout << name << " is a Teacher management " << age << " yeas old, "
<< zch << ", " << zw << endl;
}
};
CTeacherManagement::CTeacherManagement(int num,const char *na, int a, const char *zc,const char *z): CStaff(num, na, a),CTeacher(num, na, a,zc),CManagement( num,na, a,z)
{
strcpy(zw, z);
}
int main()
{
CStaff s1(101,"Zhao",20);
CTeacher t1(102,"Zhang",30,"Lecture");
CManagement m1(103,"Wang", 50,"dean");
CTeacherManagement tm1(104,"Li",40, "Peofessor", "department head");
s1.Display();
t1.Display();
m1.Display();
tm1.Display();
return 0;
}