实验五

#include <iostream>
#include "MachinePets.h"
#include "Petcats.h"
#include "Petdogs.h"
using namespace std;

void play(MachinePets *m){
    cout<<m->getnickname()<<" say "<<m->talk()<<endl;
}

int main() {
Petcats cat("miku");
Petdogs dog("da huang");
play(&cat); 
play(&dog);
system("pause");
}
main.cpp
#ifndef _MACHINEPETS
#define _MACHINEPETS
#include <string>
using namespace std;

class MachinePets{
public:
    MachinePets(string s);
    virtual string talk()=0;
    string getnickname();
private:
    string nickname;
};
#endif

MachinePets.h
MachinePets.h
#include "MachinePets.h"
#include <string>
using namespace std;

MachinePets::MachinePets(string s):nickname(s){
}

string MachinePets::getnickname(){
    return nickname;
}
MachinePets.cpp
#ifndef _PETDOGS
#define _PETDOGS
#include"MachinePets.h"
#include<string>
using namespace std;

class Petdogs:public MachinePets{
public:
    Petdogs(const string s);
    string talk();
};
#endif
Petdogs.h
#include <string>
#include"Petdogs.h"
using namespace std;

Petdogs::Petdogs(const string s):MachinePets(s){}

string Petdogs::talk(){
    return "wang wang~";
}
Petdogs.cpp
#ifndef _PETCATS
#define _PETCATS
#include"MachinePets.h"
#include<string>
using namespace std;

class Petcats:public MachinePets{
public:
    Petcats(string s);
    string talk();
};
#endif
Petcats.h
#include "Petcats.h"
#include <string>
using namespace std;

Petcats::Petcats(const string s):MachinePets(s){
}

string Petcats::talk(){
    return "miao wu~";
}
Petcats.cpp

 

转载于:https://www.cnblogs.com/wsggwsc/p/10972885.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值