C++ 继承与派生—笔记
1. 编写一个程序,其中有一个汽车类Vehicle,它具有一个需要传递参数的构造函数,类中的数据成员:车轮个数wheels和车重weight为保护属性;小车类Car是它的私有派生类,其中包含载人数passager_load;卡车类Truck是Vehicle的私有派生类,其中包含载人数passager_load和载重量payload。每个类都有相关数据的输出方法。
#include
#include
using namespace std;
class Vehicle//声明汽车类
{
protected:
int wheel;//车轮
double weight;//车重
public:
Vehicle(int w,double i){//构造函数
wheel = w;
weight = i;
}
void show(){//输出函数
cout<<"车轮个数:"< }
};
class Car:private Vehicle//声明小车类,私有继承汽车类
{
private:
int passager_load;//载人数
public:
Car(int w,double i,int p):Vehicle(w,i){//构造函数
passager_load = p;
}
void display(){//输出函数
cout<<"\n";
Vehicle::show();
cout<<"\t\t载人数:"< }
};
class Truck:private Vehicle//声明卡车类,私有继承汽车类
{
private:
int passager_load;//载人数
int payload;//载重量
public:
Truck(int w,double i,int p,int l):Vehicle(w,i){//构造函数
passager_load = p;
payload = l;
}
void display1(){//输出函数
Vehicle::show();
cout<<"\t\t载人数:"< }
};
int main()
{
Vehicle V(4,20);
V.show();
Car C(4,40.0,4);
C.display();
Truck T(6,80.0,2,100);
T.display1();
system("pause");
return 0;
}
2. 假定居民的基本数据包括身份证号、姓名、性别和出生日期,而居民中的成年人又多两项数据:最高学历和职业,成年人中的党员又多一项数据:党员类别。现要求建立三个类,让成年人类继承居民类,而党员类继承成年人类,并要求在每个类中都提供有数据输入和输出的功能。
#include
#include
using namespace std;
class Residents
{
private:
int number;//身份证号
string name;//姓名
string sex;//性别
int birthday;//出生日期
public:
void input(){
cout<<"请按顺序(身份证号、姓名、性别、出生日期)输入居民信息:"< cin>>number>>name>>sex>>birthday;
}
void output() {
cout<<"\n\r输出居民信息:"< cout<<" "<<number<<" "<<name<<" "<<sex<<" "<<birthday<<endl;
}
};
class Adult:public Residents
{
private:
string Degree;//最高学历
string Occupation;//职业
public:
void input1(){
Residents::input();
cout<<"请输入最高学历和职业:"<<endl;
cin>>Degree>>Occupation;
}
void output1() {
cout<<"\n\r输出成年人信息:"< Residents::output();
cout<<Degree<<" "<<Occupation<<endl;
}
};
class Party: public Adult
{
private:
string parties; //党员类别
public:
void input2() {
Adult::input1();
cout<<"请输入党员类别:"<<endl;
cin>>parties;
}
void output2(){
cout<<"\n\r输出党员信息:"<<endl;
Adult::output1();
cout<<parties<<endl;
}
};
int main()
{
//测试居民类
Residents R;
R.input();
R.output();
cout<<"\n"<
//测试成人类
Adult A;
A.input1();
A.output1();
cout<<"\n"<
// 测试党员类
Party P;
P.input2();
P.output2();
system("pause");
return 0;
}
3. 数据成员 length,width;
成员函数 Rectangle( double l, double w ); //构造函数
double Area(); //返回矩形面积
double GetLength(); //返回数据成员length的值
double GetEidth(); //返回数据成员width的值
再定义Rectangle的派生类Rectangular,它包含:
数据成员 height
成员函数 Rectangular( double l, double w, double h ); //构造函数
double Volume(); //返回长方体体积
double GetHeight(); //返回数据成员height的值
在main函数中测试类体系,建立两个类的对象,显示它们的数据和面积、体积。
#include
#include
using namespace std;
class Rectangle//声明类
{
public:
double length;
double width;
Rectangle();//无参构造
Rectangle(double l, double w); //构造函数
double Area(); //返回矩形面积
double GetLength(); //返回数据成员length的值
double GetWidth(); //返回数据成员width的值
};
Rectangle::Rectangle(double l,double w)
{
length = l;
width = w;
}
double Rectangle::Area()
{
return (length * width);
}
double Rectangle::GetLength()
{
return length = 0.0;
}
double Rectangle::GetWidth()
{
return width = 0.0;
}
class Rectangular:public Rectangle
{
private:
double height;
public:
Rectangular();//无参构造
Rectangular( double l, double w, double h ); //构造函数
double Volume(); //返回长方体体积
double GetHeight(); //返回数据成员height的值
};
Rectangular::Rectangular(double l,double w,double h):Rectangle(l,w)
{
height = h;
}
double Rectangular::Volume()
{
return (length * width * height);
}
double Rectangular::GetHeight()
{
return height = 0.0;
}
int main()
{
Rectangle r(30,40);
cout<<"面积为:"<
Rectangular t(30,40,50);
cout<<"体积为:"<
system("pause");
return 0;
}
**4. 编写一个师生数据输入和显示程序,学生数据有编号、姓名、班号和成绩,教师数据有编号、姓名、职称和部门。要求将编号、姓名、输入和显示设计成一个类Person,并作为学生数据操作类Student和教师数据操作类Teacher的基类。
**
#include
#include
using namespace std;
class Person//声明类person
{
public:
string name;//姓名
int number;//编号
Person();//无参构造
Person(string n,int num){//构造函数
name = n;
number = num;
}
void show(){//输出
cout<<"姓名:"< }
};
class Student:public Person//声明学生类
{
private:
int Class;//班号
int score;//成绩
public:
Student();//无参构造
Student(string n,int num,int C,int s):Person(n,num){//构造函数
Class = C;
score = s;
}
void display(){//输出函数
show();
cout<<"\t"<<"班号:"< }
};
class Teacher:public Person//声明教师类
{
private:
string title;//职称
string section;//部门
public:
Teacher();//无参构造
Teacher(string n,int num,string t,string s):Person(n,num){//构造函数
title = t;
section = s;
}
void display(){//输出函数
show();
cout<<"\t"<<"职称:"< }
};
int main()
{
Student S("小李",123456,01,500);
S.display();
Teacher T("老张",987654,"班主任","数学");
T.display();
system("pause");
return 0;
}