当前位置:
文档之家› c++类的继承与派生 实验报告
c++类的继承与派生 实验报告
using namespace std;
int main()
{
globe gl(2); //球
gl.display();
cylinder cy(2,3); //圆柱体
cy.display1();
cone co(2,3); //圆锥体
co.display2();
return 0;
}
(源程序清单等)
要求:
(1)定义一个基类圆,至少含有一个数据成员:半径;
(2)定义基类的派生类:球、圆柱、圆锥,都含有求表面积和体积的成员函数和输出函数。
(3)定义主函数,求球、圆柱、圆锥的和体积。
算
法
描
述
及
实
验
步
骤
1首先定义一个基类person
2派生类student和teacher
3实现客户信息的手动输入
4实现客户输出信息的需求
{
radius=r;
}
void area();
void volume();
void display();
};
class cylinder:public circle
{
private:
double height;
public:
cylinder(double r,double h):circle(r)
{
volume1();
}
void cone::area2()
{
cout<<"则圆锥体的面积:"<<pi*radius*sqrt(radius*radius+height*height)<<endl;//利用sqat函数
}
void cone::volume2()
{
cout<<"则圆锥体的体积:"<<(pi*radius*radius*height)/3;
}
void area2();
void volume2();
void display2();
};
实验二: circle1.cpp
#define pi 3.1415926 //定义π
#include "circle1.h"
#include<cmath>
#include<iostream>
using namespace std;
5实现客户的循环利用
6首先定义一个基类circle
7派生类cylinder和cone
8构造函数及调用有关函数
9定义求体积及面积的函数
调
试
过
程
及
实
验
结
果
调试过程中出现较少的语法错误,主要是链接以及友元的使用不熟练等
实验一的调试结果:学生及教师的信息输入:
学生及教师信息的输出:(有清屏的实现)
实验二的调试结果:
{
cout<<"输出第"<<i+1<<"位教师的信息:\n";
teac[i].display2();
}
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
}
cout<<"请问你是否继续重新返回操作?(y/n)\n";
cin>>d;
if(d=='n'||d=='N')break;
}
void globe::display()
{
cout<<"球的半径:"<<radius<<endl;
area();
volume();
}
void cylinder::area1()
{
cout<<"则圆柱的面积:"<<2*pi*radius*radius+2*pi*radius*height<<endl;
}
void cone::display2()
{
cout<<"圆锥体的半径:"<<radius<<" ;圆锥体的高:"<<height<<endl;
area2();
volume2();
cout<<endl;
}
实验二: circle.cpp
#include "circle1.h"
#include<iostream>
}
void cylinder::volume1()
{
cout<<"则圆柱的体积:"<<pi*radius*radius*height<<endl;
}
void cylinder::display1()
{
cout<<"圆柱的半径:"<<radius<<" ;圆柱的高:"<<height<<endl;
area1();
cin>>c;
for(int i=0;i<c;i++)
{
cout<<"请输入第"<<i+1<<"教师的信息:\n";
teac[i].set2();
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
}
}
system("cls");
cout<<"请问你是否查看全部信息?(y/n)\n";
12)注意头文件的关联;
13)注意循环的使用
(对实验结果进行分析,实验心得体会及改进意见)
附
录
附
录
附
录
附
录
实验的源程序:
实验一: person1.h
class person
{
private:
char number[20];
char name[20];
public:
void set();
void display();
height=h;
}
void area1();
void volume1();
void display1();
};
class cone:public circle
{
private:
double height;
public:
cone(double r,double h):circle(r)
{
height=h;
}
return 0;
}
实验二: circle1.h
class circle
{
protected:
double radius;
public:
circle(double r);
virtual ~circle();
};
class globe:public circle
{
public:
globe(double r):circle(r)
}
void student::set1()
{
set();
cout<<"请输入学生的班级名classname:\n";
cin>>classname;
cout<<"请输入学生的成绩score:\n";
cin>>score;
}
void student::display1()
{
display();
cout<<"学生的班级名:"<<setw(10)<<classname<<" 学生的成绩:"<<setw(10)<<score<<endl;
char a,d;
int b;
int c;
student stud[max];
teacher teac[max];
while(1)
{
cout<<"请问你是否需要输入学生信息?(y/n)\n";
cin>>a;
if(a=='y'||a=='Y')
{
cout<<"请问你要输入几个学生的信息(n不大于1000人)";
}
void teacher::set2()
{
set();
cout<<"请输入教师的职业名:\n";
cin>>occupation;
cout<<"请输入教师的部门:\n";
cin>>department;
}
void teacher::display2()
{
display();
cout<<"教师的职业名:"<<setw(10)<<occupation<<" 教师的部门:"<<setw(10)<<department<<endl;
};
class student:public person