其他分享
首页 > 其他分享> > 实验5

实验5

作者:互联网

一 机械宠物

#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()形参,传递参数
play(&dog); // 按照play()形参,传递参数
system("pause");
}
main
#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()形参,传递参数
play(&dog); // 按照play()形参,传递参数
system("pause");
}

main.cpp
MachinePets.h
#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
#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 "MachinePets.h"
#include <string>
using namespace std;

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

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

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

string Petdogs::talk(){
    return "wang wang~";
}
PetDogs.cpp
#include "Petcats.h"
#include <string>
using namespace std;

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

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

 

标签:Petdogs,string,namespace,实验,MachinePets,Petcats,include
来源: https://www.cnblogs.com/Airlies/p/10972864.html