继承和派生
- 格式:doc
- 大小:87.60 KB
- 文档页数:5
(1)定义一个基类圆,至少含有一个数据成员:半径;
(2)定义基类的派生类:球、圆柱、圆锥,都含有求表面积和体积的成员函数和输出函数。
(3)定义主函数,求球、圆柱、圆锥的和体积。
调
试
过
程
及
实
验
结
果
总
结
对于继承应用不熟练。
附
录
1、
#include<iostream>
#include<string>
实
验
环
境
Vc++6.0
实
验
内
容
二、实验内容
1.编写一个学生和教师相关数据输入和显示程序。学生数据有编号、姓名、班级和成绩,教师数据有编号、姓名、职称和部门。要求:将编号、姓名及其输入和显示设计成一个类person,并作为学生数据操作类student和教师类数据操作类teacher的基类。
编写程序计算出球、圆柱和圆锥的表面积和体积。
}
void circular_cone::b2()
{
cout<<"圆锥的表面积为:";
cout<<PI*radius*radius+PI*radius*le<<endl;
}
void circular_cone::t2()
{
cout<<"圆锥的体积为:";
cout<<PI*radius*radius*he*1/3<<endl;
{
hed globe::b()
{
cout<<"球体的表面积为:";
cout<<4*PI*radius*radius<<endl;
}
void globe::t()
{
cout<<"球体的体积为:";
cout<<4*PI*1/3*radius*radius*radius<<endl;
void t1();
circular_column(int a,int h):circle(a)
{
height=h;
}
};
class circular_cone:public circle
{
private:
int he;
int le;
public:
void b2();
void t2();
circular_cone(int a,int h,int l):circle(a)
}
class steacher: public person
{
private:
string ocp;
string bu;
public:
steacher(int n,string m,string o,string u):person(n,m)
{
ocp=o;
bu=u;
}
void show2();
};
void steacher::show2()
using namespace std;
class person
{
private:
int num;
string name;
public:
person(int n,string m);
void print();
void show();
};
person::person(int n, string m)
{
};
class globe:public circle
{
public:
void b();
void t();
globe(int a):circle(a){}
};
class circular_column:public circle
{
private:
int height;
public:
void b1();
}
void circular_column::b1()
{
cout<<"圆柱体的表面积为:";
cout<<2*PI*radius*radius+2*PI*radius*height<<endl;
}
void circular_column::t1()
{
cout<<"圆柱体的体积为:";
cout<<PI*radius*radius*height<<endl;
ste1.show2();
return 0;
}
2、
#include<iostream>
using namespace std;
const double PI=3.14;
class circle
{
protected:
int radius;
public:
circle(int a):radius(a){}
}
void main()
{
globe g1(4);
circular_column c2(4,6);
circular_cone c3(3,4,5);
g1.b();
g1.t();
c2.b1();
c2.t1();
c3.b2();
c3.t2();
}
{
show();
cout<<" "<<ocp<<" "<<bu<<endl;;
}
int main()
{
student stu1(12,"二毛",88,"541");
steacher ste1(12,"二毛","教师","教学部");
stu1.print();
stu1.show1();
ste1.print();
int score;
string cl;
public:
student(int n,string m,int b,string c):person(n,m)
{
score=b;
cl=c;
}
void show1();
};
void student::show1()
{
show();
cout<<" "<<score<<" "<<cl<<endl;;
课程名称
c++面向对象程序设计
班级
1320541
实验日期
2014.
姓名
陈敏
学号
03
实验成绩
实验名称
类的继承和派生
实
验
目
的
及
要
求
一、实验目的
1、理解继承的含义,掌握派生类的定义方法和实现;
2、理解公有继承下基类成员对派生类成员和派生类对象的可见性,能正确地访问继承层次中的各种类成员;
3、理解保护成员在继承中的作用,能够在适当的时候选择使用保护成员以便派生类成员可以访问基类的部分非公开的成员。
num=n;
name=m;
}
void person::print()
{
cout<<"请输入编号和姓名。"<<endl;
cin>>num>>name;
}
void person::show()
{
cout<<num<<" "<<name;
}
class student: public person
{
private: