/******************************************************************************************
Person 类保存人的名和姓。除了构造函数外,它还有Show()方法,用于显示名和姓。Gunslinger 类以
Person类虚基类派生而来,它包含一个Draw()成员,该方法返回一个double的值,表示枪手的拔枪时间
。这个类还包含一个int成员,表示枪手上枪上的刻痕数。最后,这个类还包含一个Show()函数,用于显
示所有这些信息。
PokerPlayer类以Person类为虚基类派生而来。它包含一个Draw()成员,该函数返回一个1~50之间的随机数
,用于表示扑克牌的值(也可以定义一个Card类,其中包含花色和面值成员,然后让Draw()返回一个Card
对象)。PokerPlayer类使用Person类的show()函数。BadDude类从Gunslinger和PokerPlayer类公有派生而来
。它包含Gdraw()成员(返回坏蛋拔枪的时间)和 Cdraw()成员(返回下一张扑克牌),另外还有一个合适的
Show()函数。请定义这些类和方法以及其他必要的方法.
*******************************************************************************************/
/********************************************************************************************
use_person.cpp
功能:提供测试代码.
备注:这一部分大部分是看标准答案的,然后自己加以修改.
*********************************************************************************************/
#include <iostream>
#include "person.h"
const int SIZE = 5 ;
int main(void)
{
int ct, i;
Person * pp[SIZE];
for (ct = 0; ct < SIZE; ct++)
{
char choice;
cout << "Enter the gang category:\n"
<< "o: ordinary person g: gunslinger "
<< "p: pokerplayer b: bad dude q: quit\n";
cin >> choice ;
while (strchr("ogpbq", choice) == NULL)
{
cout << "Please enter an o, g, p, b, or q: ";
cin >> choice;
}
if (choice == 'q')
break;
switch(choice)
{
case 'o': pp[ct] = new Person;
break;
case 'g': pp[ct] = new Gunslinger;
break;
case 'p': pp[ct] = new PokerPlayer;
break;
case 'b': pp[ct] = new BadDude;
break;
}
while((cin.get()) != '\n')
continue ;
pp[ct]->set();
}
cout << "\nHere is your pp:\n";
for (i = 0; i < ct; i++)
{
cout << '\n';
pp[i]->Show();
}
for (i = 0; i < ct; i++)
delete pp[i];
cout << "\nBye!\n";
return 0;
}
/***********************************************************************
person.h
功能:提供各个类的声明。
备注:1.当派生类是从虚基类派生而来的时候,要更改构造函数初始化的规则.
2.使用公有MI的时候,注意派生类的方法,有时候需要明确指出用哪一个
不然的话会导致二义性。
***********************************************************************/
#ifndef PERSON_H_
#define PERSON_H_
using std::cin ;
using std::cout ;
using std::string ;
using std::endl ;
//class Person
class Person
{
private :
string fname ;
string lname ;
public :
Person() {}
Person(const string &fn , const string &ln):fname(fn) , lname(ln) { }
Person(const Person & p) ;
virtual ~Person() {}
virtual void Show() const ;
virtual void set() ;
} ;
//class Gumslinger
class Gunslinger : virtual public Person
{
private :
double time ;
int hack ;
public :
Gunslinger():Person() { time = 0.0 ; hack = 0 ;}
Gunslinger(const Person & p , double t , int h) : Person(p),time(t),hack(h) {}
Gunslinger(const string &fn ,const string &ln,double t,int h ):Person(fn,ln) ,time(t),hack(h) {}
virtual double Draw(){ return time ;}
virtual void Show() const ;
virtual void set() ;
} ;
//class PokerPlayer
class PokerPlayer : virtual public Person
{
private :
int ipoke ;
public :
PokerPlayer():Person(),ipoke(0) {}
PokerPlayer(const Person & p , int ip) : Person(p) ,ipoke(ip) {}
PokerPlayer(const string &fn,const string &ln,int ip) : Person(fn,ln) ,ipoke(ip) {}
int Draw() {return (rand() % 52 + 1) ;}
virtual ~PokerPlayer() {}
virtual void set() ;
} ;
//class BadDude
class BadDude : public Gunslinger ,public PokerPlayer
{
public :
BadDude() : Person() ,Gunslinger() ,PokerPlayer() {}
BadDude(const string &fn ,const string &ln, double t ,int h ,int ip) :Person(fn,ln) ,Gunslinger(fn,ln,t,h) ,PokerPlayer(fn,ln,ip) {}
BadDude(const Person &p , double t,int h,int ip) :Person(p),Gunslinger(p,t,h) ,PokerPlayer(p,ip) {}
~BadDude() {}
double Gdraw() {return Gunslinger::Draw() ;}
int Cdraw() {return PokerPlayer::Draw() ;}
virtual void Show() const ;
virtual void set() { Gunslinger::set(); }
} ;
#endif
/***************************************************************
person.cpp
功能:提供方法的定义。
备注:要使用getline,一定要包含<string> ,不过不知道为什么还要
加上using namespace std?
***************************************************************/
#include<iostream>
#include"person.h"
#include<string>
using namespace std ;
//Person's method's
void Person::set()
{
cout << "Enter the fname : " << endl ;
getline(cin,fname) ;
cout << "Enter the lname : " << endl ;
getline(cin,lname) ;
}
Person::Person(const Person & p)
{
fname = p.fname ;
lname = p.lname ;
}
void Person::Show() const
{
cout << fname <<" " << lname << endl ;
}
//Gunslinger's methods
void Gunslinger::set()
{
Person::set() ;
cout << "Enter the time: " << endl ;
cin >> time ;
cout << "Enter number of hack: " << endl ;
cin >> hack ;
}
void Gunslinger::Show() const
{
Person::Show() ;
cout << "Time : " << time << endl ;
cout << "Hack : " << hack << endl ;
}
//PokerPlayer's methods
void PokerPlayer::set()
{
Person::set() ;
cout << "Enter the ipoke : " << endl ;
cin >> ipoke ;
}
//BadDude's methods
void BadDude::Show()const
{
Gunslinger::Show() ;
}
C++ 学习--------------公有MI 与 虚基类
最新推荐文章于 2023-05-23 09:24:02 发布