十三章

//

//  main.cpp

//  C++PrimerPlusThirteen

//

//  Created by amengdev on 16/3/30.

//  Copyright © 2016 amengdev. All rights reserved.

//

 

#include <iostream>

#include "string.h"

using namespace std;

 

//基类

class Base

{

private:

    char *bname;

    int blen;

public:

    //构造函数和析构函数

    Base();

    Base(const char *name);

    Base(const Base &b);

    ~Base();

    //普通函数

    //运算符重载函数

    Base & operator=(const Base & b);

    friend ostream& operator<<(ostream &os,const Base &b);

};

 

//构造函数和析构函数

Base::Base()

{

    cout<<"基类构造函数1"<<endl;

    blen=0;

    bname=new char[1];

    bname[0]='\0';

}

Base::Base(const char *name)

{

    cout<<"基类构造函数2"<<endl;

    blen=strlen(name);

    bname=new char[blen+1];

    strcpy(bname, name);

}

Base::Base(const Base &b)

{

    cout<<"基类构造函数3"<<endl;

    blen=b.blen;

    bname=new char[blen+1];

    strcpy(bname, b.bname);

}

Base::~Base()

{

    cout<<"基类析构函数"<<endl;

    delete [] bname;

}

//普通函数

//运算符重载函数

Base &  Base::operator=(const Base & b)

{

    cout<<"赋值构造函数"<<endl;

    if(this==&b)

        return *this;

    delete [] bname;

    blen=strlen(b.bname);

    bname=new char[blen+1];

    strcpy(bname, b.bname);

    return *this;

}

ostream& operator<<(ostream &os,const Base &b)

{

    os<<b.bname<<endl;

    return os;

}

//子类

class BasePlus : public Base

{

private:

    char *pname;

    int plen;

public:

    //构造函数和析构函数

    BasePlus();

    BasePlus(const char *name1,const char *name2);

    BasePlus(const BasePlus & b);

    ~BasePlus();

    //普通函数

    //运算符重载函数

    BasePlus & operator=(const BasePlus & p);

    friend ostream& operator<<(ostream &os,const BasePlus &b);

};

 

//构造函数和析构函数

BasePlus::BasePlus() : Base()

{

    cout<<"子类构造函数1"<<endl;

    plen=0;

    pname=new char[1];

    pname[0]='\0';

}

BasePlus::BasePlus(const char *name1,const char *name2) : Base(name1)

{

    cout<<"子类构造函数2"<<endl;

    plen=strlen(name2);

    pname=new char[plen+1];

    strcpy(pname, name2);

}

BasePlus::BasePlus(const BasePlus & b) : Base(b)

{

    cout<<"子类构造函数2"<<endl;

    plen=strlen(b.pname);

    pname=new char[plen+1];

    strcpy(pname, b.pname);

}

BasePlus::~BasePlus()

{

    cout<<"子类析构函数"<<endl;

    delete [] pname;

}

//普通函数

//运算符重载函数

 

BasePlus & BasePlus::operator=(const BasePlus & p)

{

    if(this==&p)

        return *this;

    Base::operator=(p); //调用基类=运算符重载函数进行内存处理

    delete [] pname;

    plen=strlen(p.pname);

    pname=new char[plen+1];

    strcpy(pname, p.pname);

    return *this;

}

ostream& operator<<(ostream &os,const BasePlus &b)

{

    os<<(const Base & )b;

    os<<b.pname<<endl;

    return os;

}

 

int main()

{

//    Base b1;

//    cout<<b1<<endl;

//    Base b2("bname2");

//    cout<<b2<<endl;

//    Base b3(b2);

//    cout<<b3<<endl;

//    Base b4;

//    b4=b3;

//    cout<<b4<<endl;

//    cout<<"============="<<endl;

    BasePlus p1;

    cout<<p1<<endl;

    BasePlus p2("bname2","pname2");

    cout<<p2<<endl;

    BasePlus p3(p2);

    cout<<p3<<endl;

    BasePlus p4;

    p4=p3;

    cout<<p4<<endl;

    return 0;

}

 

 

/*

 //类继承,抽象基类(ABC)的基本使用

 //抽象基类的构造方式:找到所有子类的公共成员和方法

 //虚函数

//抽象基类

class Base

{

private:

    string name;

    int account;

    int balance;

public:

    //构造函数和析构函数

    Base();

    Base(const string &n,int a,int b);

    Base(const Base &b);

    //普通函数

    virtual bool Deposit(int m)=0;    //存款

    virtual bool Withdraw(int m)=0;   //取款

    int Balance();          //求结余

    virtual void Show()=0;            //显示信息

    virtual void Show2()=0;           //显示信息(测试使用)

    //运算符重载函数

    

};

//构造函数和析构函数

Base::Base() : name(),account(0),balance(0)

{

};

Base::Base(const string &n,int a,int b) : name(n),account(a),balance(b)

{

};

Base::Base(const Base &b) : name(b.name),account(b.account),balance(b.balance)

{

};

//普通函数

bool Base::Deposit(int m)    //存款

{

    if(m<0)

        return false;

    balance+=m;

    return true;

}

bool Base::Withdraw(int m)   //取款

{

    if(m<0)

        return false;

    balance-=m;

    return true;

}

int Base::Balance()          //求结余

{

    return balance;

}

void Base::Show()            //显示信息

{

    cout<<"name:"<<name<<endl;

    cout<<"account:"<<account<<endl;

    cout<<"balance:"<<balance<<endl;

}

void Base::Show2()

{

    cout<<"balance:"<<balance<<endl;

}

//运算符重载函数

 

//继承子类Brass

class Brass : public Base

{

private:

public:

    //构造函数和析构函数

    Brass();

    Brass(const string &n,int a,int b);

    Brass(const Brass &b);

    //普通函数

    virtual bool Deposit(int m);    //存款

    virtual bool Withdraw(int m);   //取款

    virtual void Show();            //显示信息

    virtual void Show2();           //显示信息(测试使用)

    //运算符重载函数

};

//构造函数和析构函数

Brass::Brass() : Base()

{

    

}

Brass::Brass(const string &n,int a,int b) : Base(n,a,b)

{

    

}

Brass::Brass(const Brass &b) : Base(b)

{

    

}

//普通函数

bool Brass::Deposit(int m)    //存款

{

    if(Base::Deposit(m))

        return true;

    return false;

}

bool Brass::Withdraw(int m)   //取款

{

    if(m>Balance())

        return false;

    if(Base::Withdraw(m))

    {

        return true;

    }

    return false;

}

void Brass::Show()            //显示信息

{

    Base::Show();

}

void Brass::Show2()

{

    Base::Show2();

}

//运算符重载函数

 

//子类BrassPlus

class BrassPlus : public Base

{

private:

    int maxLoad;

    int rating;

    int touzhi;

public:

    //构造函数和析构函数

    BrassPlus();

    BrassPlus(const string &n,int a,int b,int m,int r,int t);

    BrassPlus(const BrassPlus &b);

    BrassPlus(const Brass &b,int m,int r,int t);

    //普通函数

    virtual bool Deposit(int m);    //存款

    virtual bool Withdraw(int m);   //取款

    virtual void Show();            //显示信息

    virtual void Show2();           //显示信息(测试使用)

    //运算符重载函数

};

 

//构造函数和析构函数

BrassPlus::BrassPlus() : Base()

{

    

}

BrassPlus::BrassPlus(const string &n,int a,int b,int m,int r,int t) : Base(n,a,b),maxLoad(m),rating(r),touzhi(t)

{

    

}

BrassPlus::BrassPlus(const BrassPlus &b) : Base(b),maxLoad(b.maxLoad),rating(b.rating),touzhi(b.touzhi)

{

    

}

BrassPlus::BrassPlus(const Brass &b,int m,int r,int t) : Base(b),maxLoad(m),rating(r),touzhi(t)

{

    

}

//普通函数

bool BrassPlus::Deposit(int m)    //存款

{

    if(m<0)

        return false;

    if(touzhi==0)

    {

        Base::Deposit(m);

    }

    else

    {

        if(m<=touzhi)

        {

            touzhi-=m;

        }

        else

        {

            Base::Deposit(m-touzhi);

            touzhi=0;

        }

    }

    return true;

}

bool BrassPlus::Withdraw(int m)   //取款

{

    if(m<0)

        return false;

    if(m<=Base::Balance())

    {

        Base::Withdraw(m);

    }

    else

    {

        if(m>(Base::Balance()-touzhi+maxLoad))

        {

            return false;

        }

        else

        {

            touzhi=touzhi+m-Base::Balance();

            Base::Withdraw(Base::Balance());

        }

    }

    return true;

}

void BrassPlus::Show()            //显示信息

{

    Base::Show();

    cout<<"maxLoad:"<<maxLoad<<endl;

    cout<<"rating:"<<rating<<endl;

    cout<<"touzhi:"<<touzhi<<endl;

}

void BrassPlus::Show2()

{

    Base::Show2();

    cout<<"touzhi:"<<touzhi<<endl;

}

//运算符重载函数

int main()

{

    Base *b[10];

    b[0]=new Brass("name0",0,10);

    b[0]->Show2();

    b[0]->Deposit(10);

    b[0]->Show2();

    b[0]->Withdraw(5);

    b[0]->Show2();

    cout<<endl;

    b[1]=new BrassPlus("name1",1,20,20,5,0);

    b[1]->Show2();

    b[1]->Deposit(5);

    b[1]->Show2();

    b[1]->Withdraw(10);

    b[1]->Show2();

    b[1]->Withdraw(20);

    b[1]->Show2();

    

    return 0;

}

*/

 

/*

 //类继承的基本使用

 //虚函数使用

 //此类对类函数设计有代表性

//基类

class Brass{

private:

    string name;

    int account;

    int balance;

public:

    //构造函数和析构函数

    Brass();

    Brass(const string &n,int a,int b);

    Brass(const Brass &b);

    //普通函数

    virtual void Set(const string &n,int a,int b);

    virtual void Set(const Brass &b);

    virtual void Show();    //显示账户信息

    virtual void Show2();   //显示账户信息,测试使用

    virtual bool Deposit(int m);     //存款

    virtual bool Withdraw(int m);    //取款

    int Balance();      //求余额

    //运算符重载函数

    

};

//构造函数和析构函数

Brass::Brass():name(),account(0),balance(0)

{

}

Brass::Brass(const string &n,int a,int b):name(n),account(a),balance(b)

{

}

Brass::Brass(const Brass &b):name(b.name),account(b.account),balance(b.balance)

{

}

//普通函数

void Brass::Set(const string &n,int a,int b)

{

    name=n;

    account=a;

    balance=b;

}

 

void Brass::Set(const Brass &b)

{

    name=b.name;

    account=b.account;

    balance=b.balance;

}

void Brass::Show()    //显示账户信息

{

    cout<<"name:"<<name<<endl;

    cout<<"account:"<<account<<endl;

    cout<<"balance:"<<balance<<endl;

}

void Brass::Show2()   //显示账户信息,测试使用

{

    cout<<"balance:"<<balance<<endl;

}

bool Brass::Deposit(int m)//存款

{

    if(m<0)

        return false;

    balance+=m;

    return true;

}

bool Brass::Withdraw(int m) //取款

{

    if(m<0)

        return false;

    if(m>balance)

        return false;

    balance=balance-m;

    return true;

}

int Brass::Balance()      //求余额

{

    return balance;

}

//子类

class BrassPlus : public Brass

{

private:

    int maxLoan;

    int rate;

    int touzhi;

public:

    //构造函数和析构函数

    BrassPlus();

    BrassPlus(const string &n,int a,int b,int m,int r,int t);

    BrassPlus(const Brass &b,int m,int r,int t);

    //普通函数

    virtual void Set(const string &n,int a,int b,int m,int r,int t);    //重置

    virtual void Set(const Brass &b,int m,int r,int t); //重置

    virtual void Show();    //显示账户信息

    virtual void Show2();   //显示账户信息,测试使用

    virtual bool Deposit(int m);     //存款

    virtual bool Withdraw(int m);    //取款

    //运算符重载函数

};

//子类

 

//构造函数和析构函数

BrassPlus::BrassPlus() : Brass()

{

}

BrassPlus::BrassPlus(const string &n,int a,int b,int m,int r,int t) : Brass(n,a,b),maxLoan(m),rate(r),touzhi(t)

{

}

BrassPlus::BrassPlus(const Brass &b,int m,int r,int t) : Brass(b),maxLoan(m),rate(r),touzhi(t)

{

}

//普通函数

void BrassPlus::Set(const string &n,int a,int b,int m,int r,int t)    //重置

{

    Brass::Set(n, a, b);

    maxLoan=m;

    rate=r;

    touzhi=t;

}

void BrassPlus::Set(const Brass &b,int m,int r,int t) //重置

{

    Brass::Set(b);

    maxLoan=m;

    rate=r;

    touzhi=t;

}

void BrassPlus::Show()    //显示账户信息

{

    Brass::Show();

    cout<<"maxLoan:"<<maxLoan<<endl;

    cout<<"rate:"<<rate<<endl;

    cout<<"touzhi"<<touzhi<<endl;

}

 

void BrassPlus::Show2()   //显示账户信息,测试使用

{

    Brass::Show2();

    cout<<"touzhi:"<<touzhi<<endl;

    cout<<endl;

}

bool BrassPlus::Deposit(int m)     //存款

{

    if(m<0)

        return false;

    if(touzhi==0)

    {

        Brass::Deposit(m);

    }

    else

    {

        if(m<=touzhi)

        {

            touzhi-=m;

        }

        else

        {

            Brass::Deposit(m-touzhi);

            touzhi=0;

        }

    }

    return true;

}

bool BrassPlus::Withdraw(int m)    //取款

{

    if(m<0)

        return false;

    if(m<=Brass::Balance())

    {

        Brass::Withdraw(m);

    }

    else

    {

        if(m>(Brass::Balance()-touzhi+maxLoan))

        {

            return false;

        }

        else

        {

            touzhi=touzhi+m-Brass::Balance();

            Brass::Withdraw(Brass::Balance());

        }

    }

    return true;

}

//运算符重载函数

 

int main()

{

    //基类测试

//    Brass b1;

//    b1.Set("name1", 1, 10);

//    b1.Show();

//    b1.Deposit(10);

//    b1.Show();

//    b1.Withdraw(5);

//    b1.Show();

    //子类测试

//    //姓名-账号-余额-最大透支额度-比率-当前透支额度

//    BrassPlus bp1("name1",1,10,10,1,0);

//    bp1.Show2();

//    bp1.Deposit(10);

//    bp1.Show2();

//    bp1.Withdraw(5);

//    bp1.Show2();

//    bp1.Withdraw(20);

//    bp1.Show2();

//    bp1.Withdraw(3);

//    bp1.Show2();

//    bp1.Deposit(4);

//    bp1.Show2();

//    bp1.Deposit(10);

//    bp1.Show2();

//    return 0;

    

//    //使用指针或引用,通过基类表达基类或子类,实现多态性,即使用基类的指针或引用可以调用子类的函数(子类需要共有继承基类)

//    Brass *b1;

//    b1=new Brass("name1", 1, 10);

//    b1->Show();

//    cout<<endl;

//    Brass *b2;

//    b2=new BrassPlus("name1",1,10,10,1,0);

//    b2->Show();

    

    return 0;

}

*/

/*

 //类继承基本使用扩展

//基类

class TableTennisPlayer

{

private:

    string firstname;

    string lastname;

    bool hasTable;

public:

    //构造函数和析构函数

    TableTennisPlayer();

    TableTennisPlayer(const string &fn,const string &ln,bool ht=false);

    ~TableTennisPlayer();

    //普通函数

    void Reset1(const string &fn,const string &ln,bool ht=false);

    void Reset1(const TableTennisPlayer & tp);

    //运算符重载函数

    friend ostream& operator<<(ostream &os,const TableTennisPlayer & tp);

    

};

//构造函数和析构函数

TableTennisPlayer::TableTennisPlayer()

{

    

}

TableTennisPlayer::TableTennisPlayer(const string &fn,const string &ln,bool ht) : firstname(fn),lastname(ln),hasTable(ht)

{

    

}

TableTennisPlayer::~TableTennisPlayer()

{

    

}

//普通函数

void TableTennisPlayer::Reset1(const string &fn,const string &ln,bool ht)

{

    firstname=fn;

    lastname=ln;

    hasTable=ht;

}

void TableTennisPlayer::Reset1(const TableTennisPlayer & tp)

{

    firstname=tp.firstname;

    lastname=tp.lastname;

    hasTable=tp.hasTable;

}

//运算符重载函数

ostream& operator<<(ostream &os,const TableTennisPlayer & tp)

{

    os<<tp.firstname<<" "<<tp.lastname<<" "<<tp.hasTable<<endl;

    return os;

}

//子类

class RatedPlayer:public TableTennisPlayer

{

private:

    int rating;

public:

    //构造函数和析构函数

    RatedPlayer();

    RatedPlayer(const string &fn,const string &ln,bool ht,int rt);

    RatedPlayer(const TableTennisPlayer &tp,int rt);

    //普通函数

    void Reset2(const string&fn,const string &ln,bool ht,int rt);

    void Reset2(const TableTennisPlayer &tp,int rt);

    //运算符重载函数

    friend ostream& operator<<(ostream &os,const RatedPlayer &rp);

    

};

 

//构造函数和析构函数

RatedPlayer::RatedPlayer()

{

    

}

RatedPlayer::RatedPlayer(const string &fn,const string &ln,bool ht,int rt) : TableTennisPlayer(fn,ln,ht),rating(rt)

{

}

RatedPlayer::RatedPlayer(const TableTennisPlayer &tp,int rt) : TableTennisPlayer(tp),rating(rt)

{

}

//普通函数

void RatedPlayer::Reset2(const string&fn,const string &ln,bool ht,int rt)

{

    Reset1(fn, ln,ht);

    rating=rt;

}

void RatedPlayer::Reset2(const TableTennisPlayer &tp,int rt)

{

    Reset1(tp);

    rating=rt;

}

//运算符重载函数

ostream& operator<<(ostream &os,const RatedPlayer &rp)

{

    os<<rp.rating<<endl;

    return os;

}

int main()

{

    TableTennisPlayer tp1;

    cout<<"tp1:"<<tp1;

    TableTennisPlayer tp2("wu","cun1",true);

    cout<<"tp2:"<<tp2;

    tp1.Reset1("wu", "cun2",true);

    cout<<"tp1:"<<tp1<<endl;

    tp1.Reset1(tp2);

    cout<<"tp1:"<<tp1;

    

    RatedPlayer rp1;

    cout<<"rp1:"<<rp1;

    RatedPlayer rp2("w","c1",true,5);

    cout<<"rp2:"<<rp2;

    

    

}

 

*/

 

 

 

 

 

 

 

/*

 //类继承的基本使用

//基类定义

class TableTennisPlayer

{

private:

    string firstname;

    string lastname;

    bool hasTable;

public:

    TableTennisPlayer(const string & fn,const string & ln,const bool ht=false);

    void Name() const;

    bool HasTable() const;

    void ResetTable(bool v);

};

//基类方法

TableTennisPlayer::TableTennisPlayer(const string & fn,const string & ln,const bool ht) : firstname(fn),lastname(ln),hasTable(ht)

{

}

void TableTennisPlayer::Name() const

{

    cout<<firstname<<" "<<lastname<<endl;

}

bool TableTennisPlayer::HasTable()const

{

    return hasTable;

}

void TableTennisPlayer::ResetTable(bool v)

{

    hasTable=v;

}

 

//派生类

class RatedPlayer : public TableTennisPlayer

{

private:

    int rating;

public:

    RatedPlayer(const string & fn,const string & ln,bool ht=false,int rt=0);

    RatedPlayer(const TableTennisPlayer & p,int rt=0);

    int Rating() const;

    void ResetRating(int rt);

    

};

RatedPlayer::RatedPlayer(const string & fn,const string & ln,bool ht,int rt):TableTennisPlayer(fn,ln,ht)

{

    rating=rt;

}

RatedPlayer::RatedPlayer(const TableTennisPlayer & p,int rt):TableTennisPlayer(p),rating(rt)

{

}

int RatedPlayer::Rating() const

{

    return rating;

}

void RatedPlayer::ResetRating(int rt)

{

    rating=rt;

}

 

int main() {

    TableTennisPlayer p1("wu","cun1",true);

    RatedPlayer p2("wu","cun2",false,5);

    p1.Name();

    p2.Name();

    if(p1.HasTable())

    {

        cout<<"p1:ok"<<endl;

    }

    else

    {

        cout<<"p1:no"<<endl;

    }

    if(p2.HasTable())

    {

        cout<<"p2:ok"<<endl;

    }

    else

    {

        cout<<"p2:no"<<endl;

    }

    p2.ResetRating(10);

    cout<<p2.Rating()<<endl;

    p2.ResetTable(true);

    if(p2.HasTable())

    {

        cout<<"p2:ok"<<endl;

    }

    else

    {

        cout<<"p2:no"<<endl;

    }

    RatedPlayer p3(p1,9);

    p3.Name();

    return 0;

    

}

 

*/

//

//  main.cpp

//  C++PrimerPlusThirteen

//

//  Created by amengdev on 16/3/30.

//  Copyright © 2016 amengdev. All rights reserved.

//

 

#include <iostream>

#include "string.h"

using namespace std;

 

//基类

class Base

{

private:

    char *bname;

    int blen;

public:

    //构造函数和析构函数

    Base();

    Base(const char *name);

    Base(const Base &b);

    ~Base();

    //普通函数

    //运算符重载函数

    Base & operator=(const Base & b);

    friend ostream& operator<<(ostream &os,const Base &b);

};

 

//构造函数和析构函数

Base::Base()

{

    cout<<"基类构造函数1"<<endl;

    blen=0;

    bname=new char[1];

    bname[0]='\0';

}

Base::Base(const char *name)

{

    cout<<"基类构造函数2"<<endl;

    blen=strlen(name);

    bname=new char[blen+1];

    strcpy(bname, name);

}

Base::Base(const Base &b)

{

    cout<<"基类构造函数3"<<endl;

    blen=b.blen;

    bname=new char[blen+1];

    strcpy(bname, b.bname);

}

Base::~Base()

{

    cout<<"基类析构函数"<<endl;

    delete [] bname;

}

//普通函数

//运算符重载函数

Base &  Base::operator=(const Base & b)

{

    cout<<"赋值构造函数"<<endl;

    if(this==&b)

        return *this;

    delete [] bname;

    blen=strlen(b.bname);

    bname=new char[blen+1];

    strcpy(bname, b.bname);

    return *this;

}

ostream& operator<<(ostream &os,const Base &b)

{

    os<<b.bname<<endl;

    return os;

}

//子类

class BasePlus : public Base

{

private:

    char *pname;

    int plen;

public:

    //构造函数和析构函数

    BasePlus();

    BasePlus(const char *name1,const char *name2);

    BasePlus(const BasePlus & b);

    ~BasePlus();

    //普通函数

    //运算符重载函数

    BasePlus & operator=(const BasePlus & p);

    friend ostream& operator<<(ostream &os,const BasePlus &b);

};

 

//构造函数和析构函数

BasePlus::BasePlus() : Base()

{

    cout<<"子类构造函数1"<<endl;

    plen=0;

    pname=new char[1];

    pname[0]='\0';

}

BasePlus::BasePlus(const char *name1,const char *name2) : Base(name1)

{

    cout<<"子类构造函数2"<<endl;

    plen=strlen(name2);

    pname=new char[plen+1];

    strcpy(pname, name2);

}

BasePlus::BasePlus(const BasePlus & b) : Base(b)

{

    cout<<"子类构造函数2"<<endl;

    plen=strlen(b.pname);

    pname=new char[plen+1];

    strcpy(pname, b.pname);

}

BasePlus::~BasePlus()

{

    cout<<"子类析构函数"<<endl;

    delete [] pname;

}

//普通函数

//运算符重载函数

 

BasePlus & BasePlus::operator=(const BasePlus & p)

{

    if(this==&p)

        return *this;

    Base::operator=(p); //调用基类=运算符重载函数进行内存处理

    delete [] pname;

    plen=strlen(p.pname);

    pname=new char[plen+1];

    strcpy(pname, p.pname);

    return *this;

}

ostream& operator<<(ostream &os,const BasePlus &b)

{

    os<<(const Base & )b;

    os<<b.pname<<endl;

    return os;

}

 

int main()

{

//    Base b1;

//    cout<<b1<<endl;

//    Base b2("bname2");

//    cout<<b2<<endl;

//    Base b3(b2);

//    cout<<b3<<endl;

//    Base b4;

//    b4=b3;

//    cout<<b4<<endl;

//    cout<<"============="<<endl;

    BasePlus p1;

    cout<<p1<<endl;

    BasePlus p2("bname2","pname2");

    cout<<p2<<endl;

    BasePlus p3(p2);

    cout<<p3<<endl;

    BasePlus p4;

    p4=p3;

    cout<<p4<<endl;

    return 0;

}

 

 

/*

 //类继承,抽象基类(ABC)的基本使用

 //抽象基类的构造方式:找到所有子类的公共成员和方法

 //虚函数

//抽象基类

class Base

{

private:

    string name;

    int account;

    int balance;

public:

    //构造函数和析构函数

    Base();

    Base(const string &n,int a,int b);

    Base(const Base &b);

    //普通函数

    virtual bool Deposit(int m)=0;    //存款

    virtual bool Withdraw(int m)=0;   //取款

    int Balance();          //求结余

    virtual void Show()=0;            //显示信息

    virtual void Show2()=0;           //显示信息(测试使用)

    //运算符重载函数

    

};

//构造函数和析构函数

Base::Base() : name(),account(0),balance(0)

{

};

Base::Base(const string &n,int a,int b) : name(n),account(a),balance(b)

{

};

Base::Base(const Base &b) : name(b.name),account(b.account),balance(b.balance)

{

};

//普通函数

bool Base::Deposit(int m)    //存款

{

    if(m<0)

        return false;

    balance+=m;

    return true;

}

bool Base::Withdraw(int m)   //取款

{

    if(m<0)

        return false;

    balance-=m;

    return true;

}

int Base::Balance()          //求结余

{

    return balance;

}

void Base::Show()            //显示信息

{

    cout<<"name:"<<name<<endl;

    cout<<"account:"<<account<<endl;

    cout<<"balance:"<<balance<<endl;

}

void Base::Show2()

{

    cout<<"balance:"<<balance<<endl;

}

//运算符重载函数

 

//继承子类Brass

class Brass : public Base

{

private:

public:

    //构造函数和析构函数

    Brass();

    Brass(const string &n,int a,int b);

    Brass(const Brass &b);

    //普通函数

    virtual bool Deposit(int m);    //存款

    virtual bool Withdraw(int m);   //取款

    virtual void Show();            //显示信息

    virtual void Show2();           //显示信息(测试使用)

    //运算符重载函数

};

//构造函数和析构函数

Brass::Brass() : Base()

{

    

}

Brass::Brass(const string &n,int a,int b) : Base(n,a,b)

{

    

}

Brass::Brass(const Brass &b) : Base(b)

{

    

}

//普通函数

bool Brass::Deposit(int m)    //存款

{

    if(Base::Deposit(m))

        return true;

    return false;

}

bool Brass::Withdraw(int m)   //取款

{

    if(m>Balance())

        return false;

    if(Base::Withdraw(m))

    {

        return true;

    }

    return false;

}

void Brass::Show()            //显示信息

{

    Base::Show();

}

void Brass::Show2()

{

    Base::Show2();

}

//运算符重载函数

 

//子类BrassPlus

class BrassPlus : public Base

{

private:

    int maxLoad;

    int rating;

    int touzhi;

public:

    //构造函数和析构函数

    BrassPlus();

    BrassPlus(const string &n,int a,int b,int m,int r,int t);

    BrassPlus(const BrassPlus &b);

    BrassPlus(const Brass &b,int m,int r,int t);

    //普通函数

    virtual bool Deposit(int m);    //存款

    virtual bool Withdraw(int m);   //取款

    virtual void Show();            //显示信息

    virtual void Show2();           //显示信息(测试使用)

    //运算符重载函数

};

 

//构造函数和析构函数

BrassPlus::BrassPlus() : Base()

{

    

}

BrassPlus::BrassPlus(const string &n,int a,int b,int m,int r,int t) : Base(n,a,b),maxLoad(m),rating(r),touzhi(t)

{

    

}

BrassPlus::BrassPlus(const BrassPlus &b) : Base(b),maxLoad(b.maxLoad),rating(b.rating),touzhi(b.touzhi)

{

    

}

BrassPlus::BrassPlus(const Brass &b,int m,int r,int t) : Base(b),maxLoad(m),rating(r),touzhi(t)

{

    

}

//普通函数

bool BrassPlus::Deposit(int m)    //存款

{

    if(m<0)

        return false;

    if(touzhi==0)

    {

        Base::Deposit(m);

    }

    else

    {

        if(m<=touzhi)

        {

            touzhi-=m;

        }

        else

        {

            Base::Deposit(m-touzhi);

            touzhi=0;

        }

    }

    return true;

}

bool BrassPlus::Withdraw(int m)   //取款

{

    if(m<0)

        return false;

    if(m<=Base::Balance())

    {

        Base::Withdraw(m);

    }

    else

    {

        if(m>(Base::Balance()-touzhi+maxLoad))

        {

            return false;

        }

        else

        {

            touzhi=touzhi+m-Base::Balance();

            Base::Withdraw(Base::Balance());

        }

    }

    return true;

}

void BrassPlus::Show()            //显示信息

{

    Base::Show();

    cout<<"maxLoad:"<<maxLoad<<endl;

    cout<<"rating:"<<rating<<endl;

    cout<<"touzhi:"<<touzhi<<endl;

}

void BrassPlus::Show2()

{

    Base::Show2();

    cout<<"touzhi:"<<touzhi<<endl;

}

//运算符重载函数

int main()

{

    Base *b[10];

    b[0]=new Brass("name0",0,10);

    b[0]->Show2();

    b[0]->Deposit(10);

    b[0]->Show2();

    b[0]->Withdraw(5);

    b[0]->Show2();

    cout<<endl;

    b[1]=new BrassPlus("name1",1,20,20,5,0);

    b[1]->Show2();

    b[1]->Deposit(5);

    b[1]->Show2();

    b[1]->Withdraw(10);

    b[1]->Show2();

    b[1]->Withdraw(20);

    b[1]->Show2();

    

    return 0;

}

*/

 

/*

 //类继承的基本使用

 //虚函数使用

 //此类对类函数设计有代表性

//基类

class Brass{

private:

    string name;

    int account;

    int balance;

public:

    //构造函数和析构函数

    Brass();

    Brass(const string &n,int a,int b);

    Brass(const Brass &b);

    //普通函数

    virtual void Set(const string &n,int a,int b);

    virtual void Set(const Brass &b);

    virtual void Show();    //显示账户信息

    virtual void Show2();   //显示账户信息,测试使用

    virtual bool Deposit(int m);     //存款

    virtual bool Withdraw(int m);    //取款

    int Balance();      //求余额

    //运算符重载函数

    

};

//构造函数和析构函数

Brass::Brass():name(),account(0),balance(0)

{

}

Brass::Brass(const string &n,int a,int b):name(n),account(a),balance(b)

{

}

Brass::Brass(const Brass &b):name(b.name),account(b.account),balance(b.balance)

{

}

//普通函数

void Brass::Set(const string &n,int a,int b)

{

    name=n;

    account=a;

    balance=b;

}

 

void Brass::Set(const Brass &b)

{

    name=b.name;

    account=b.account;

    balance=b.balance;

}

void Brass::Show()    //显示账户信息

{

    cout<<"name:"<<name<<endl;

    cout<<"account:"<<account<<endl;

    cout<<"balance:"<<balance<<endl;

}

void Brass::Show2()   //显示账户信息,测试使用

{

    cout<<"balance:"<<balance<<endl;

}

bool Brass::Deposit(int m)//存款

{

    if(m<0)

        return false;

    balance+=m;

    return true;

}

bool Brass::Withdraw(int m) //取款

{

    if(m<0)

        return false;

    if(m>balance)

        return false;

    balance=balance-m;

    return true;

}

int Brass::Balance()      //求余额

{

    return balance;

}

//子类

class BrassPlus : public Brass

{

private:

    int maxLoan;

    int rate;

    int touzhi;

public:

    //构造函数和析构函数

    BrassPlus();

    BrassPlus(const string &n,int a,int b,int m,int r,int t);

    BrassPlus(const Brass &b,int m,int r,int t);

    //普通函数

    virtual void Set(const string &n,int a,int b,int m,int r,int t);    //重置

    virtual void Set(const Brass &b,int m,int r,int t); //重置

    virtual void Show();    //显示账户信息

    virtual void Show2();   //显示账户信息,测试使用

    virtual bool Deposit(int m);     //存款

    virtual bool Withdraw(int m);    //取款

    //运算符重载函数

};

//子类

 

//构造函数和析构函数

BrassPlus::BrassPlus() : Brass()

{

}

BrassPlus::BrassPlus(const string &n,int a,int b,int m,int r,int t) : Brass(n,a,b),maxLoan(m),rate(r),touzhi(t)

{

}

BrassPlus::BrassPlus(const Brass &b,int m,int r,int t) : Brass(b),maxLoan(m),rate(r),touzhi(t)

{

}

//普通函数

void BrassPlus::Set(const string &n,int a,int b,int m,int r,int t)    //重置

{

    Brass::Set(n, a, b);

    maxLoan=m;

    rate=r;

    touzhi=t;

}

void BrassPlus::Set(const Brass &b,int m,int r,int t) //重置

{

    Brass::Set(b);

    maxLoan=m;

    rate=r;

    touzhi=t;

}

void BrassPlus::Show()    //显示账户信息

{

    Brass::Show();

    cout<<"maxLoan:"<<maxLoan<<endl;

    cout<<"rate:"<<rate<<endl;

    cout<<"touzhi"<<touzhi<<endl;

}

 

void BrassPlus::Show2()   //显示账户信息,测试使用

{

    Brass::Show2();

    cout<<"touzhi:"<<touzhi<<endl;

    cout<<endl;

}

bool BrassPlus::Deposit(int m)     //存款

{

    if(m<0)

        return false;

    if(touzhi==0)

    {

        Brass::Deposit(m);

    }

    else

    {

        if(m<=touzhi)

        {

            touzhi-=m;

        }

        else

        {

            Brass::Deposit(m-touzhi);

            touzhi=0;

        }

    }

    return true;

}

bool BrassPlus::Withdraw(int m)    //取款

{

    if(m<0)

        return false;

    if(m<=Brass::Balance())

    {

        Brass::Withdraw(m);

    }

    else

    {

        if(m>(Brass::Balance()-touzhi+maxLoan))

        {

            return false;

        }

        else

        {

            touzhi=touzhi+m-Brass::Balance();

            Brass::Withdraw(Brass::Balance());

        }

    }

    return true;

}

//运算符重载函数

 

int main()

{

    //基类测试

//    Brass b1;

//    b1.Set("name1", 1, 10);

//    b1.Show();

//    b1.Deposit(10);

//    b1.Show();

//    b1.Withdraw(5);

//    b1.Show();

    //子类测试

//    //姓名-账号-余额-最大透支额度-比率-当前透支额度

//    BrassPlus bp1("name1",1,10,10,1,0);

//    bp1.Show2();

//    bp1.Deposit(10);

//    bp1.Show2();

//    bp1.Withdraw(5);

//    bp1.Show2();

//    bp1.Withdraw(20);

//    bp1.Show2();

//    bp1.Withdraw(3);

//    bp1.Show2();

//    bp1.Deposit(4);

//    bp1.Show2();

//    bp1.Deposit(10);

//    bp1.Show2();

//    return 0;

    

//    //使用指针或引用,通过基类表达基类或子类,实现多态性,即使用基类的指针或引用可以调用子类的函数(子类需要共有继承基类)

//    Brass *b1;

//    b1=new Brass("name1", 1, 10);

//    b1->Show();

//    cout<<endl;

//    Brass *b2;

//    b2=new BrassPlus("name1",1,10,10,1,0);

//    b2->Show();

    

    return 0;

}

*/

/*

 //类继承基本使用扩展

//基类

class TableTennisPlayer

{

private:

    string firstname;

    string lastname;

    bool hasTable;

public:

    //构造函数和析构函数

    TableTennisPlayer();

    TableTennisPlayer(const string &fn,const string &ln,bool ht=false);

    ~TableTennisPlayer();

    //普通函数

    void Reset1(const string &fn,const string &ln,bool ht=false);

    void Reset1(const TableTennisPlayer & tp);

    //运算符重载函数

    friend ostream& operator<<(ostream &os,const TableTennisPlayer & tp);

    

};

//构造函数和析构函数

TableTennisPlayer::TableTennisPlayer()

{

    

}

TableTennisPlayer::TableTennisPlayer(const string &fn,const string &ln,bool ht) : firstname(fn),lastname(ln),hasTable(ht)

{

    

}

TableTennisPlayer::~TableTennisPlayer()

{

    

}

//普通函数

void TableTennisPlayer::Reset1(const string &fn,const string &ln,bool ht)

{

    firstname=fn;

    lastname=ln;

    hasTable=ht;

}

void TableTennisPlayer::Reset1(const TableTennisPlayer & tp)

{

    firstname=tp.firstname;

    lastname=tp.lastname;

    hasTable=tp.hasTable;

}

//运算符重载函数

ostream& operator<<(ostream &os,const TableTennisPlayer & tp)

{

    os<<tp.firstname<<" "<<tp.lastname<<" "<<tp.hasTable<<endl;

    return os;

}

//子类

class RatedPlayer:public TableTennisPlayer

{

private:

    int rating;

public:

    //构造函数和析构函数

    RatedPlayer();

    RatedPlayer(const string &fn,const string &ln,bool ht,int rt);

    RatedPlayer(const TableTennisPlayer &tp,int rt);

    //普通函数

    void Reset2(const string&fn,const string &ln,bool ht,int rt);

    void Reset2(const TableTennisPlayer &tp,int rt);

    //运算符重载函数

    friend ostream& operator<<(ostream &os,const RatedPlayer &rp);

    

};

 

//构造函数和析构函数

RatedPlayer::RatedPlayer()

{

    

}

RatedPlayer::RatedPlayer(const string &fn,const string &ln,bool ht,int rt) : TableTennisPlayer(fn,ln,ht),rating(rt)

{

}

RatedPlayer::RatedPlayer(const TableTennisPlayer &tp,int rt) : TableTennisPlayer(tp),rating(rt)

{

}

//普通函数

void RatedPlayer::Reset2(const string&fn,const string &ln,bool ht,int rt)

{

    Reset1(fn, ln,ht);

    rating=rt;

}

void RatedPlayer::Reset2(const TableTennisPlayer &tp,int rt)

{

    Reset1(tp);

    rating=rt;

}

//运算符重载函数

ostream& operator<<(ostream &os,const RatedPlayer &rp)

{

    os<<rp.rating<<endl;

    return os;

}

int main()

{

    TableTennisPlayer tp1;

    cout<<"tp1:"<<tp1;

    TableTennisPlayer tp2("wu","cun1",true);

    cout<<"tp2:"<<tp2;

    tp1.Reset1("wu", "cun2",true);

    cout<<"tp1:"<<tp1<<endl;

    tp1.Reset1(tp2);

    cout<<"tp1:"<<tp1;

    

    RatedPlayer rp1;

    cout<<"rp1:"<<rp1;

    RatedPlayer rp2("w","c1",true,5);

    cout<<"rp2:"<<rp2;

    

    

}

 

*/

 

 

 

 

 

 

 

/*

 //类继承的基本使用

//基类定义

class TableTennisPlayer

{

private:

    string firstname;

    string lastname;

    bool hasTable;

public:

    TableTennisPlayer(const string & fn,const string & ln,const bool ht=false);

    void Name() const;

    bool HasTable() const;

    void ResetTable(bool v);

};

//基类方法

TableTennisPlayer::TableTennisPlayer(const string & fn,const string & ln,const bool ht) : firstname(fn),lastname(ln),hasTable(ht)

{

}

void TableTennisPlayer::Name() const

{

    cout<<firstname<<" "<<lastname<<endl;

}

bool TableTennisPlayer::HasTable()const

{

    return hasTable;

}

void TableTennisPlayer::ResetTable(bool v)

{

    hasTable=v;

}

 

//派生类

class RatedPlayer : public TableTennisPlayer

{

private:

    int rating;

public:

    RatedPlayer(const string & fn,const string & ln,bool ht=false,int rt=0);

    RatedPlayer(const TableTennisPlayer & p,int rt=0);

    int Rating() const;

    void ResetRating(int rt);

    

};

RatedPlayer::RatedPlayer(const string & fn,const string & ln,bool ht,int rt):TableTennisPlayer(fn,ln,ht)

{

    rating=rt;

}

RatedPlayer::RatedPlayer(const TableTennisPlayer & p,int rt):TableTennisPlayer(p),rating(rt)

{

}

int RatedPlayer::Rating() const

{

    return rating;

}

void RatedPlayer::ResetRating(int rt)

{

    rating=rt;

}

 

int main() {

    TableTennisPlayer p1("wu","cun1",true);

    RatedPlayer p2("wu","cun2",false,5);

    p1.Name();

    p2.Name();

    if(p1.HasTable())

    {

        cout<<"p1:ok"<<endl;

    }

    else

    {

        cout<<"p1:no"<<endl;

    }

    if(p2.HasTable())

    {

        cout<<"p2:ok"<<endl;

    }

    else

    {

        cout<<"p2:no"<<endl;

    }

    p2.ResetRating(10);

    cout<<p2.Rating()<<endl;

    p2.ResetTable(true);

    if(p2.HasTable())

    {

        cout<<"p2:ok"<<endl;

    }

    else

    {

        cout<<"p2:no"<<endl;

    }

    RatedPlayer p3(p1,9);

    p3.Name();

    return 0;

    

}

 

*/

posted on 2016-04-21 09:22  W.C  阅读(112)  评论(0编辑  收藏  举报

导航