(C++)实验报告六:继承与派生
- 格式:doc
- 大小:66.00 KB
- 文档页数:4
实验6 继承与派生
一、实验目的
1.理解继承与派生、单继承与多继承的概念;
2.理解基类与派生类的定义及使用方法,派生类对象的定义与初始化方法;
3.理解继承与派生过程中,把派生类作为基类构成类族的概念及虚基类的概念。
二、实验环境
一台PC机,Windows XP操作系统,Visual C++ 6.0开发环境。
三、实验内容
1、由在校人员类(Person)作为基类派生出学生类(Student):
实验步骤:
#include <iostream>
#include <string>
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: "<<ID<<endl;
cout<<"name : "<<name<<endl;
cout<<"sex: "<<sex<<endl;
cout<<"age: "<<age<<endl;
}
private:
int ID;
string name;
char sex;
int age;
};
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: "<<math<<endl;
cout<<"physical: "<<physical<<endl;
cout<<"english: "<<english<<endl;
cout<<"C++: "<<cpp<<endl;
cout<<"total: "<<total<<endl;
}
private:
float math,physical,english,cpp,total;
};
void main(){
Person p1(1,"张帅",'M',22);
p1.show();
cout<<endl;
Student s1(9901,"林维",'S',21,65,70,75,88);
s1.show();
}
实验结果:
2、由学生类、课程类作为基类,共同派生出选课类。
实验步骤:
#include <iostream>
#include <string>
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: "<<No<<endl;
cout<<"Name : "<<Name<<endl;
cout<<"Sex: "<<Sex<<endl;
cout<<"Age: "<<Age<<endl;
}
private:
int No;
string Name;
char Sex;
int Age;
};
class Lesson{
public:
Lesson(int no,char *name,int hour){
Cno=no;
Cname=name;
Chour=hour;
}
void show(){
cout<<"Cno: "<<Cno<<endl;
cout<<"Cname: "<<Cname<<endl;
cout<<"Chour: "<<Chour<<endl;
}
private:
int Cno; string Cname; int Chour;
};
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: "<<Score<<endl;
}
private:
int Score;
};
void main(){
SL s1(25,"MrLin",'S',25,911,"c++",25,100);
s1.show();
}
实验结果:
3、由二维坐标点类Point作为基类派生出圆类Circle;再由圆类Circle作为基类派生出圆柱体类Cylinder。
实验步骤:
#include <iostream>
#include <string>
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<<"("<<x<<","<<y<<")"<<endl;}
protected:
int x,y;
};
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<<"圆半径:"<<radius<<endl;
}
protected:
float radius;
};
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<<"圆柱体高度:"<<height<<endl;
}
private:
float height;
};
int main(){
Point p1(1,2);
p1.show();
cout<<endl;
Circle c1(2,2,3);
c1.show();
cout<<endl;
cylinder cy1;
cy1.show();
return 0;
}
实验结果:
四、实验小结
自己写。