C++实验报告4
实验题目10
【多级派生】 按以下的提示,由基类的设计和测试开始,逐渐地完成各个类的设计,求出圆格柱体的表面积、体积并输出并且完成要求的计算任务:
(1)先建立一个Point(点)类,包含数据成员x,y(坐标点),实现需要的成员函数,并设计main函数完成测试;
(2)以Point为基类,派生出一个Circle(圆)类,增加数据成员r(半径),以及求面积的成员函数area,实现其他需要的成员函数,设计main函数完成测试; (3)再以Circle类为直接基类,派生出一个Cylinder(圆柱体)类,再增加数据成员h(高),,以及求圆柱表面积的成员函数area和求圆柱体积的成员函数volume,实现需要的成员函数,并设计main函数完成测试。
要求编写程序,设计出各类中“需要的成员函数”,包括构造函数、析构函数、修改数据成员和获取数据成员的公共接口、用于输出的重载运算符“<<”函数等。
源程序:
#include<iostream>
#include<cmath>
using namespace std;
class Piont
{
public :
Piont(double a, double b) :x(a), y(b) {};
double distance(Piont &);
protected:
double x, y;
double long1;
const float pi = 3.14;
};
class Circle :public Piont
{
public:
Circle(double a, double b, double c) :Piont(a, b), r(c) {};
double area();
protected:
double r;
double s;
};
class Cylinder :public Circle
{
public:
Cylinder();
Cylinder(double a, double b, double c) :Circle(a,b,c){};
double change(double);
double area(Cylinder&);
double volume();
protected:
double h;
};
double Piont::distance(Piont& a)
{
double d;
d = (x - a.x) * (x - a.x) + (y - a.y)*(y - a.y);
long1 = sqrt(d);
return long1;
}
double Cylinder::change(double l)
{
h = l;
return h;
}
double Circle::area()
{
s= pi * r * r;
return s;
}
double Cylinder::area(Cylinder& a)
{
double s1,s2;
s1 = h * 2 * pi * r;
s2 = s + a.s+s1;
return s2;
}
double Cylinder::volume()
{
int v;
v = s * h;
return v;
}
int main()
{
Cylinder p1(5.5,5.5,5.5), p2(8,8,5.5);
p1.distance(p2);
p1.change(p1.distance(p2));
p1.Circle::area();
p2.Circle::area();
p1.Cylinder::area(p2);
p1.volume();
cout << "该圆柱体的表面积是" << p1.Cylinder::area(p2) << "平方米" << "\n";
cout << "该圆柱体的体积是" << p1.volume() << "立方米" << endl;
return 0;
}
实验题目11
【多重继承】 分别定义Teacher(教师)类和Cadre(干部)类,采用多重继承方式由这两个类派生出新类Teacher_Cadre(教师兼干部)。
要求:
(1)在两个基类中都包含姓名、年龄、性别、地址、电话等数据成员。 (2)在Teacher类中还包含数据成员title(职称),在Cadre类中还包含数据成员post(职务),在Teacher_Cadre类中还包含数据成员wages(工资)。 (3)对两个基类中的姓名、年龄、性别、地址、电话等数据成员用相同的名字,在引用这些数据成员时,指定作用域。
(4)在类体中声明成员函数,在类外定义成员函数。
(5)在派生类Teacher_Cadre的成员函数show中调用Teacher类中的display函数,输出姓名、年龄、性别、职称、地址、电话,然后再用cout语句输出职务与工资。
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
class Teacher
{
public:
Teacher(string a, string b, string c, string d, string e, int f) :name(a), sex(b), add(c), tele(d), title(e), age(f) {};
void display();
protected:
string name;
string sex;
string add;
string tele;
string title;
int age;
};
class Cadre
{
public:
Cadre(string a, string b, string c, string d, string e, int f) :name(a), sex(b), add(c), tele(d), post(e), age(f) {};
protected:
string name;
string sex;
string add;
string tele;
string post;
int age;
};
class Teacher_Cardre:public Teacher,public Cadre
{
public:
Teacher_Cardre(string a, string b, string c, string d, string e1, string e2, int f, int y) :Teacher(a, b, c, d, e1, f), Cadre(a, b, c, d, e2, f),wage(y) {};
void show();
protected:
int wage;
};
void Teacher::display()
{
cout << "name:" << name << endl;
cout << "age:" << age << endl;
cout << "sex:" << sex << endl;
cout << "address:" <<add<< endl;
cout << "phone:" << tele << endl;
cout << "title:" << title << endl;
}
void Teacher_Cardre::show()
{
Teacher::display();
cout << "post:" << post << endl;
cout << "wages:" << wage<< "元"<<endl;
}
int main()
{
Teacher_Cardre t1("***","*", "***", "19070401", "教师", "班主任", 28, 6000);
t1.show();
return 0;
}
实验题目12
写一个程序,定义抽象基类Shape,由它派生出2个派生类:Circle(圆形)、Rectangle(矩形),用一个普通函数printarea分别输出以上二者的面积,2个图形的数据在定义对象时给定。
#include<iostream>
#include<cstring>
using namespace std;
class Shape
{
public:
Shape() {};
Shape(double a,double b):longth(a), weight(b) {};
double longth;
double weight;
};
class Circle:public Shape
{
public:
Circle(double a, double b) :Shape(a, b) {};
Circle(double c) :radius(c){};
double radius;
const double pi = 3.1475926;
};
class Rectangle :public Shape
{
public:
Rectangle(double a, double b) :Shape(a, b) {};
protected:
};
double printarea(Circle &a)
{
return (a.pi * a.radius * a.radius);
}
double printarea(Rectangle &b)
{
return (b.longth * b.weight);
}
int main()
{
Circle c1(5.5);
Rectangle r1(5.5, 6.6);
cout << "圆的面积为:" << printarea(c1) << endl;
cout << "长方形的面积为:" << printarea(r1) << endl;
return 0;
}
实验题目15
写一个程序,定义抽象基类Shape,由它派生出3个派生类:Circle(圆形),Square(正方形),Rectangle(矩形)。用虚函数分别计算几种图形面积,并求它们的和。要求用基类指针数组,使它的每一个元素指向一个派生类对象。
#include<iostream>
#include<cstring>
using namespace std;
class Shape
{
public:
Shape() {};
Shape(double a,double b):longth(a), weight(b) {};
virtual double printarea() = 0;
protected:
double longth;
double weight;
};
class Circle:public Shape
{
public:
Circle(double a, double b) :Shape(a, b) {};
Circle(double c) :radius(c){};
virtual double printarea();
protected:
double radius;
const double pi = 3.1475926;
};
class Rectangle :public Shape
{
public:
Rectangle(double a, double b) :Shape(a, b) {};
virtual double printarea();
protected:
};
class Square :public Shape
{
public:
Square(double a, double b) :Shape(a, b) {};
virtual double printarea();
protected:
};
double Circle::printarea()
{
return (pi * radius * radius);
}
double Rectangle::printarea()
{
return (longth * weight);
}
double Square::printarea()
{
return (longth * weight);
}
int main()
{
Shape* t1[3];
Circle c1(5.5);
Rectangle r1(5.5, 6.6);
Square s1(5.5, 5.5);
t1[0] = &c1;
t1[1] = &r1;
t1[2] = &s1;
cout << "圆的面积为:" << t1[0]->printarea() << endl;
cout << "长方形的面积为:" << t1[1]->printarea() << endl;
cout << "正方形的面积为:" << t1[2]->printarea() << endl;
cout << "总面积为:";
cout << t1[0]->printarea() + t1[1]->printarea() + t1[2]->printarea() << endl;
return 0;
}