13.4

//abc.h
#ifndef ABC_H
#define ABC_H

#include<iostream>

class ABC{

private:
	char* label;
	int rating;
public:
	ABC(const char* l="null",int r=0);
	ABC(const ABC& a);
	virtual ~ABC();
	ABC& operator=(const ABC &a);
	friend std::ostream &operator <<(std::ostream& os,const ABC &a);
		virtual void view()const=0;
};
class baseDMA:public ABC
{
public:
	baseDMA(const char *l="null",int r=0);
	baseDMA(const baseDMA &b);
	virtual~ baseDMA();
   virtual void view()const;
   friend std ::ostream & operator<<(std::ostream &os,const baseDMA &b);
   baseDMA & operator=(const baseDMA & b);
};
class lacksDMA :public ABC
{
private:
	enum { COL_LEN=40};
	char color[COL_LEN];
public:
	lacksDMA(const char* c = "blank",const char *l ="null",int r= 0);
	lacksDMA(const char* c,const ABC&a);
	virtual void view()const;
	friend std::ostream & operator <<(std ::ostream &os,const lacksDMA &rs);
};
 
class hasDMA:public ABC
{
private:
	char* style;
public:
	hasDMA(const char* s="none",const char*l="null",int r=0);
	hasDMA(const char* s,const  ABC &a);
	virtual void view()const;
	hasDMA(const hasDMA &hs);
	~hasDMA();
	hasDMA& operator=(const hasDMA &rs);
	friend std::ostream & operator <<(std::ostream &os,const hasDMA &rs);
};
#endif
//abc.cpp
#include"abc.h"
using std ::cout;
using std::endl;

ABC::ABC(const char *l,int r)
{
	label = new char[strlen(l)+1];
	strcpy(label,l);
	rating = r;
}
ABC::ABC(const ABC &a)
	{
		label =new char[strlen(a.label)+1];
		strcpy(label,a.label);
		rating = a.rating;
}
ABC::~ABC()
{
	delete[]label;
}
ABC& ABC::operator=(const ABC &a)
{
	if(this ==& a)
		return *this;
	delete []label;
label =new char[strlen(a.label)+1];
		strcpy(label,a.label);
		rating = a.rating;
		return *this;
}
void ABC::view()const
{
	cout<<"label: "<<label<<endl;
	cout<<"rating: "<<rating<<endl;
}

	std ::ostream& operator<<(std::ostream &os,const ABC &a)
	{
		os<<"label: "<<a.label<<endl;
		os<<"rating:"<<a.rating<<endl;
		return os;
	}

	baseDMA ::baseDMA(const char *l,int r):ABC(l,r)
	{
	}

	baseDMA ::baseDMA(const baseDMA &b):ABC(b)
	{
	}
	baseDMA ::~baseDMA()
	{
	}
	void baseDMA::view()const
	{
		ABC::view();
	}
	std::ostream &operator<<(std::ostream &os,const baseDMA &b)
	{
		os<<(const ABC &)b<<endl;
		return os;
	}
	baseDMA& baseDMA::operator=(const baseDMA &b)
	{
		if(this ==&b)
			return *this;
		ABC::operator=(b);
		return *this;

	}

	lacksDMA::lacksDMA(const char*c,const char*l,int r):ABC(l,r)
	{
		strcpy(color,c);
	}

	lacksDMA::lacksDMA(const char* c,const ABC &a):ABC(a)
	{
		strcpy(color,c);
	}

	void lacksDMA::view()const
	{
		cout<<"color: "<<color<<endl;
		ABC::view();
	}
	std::ostream& operator <<(std ::ostream &os,const lacksDMA &rs)
	{
		os<<"color: "<<rs.color<<endl;
		os<<(const ABC&)rs<<endl;
		return os;
	}
	hasDMA::hasDMA(const char*s,const char*l,int r):ABC(l,r)
	{
		style=new char[strlen(s)+1];
		strcpy(style,s);
	}
	hasDMA::hasDMA(const char *s,const ABC &a):ABC(a)
	{
		style=new char[strlen(s)+1];
		strcpy(style,s);
	}
	void hasDMA ::view()const
	{
		cout<<"style: "<<style<<endl;
		ABC::view();
	}
	hasDMA::hasDMA(const hasDMA &hs):ABC(hs)
	{
		style=new char[strlen(hs.style)+1];
		strcpy(style,hs.style);
	}
	hasDMA::~hasDMA()
	{
		delete[]style;
	}
	hasDMA & hasDMA::operator=(const hasDMA &rs)
	{
		if(this ==&rs)
			return *this;

		ABC::operator=(rs);
		delete[]style;
		style=new char[strlen(rs.style)+1];
		strcpy(style,rs.style);
		return *this;
	}
	std ::ostream & operator<<(std::ostream &os,const hasDMA &rs)
	{
		os<<"style: "<<rs.style<<endl;
		os<<(const ABC&)rs<<endl;
		return os;
	}
//main.cpp
#include<iostream>
#include<string>
#include"abc.h"
const int CLIENTS =4;


int main()
{
	using std::cin;
	using std::cout;
	using std::endl;
	ABC *base[3];
    char *label=new char[40];
	 int rating;
	 char kind;
	 char *color=new char[40];
	 char*style=new char[40];

	/*  cin.getline(color,40);

	  ABC * ab=new hasDMA( color,"aaa",3);
	  ab->view();
	  system("pause");
	  return 0;
}*/
	 for (int i=0;i<3;i++)
	 {
		 cout<<"Enter label : ";
		 cin.getline(label,40);
		 cout<<"enter rating : ";
		 cin>>rating;
		 cout<<"enter 1 for baseDMA,enter 2 for lacksDMA,enter 3 for hasDMA: ";
			 while(cin>>kind &&(kind !='1'&&kind !='2'&&kind !='3'))
			 
				 cout <<"enter either 1 or 2,3 :";
			 cin.get();//必须加上读取回车键,否则下一个getline读取不了
		          if (kind=='1')
					 base[i]= new baseDMA(label,rating);
				 else if(kind =='2')
				 {
					 cout<<"enter color: ";
					 cin.getline(color,40);
					 base[i]=new lacksDMA(color,label,rating);
				 }
				 else 
				 {
					 cout<<"enter style: ";
					 cin.getline(style,40);
					 base[i] = new hasDMA(style,label,rating);
				 }
				 while(cin.get()!='\n')
				        continue;
	 }
	 
			 cout<<endl;
	
			 for(int i=0;i<3;i++)
			 {
				 base[i]->view();
				 //cout<<*base[i];

			 }
			 for(int i=0;i<3;i++)
			 {
				 delete base[i];
			 }
			 cout<<"done."<<endl;
			 system("pause");

			 return 0;
	 }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值