C++纯虚函数 抽象类Memo派生出两个类Meeting和Interview类共有成员函数show( )

派生的构造函数和析构函数
1.基类的构造函数不能被继承,派生类需声明自己的构造函数。
2.定义构造函数的时候只需要对自己新增的成员初始化,对继承
来的基类成员的初始化会自动调用基类构造函数完成。
3.但是  派生类的构造函数需要给基类的构造函数传递参数。

  单一继承的构造函数定义:
  派生类名::类名(基类所需形参,本类所需形参):基类名(参数表){};
 

实例:

定义一个抽象类Memo,成员数据包括:
(1) 时间begin_time,为string类对象;
(2) 地点place,string类对象;
(3) 内容content,string类对象。
从Memo派生出两个类:Meeting和Interview。
Meeting中还包括结束时间end_time,为string类对象。
Interview还包括被访问人interviewee,为string类对象。
这几个类都有成员函数show( )来显示各数据成员的值。分别设计这三个类,
并在主函数中进行测试,实现多态性。

 

 

多态还有一个特征就是使用指针选择要调用的函数,在运行期间=>即为 动态多态



/*抽象类:内部定义了纯虚函数的类 */
#include<iostream>
#include<string>
using namespace std;
class Memo {
protected:
	string begin_time,place,content;
public:
	Memo(string b,string p,string c){begin_time=b;place=p;content=c;}
	virtual void show()const=0;
	~Memo(){}
};

/*纯虚函数要实例化*/
class Meeting :public Memo{
private:
	string end_time;
public:
	Meeting(string b,string p,string c,string e):Memo(b,p,c),end_time(e){}
	virtual void show()const;
	~Meeting(){}
};
void Meeting::show()const{
	cout<<"begin_time="<<begin_time<<"\nplace="<<place<<"\nend_time="<<end_time<<"\ncontent="<<content<<endl<<endl;
}


class Interview:public Memo{
private:
	string interviewee;
public:
	Interview(string b,string p,string c,string i);
	virtual void show()const;
};

Interview::Interview(string b,string p,string c,string i):
Memo(b,p,c),interviewee(i){}

void Interview::show()const{
	cout<<"begin_time="<<begin_time<<"\nplace="<<place<<"\ninterview="<<interviewee<<"\ncontent="<<content<<endl<<endl;
}


void main(){
	Memo *memo;
	Interview interview("12:30","shanghai","工作","mulina");
	memo=&interview;   /*可以将派生类的对象赋值给基类的指针,基类指针指向派生中基类的部分*/
	memo->show();
	Meeting meeting("12:30","4楼会议室","交流会","14:30");
	memo=&meeting;
	memo->show();
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值