C++PP 14-4

//person.h


#ifndef PERSON_H_

#define PERSON_H_


#include <iostream>
using std::ostream;


class Person
{
private:
char * m_lastname;    //名
char * m_firstname;   //姓
public:
Person();
Person(char * l, char * f);
virtual ~Person();
Person(const Person & p);                                   //复制构造函数
Person & operator=(const Person & p);                       //重载赋值运算符
friend ostream & operator<<(ostream & os,const Person & p); //重载输出运算符
virtual void Set();                                                 //设置姓名
virtual void Show();                                                //显示姓      名
};


class Gunslinger : virtual public Person     //枪手类
{
private:
int m_henji;  //手枪痕迹数
public:
Gunslinger();
~Gunslinger();
double Draw();  //枪手拔枪时间
void Set();
void Show();    
};


class PokerPlayer : virtual public Person //扑克类
{
private:
public:
PokerPlayer();
~PokerPlayer();
int Draw();  //返回一个1-52的随机值
void Set();
void Show(); //使用Person的Show()函数
};


class BadDude : public Gunslinger, public PokerPlayer
{
private:
public:
BadDude();
~BadDude();
double Gdraw();  //返回坏蛋拔枪的时间
int Cdraw();     //返回下一张扑克牌
void Set();
void Show();
};


#endif


//person.cpp

#include "stdafx.h"
#include <iostream>
#include "person.h"


#include <ctime>
#include <cstdlib>


//Person类
Person::Person()                //默认构造函数
{
m_lastname = new char[2];   //分配内存
m_lastname[0] = 'S';
m_lastname[1] = '\0';       //不加这个的话会导致默认构造函数出现乱码
m_firstname = new char[2];  //即使用默认构造函数时输入一个字符后会乱码
m_firstname[0] = 'B';
m_firstname[1] = '\0';
}


Person::Person(char *l, char *f)  //有2个参数的构造函数
{
int len;                        //计算字符参数长度变量
len = strlen(l);                //计算长度
m_lastname = new char[len + 1]; //动态分配内存  长度要加1
strcpy(m_lastname, l);          //字符指针复制使用strcpy
len = strlen(f);
m_firstname = new char[len + 1];
strcpy(m_firstname, f);
}


Person::~Person()
{
delete[] m_lastname;
delete[] m_firstname;
}


Person & Person::operator=(const Person & p)
{
if (this == &p)          //检查自我复制
return *this;
int len;
delete[] m_lastname;              //删除以前的内存
len = strlen(p.m_lastname);       //计算长度
m_lastname = new char[len + 1];   //分配长度
strcpy(m_lastname, p.m_lastname); //复制
delete[] m_firstname;
len = strlen(p.m_firstname);
m_firstname = new char[len + 1];
strcpy(m_firstname, p.m_firstname);
return *this;                    //返回被赋值的对象
}


ostream & operator<<(ostream & os,const Person & p)  //重载<<运算符

//os << "名: " << p.m_lastname << " . " << "姓: " << p.m_firstname << '\n';
//os << "名: " << p.m_lastname  << "姓: " << p.m_firstname << '\n';
os << "名: " << p.m_lastname;
std::cout << "-----";
os << "姓: " << p.m_firstname;
return os;
}


void Person::Set() //设置对象值
{
using std::cout;
using std::cin;
cout << "输入名: ";               //显示DEBUG ERROR
cin.getline(m_lastname,20);      //不知道为毛
cout << "输入姓: ";              //不能使用cin.get();
cin.getline(m_firstname, 20);    //使用cin.getline()即可
//int len;
//len = strlen(l);        //计算名的长度
//strcpy(m_lastname, l);  //复制
//len = strlen(f);        //计算姓的长度
//strcpy(m_firstname, f);
}


void Person::Show()  //显示Person信息
{
using std::cout;
using std::endl;
cout << "名为: " << m_lastname << endl;
cout << "姓为: " << m_firstname << endl;
}


//Gunslinger类
Gunslinger::Gunslinger() :Person()  //使用列表初始化
{
int m_henji = 1;
}


Gunslinger::~Gunslinger()
{


}


double Gunslinger::Draw()  //拔手枪所需要的时间
{
return 0.442;
}


void Gunslinger::Set()  //设置对象
{
Person::Set();
std::cout << " 输入手枪刻痕数: ";
std::cin >> m_henji;
}


void Gunslinger::Show()
{
using std::endl;
Person::Show();
std::cout << "手枪刻痕数为: " << m_henji << endl;
}


//PokerPlayer类
PokerPlayer::PokerPlayer()
{


}


PokerPlayer::~PokerPlayer()
{


}


int PokerPlayer::Draw()  //产生随机数1-52,相当于扑克牌
{
srand(time(0));
return rand() % 52 + 1;
}


void PokerPlayer::Set()  //设置对象
{
Person::Set();
}


void PokerPlayer::Show()
{
Person::Show();
}


//BadDude类
BadDude::BadDude()
{


}


BadDude::~BadDude()
{


}


double BadDude::Gdraw()  //返回坏蛋的拔枪时间
{
return Gunslinger::Draw();

}


int BadDude::Cdraw()  //返回下一张扑克牌
{
return PokerPlayer::Draw();
}


void BadDude::Set()
{
Gunslinger::Set();
}


void BadDude::Show()  
{
using std::cout;
using std::endl;
Person::Show();
cout << "坏蛋拔枪时间: " << Gdraw() << endl;
cout << "下一次扑克牌: " << Cdraw() << endl;
}


//mian

#include "stdafx.h"
#include <iostream>
#include "person.h"


const int SIZE = 5;


int _tmain(int argc, _TCHAR* argv[])
{
using std::cout;
using std::endl;
//测试Perso类
//Person p1;
//p1.Set("cao","chao");
//p1.Show();
//std::cout << p1 << std::endl;
//Person p2("cao", "chao");
    //cout << p2 << endl;
//p2.Show();
//Person p3;
//p3 = p2;
//p3.Set();
//cout << p3 << endl;  


//Person * p[SIZE];               //使用指针数组报异常
//Person * p = new Person[SIZE];      
//p[1].Set();
//p[1].Show();

Person * p1;
p1 = new Gunslinger;
p1->Set();
p1->Show();


std::cin.get();
std::cin.get();
return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值