CPP_Basic_Code_P10.1-PP10.10.8

版权声明:本文虽为 贴墙上的咖啡 原创,未经允许仍可转载. https://blog.csdn.net/enochhugh/article/details/71514268

##CPP_Basic_Code_P10.1-PP10.10.8

//  The Notes Created by Z-Tech on 2017/2/17.
//  All Codes Boot on 《C++ Primer Plus》V6.0
//  OS:MacOS 10.12.4
//  Translater:clang/llvm8.0.0 &g++4.2.1
//  Editer:iTerm 2&Sublime text 3
//  IDE: Xcode8.2.1&Clion2017.1

//P10.1-P10.3
Z_Head.h
#ifndef CLION_VERSION_Z_HEAD_H
#define CLION_VERSION_Z_HEAD_H

#include <string>

class Stock
{
private:
    std::string company;
    long shares;
    double share_val;
    double total_val;
    void set_tot() {total_val=shares*share_val;}

public:
    void acquire(const std::string& co,long n,double pr);
    void buy(long num,double price);
    void sell(long num,double price);
    void update(double price);
    void show();
};

#endif

Main.cpp
#include <iostream>
#include "Z_Head.h"

int main()
{
    Stock fluffy_the_cat;
    fluffy_the_cat.acquire("NanoSmart",20,12.50);
    fluffy_the_cat.show();
    fluffy_the_cat.buy(15,18.125);
    fluffy_the_cat.show();
    fluffy_the_cat.sell(400,20.00);
    fluffy_the_cat.show();
    fluffy_the_cat.buy(300000,40.125);
    fluffy_the_cat.show();
    fluffy_the_cat.sell(300000,0.125);
    fluffy_the_cat.show();
    fluffy_the_cat.update(99.99);
    fluffy_the_cat.show();
    return 0;
}

SubFunctions.cpp
#include <iostream>
#include "Z_Head.h"

void Stock::acquire(const std::string& co,long n,double pr)
{
    company=co;
    if (n<0)
    {
        std::cout<<"Number of shares can't be negative; "
                 <<company<<" shares set to 0.\n";
        shares=0;
    }
    else
        shares=n;
    share_val=pr;
    set_tot();
}

void Stock::buy(long num,double price)
{
    if (num<0)
    {
        std::cout<<"Number of shares can't be negative. "
                 <<"Transaction is aborted.\n";
    }
    else
    {
        shares+=num;
        share_val=price;
        set_tot();
    }

}

void Stock::sell(long num,double price)
{
    using std::cout;
    if (num<0)
    {
        cout<<"Number of shares can't be negative. "
                 <<"Transaction is aborted.\n";
    }
    else if (num>shares)
    {
        cout<<"Number of shares can't be negative. "
            <<"Transaction is aborted.\n";
    }
    else
    {
        shares-=num;
        share_val=price;
        set_tot();
    }
}

void Stock::update(double price)
{
    share_val=price;
    set_tot();
}

void Stock::show()
{
    std::cout<<"Company: "<<company
             <<" Shares: "<<shares<<'\n'
             <<" Shares Price: $"<<share_val
             <<" Total Worth: $"<<total_val<<'\n';
}

//P10.4-P10.6
Z_Head.h
#ifndef CLION_VERSION_Z_HEAD_H
#define CLION_VERSION_Z_HEAD_H

#include <string>

class Stock
{
private:
    std::string company;
    long shares;
    double share_val;
    double total_val;
    void set_tot() {total_val=shares*share_val;}

public:
    Stock();
    Stock(const std::string& co,long n=0,double pr=0.0);
    ~Stock();
    void buy(long num,double price);
    void sell(long num,double price);
    void update(double price);
    void show();
};

#endif

Main.cpp
#include <iostream>
#include "Z_Head.h"

int main()
{
    {
        using std::cout;
        cout<<"Using constructors to create new objects\n";
        Stock stock1("NanoSmart",12,20.0);
        stock1.show();
        Stock stock2=Stock("Boffo Objects",2,2.0);
        stock2.show();

        cout<<"Assigning stock1 to stock2:\n";
        stock2=stock1;
        cout<<"Listing stock1 and stock2:\n";
        stock1.show();
        stock2.show();

        cout<<"Using a constructors to reset an objects\n";
        stock1=Stock("Nifty Foods",10,50.0);
        cout<<"Revised stock1:\n";
        stock1.show();
        cout<<"Done.\n";
    }
    return 0;
}

SubFunctions.cpp
#include <iostream>
#include "Z_Head.h"

Stock::Stock()//构造函数
{
    std::cout<<"Default constructor called\n";
    company="no name";
    shares=0;
    share_val=0.0;
    total_val=0.0;
}

Stock::Stock(const std::string& co,long n,double pr)
{

    std::cout<<"Constructor using "<<co<<" called\n";
    company=co;
    if (n<0)
    {
        std::cout<<"Number of shares can't be negative; "
                 <<company<<" shares set to 0.\n";
        shares=0;
    }
    else
        shares=n;
    share_val=pr;
    set_tot();
}

Stock::~Stock()//析构函数
{
    std::cout<<"Bye, "<<company<<"!\n";
}

void Stock::buy(long num,double price)
{
    if (num<0)
    {
        std::cout<<"Number of shares can't be negative. "
                 <<"Transaction is aborted.\n";
    }
    else
    {
        shares+=num;
        share_val=price;
        set_tot();
    }

}

void Stock::sell(long num,double price)
{
    using std::cout;
    if (num<0)
    {
        cout<<"Number of shares can't be negative. "
                 <<"Transaction is aborted.\n";
    }
    else if (num>shares)
    {
        cout<<"Number of shares can't be negative. "
            <<"Transaction is aborted.\n";
    }
    else
    {
        shares-=num;
        share_val=price;
        set_tot();
    }
}

void Stock::update(double price)
{
    share_val=price;
    set_tot();
}

void Stock::show()
{
    using std::cout;
    using std::ios_base;
    //强制使用小数点
    ios_base::fmtflags orig=cout.setf(ios_base::fixed,ios_base::floatfield);
    std::streamsize prec=cout.precision(3);

    cout<<"Company: "<<company
             <<" Shares: "<<shares<<'\n'
             <<" Shares Price: $"<<share_val;
    cout.precision(2);
    cout<<" Total Worth: $"<<total_val<<'\n';

    cout.setf(orig,ios_base::floatfield);
    cout.precision(prec);
}

//P10.7-P10.9
Z_Head.h
#ifndef CLION_VERSION_Z_HEAD_H
#define CLION_VERSION_Z_HEAD_H

#include <string>

class Stock
{
private:
    std::string company;
    long shares;
    double share_val;
    double total_val;
    void set_tot() {total_val=shares*share_val;}

public:
    Stock();
    Stock(const std::string& co,long n=0,double pr=0.0);
    ~Stock();
    void buy(long num,double price);
    void sell(long num,double price);
    void update(double price);
    void show() const;
    const Stock& topval(const Stock& s) const;
};

#endif

Main.cpp
#include <iostream>
#include "Z_Head.h"

const int STKS=4;
int main()
{
    Stock stocks[STKS]
    {
        Stock("NanoSmart",12,20.0),
        Stock("Boffo Objects",200,2.0),
        Stock("Monolithic Oblisks",130,3.25),
        Stock("Fleep Enterprises",60,6.5)
    };
    std::cout<<"Stock holding:\n";
    int st;
    for (st=0;st<STKS;st++)
        stocks[st].show();

    const Stock* top=&stocks[0];
    for (st=1;st<STKS;st++)
        top=&top->topval(stocks[st]);//难点,对引用对象再次取地址就是指针!!!
    std::cout<<"\nMost valuable holding:\n";
    top->show();
    return 0;
}

SubFunctions.cpp
#include <iostream>
#include "Z_Head.h"

Stock::Stock()//构造函数1
{
    std::cout<<"Default constructor called\n";
    company="no name";
    shares=0;
    share_val=0.0;
    total_val=0.0;
}

Stock::Stock(const std::string& co,long n,double pr)//构造函数2
{

    std::cout<<"Constructor using "<<co<<" called\n";
    company=co;
    if (n<0)
    {
        std::cout<<"Number of shares can't be negative; "
                 <<company<<" shares set to 0.\n";
        shares=0;
    }
    else
        shares=n;
    share_val=pr;
    set_tot();
}

Stock::~Stock()//析构函数
{
}

void Stock::buy(long num,double price)
{
    if (num<0)
    {
        std::cout<<"Number of shares can't be negative. "
                 <<"Transaction is aborted.\n";
    }
    else
    {
        shares+=num;
        share_val=price;
        set_tot();
    }

}

void Stock::sell(long num,double price)
{
    using std::cout;
    if (num<0)
    {
        cout<<"Number of shares can't be negative. "
                 <<"Transaction is aborted.\n";
    }
    else if (num>shares)
    {
        cout<<"Number of shares can't be negative. "
            <<"Transaction is aborted.\n";
    }
    else
    {
        shares-=num;
        share_val=price;
        set_tot();
    }
}

void Stock::update(double price)
{
    share_val=price;
    set_tot();
}

void Stock::show() const
{
    using std::cout;
    using std::ios_base;
    //强制使用小数点
    ios_base::fmtflags orig=cout.setf(ios_base::fixed,ios_base::floatfield);
    std::streamsize prec=cout.precision(3);

    cout<<"Company: "<<company
             <<" Shares: "<<shares<<'\n'
             <<" Shares Price: $"<<share_val;
    cout.precision(2);
    cout<<" Total Worth: $"<<total_val<<'\n';

    cout.setf(orig,ios_base::floatfield);
    cout.precision(prec);
}

const Stock& Stock::topval(const Stock& s) const
{
    if (s.total_val>total_val)
        return s;
    else
        return *this;//this指针,指向本对象
}

//P10.10-P10.12
Z_Head.h
#ifndef CLION_VERSION_Z_HEAD_H
#define CLION_VERSION_Z_HEAD_H

typedef unsigned long Item;

class Stack
{
private:
    enum {MAX=10};
    Item items[MAX];
    int top;
public:
    Stack();
    bool isempty() const;
    bool isfull() const;
    bool push(const Item& item);
    bool pop(Item& item);
};

#endif

SubFunctions.cpp
#include "Z_Head.h"

Stack::Stack()
{
    top=0;
}

bool Stack::isempty() const
{
    return top==0;
}

bool Stack::isfull() const
{
    return top==MAX;
}

bool Stack::push(const Item& item)
{
    if (top<MAX)
    {
        items[top++]=item;
        return true;
    }
    else
        return false;
}

bool Stack::pop(Item& item)
{
    if (top>0)
    {
        item=items[--top];
        return true;
    }
    else
        return false;
}

Main.cpp
#include <iostream>
#include "Z_Head.h"
//#include <cctype>

int main()
{
    using namespace std;
    Stack st;
    char ch;
    unsigned long po;
    cout<<"Please enter A to add a purchase order,\n"
        <<"p to process a po,or Q to quit.\n";
    while (cin>>ch&&toupper(ch)!='Q')
    {
        while (cin.get()!='\n')
            continue;
        if (!isalpha(ch))
        {
            cout<<'\a';
            continue;
        }
        switch (ch)
        {
            case 'A':
            case 'a':cout<<"Enter a PO number to add: ";
                    cin>>po;
                    if (st.isfull())
                        cout<<"stack already full\n";
                    else
                        st.push(po);
                    break;
            case 'P':
            case 'p':if (st.isempty())
                    cout<<"stack already empty\n";
                    else
                    {
                        st.pop(po);
                        cout<<"PO #"<<po<<" popped\n";
                    }
                    break;
        }
        cout<<"Please enter A to add purchas order,\n"
            <<"P to process a PO,or Q to quit.\n";
    }
    cout<<"Bye.\n";
    return 0;
}

//PP10.10.1
#include <iostream>
class Bank_Account
{
private:
    char user_name[35];
    char user_account[25];
    double user_money;
public:
    Bank_Account(const char* str1="Nameless",const char* str2="Nameless",double my=0.0);
    ~Bank_Account();
    void Show_Account(void) const;
    void deposit(double my);
    void withdraw(double my);
};

int main()
{
    char nameT[35]="Yang XuZhou";
    char Account[20]="EnochHugh_Extra";
    double money=999.99;

    Bank_Account userT[2];
    userT[0].Show_Account();
    userT[0]={nameT,Account,money};
    userT[0].Show_Account();
    userT[0].deposit(12.99);
    userT[0].Show_Account();
    userT[0].withdraw(99.22);
    userT[0].Show_Account();

    return 0;
}

Bank_Account::Bank_Account(const char* str1,const char* str2,double my)
{
    strcpy(user_name,str1);
    strcpy(user_account,str2);
    user_money=my;
}

Bank_Account::~Bank_Account()
{

}

void Bank_Account::Show_Account(void) const
{
    std::cout<<"Name: "<<user_name<<"\n";
    std::cout<<"Account: "<<user_account<<"\n";
    std::cout<<"Money: "<<user_money<<"\n";
}

void Bank_Account::deposit(double my)
{
    if (my>0)
        user_money+=my;
    else
        std::cout<<"wrong nmumber!";
}

void Bank_Account::withdraw(double my)
{
    if (user_money>=my)
        user_money-=my;
    else
    {
        std::cout<<"You just could withdraw $"<<user_money<<" now.\n";
        std::cout<<"You Can't withdraw over money you have.\n";
    }
}

//PP10.10.2
#include <iostream>
class Person
{
private:
    static const int LIMIT=25;
    std::string lname;
    char fname[LIMIT];
public:
    Person(){lname="";fname[0]='\0';}
    Person(const std::string& ln,const char* fn="Heyyou");
    void Show() const;
    void FormalShow() const;
};

int main()
{
    Person one;
    Person two("Smythercraft");
    Person three("Dimwiddy","Sam");
    one.Show();
    std::cout<<std::endl;

    two.Show();
    std::cout<<std::endl;
    two.FormalShow();
    std::cout<<std::endl;

    three.Show();
    std::cout<<std::endl;
    three.FormalShow();
    return 0;
}

Person::Person(const std::string& ln,const char* fn)
{
    lname=ln;
    strcpy(fname,fn);
}

void Person::Show() const
{
    std::cout<<"lName: "<<lname<<"\n";
    std::cout<<"fName: "<<fname<<"\n";
}

void Person::FormalShow() const
{
    std::cout<<"fName: "<<fname<<"\n";
    std::cout<<"lName: "<<lname<<"\n";
}

//PP10.10.3
#include <iostream>
class Golf
{
private:
    static const int Len=40;
    char fullname[Len];
    int hangdicap;

public:
    Golf(const char* name,int hc);
    Golf();
    ~Golf();
    const Golf& setgolf(const Golf& g);
    void hangdicapf(int hc);
    void showgolf() const;
};

static const int Num=3;

int main()
{
    using namespace std;
    Golf ZhangHu[Num]
            {
                    Golf(),
                    Golf("Something is perfect!",66),
                    Golf("Anything is fucking!",99)
            };
    for (int j=0;j<Num;j++)
    {
        cout<<"NO. "<<j+1<<": "<<endl;
        ZhangHu[j].showgolf();
    }

    Golf* T1=new Golf{"What?",12};
    cout<<"T1: ";
    T1->showgolf();

    Golf T2{"是不是傻",12};
    cout<<"T2: ";
    T2.showgolf();

    delete T1;
    return 0;
}

Golf::Golf(const char* name,int hc)
{
    strcpy(fullname,name);
    hangdicap=hc;
}

Golf::Golf()//警告!!默认构造函数和函数原型初始化不能同时使用!!
{
    strcpy(fullname,"Nameless");
    hangdicap=0;
}

const Golf& Golf::setgolf(const Golf& g)//关键核心代码
{
    strcpy(fullname,g.fullname);
    hangdicap=g.hangdicap;
    return *this;
}

Golf::~Golf()
{

}

void Golf::hangdicapf(int hc)
{
    hangdicap=hc;
}

void Golf::showgolf() const
{
    std::cout<<"name: "<<fullname<<std::endl;
    std::cout<<"handicap: "<<hangdicap<<std::endl;
}

//PP10.10.4
namespace SALES
{
    const int QUARTERS=4;
    class Sales
    {
    private:
        double sales[QUARTERS];
        double average;
        double max;
        double min;
    public:
        Sales(const double ar[],int n);
        Sales();
        ~Sales();
        void showSales() const;
        double findMax(double ar[],int ARSZ);
        double findMin(double ar[],int ARSZ);
    };
}

int main()
{
    const double price[SALES::QUARTERS] {12.34,34.54,66.99,99.123};
    int ZHS=6;
    SALES::Sales* xv=new SALES::Sales[2]
    {
        SALES::Sales(price,ZHS),
        SALES::Sales()
    };
    xv->showSales();
    std::cout<<"\n";
    (xv+1)->showSales();

    delete []xv;
    return 0;
}

namespace SALES
{
    Sales::Sales(const double ar[],int n)
    {
        int realv=(QUARTERS>n?n:QUARTERS);
        double total=0;
        for (int i=0;i<realv;i++)
        {
            sales[i]=ar[i];
            total+=ar[i];
        }
        average=total/realv;
        max=findMax(sales,realv);
        min=findMin(sales,realv);
    }

    Sales::Sales()
    {
        using std::cin;
        using std::cout;
        using std::endl;
        cout<<"Please enter the number: "<<endl;
        double total=0;
        for (int i=0;i<QUARTERS;i++)
        {
            cin>>sales[i];
            if (!cin)
            {
                cin.clear();
                while (cin.get()!='\n')
                    continue;
            }
            total+=sales[i];
            average=total/QUARTERS;
            max=findMax(sales,QUARTERS);
            min=findMin(sales,QUARTERS);
        }
    }

    void Sales::showSales() const
    {
        using std::cout;
        using std::endl;
        cout<<"*xv sales:";
        for (int i=0;i<QUARTERS;i++)
            cout<<sales[i]<<" ";
        cout<<endl;
        cout<<"*xv average:"<<average<<endl;
        cout<<"*xv max:"<<max<<endl;
        cout<<"*xv min:"<<min<<endl;
    }

    double Sales::findMax(double ar[],int ARSZ)
    {
        double max=ar[0];
        for (int i=1;i<ARSZ;i++)
        {
            if (ar[i]>max)
                max=ar[i];
        }
        return max;
    }

    double Sales::findMin(double ar[],int ARSZ)
    {
        double min=ar[0];
        for (int i=1;i<ARSZ;i++)
        {
            if (ar[i]<min)
                min=ar[i];
        }
        return min;
    }

    Sales::~Sales()
    {

    }
}

//PP10.10.5
#include <iostream>
struct customer
{
    char fullname[35];
    double payment;
};

typedef customer Item;

class Stack
{
private:
    enum {MAX=10};
    Item items[MAX];
    int top;
public:
    Stack();
    bool isempty() const;
    bool isfull() const;
    bool push(const Item& item);
    bool pop(Item& item);
};

void get_customer(Item& cm);

int main()
{
    using namespace std;
    Stack st;
    char ch;

    customer po;
    double paymentx=0;
    cout<<"Please enter A to add a purchase order,\n"
        <<"p to process a po,or Q to quit.\n";
    while (cin>>ch&&toupper(ch)!='Q')
    {
        while (cin.get()!='\n')
            continue;
        if (!isalpha(ch))
        {
            cout<<'\a'<<"Try again!\n";
            continue;
        }
        switch (ch)
        {
            case 'A':
            case 'a':
                if (st.isfull())
                    cout<<"stack already full\n";//检测类内部数组是否溢出
                else
                {
                    get_customer(po);//读取输入存入po
                    st.push(po);//将po直接赋值推入类对象st的结构数组中
                }
                break;
            case 'P':
            case 'p':if (st.isempty())//检测类内部数组是否已空
                    cout<<"stack already empty\n";
                else
                {
                    st.pop(po);//将类对象st结构数组中上一个结构重新赋值还给po
                    paymentx+=po.payment;//计算累加
                    cout<<"Fullname: "<<po.fullname<<endl;
                    cout<<"Total Payment: "<<paymentx<<endl;
                }
                break;
            default:
                break;
        }
        cout<<"Please enter A to add purchas order,\n"
            <<"P to process a PO,or Q to quit.\n";
    }
    cout<<"Bye.\n";
    return 0;
}

Stack::Stack()
{
    top=0;
}

bool Stack::isempty() const
{
    return top==0;
}

bool Stack::isfull() const
{
    return top==MAX;
}

bool Stack::push(const Item& item)
{
    if (top<MAX)
    {
        items[top++]=item;
        return true;
    }
    else
        return false;
}

bool Stack::pop(Item& item)
{
    if (top>0)
    {
        item=items[--top];
        return true;
    }
    else
        return false;
}

void get_customer(Item& cm)
{
    using std::cout;
    using std::cin;
    using std::endl;
    cout<<"Please enter name: "<<endl;
    cin.get(cm.fullname,35);
    cout<<"Please enter Pay: "<<endl;
    cin>>cm.payment;
    cin.get();
}

//PP10.10.6
class Move
{
private:
    double x;
    double y;
public:
    Move(double a=0,double b=0);
    void showmove() const;
    Move add(const Move& m) const;
    void reset(double a=0,double b=0);
};

Move::Move(double a,double b)
{
    x=a;
    y=b;
}

void Move::showmove() const
{
    std::cout<<"X= "<<x<<",Y= "<<y<<std::endl;
}

Move Move::add(const Move& m) const
{
    Move temp;
    temp.x=x+m.x;
    temp.y=y+m.y;
    return temp;
}

void Move::reset(double a,double b)
{
    x=a;
    y=b;
}

int main()
{
    using std::cout;
    using std::endl;
    Move move1(1,2);
    Move move2(4,8);
    Move move3;//不手动初始化不要加()

    cout<<"Move1: ";
    move1.showmove();

    cout<<"Move2: ";
    move2.showmove();

    cout<<"Move3: ";
    move3.showmove();

    cout<<"Move1+Move2: ";
    move1=move1.add(move2);//返回对象赋值,隐式1+显式2
    move1.showmove();

    cout<<"Move1 REST: ";
    move1.reset(2,2);
    move1.showmove();

    return 0;
}

//PP10.10.7
#include <iostream>
class Plorg
{
private:
    char Fullname[20];
    int CI;
public:
    Plorg(const char* fn,int c);
    Plorg();
    ~Plorg();
    void change_CI(int c);
    void show_Plorg() const;
};

Plorg::Plorg(const char* fn,int c)
{
    strcpy(Fullname,fn);
    CI=c;
}

Plorg::Plorg()
{
    strcpy(Fullname,"Plorga");
    CI=50;
}

Plorg::~Plorg()
{

}

void Plorg::change_CI(int c)
{
    CI=c;
}

void Plorg::show_Plorg() const
{
    std::cout<<"Fullname: "<<Fullname<<std::endl;
    std::cout<<"CI: "<<CI<<std::endl;
}

const char* temp="Yang Xuzhou";
int main()
{
    Plorg P1;
    Plorg P2{temp,99};
    P1.show_Plorg();
    P2.show_Plorg();

    P1.change_CI(25);
    P2.change_CI(122);
    P1.show_Plorg();
    P2.show_Plorg();

    return 0;
}

//PP10.10.8
Z_Head.h
#ifndef CLION_VERSION_Z_HEAD_H
#define CLION_VERSION_Z_HEAD_H

const int SIZ=49;
const int LIMAX=3;

struct film
{
    char title[SIZ];
    int rating;
};

typedef film Item;

class List
{
private:
    Item items[LIMAX];
    int count;
public:
    List();
    bool isempty();
    bool isfull();
    //int itemcount();
    bool additem(Item& item);
    void visit(void (*pf)(Item&));
};

#endif

SubFunctions.cpp
//#include <iostream>
#include "Z_Head.h"

List::List()
{
    count=0;
}

bool List::isempty()
{
    return count==0;
}

bool List::isfull()
{
    return count==LIMAX;
}

//int List::itemcount()
//{
//    return count;
//}

bool List::additem(Item& item)
{
    if (count==LIMAX)
        return false;
    else
    {
        items[count++]=item;//注意是将显式赋值给隐式,且序号增加
        return true;
    }
}

void List::visit(void (*pf)(Item&))//通过visit函数,(*pf)才能访问私有数据
{
    for (int i=0;i<count;i++)
        (*pf)(items[i]);//此处十分关键
}

Main.cpp
#include <iostream>
#include "Z_Head.h"

void showfilm(Item& item);

int main()
{
    using namespace std;
    List movies;
    Item temp;

    if (movies.isfull())//确认初始化时分配空间是否足够
    {
        cout<<"No more room in list.\n";
        exit(1);
    }
    cout<<"Enter first movie title: \n";
    while (cin.getline(temp.title,SIZ)&&temp.title[0]!='\0')//输入名称并确保不为空
    {
        cout<<"Enter your rating:(1-10) "<<endl;
        cin>>temp.rating;
        cin.get();
        if (!movies.additem(temp))//添加成功则不会执行
        {
            cout<<"List is full! \n";
            break;
        }
        if (movies.isfull())//添加后确认是否已满
        {
            cout<<"You have filled the list.\n";
            break;
        }
        cout<<"Enter next movie title:(empty line to quit.) ";
    }
    if (movies.isempty())//读取完成后确认是否存入了数据
        cout<<"No data entered.\n";
    else
    {
        cout<<"Here is the movie list: \n";
        movies.visit(showfilm);//showfilm函数作为函数名(函数指针)参数被对象调用
    }
    cout<<"Bye!\n";

    return 0;
}

void showfilm(Item& item)
{
    std::cout<<"Movie title: "<<item.title<<std::endl;
    std::cout<<"Movie Rating: "<<item.rating<<std::endl;
}

猜你喜欢

转载自blog.csdn.net/enochhugh/article/details/71514268
pp