📄 程序12.4:例题与应用.cpp
字号:
/* 程序12.4:例题与应用.cpp:*/
#include<iostream> //包含头文件
#include<string> //包含头文件
using namespace std; //使用名字空间std
class Person //声明基类Person
{
protected:
string cName;
int iAge;
string cBirth;
string cPhone;
public:
Person(string cName,int iAge,string cBirth,string cPhone)
{
this->cName=cName;
this->iAge=iAge;
this->cBirth=cBirth;
this->cPhone=cPhone;
}
virtual void Display() //声明基类成员函数
{
cout<<"输入的姓名是 : "<<cName<<endl;
cout<<"输入的年龄是 : "<<iAge<<endl;
cout<<"输入的生日是 : "<<cBirth<<endl;
cout<<"输入的电话是 : "<<cPhone<<endl;
}
};
class Employee:public Person //声明子类Employee
{
private:
float fSalary;
public:
Employee(string cName,int iAge,string cBirth,string cPhone,
float fSalary):Person(cName,iAge,cBirth,cPhone)
{
this->fSalary=fSalary;
}
void Display() //声明子类成员函数
{
cout<<"\n------显示职员的个人信息------"<<endl;
Person::Display();
cout<<"输入的工资是 : "<<fSalary<<endl;
}
};
class Student:public Person//声明子类Student
{
private:
float fScore;
public:
Student(string cName,int iAge,string cBirth,string cPhone,
float fScore):Person(cName,iAge,cBirth,cPhone)
{
this->fScore=fScore;
}
void Display() //声明子类成员函数
{
cout<<"\n------显示学生的个人信息------"<<endl;
Person::Display();
cout<<"输入的分数是 : "<<fScore<<endl;
}
};
int main()
{
Person *Ptr;
Ptr= new Employee("abc",20,"315","110",900.5);
Ptr->Display();
Ptr= new Student("efg",17,"610","119",99.5);
Ptr->Display();
delete Ptr;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -