第13章 类继承

由于优快云博客一直无法访问,导致博客一直没有更新!摔!

1.一个普通的类继承,包括构造函数传对象引用和参数列表初始化

头文件:
#ifndef TABLETENN1_H_
#define TABLETENN1_H_
#include<string>
using namespace std;
class TableTennisPlayer
{
private:
    string firstname;
    string lastname;
    bool hasTable;
public:
    TableTennisPlayer(const string &fn="none",const string &ln="none",bool ht=false);
    void Name()const;
    bool HasTable()const
    {
        return hasTable;
    }
    void ResetTable(bool v)
    {
        hasTable=v;
    }
};
class RatedPlayer:public TableTennisPlayer
{
private:
    int rating;
public:
    RatedPlayer(int r=0,const string &fn="none",const string &ln="none",bool ht=false);
    RatedPlayer(int r,const TableTennisPlayer &tp);
    int Rating()const
    { return rating;}
    void ResetRating(int r)
    { rating=r;}
};
#endif
函数实现:
#include<iostream>
#include"tabtenn1.h"
using namespace std;
TableTennisPlayer::TableTennisPlayer(const string &fn,const string &ln,bool ht)
{
    firstname=fn;
    lastname=ln;
    hasTable=ht;
}
void TableTennisPlayer::Name()const
{
    cout<<lastname<<", "<<firstname;
}
RatedPlayer::RatedPlayer(int r,const string &fn,const string &ln,bool ht)
{
    TableTennisPlayer(fn,ln,ht);
    rating=r;
}
RatedPlayer::RatedPlayer(int r, const TableTennisPlayer &tp):TableTennisPlayer(tp)
{
    rating=r;
}
函数调用:
#include<iostream>
#include"tabtenn1.h"
using namespace std;
int main()
{
    TableTennisPlayer player1("Tara","Boomdea",false);
    RatedPlayer rplayer1(1140,"Maloory","Duck",true);
    rplayer1.Name();
    if(rplayer1.HasTable())
        cout<<" has a table!"<<endl;
    else
        cout<<" hasn't a table!"<<endl;
    player1.Name();
    if(player1.HasTable())
        cout<<" has a table!"<<endl;
    else
        cout<<" hasn't a table!"<<endl;
    cout<<"Name:";
    rplayer1.Name();
    cout<<";Rating: "<<rplayer1.Rating();

    RatedPlayer rplayer2(1212,player1);
    cout<<"Name: ";
    rplayer2.Name();
    cout<<";Rating: "<<rplayer2.Rating()<<endl;

    return 0;
}

2.虚函数和抽象基类(带有纯虚函数的基类)省去前面不带抽象基类的程序,直接写的完整版。

头文件:

#ifndef ACCTABC_H_
#include<iostream>
#include<string>
using namespace std;
class AcctABC
{
private:
<span style="white-space:pre">	</span>string fullname;
<span style="white-space:pre">	</span>long acctNum;
<span style="white-space:pre">	</span>double balance;
protected:
<span style="white-space:pre">	</span>string &Fullname()
<span style="white-space:pre">	</span>{return fullname;}
<span style="white-space:pre">	</span>long AcctNum()
<span style="white-space:pre">	</span>{return acctNum;}
public:
<span style="white-space:pre">	</span>AcctABC(const string &s="none",long an=-1,double bal=0.0);
<span style="white-space:pre">	</span>void Deposit(double amt);
<span style="white-space:pre">	</span>virtual void Withdraw(double amt)=0;
<span style="white-space:pre">	</span>double Balance()
<span style="white-space:pre">	</span>{return balance;}
<span style="white-space:pre">	</span>virtual void ViewAcct()=0;
<span style="white-space:pre">	</span>virtual ~AcctABC(){};
};
class Brass:public AcctABC
{
public:
<span style="white-space:pre">	</span>Brass(const string &s="none",long an=-1,double bal=0.0);//??
    void Withdraw(double amt);           //基类中定义为了虚函数 子类不必在加virtual
    void ViewAcct();
    ~Brass(){};
};
class BrassPlus:public Brass
{
private:
<span style="white-space:pre">	</span>double maxLoan;
<span style="white-space:pre">	</span>double rate;
<span style="white-space:pre">	</span>double owesBank;
public:
<span style="white-space:pre">	</span>BrassPlus(const string &s="none",long an=-1,double bal=0.0,double ml=500,double r=0.1);
<span style="white-space:pre">	</span>BrassPlus(const Brass &ba,double ml=500,double r=0.1);
<span style="white-space:pre">	</span> void Withdraw(double amt);         //基类中定义为了虚函数 子类不必在加virtual
<span style="white-space:pre">	</span> void ViewAcct();
<span style="white-space:pre">	</span>void RestMax(double m)
<span style="white-space:pre">	</span>{maxLoan=m;}
<span style="white-space:pre">	</span>void RestRate(double r)
<span style="white-space:pre">	</span>{rate=r;}
<span style="white-space:pre">	</span>void RestOwes()
<span style="white-space:pre">	</span>{owesBank=0;}
};
#endif
函数实现:

#include<iostream>
#include"acctabc.h"
using namespace std;
AcctABC::AcctABC(const string &s,long an,double bal)   //在类的实现部分 不能有默认的参数值 否则会导致重定义
{
    fullname=s;
    acctNum=an;
    balance=bal;
}
void AcctABC::Deposit(double amt)
{
    balance=balance+amt;
}
void AcctABC::Withdraw(double amt)
{
    balance-=amt;
}


Brass::Brass(const string &s,long an,double bal):AcctABC(s,an,bal) //在类的实现部分 不能有默认的参数值 否则会导致重定义
{
}
void  Brass::Withdraw(double amt)
{
    if(amt<=Balance())
        AcctABC::Withdraw(amt);
    else
        cout<<"exceeds your balance!"<<endl;
}
void Brass::ViewAcct()
{
    cout<<"Brass Client:"<<Fullname()<<endl;
    cout<<"Account Number:"<<AcctNum()<<endl;
    cout<<"Balance :$"<<Balance()<<endl;
}


BrassPlus::BrassPlus(const string &s,long an,double bal,double ml,double r):Brass(s,an,bal) //在类的实现部分 不能有默认的参数值 否则会导致重定义
{
    maxLoan=ml;
    rate=r;
    owesBank=0.0;
}
BrassPlus::BrassPlus(const Brass &ba,double ml,double r):Brass(ba)     //在类的实现部分 不能有默认的参数值 否则会导致重定义
{
    maxLoan=ml;
    rate=r;
    owesBank=0.0;
}
void BrassPlus::Withdraw(double amt)
{
    double bal=Balance();
    if(amt<=bal)
        AcctABC::Withdraw(amt);
    else if(amt<=bal+maxLoan-owesBank)
    {
        double advance=amt-bal;
        owesBank+=advance*(1.0+rate);
        cout<<"Bank advance:$"<<advance<<endl;
        cout<<"Finance charge:$"<<advance*rate<<endl;
        Deposit(advance);
        AcctABC::Withdraw(amt);
    }
    else 
        cout<<"Credit limit exceeded!"<<endl;
}
void BrassPlus::ViewAcct()
{
    Brass::ViewAcct();
    cout<<"<Max loan:$"<<maxLoan<<endl;
    cout<<"Owed to bank:$"<<owesBank<<endl;
    cout<<"Loan rate:"<<rate<<endl;
}
函数调用:
#include<iostream>
#include<string>
#include"acctabc.h"
using namespace std;
const int N=4;
int main()
{
    AcctABC *p[N];
    string tname;
    long tnum;
    double tbal;
    int kind;
    for(int i=0;i<N;i++)
    {
        cout<<"Enter name of #"<<i+1<<":";
        //getline(cin,tname);
        cin>>tname;
        cout<<"Enter number :";
        cin>>tnum;
        cout<<"Enter balance:";
        cin>>tbal;
        cout<<"Enter the type of client:";
        cin>>kind;
        while(kind!=1&&kind!=2)
        {    cout<<"type error!";
            cout<<"Enter the type of client:";
            cin>>kind;
        }
        if(kind==1)
        {
            p[i]= new Brass(tname,tnum,tbal); //用new 
        }
        else 
        {
            double trate;
            double tmax;
            cout<<"Enter the rate:";
            cin>>trate;
            cout<<"Enter the MaxLoan:";
            cin>>tmax;
            p[i]=new BrassPlus(tname,tnum,tbal,tmax,trate);
        }
    //    while (cin.get()!='\n')
        //  continue;  后面的不合法输入在缓冲中的给清理掉,为下一次输入扫清障碍。还要明白一点cin.get()是不会忽略空格和回车的,
        //每次输入的最后你总是敲击enter以表明你的输入结束从而为上述语句总能处理成功提供保障。
    }
        for(int i=0;i<N;i++)
        {
            p[i]->ViewAcct();
            cout<<endl;
        }
        p[0]->Withdraw(1200);
        p[0]->ViewAcct();
        for(int i=0;i<N;i++)
        {
            delete p[i];
        }
        cout<<"Done!\n";
        return 0;
}

3.继承和动态内存分配

头文件:
#ifndef DMA_H_
#define DMA_H_
#include<iostream>
using namespace std;   //俩个要一起用 有第二个必须要有第一个
class baseDMA
{
private:
	char *label;
	int rating;
public:
	baseDMA(char *l="none",int r=0);
	baseDMA(const baseDMA &rs);  //派生类继承后使用 
	baseDMA &operator=(const baseDMA &rs);
	friend ostream &operator<<(ostream &os,const baseDMA &b);
	~baseDMA();
};
class lackDMA:public baseDMA
{
private:
	enum{LEN=40};
	char color[LEN];
public:
	lackDMA(char *l="none",int r=0,char *c="none");
	lackDMA(const baseDMA &b,char *c="none");
	friend ostream &operator<<(ostream &os,const lackDMA &l);
};
class hasDMA:public baseDMA
{
private:
	char *style;
public:
	hasDMA(char *l="none",int r=0,char *s="none");
	hasDMA(const baseDMA &b,char *s);
	hasDMA(const hasDMA &rs);//可以使用baseDMA中的复制构造函数来复制共有的部分
	hasDMA &operator=(const hasDMA &rs);
	friend ostream &operator<<(ostream &os,const hasDMA &h);
	~hasDMA();
};
#endif

函数实现:
#include"dma.h"
#include<cstring>
#include<iostream>
using namespace std;
baseDMA::baseDMA(char *l,int r)
{
	//label=l;//错误!!!!
	label=new char[strlen(l)+1];
	strcpy(label,l);
	rating=r;
}
baseDMA::baseDMA(const baseDMA &rs)
{
	label=new char[strlen(rs.label)+1];
	strcpy(label,rs.label);
	rating=rs.rating;
}
baseDMA &baseDMA::operator=(const baseDMA &rs)
{
	if(this==&rs)
		return *this;
	delete []label;
	label=new char[strlen(rs.label)+1];
	strcpy(label,rs.label);
	rating=rs.rating;
	return *this;
}
ostream &operator<<(ostream &os,const baseDMA &b)
{
	os<<"label:"<<b.label<<endl;
	os<<"rating:"<<b.rating<<endl;
	return os;
}
baseDMA::~baseDMA()
{
	delete[]label;
}


lackDMA::lackDMA(char *l,int r,char *c):baseDMA(l,r)
{
	strncpy(color,c,39);
	color[39]='\0';
}
lackDMA::lackDMA(const baseDMA &b,char *c):baseDMA(b)  //传入类型一定为baseDMA
{
	strncpy(color,c,39);
	color[39]='\0';
}
ostream &operator<<(ostream &os,const lackDMA &l)
{
	os<<(const baseDMA &)l;//强制类型转换为基类 然后<<在基类中已经重载
	os<<"Color:"<<l.color<<endl;
	return os;
}

hasDMA::hasDMA(char *l,int r,char *s):baseDMA(l,r)
{
	style=new char[strlen(s)+1];
	strcpy(style,s);
}
hasDMA::hasDMA(const baseDMA &b,char *c):baseDMA(b)
{
	style=new char[strlen(c)+1];
	strcpy(style,c);
}
hasDMA::hasDMA(const hasDMA &rs):baseDMA(rs)
{
	style=new char[strlen(rs.style)+1];
	strcpy(style,rs.style);
}
hasDMA &hasDMA::operator=(const hasDMA &rs)
{
	if(this==&rs)
		return *this;
	baseDMA::operator=(rs);//使用基类已经重载的=来进行 共有部分 的赋值
	delete[] style;
	style=new char[strlen(rs.style)+1];
	strcpy(style,rs.style);
	return *this;
}
ostream &operator<<(ostream &os,const hasDMA &h)
{
	os<<(const baseDMA &)h;
	os<<"style:"<<h.style;
	return os;
}
hasDMA::~hasDMA()
{
	delete[]style;
}

函数调用:
#include<iostream>
#include"dma.h"
using namespace std;
int main()
{
	baseDMA shirt("Portable",8);
	lackDMA ballon("red",4,"Blimpo");
	hasDMA map("Mercator",5,"buffer");
	cout<<"Display baseDMA object:\n";
	cout<<shirt<<endl;
	cout<<"Display lackDMA object:\n";
	cout<<ballon<<endl;
	cout<<"Display hasDMA object:\n";
	cout<<map<<endl;
	lackDMA ballon2(ballon);
	cout<<"Result of lackDMA copy:\n";
	cout<<ballon2<<endl;
	hasDMA map2;
	map2=map;
	cout<<"result of hasDMA assignment:\n";
	cout<<map2<<endl;
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值