1. 程式人生 > >第十四章 程式設計練習(2)

第十四章 程式設計練習(2)

程式設計練習4

//Person.h
#ifndef PERSON_H_
#define PERSON_H_

#include <string>
#include <iostream>
class Person
{
private:
    std::string name;
protected:
    const std::string &GetName() const { return name; }
public:
    Person() { name = "NULL"; }
    Person(const std::string &s) { name = s; }
    Person(const
char *s) { name = s; } virtual ~Person() {} virtual void Show() const { std::cout << "Person" << std::endl; std::cout << "name: " << name << std::endl; } }; class Gunslinger : virtual public Person { private: int nick; double DrawTime; protected: int
GetNick() const { return nick; } public: Gunslinger() : Person() { nick = 0; DrawTime = 0; } Gunslinger(const std::string &s, int n, double d) : Person(s) { nick = n; DrawTime = d; } Gunslinger(const char *s, int n, double d) : Person(s) { nick = n; DrawTime = d; } double Draw() const
{ return DrawTime; } virtual void Show() const { std::cout << "Gunslinger" << std::endl; std::cout << "name: " << GetName() << std::endl; std::cout << "nick: " << nick << std::endl; std::cout << "DrawTime: " << DrawTime << std::endl; } }; class PokerPlayer : virtual public Person { public: PokerPlayer() : Person() {} PokerPlayer(const std::string &s) : Person(s) {} PokerPlayer(const char *s) : Person(s) {} int Draw() const { return std::rand() % 53 + 1; } }; class BadDude : public Gunslinger, public PokerPlayer { public: BadDude() : Person(), Gunslinger(), PokerPlayer() {} BadDude(const std::string &s) : Person(s) {} BadDude(const char *s) : Person(s) {} BadDude(const std::string &s, int n, double d) : Person(s), Gunslinger(s, n, d) {} BadDude(const char *s, int n, double d) : Person(s), Gunslinger(s, n, d) {} double Gdraw() const { return Gunslinger::Draw(); } int Cdraw() const { return PokerPlayer::Draw(); } virtual void Show() const { std::cout << "BadDude" << std::endl; std::cout << "name: " << GetName() << std::endl; std::cout << "nick: " << GetNick() << std::endl; std::cout << "DrawTime: " << Gdraw() << std::endl; std::cout << "Card: " << Cdraw() << std::endl; } }; #endif
//main.cpp
#include "Person.h"

int main()
{
    Person p("Tom");
    Gunslinger g("Tom", 3, 1.2);
    PokerPlayer po("Jerry");
    BadDude b("Jimmy", 2, 1.1);

    Person *ptr = &p;
    ptr->Show();

    ptr = &g;
    ptr->Show();

    ptr = &po;
    ptr->Show();

    ptr = &b;
    ptr->Show();
    system("pause");
    return 0;
}

程式設計練習5
emp.h標頭檔案和測試主檔案題目給出來了,這裡就把類的定義貼出來

//emp.cpp
#include "emp.h"

abstr_emp::abstr_emp() : fname("NULL"), lname("NULL"), job("NULL")
{
}

abstr_emp::abstr_emp(const std::string & fn, const std::string & ln, const std::string & j) : fname(fn), lname(ln), job(j)
{
}

abstr_emp::abstr_emp(const abstr_emp & e) : fname(e.fname), lname(e.lname), job(e.job)
{
}

void abstr_emp::ShowAll() const
{
    std::cout << "fname: " << GetFname() << std::endl;
    std::cout << "lname: " << GetLname() << std::endl;
    std::cout << "job: " << GetJob() << std::endl;
}

void abstr_emp::SetAll()
{
    SetFname();
    SetLname();
    SetJob();
}

std::ostream &operator<<(std::ostream &os, const abstr_emp &e)
{
    os << e.fname << ", " << e.lname << ", " << e.job;
    return os;
}

abstr_emp::~abstr_emp()
{
}

employee::employee() : abstr_emp()
{
}

employee::employee(const std::string & fn, const std::string & ln, const std::string & j) : abstr_emp(fn, ln, j)
{
}

void employee::ShowAll() const
{
    abstr_emp::ShowAll();
}

void employee::SetAll()
{
    abstr_emp::SetAll();
}

manager::manager() : abstr_emp()
{
    inchargeof = 0;
}

manager::manager(const std::string & fn, const std::string & ln, const std::string & j, int ico) : abstr_emp(fn, ln, j)
{
    inchargeof = ico;
}

manager::manager(const abstr_emp & e, int ico) : abstr_emp(e)
{
    inchargeof = ico;
}

manager::manager(const manager & m) : abstr_emp((const abstr_emp &)m)
{
    inchargeof = m.inchargeof;
}

void manager::ShowAll() const
{
    abstr_emp::ShowAll();
    std::cout << "inchargeof: " << InChargeOf() << std::endl;
}

void manager::SetAll()
{
    abstr_emp::SetAll();
    SetInChargeOf();
}

fink::fink() : abstr_emp()
{
    reportsto = "NULL";
}

fink::fink(const std::string & fn, const std::string & ln, const std::string & j, const std::string & rpo) : abstr_emp(fn, ln, j)
{
    reportsto = rpo;
}

fink::fink(const abstr_emp & e, const std::string & rpo) : abstr_emp(e)
{
    reportsto = rpo;
}

fink::fink(const fink & e) : abstr_emp((const abstr_emp &)e)
{
    reportsto = e.reportsto;
}

void fink::ShowAll() const
{
    abstr_emp::ShowAll();
    std::cout << "reportsto" << ReportsTo() <<  std::endl;
}

void fink::SetAll()
{
    abstr_emp::SetAll();
    SetReportsto();
}

highfink::highfink() : abstr_emp(), manager(), fink()
{
}

highfink::highfink(const std::string & fn, const std::string & ln, const std::string & j, const std::string & rpo, int ico) : abstr_emp(fn, ln, j), manager(fn, ln, j, ico), fink(fn, ln, j, rpo)
{
}

highfink::highfink(const abstr_emp & e, const std::string & rpo, int ico) : abstr_emp(e), manager(e, ico), fink(e, rpo)
{
}

highfink::highfink(const fink & f, int ico) : abstr_emp((const abstr_emp &)f), manager((const abstr_emp &)f, ico), fink(f)
{
}

highfink::highfink(const manager & m, const std::string & rpo) : abstr_emp((const abstr_emp &)m), manager(m), fink((const abstr_emp &)m, rpo)
{
}

highfink::highfink(const highfink & h) : abstr_emp((const abstr_emp &)h), manager((const manager &)h), fink((const fink &)h)
{
}

void highfink::ShowAll() const
{
    abstr_emp::ShowAll();
    std::cout << "inchargeof: " << InChargeOf() << std::endl;
    std::cout << "reportsto: " << ReportsTo() << std::endl;
}

void highfink::SetAll()
{
    abstr_emp::SetAll();
    manager::SetInChargeOf();
    fink::SetReportsto();
}