⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chongwuxiaowu.cpp

📁 利用C++的类和对象编写C++的应用程序
💻 CPP
字号:
//综合程序:Annie的宠物小屋
#include<iostream.h>
#include<string.h>
// 定义Animal类
class Animal
{   
protected:
	char *Color;
	char *Food;
	double Weight;
public:
	char *Type;
	void Anl(char*p1,char*p2,char*p3,double a) //定义Anl函数实现数据的传递,实现系统主函数的初始化             
	{Type=new char [strlen(p1)+1];
	 strcpy(Type,p1);
  	 Color=new char [strlen(p2)+1];
	 strcpy(Color,p2);
	 Food=new char [strlen(p3)+1];
	 strcpy(Food,p3);
	 Weight=a;
	}
	virtual void ShowMe()=0;
	friend istream& operator>>(istream&stream,Animal*a) //重载">>"运算符,使得能够通过cin直接读入宠物的种类、颜色、喜爱的食物和体重
	{cout<<endl<<"please enter:"<<a->Type<<"'s color,food and weight:"<<endl;
	 stream>>a->Color>>a->Food>>a->Weight;
	 return stream;
	}
};
//派生Cat类
class Cat:public Animal
{public:
 void ShowMe()
 {cout<<Type<<"     color is:"<<Color<<"  food is:"<<Food<<"  weight is:"<<Weight<<endl;}		
};
//派生Dog类
class Dog:public Animal
{public:
 void ShowMe()
 {cout<<Type<<"     color is:"<<Color<<"  food is:"<<Food<<"  weight is:"<<Weight<<endl;}		
};
//派生Snake类
class Snake:public Animal
{public:
 void ShowMe()
 {cout<<Type<<"   color is:"<<Color<<"  food is:"<<Food<<"  weight is:"<<Weight<<endl;}		
};
//定义shelves类
class shelves
{   
	static int k;
	Animal *p[12];
public:
	shelves() //构造函数
	{for(int j=0;j<=11;j++)
	 p[j]=NULL;
	}
	void Add(int y0,Animal *q1) //在选择一个序号的空笼子增加任何一种动物
	{if(y0<0||y0>11)cout<<"there is no shelve of this number.";
     else
	 {if(p[y0]!=NULL)cout<<"this shelve is full."<<endl;
	 else 
	 {p[y0]=q1;
	  cin>>p[y0];
	  k++;
	 }
	 }
	} 
	void Delete(int y1) //删除对应序号的笼子的动物
	{if(y1<0||y1>11)cout<<"there is no shelve of this number.";
     else
	 {if(p[y1]==NULL)cout<<"this shelve has been empty for a long time."<<endl;
	 else 
	 {p[y1]=NULL;
	  cout<<"it has been deleted."<<endl;
	  k--;
	 }
	 }
	}
	void Check(int y2) //查询对应序号笼子的情况
	{if(y2<0||y2>11)cout<<"there is no shelve of this number.";
     else
	 {if(p[y2]==NULL)
	  cout<<"empty."<<endl;
	  else p[y2]->ShowMe();
	 }
	}
	void SumCheck() //统计动物的总数与类型
	{int j=0;
	 int D=0,S=0,C=0;
	 for(j;j<=11;j++)
	 {if(p[j]==NULL) continue;
	  else
	  {if(!strcmp("cat",p[j]->Type))C++;
	   if(!strcmp("dog",p[j]->Type))D++;
	   if(!strcmp("snake",p[j]->Type))S++;
	  }
	 }
	 cout<<"--------------------------------------------------"<<endl;//显示总体情况
	 cout<<"the result of Annie's shelves is below:"<<endl;
	 cout<<"the number of cats is:"<<C<<endl;
	 cout<<"the number of dogs is:"<<D<<endl;
	 cout<<"the number of snakes is:"<<S<<endl;
	 cout<<"the number of full shelves is:"<<k<<endl;
	 cout<<"the number of empty shelves is:"<<12-k<<endl<<endl;
	}
};
int shelves::k=0; //静态成员函数初始化
//定义主函数
void main()
{
	shelves sh;
	Cat c;
	Dog d;
	Snake s;
	sh.SumCheck();
	for(int w=0;w<12;w++) 
	{cout<<"shelve "<<w<<" is ";
	 sh.Check(w);}
	c.Anl("cat","black","fish",15);//初始化并给出例子
	d.Anl("dog","yellow","bone",30);
	s.Anl("snake","white","frog",5);
	cout<<endl<<"examples as follow:"<<endl;//显示初始化的例子
	c.ShowMe();
	d.ShowMe();
	s.ShowMe();
	cout<<endl;
	while(1!=0){//循环操作;关闭对话框才可以结束程序
	int x;
loop:cout<<"the number (0--11) of shelve you chose is: ";//输入想执行操作的笼子编号
	cin>>x;
	if(x<0||x>11)  //防止错误发生
	{cout<<"there is no shelve of this number."<<endl<<endl;
	 goto loop;}
    sh.Check(x);
	int y,n,m;
loop1:cout<<endl<<"do you want to delete shelve "<<x<<" ?  enter 1(yes) or 0(no),please."<<endl;//删除笼子里的宠物
    cin>>y;
    if(y<0||y>1) //防止错误发生
	{cout<<"Error!"<<endl;
	 goto loop1;}
	if(y==1) sh.Delete(x);
loop2:cout<<endl<<"do you want to add animal to shelve "<<x<<" ?  enter 1(yes) or 0(no),please."<<endl;//添加宠物
	cin>>n;
    if(n<0||n>1) //防止错误发生
	{cout<<"Error!"<<endl;
	 goto loop2;}
	if(n==1)
	{loop3:cout<<endl<<"what animal do you want to add to shelve "<<x<<" ?   1=(cat) 2=(dog) 3=(snake)"<<endl;//选择宠物
	 cin>>m;
	 if(m<1||m>3) //防止错误发生
	{cout<<"Error!"<<endl;
	 goto loop3;}
     switch(m)
	 {case 1:{Animal *p=&c;   sh.Add(x,p);}  //基类指针作为操作的中间量变
      case 2:{Animal *p=&d;   sh.Add(x,p);}
      case 3:{Animal *p=&s;   sh.Add(x,p);}
	 }
	}
    sh.SumCheck();  //显示总体情况
	for(w=0;w<12;w++) 
	{cout<<"shelve "<<w<<" is ";
	 sh.Check(w);}
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -