您好,欢迎来到五一七教育网。
搜索
您的当前位置:首页(C++)实验报告六:继承与派生

(C++)实验报告六:继承与派生

来源:五一七教育网


实验6 继承与派生

一、实验目的

1.理解继承与派生、单继承与多继承的概念;

2.理解基类与派生类的定义及使用方法,派生类对象的定义与初始化方法; 3.理解继承与派生过程中,把派生类作为基类构成类族的概念及虚基类的概念。 二、实验环境

一台PC机,Windows XP操作系统,Visual C++ 6.0开发环境。 三、实验内容

1、由在校人员类(Person)作为基类派生出学生类(Student): 实验步骤:

#include

#include using namespace std; class Person{ public: Person(int i,char *n, char s, int a){ ID=i; name=n; sex=s; age=a; }; int getID(){ return ID; } void show(){ cout<<\"ID: \"<class Student:public Person{ public:

Student(int i,char *n,char s,int a,float m,float p,float e,float c):Person(i,n,s,a){ math=m; physical=p; english=e; cpp=c; total=math+physical+english+cpp; }

void show(){ Person::show(); cout<<\"math: \"<void main(){

Person p1(1,\"张帅\",'M',22); p1.show(); cout<实验结果:

2、由学生类、课程类作为基类,共同派生出选课类。 实验步骤:

#include #include using namespace std; class Student{ public: Student(int i,char *n, char s, int a){ No=i; Name=n; Sex=s; Age=a; }; void show(){ cout<<\"No: \"<class Lesson{ public:

Lesson(int no,char *name,int hour){ Cno=no; Cname=name; Chour=hour; } void show(){ cout<<\"Cno: \"<class SL:public Student,public Lesson{ public: SL(int i,char *n,char s,int a,int no,char *name,int hour, int score):Student(i,n,s,a),Lesson(no,name,hour){ Score=score; } void show(){ Student::show(); Lesson::show();

cout<<\"Score: \"<void main(){ SL s1(25,\"MrLin\",'S',25,911,\"c++\",25,100); s1.show(); }

实验结果:

3、由二维坐标点类Point作为基类派生出圆类Circle;再由圆类Circle作为基类派生出圆柱体类Cylinder。

实验步骤:

#include #include using namespace std; class Point { public: Point(int xx=0, int yy=0){ x=xx; y=yy; } int getX() { return x; } int getY() { return y; } void show() {cout<<\"(\"<class Circle:virtual public Point{ public: Circle(int xx=0,int yy=0,float r=1):Point(xx,yy){ radius=r; } int getR() {return radius;} void show(){ cout<<\"圆心坐标:\"; Point::show(); cout<<\"圆半径:\"<class cylinder:public Circle{ public: cylinder(int xx=0,int yy=0,float r=1,float h=2):Point(xx,yy),Circle(r){ height=h; } int getH() {return height;} void show(){ Circle::show(); cout<<\"圆柱体高度:\"<int main(){ Point p1(1,2); p1.show(); cout<Circle c1(2,2,3); c1.show(); cout<实验结果:

四、实验小结 自己写

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- 517ttc.cn 版权所有 赣ICP备2024042791号-8

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务