续。。。编程实现:利用继承性与派生类来管理学生老师档案:由person(人员)类出发(作为基类),派生出student(学生)类及teacher(教师)类;而后又由student(学生)类出发(作为基类),派生出graduateStudent(研究生)类。可假定这几个类各自具有的数据成员为: person(人员)类:姓名、性别、年龄。 student(学生)类:姓名、性别、年龄、学号、系别。 graduateStudent(研究生)类:姓名、性别、年龄、学号、系别、导师。 teacher(教师)类:姓名、性别、年龄、职称、主讲课程。 程序代码: #include<iostream> #include<string> using namespace std; class person //person类,它将作为其他几个类的基类 { string name; //姓名 string sex; //性别 int age; //年龄 public: person(string na,string sx,int ag); //构造函数 void display(); //负责显示person类对象的有关数据 }; person::person(string na,string sx,int ag) { name=na; sex=sx; age=ag; } void person::display() { cout<<"姓名:"<<name<<endl; cout<<"性别:"<<sex<<endl; cout<<"年龄:"<<age<<endl; } class teacher:public person //teacher类,由基类person所派生 { string post; //增添数据成员:职称 string course; //增添数据成员:担任课程 public: teacher(string na,string sx,int ag,string po,string co):person(na,sx,ag) { post=po; course=co; } void show(); }; void teacher::show() { cout<<"职称:"<<post<<endl; cout<<"担任课程:"<<course<<endl; } class student:public person //student类,由基类person所派生 { int Reg_Number; //增添数据成员:学号 string department; //增添数据成员:系别 public: student(string na,string sx,int ag,int reg,string depart):person(na,sx,ag) { Reg_Number=reg; department=depart; } void show1();aaaa }; void student::show1() { cout<<"学号:"<<Reg_Number<<endl; cout<<"系别:"<<department<<endl; } class graduateStudent:public person //graduateStudent类,由基类person所派生 { string advisor; //增添数据成员:导师 public: graduateStudent(string na,string sx,int ag,string ad):person(na,sx,ag) { advisor=ad;} void show2();}; void graduateStudent::show2() {cout<<"导师:"<<advisor<<endl; } int main() {student s("tang","男",19,2019,"信息"); cout<<"学生信息:"<<endl; s.display();s.show1(); cout<<endl; teacher t("xie","女",30,"教授","英语"); cout<<"教师信息:"<<endl; t.display();t.show(); cout<<endl; graduateStudent g("wang","男",24,"zhang"); cout<<"研究生信息"<<endl; g.display(); g.show2();return 0;} 程序测试及运行结果:
程序代码: #include<iostream> using namespace std; class vehicle//定义一个类 { public://公有部分 int wheels; int weight; vehicle(int wh,int we)//基类构造函数 {wheels=wh; weight=we; } void display()//声明输出函数 { cout<<"车轮个数"<<wheels<<endl; cout<<"车重"<<weight<<endl; } }; class car:private vehicle//声明私有派生类car {public: int passenger_load; car(int wh,int we,int pl):vehicle(wh,we)//定义派生类car的构造函数 { passenger_load=pl; } void show()//输出 { cout<<"车型:小车"<<endl; cout<<"车轮:"<<wheels<<"个"<<endl; cout<<"重量:"<<weight<<"公斤"<<endl; cout<<"含载人数:"<<passenger_load<<endl; } }; class truck:private vehicle//声明私有派生类truck { public: int passenger_load; int payload; truck(int wh,int we,int pl,int pa):vehicle(wh,we)//定义派生类truck构造函数 { passenger_load=pl; payload=pa; } void show()//输出 { cout<<"车型:卡车"<<endl; cout<<"车轮:"<<wheels<<"个"<<endl; cout<<"重量:"<<weight<<"公斤"<<endl; cout<<"含载人数:"<<passenger_load<<endl; } }; int main()//主函数 { car car1(4,2000,5); truck tru1(10,8000,1,340000); cout<<"输出结果"<<endl; car1.show(); tru1.show(); } 程序测试及运行结果:
计费方式是起价8元,其中含3公里费用, 以后每半公里收费0.7元。 程序代码: #include<iostream> #include<string> using namespace std; class Station//定义一个类 { public: string begin; string final; Station(string b,string f) { begin=b; final=f; } }; class Mile { public: double m; Mile(double l) { m=l; } void getdata() { cin>>m; } }; class Price:public Station,public Mile { public: Price(string b="1",string f="2",int l=0):Station(b,f),Mile(l){} void disp() { double price; if(m<3) price=8; else price=8+(m-3)*0.7; cout<<begin<<final<<endl; cout<<"距离:"<<m; cout<<"价格:"<<price; } }; int main() { Price A; A.getdata(); Price B("关山口","中山公园",32.6); cout<<"输出结果:"<<endl; A.disp(); B.disp(); }
程序测试及运行结果:
程序代码: #include<iostream> #include<string> #include<iomanip> #include<math.h> using namespace std; const double pi=3.1415926; class Shape { protected: double area; public: void virtual show(); }; void Shape::show() { cout<<area; } class Circle:public Shape { double ri; public: Circle(){} Circle(double r); void virtual show(); }; Circle::Circle(double r) { ri=r; area=ri*ri*pi; } void Circle::show() { cout<<"圆的半径是:"<<ri<<endl<<"圆的面积:"<<area<<endl; } class Square:public Shape { double lo; public: Square(){} Square(double); void show(); }; Square::Square(double l) { lo=l; area=lo*lo; } void Square::show() { cout<<"正方形的面积:"<<area<<endl; } class Rectangle:public Shape { double lon,kuan; public: Rectangle(){} Rectangle(double l,double k); void virtual show(); }; Rectangle::Rectangle(double l,double k) { lon=l; kuan=k; area=l*k; } void Rectangle::show() {cout<<"矩形的长:"<<lon<<endl; cout<<"矩形的宽:"<<kuan<<endl; cout<<"矩形的面积:"<<area<<endl; } class Trapezoid:public Shape {double up_lon,do_lon; public: Trapezoid(){} Trapezoid(double,double); void show();}; Trapezoid::Trapezoid(double up,double doi) { up_lon=up; do_lon=doi; area=up*doi;} void Trapezoid::show(){ cout<<"梯形的面积:"<<area<<endl;} class Triangle:public Shape {double a,b,c; public: Triangle(){} Triangle(double aa,double bb,double cc); void show();}; Triangle::Triangle(double aa,double bb,double cc) {a=aa; b=bb; c=cc; double p=(a+b+c)*0.5; area=sqrt(p*(p-a)*(p-b)*(p-c));} void Triangle::show() {cout<<"三角形的面积:"<<area<<endl; } int main() { Shape sh; Circle c1(2); Square sq(2); Rectangle re(3,2); Trapezoid Td(2,3); Triangle Te(3,4,5); Shape *p=&sh; p=&c1; p->show(); p=&sq; p->show(); p=&re; p->show(); p=&Td; p->show(); p=&Te; p->show(); return 0; } 程序测试及运行结果: |
||||||||
分析与讨论:
答: private继承:将基类中公有成员和保护成员在派生类中都设置为私有成员 Protected继承:将基类中公有成员和保护成员在派生类中都设置为保护成员 Public继承:基类成员的访问权限在派生类中保持不变。
答: 首先基类构造函数; 其次再调用成员类对象的构造函数; 最后调用派生类的构造函数。
答: 首先调用派生类的析构函数; 其次再调用成员类对象的析构函数; 最后调用基类的析构函数。 4、虛函数的实质是什么?写出虚函数的定义格式。 答: 虚函数是指一个类中你希望重载的成员函数,当你用一个基类指针或引用指向一个继承类对象的时候,你调用一个虚函数,实际调用的是继承类的版本。
答:是,因为抽象类是指包含纯虚函数的类,它主要用于基类,给其他派生类提供参考,由于含纯虚 函数,故抽象类不能实例化。 |
C语言/C++ 之 继承与派生、多态性与虚函数(5/5)
猜你喜欢
转载自blog.csdn.net/qq_59819866/article/details/131438214
今日推荐
周排行