0002[虚构函数]

abstract.h

#ifndef abstract_H
#define abstract_H

#include <string>

using std::string;

class System {
public:
	virtual string info() = 0;
	virtual ~System() {}
};

class Activity {
protected:
	System* system_;
	string name_;
public:
	Activity(System* system, string name) {}
	~Activity() {}
	virtual string play() = 0;
};

#endif

SystemActivity.h

#ifndef SystemActivity_H
#define SystemActivity_H


#include <string>
#include"abstract.h"

using namespace std;


class Windows : public System {
public:
	string info();
	~Windows() {};
};

class Linux : public System {
public:
	string info();
	~Linux(){};
};

class Game : public Activity {
public:
	Game(System* system, string name);
	string play();
	~Game(){};
};

class Study : public Activity {
public:
	Study(System* system, string name);
	string play();
	~Study(){}
};

#endif

SystemActivity.cpp

#include <iostream>
#include <string>
#include"abstract.h"
#include"SystemActivity.h"
using namespace std;

string Windows::info(){
		return "Windows QAQ";
};

string Linux::info(){
		return "Linux T_T";

};



Game::Game(System* system, string name):Activity(system, name) {
	system_ = system;
	name_ = name;
};
string Game::play() {
	string s = " is playing game on ";
	string ss = system_->info() ;
	s = name_ + s;
	ss = s + ss;
	return ss;
};


Study::Study(System* system, string name):Activity(system, name){
	system_ = system;
	name_ = name;
};
string Study::play() {
	string s = " is studying on ";
	string ss = system_->info() ;
	s = name_ + s;
	ss = s + ss;
	return ss;
};

main.cpp

#include <iostream>
#include <string>
#include "SystemActivity.h"

using std::cin;
using std::cout;
using std::endl;
using std::string;

int main() {
	string a, b;
	cin >> a >> b;
	Linux l;
	Windows w;
	System * linux = reinterpret_cast<System *> (&l);
	System * windows = reinterpret_cast<System *> (&w);

	Study s1(linux, a);
	Study s2(windows, a);
	Game g1(windows, b);
	Game g2(linux, b);
	Activity * activity1 = reinterpret_cast<Activity *> (&s1);
	Activity * activity2 = reinterpret_cast<Activity *> (&s2);
	Activity * activity3 = reinterpret_cast<Activity *> (&g1);
	Activity * activity4 = reinterpret_cast<Activity *> (&g2);

	cout << activity1->play() << endl;
	cout << activity2->play() << endl;
	cout << activity3->play() << endl;
	cout << activity4->play() << endl;

	system("pause");
	return 0;
}

这道题最痛苦的在于在阿比stract里面的activity没有默认构造函数。编译器会一直提示没有可用的默认构造函数

因为假如函数中没有构造函数,编译器就会给他一个默认构造函数,一旦自己写了构造函数,编译器就不再匹配给你默认构造函数

可是main函数中并没有用到默认构造函数

为什么会调用到默认构造函数呢

因为虽然在SystemActivity中有传入参数给Game和Study但是没有传到Activity,所以还是会调用Activity的默认构造函数

所以要在Game和Study后面加上:Activity()把参数也给传到Activity里面去。这样就不会调用到Activity的默认构造函数,也就不会出现没有合适的默认构造参数可用的问题了。

终于写完这道题了- -

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值