C++ Primer Plus, Chapter 14, excercise

来源:互联网 发布:djvu mac 打开 编辑:程序博客网 时间:2024/06/03 19:25

1.Wine类有一个string对象和一个Pair对象,后者有两个”valarray<’int>”对象 (包含)

//wine.h#ifndef WINE_H_#define WINE_H_#include <valarray>#include <string>typedef std::valarray<int> ArrayInt;typedef std::pair<ArrayInt, ArrayInt> PairArray;class Wine{private:    std::string label;    PairArray arr;    int year;public:    Wine() {};    Wine(const char * l, int y, const int yr[], const int bot[]);    Wine(const char * l, int y);    void Getbottles();    std::string & Label() { return label; }    int sum();    void show() const;};#endif//wine.cpp#include "stdafx.h"#include "wine.h"#include <iostream>Wine::Wine(const char * l, int y, const int yr[], const int bot[]) :label(l), year(y){    arr.first = ArrayInt(year);    arr.second = ArrayInt(year);    for (int i = 0; i < year; i++)    {        arr.first[i] = yr[i]; //first and second 是pair的属性值,不是方法,不能有()        arr.second[i] = bot[i];    }}Wine::Wine(const char * l, int y) :label(l), year(y){    arr.first = ArrayInt(year);    //需要将数组定义一下,初始化,不然没有分配内存    arr.second = ArrayInt(year);}void Wine::Getbottles(){    std::cout << "Enter Gully Wash data for " << year << " year(s):\n";    for (int i = 0; i < year; i++)    {        std::cout << "Enter year: ";        std::cin >> arr.first[i];        std::cout << "Enter bottles for that year: ";        std::cin >> arr.second[i];    }}int Wine::sum(){    return arr.second.sum();}void Wine::show() const{    std::cout << "Wine: " << label << "\n";    std::cout << "\tYear\tBottles\n";    for (int i = 0; i < year; i++)        std::cout << "\t" << arr.first[i] << "\t" << arr.second[i] << "\n";}//main.cpp#include "stdafx.h"#include "wine.h"#include <iostream>#include <ctime>int main(){    using std::cout;    using std::cin;    using std::endl;    cout << "Enter name of wine: ";    char lab[50];    cin.getline(lab, 50);    cout << "Enter number of years: ";    int yrs;    cin >> yrs;    Wine holding(lab, yrs);    holding.Getbottles();    holding.show();    const int YRS = 3;    int y[YRS] = { 1993, 1995, 1998 };    int b[YRS] = { 48, 60, 72 };    Wine more("Gushing Grape Red", YRS, y, b);    more.show();    cout << "Total bottles for " << more.Label()        << ": " << more.sum() << endl;    cout << "Bye\n";    clock_t delay = 100 * CLOCKS_PER_SEC;    clock_t start = clock();    while (clock() - start < delay)        ;    return 0;}

2.采用私有继承完成1

//wine.h#ifndef WINE_H_#define WINE_H_#include <valarray>#include <string>typedef std::valarray<int> ArrayInt;typedef std::pair<ArrayInt, ArrayInt> PairArray;class Wine:private std::string, private PairArray{private:    int year;public:    Wine() {};    Wine(const char * l, int y, const int yr[], const int bot[]);    Wine(const char * l, int y);    void Getbottles();    const std::string & Label() { return (const std::string &) *this; } //强制类型转换    int sum();    void show() const;};#endif//wine.cpp#include "stdafx.h"#include "wine.h"#include <iostream>Wine::Wine(const char * l, int y, const int yr[], const int bot[]) :std::string(l), year(y){    PairArray::first = ArrayInt(year);  //类名和作用域解析运算符    PairArray::second = ArrayInt(year);    for (int i = 0; i < year; i++)    {        PairArray::first[i] = yr[i]; //first and second 是pair的属性值,不是方法,不能有()        PairArray::second[i] = bot[i];    }}Wine::Wine(const char * l, int y) :std::string(l), year(y){    PairArray::first = ArrayInt(year);    //需要将数组定义一下,初始化,不然没有分配内存    PairArray::second = ArrayInt(year);}void Wine::Getbottles(){    std::cout << "Enter Gully Wash data for " << year << " year(s):\n";    for (int i = 0; i < year; i++)    {        std::cout << "Enter year: ";        std::cin >> PairArray::first[i];        std::cout << "Enter bottles for that year: ";        std::cin >> PairArray::second[i];    }}int Wine::sum(){    return PairArray::second.sum();}void Wine::show() const{    std::cout << "Wine: " << (const std::string &) *this << "\n";    std::cout << "\tYear\tBottles\n";    for (int i = 0; i < year; i++)        std::cout << "\t" << PairArray::first[i] << "\t" << PairArray::second[i] << "\n";}//main.cpp#include "stdafx.h"#include "wine.h"#include <iostream>#include <ctime>int main(){    using std::cout;    using std::cin;    using std::endl;    cout << "Enter name of wine: ";    char lab[50];    cin.getline(lab, 50);    cout << "Enter number of years: ";    int yrs;    cin >> yrs;    Wine holding(lab, yrs);    holding.Getbottles();    holding.show();    const int YRS = 3;    int y[YRS] = { 1993, 1995, 1998 };    int b[YRS] = { 48, 60, 72 };    Wine more("Gushing Grape Red", YRS, y, b);    more.show();    cout << "Total bottles for " << more.Label()        << ": " << more.sum() << endl;    cout << "Bye\n";    clock_t delay = 100 * CLOCKS_PER_SEC;    clock_t start = clock();    while (clock() - start < delay)        ;    return 0;}

3.定义一个QueueTp模板,用一个指向Worker类的指针队列测试该模板

不太懂,木有做

4.Person类, 多重继承(MI)

//person.h#ifndef PERSON_H_#define PERSON_H_#include <string>class Person{private:    std::string firstname;    std::string lastname;protected:    virtual void Data() const;    virtual void Get();public:    Person(const char * fn = "null", const char * ln = "null");    virtual void Show() = 0;    virtual void Set() = 0;    virtual ~Person() = 0;};class Gunslinger :virtual public Person{private:    int number;    double tm; //把拔枪的时间作为成员,直接输入protected:    void Data();    void Get();public:    Gunslinger(int n = 0, const char * fn = "null", const char * ln = "null");    Gunslinger(int n, const Person & ps);    double Draw();    void Show();    void Set();};class PokerPlayer :virtual public Person{protected:    void Data();public:    PokerPlayer(const char * fn = "null", const char * ln = "null");    PokerPlayer(const Person & ps);    void Show();    void Set();    int Draw();};class BadDude : public Gunslinger, public PokerPlayer{protected:    void Data();public:    BadDude(const char * fn = "null", const char * ln = "null", int n = 0);    BadDude(const Person & ps, int n);    BadDude(const Gunslinger & gslg);    //BadDude(const PokerPlayer & pkpl, int n);    double Gdraw();    int Cdraw();    void Show();    void Set();};#endif//person.cpp#include "stdafx.h"#include "person.h"#include <iostream>#include <ctime>using std::cin;using std::cout;using std::endl;//Person methodsvoid Person::Data() const{    cout << "Name: " << firstname << ", " << lastname << endl;}void Person::Get(){    getline(cin, firstname);    cout << "Enter lastname: ";    getline(cin, lastname);}Person::Person(const char * fn, const char * ln) :firstname(fn), lastname(ln){}Person::~Person(){}//Gunslinger methodsvoid Gunslinger::Data(){    cout << "Nick numbers: " << number << endl;    cout << "time: " << tm << endl;}void Gunslinger::Get(){    cout << "Enter the number of nicks: ";    cin >> number;    cout << "Enter the time: ";    cin >> tm;    while (cin.get() != '\n')        continue;}Gunslinger::Gunslinger(int n, const char * fn, const char * ln) :Person(fn, ln), number(n){}Gunslinger::Gunslinger(int n, const Person & ps) : Person(ps), number(n){}double Gunslinger::Draw(){     return tm;}void Gunslinger::Show(){    cout << "Category : Gunslinger:\n";    Person::Data();    Data();}void Gunslinger::Set(){    cout << "Enter Gunslinger's firstname: ";    Person::Get();    Get();}//PokerPlayer methodsvoid PokerPlayer::Data(){    cout << "Poker's value: " << Draw() << endl;} PokerPlayer::PokerPlayer(const char * fn, const char * ln) :Person(fn, ln){}PokerPlayer::PokerPlayer(const Person & ps) : Person(ps){}void PokerPlayer::Show(){    cout << "Category: PokerPlayer:\n";    Person::Data();    Data();}int PokerPlayer::Draw(){    //产生1-52的随机数并返回    srand(time(0));    while (1)    {        int n = rand() % 53;        if (n != 0)        {            return n;            break;        }           else            continue;    }}void PokerPlayer::Set(){    cout << "Enter PokerPlayer's firstname: ";    Person::Get();}//BadDude methodsvoid BadDude::Data(){    cout << "time: " << Gdraw() << endl;    cout << "Poker's value: " << Cdraw() << endl;}BadDude::BadDude(const char * fn, const char * ln, int n) :Person(fn, ln), Gunslinger(n, fn, ln){}BadDude::BadDude(const Person & ps, int n) : Person(ps), Gunslinger(n, ps){}BadDude::BadDude(const Gunslinger & gslg) : Person(gslg), Gunslinger(gslg){}/*BadDude::BadDude(const PokerPlayer & pkpl, int n) : Person(pkpl), Gunslinger(pkpl, n)  //为什么woker那个就可以{}*/double BadDude::Gdraw() //为什么随机产生的值总是与Gunslinger的Draw()一样{    return Gunslinger::Draw();}int BadDude::Cdraw()  //为什么随机产生的值总是与Poker的Draw()一样{    return PokerPlayer::Draw();}void BadDude::Show(){    cout << "Category: BadDude:\n";    Person::Data();    Data();}void BadDude::Set(){    cout << "Enter BadDude's firstname: ";    Person::Get();    Gunslinger::Get();}//main.cpp#include "stdafx.h"#include "person.h"#include <iostream>#include <ctime>const int SIZE = 5;int main(){    using std::cin;    using std::cout;    using std::endl;    using std::strchr;    Person * lolas[SIZE];    int ct;    for (ct = 0; ct < SIZE; ct++)    {        char choice;        cout << "Enter the person category:\n"            << "g: gunslinger  p:pokerplayer  "            << "b: bad dude  q: quit\n";        cin >> choice;        while (strchr("gpbq", choice) == NULL)        {            cout << "Please enter a g, p, b, or q: ";            while (cin.get() != '\n')                continue;            cin >> choice;        }        if (choice == 'q')            break;        switch (choice)        {        case 'g': lolas[ct] = new Gunslinger;            break;        case 'p': lolas[ct] = new PokerPlayer;            break;        case 'b': lolas[ct] = new BadDude;            break;        }        cin.get();        lolas[ct]->Set();    }    cout << "\nHere is your stuff:\n";    int i;    for (i = 0; i < ct; i++)    {        cout << endl;        lolas[i]->Show();    }    //怎么用Draw()/GDraw()/CDraw(),现在又不知道每个元素是什么类型......把这些值加到了show()里显示出来    for (i = 0; i < ct; i++)        delete lolas[i];    cout << "Bye.\n";    std::cin.get();    clock_t delay = 100 * CLOCKS_PER_SEC;    clock_t start = clock();    while (clock() - start < delay)        ;    return 0;}

5.abstr_emp类,多重继承

//emp.h#ifndef EMP_H_#define EMP_H_#include <iostream>#include <string>class abstr_emp{private:    std::string fname;    std::string lname;    std::string job;public:    abstr_emp();    abstr_emp(const std::string & fn, const std::string ln, const std::string & j);    virtual void ShowAll() const;  //labels and show all data    virtual void SetAll();       //prompts user for values    friend std::ostream & operator<<(std::ostream & os, const abstr_emp & e); //just displays first and last name    virtual ~abstr_emp() = 0;};class employee :public abstr_emp{public:    employee();    employee(const std::string & fn, const std::string & ln, const std::string & j);    virtual void ShowAll() const;    virtual void SetAll();};class manager :public virtual abstr_emp{private:    int inchargeof;  //number of abstr_emps managedprotected:    int InChargeOf() const { return inchargeof; }   //output            //为highfink的ShowAll()和SetAll()不两次调用abstr_emps的函数做准备    int & InChargeOf() { return inchargeof; }    //inputpublic:    manager();    manager(const std::string & fn, const std::string & ln, const std::string & j, int ico = 0);    manager(const abstr_emp & e, int ico);    manager(const manager & m);    virtual void ShowAll() const;    virtual void SetAll();};class fink :virtual public abstr_emp{private:    std::string reportsto;   //to whom fink reportsprotected:    const std::string ReportsTo() const { return reportsto; }    std::string & ReportsTo() { return reportsto; }public:    fink();    fink(const std::string & fn, const std::string & ln, const std::string & j, const std::string & rpo);    fink(const abstr_emp & e, const std::string & rpo);    fink(const fink & e);    virtual void ShowAll() const;    virtual void SetAll();};class highfink :public manager, public fink     //managerment fink{public:    highfink();    highfink(const std::string & fn, const std::string & ln, const std::string & j, const std::string & rpo, int ico);    highfink(const manager & m, const std::string & rpo);    highfink(const fink & f, int ico);    highfink(const highfink & h);    virtual void ShowAll() const;    virtual void SetAll();};#endif//emp.cpp#include "stdafx.h"#include "emp.h"using std::cin;using std::cout;using std::endl;//abstr_emp methodsabstr_emp::abstr_emp(){}abstr_emp::abstr_emp(const std::string & fn, const std::string ln, const std::string & j) :fname(fn), lname(ln), job(j){}void abstr_emp::ShowAll() const   //labels and show all data{    cout << "first name: " << fname << endl;    cout << "last name: " << lname << endl;    cout << "job: " << job << endl;}void abstr_emp::SetAll()    //prompts user for values{    getline(cin, fname);    cout << "Enter the last name: ";    getline(cin, lname);    cout << "Enter the job: ";    getline(cin, job);}std::ostream & operator<<(std::ostream & os, const abstr_emp & e)  //just displays first and last name{    os << e.fname << ", " << e.lname;    return os;}abstr_emp::~abstr_emp(){}//employee methodsemployee::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 methodsmanager::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(m){    inchargeof = m.inchargeof;}void manager::ShowAll() const{    abstr_emp::ShowAll();    cout << "number of abstr_emps managed: " << inchargeof << endl;}void manager::SetAll(){    cout << "Enter the manager's first name: ";    abstr_emp::SetAll();    cout << "Enter the number of abstr_emps managed: ";    cin >> inchargeof;    while (cin.get() != '\n')        continue;}//fink methodsfink::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(e){    reportsto = e.reportsto;}*/fink::fink(const fink & e) : fink(e)   //复制构造函数到底哪一个对呢{}void fink::ShowAll() const{    abstr_emp::ShowAll();    cout << "to whom fink reports to: " << reportsto << endl;}void fink::SetAll(){    cout << "Enter the fink's first name: ";    abstr_emp::SetAll();    cout << "Enter the person fink reports to: ";    getline(cin, reportsto);}//highfink methodshighfink::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 manager & m, const std::string & rpo) : abstr_emp(m), fink(m, rpo){}highfink::highfink(const fink & f, int ico) : abstr_emp(f), manager(f, ico){}highfink::highfink(const highfink & h) : highfink(h){}void highfink::ShowAll() const{    abstr_emp::ShowAll();    cout << "number of abstr_emps managed: " << manager::InChargeOf() << endl;    cout << "to whom fink reports to: " << fink::ReportsTo() << endl;}void highfink::SetAll(){    cout << "Enter the highfink's first name: ";    abstr_emp::SetAll();    cout << "Enter the number of abstr_emps managed: ";    cin >> manager::InChargeOf();    while (cin.get() != '\n')        continue;    cout << "Enter the person highfink reports to: ";    getline(cin, fink::ReportsTo());}//main.cpp#include "stdafx.h"#include "emp.h"#include <ctime>using namespace std;int main(){    employee em("Trip", "Harris", "Thumper");    cout << em << endl;    em.ShowAll();    cout << endl;    manager ma("Amorphia", "Spindragon", "Nuancer", 5);    cout << ma << endl;    ma.ShowAll();    cout << endl;    fink fi("Matt", "Oggs", "Oiler", "Juno Barr");    cout << fi << endl;    fi.ShowAll();    cout << endl;    highfink hf(ma, "Curly Kew");    hf.ShowAll();    cout << endl;    cout << "Press a key for next phase:\n";    cin.get();    while (cin.get() != '\n')        continue;    highfink hf2;    hf2.SetAll();    cout << "Using an abstr_emp * pointer:\n";    abstr_emp * tri[4] = { &em, &fi, &hf, &hf2 };  //只能用指针    for (int i = 0; i < 4; i++)    {        tri[i]->ShowAll();        cout << endl;    }    /*abstr_emp tri[4] = { em, fi, hf, hf2 };  //abstr_emp为抽象类,不能具体化    for (int i = 0; i < 4; i++)    {    tri[i].ShowAll();    cout << endl;    }*/    clock_t delay = 100 * CLOCKS_PER_SEC;    clock_t start = clock();    while (clock() - start < delay)        ;    return 0;}
原创粉丝点击