C++实验6 继承与派生(一)
实验名称:实验6 继承与派生(一)
所使用的开发工具及环境:PC机一套 Visual Studio 2010
实验要求:
1.硬件基本配置:Intel PentiumIII以上级别的CPU,大于64MB的内存。
2.软件要求:Window 2000操作系统,Visual Studio 6.0或更高版本开发环 境。
3.实验学时:2学时
4.实现实验内容中的题目。
5.写实验报告
实验目的:
理解继承与派生、单继承与多继承的概念;
理解基类与派生类的定义及使用方法,派生类对象的定义与初始化方法;
理解继承与派生过程中,把派生类作为基类构成类族的概念及虚基类的概念。
实验内容:
1、由在校人员类(Person)作为基类派生出学生类(Student):
#include<iostream>
#include<string>
using namespace std;
class Person{
public:
Person();
Person(int i, string n, char s, int a){id = i; name = n; sex = s; age = a;}
int getID(){return id;}
void show()
{ cout<<"id :"<<id<<"\nname : "<< name <<"\nsex : "<<sex<<"\nage : "<<age<<endl;}
private:
int id;
string name;
char sex;
int age;
};
class Student:public Person{
public:
Student();
Student(int i, string 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 = m + p + e + c; }
void show(){
cout<<"math : "<<math<<"\nphysical : "<< physical <<"\nenglish : "<<english<<"\ncpp : "<<cpp<<"\ntotal : "<<total<<endl;
}
/*void show_person(){
cout<<endl<<"Class Person is : "<<endl;
person.show();
}*/
private:
//Person person;
float math;
float physical;
float english;
float cpp;
float total;
};
int main(){
Person p1(1,"张三",'M',18);
p1.show();
cout<<endl;
Student s1(2,"王五",'W',28,100,90,80,70);
p1.show();
s1.show();
return 0;
}
2、由学生类、课程类作为基类,共同派生出选课类。
#include<iostream>
#include<string>
using namespace std;
class Student{
public:
Student(){}
Student(int i, string n, char s, int a){no = i; name = n; sex = s; age = a; }
void show(){cout<<"no : "<<no<<"\nname : "<<name<<"\nsex : "<<sex<<"\nage : "<<age<<endl;}
private:
int no;
string name;
char sex;
int age;
};
class Lesson{
public:
Lesson();
Lesson(int no,string name,int hour)
{cno = no; cname = name; chour = hour;}
void show()
{cout<<"cno : "<<cno<<"\ncname : "<<cname<<"\nchour : "<<chour<<endl;}
private:
int cno;
string cname;
int chour;
};
Lesson::Lesson(){}
class SL:public Student,public Lesson{
public:
SL(){}
SL(int i, string n, char s, int a, int no, string nm, int hour, int score ):lesson(no,nm,hour),student(i,n,s,a)
{this->score = score;}
void show(){
student.show();
lesson.show();
cout<<"score : "<<score<<endl;}
/* void show_Student(){
student.show();
}
void show_Lesson(){
lesson.show();
}*/
private:
Student student;
Lesson lesson;
int score;
};
int main(){
SL s1(1,"LaicZhang",'M',19,2,"c++",64,100);
s1.show();
}
结果截图
3、由二维坐标点类Point作为基类派生出圆类Circle;再由圆类Circle作为基类派生出圆柱体类Cylinder。
#include<iostream>
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;
}
private:
int x;
int y;
};
class Circle:public Point{
public:
Circle(int xx = 0, int yy = 0,float r =1):point(xx,yy){
radius = r;
}
double getR(){
return radius;
}
void show(){
cout<<"圆心坐标:";
point.show();
cout<<"圆半径:"<<radius<<endl;
}
private:
Point point;
float radius;
};
class Cylinder:public Circle{
public:
Cylinder(int xx = 0, int yy = 0, float r = 1, float h = 2):circle(xx,yy,r){
height = h;
}
float getH(){
return height;
}
void show(){
circle.show();
cout<<"圆柱体高度:"<<height<<endl;
}
private:
Circle circle;
float height;
};
int main(){
Point p1(5,5);
p1.show();
cout<<endl;
Circle c1(7,8,9);
c1.show();
cout<<endl;
Cylinder cy1;
cy1.show();
return 0;
}
结果截图
结果与分析 ( 收获、问题 )
理解继承与派生、单继承与多继承的概念;
理解基类与派生类的定义及使用方法,
派生类对象的定义与初始化方法;
理解继承与派生过程中,把派生类作为基类构成类族的概念及虚基类的概念。