再读C++ Primer 写了个小例子——练习多态虚函数的特性(08-01-25)

 再读C++ Primer 写了个小例子——练习多态虚函数的特性
#pragma  once

class  Animal
{
public:
    
//操作
    Animal(void);
//    Animal(int i);
    virtual ~Animal(void);
    
virtual void ShowMe() = 0;
    
int getAnimalType();
protected:
    
int animalType;
}
;

#include 
" StdAfx.h "
#include 
" .animal.h "

Animal::Animal(
void )
{
}

// Animal::Animal(int i)
// {
//     animalType = i;
// }
Animal:: ~ Animal( void )
{
}

int  Animal::getAnimalType()
{
    
return animalType;
}


#pragma  once
#include 
" animal.h "

class  Cat :
    
public  Animal
{
public:
    Cat(
void);
    
~Cat(void);
    Cat(
char* ctype,int w,char* cfood);
    
void ShowMe();
//    void operator <<(Cat & cat);
    char* getType();
    
int getWeight();
    
char* getFood();

protected:
    
char type[20];
    
int weight;
    
char food[20];
}
;

#include 
" StdAfx.h "
#include 
" .at.h "
using   namespace  std;

Cat::Cat(
void )
{
    memcpy(type,
0,sizeof(type));
    weight 
= 0;
    memcpy(food,
0,sizeof(food));
}


Cat::
~ Cat( void )
{
}

Cat::Cat(
char *  ctype, int  w, char *  cfood)
{
//    Animal(atype);
    strcpy(type,ctype);
    strcpy(food,cfood);
    weight 
= w;
    Cat::animalType
=1;
}

void  Cat::ShowMe()
{
    cout
<<"The Cat' properties"<<endl;
    cout
<<"Type: "<<getType()<<endl;
    cout
<<"Weight: "<<getWeight()<<endl;
    cout
<<"Food: "<<getFood()<<endl;
}

char *  Cat::getType()
{
    
return type;
}

int  Cat::getWeight()
{
    
return weight;
}

char *  Cat::getFood()
{
    
return food;
}


#pragma  once
#include 
" animal.h "
#include 
" iostream "


// istream & operator>>(istream& is,Dog& dog)
// {
//     cout<<"is"<<endl;
//     is>>dog.type;
//     is>>dog.food;
//     is>>dog.weight;
//     return is;
// }

class  Dog :
    
public  Animal
{
public:
    
//操作
    Dog(void);
    
~Dog(void);
    Dog(
char* ctype,int w,char* cfood);
    
void ShowMe();
//    friend istream& operator>>(istream& is,Dog& dog);
    char* getType();
    
int getWeight();
    
char* getFood();
    
//属性
protected:
    
char type[20];
    
int weight;
    
char food[20];
}
;

#include 
" StdAfx.h "
#include 
" .dog.h "
using   namespace  std;

Dog::Dog(
void )
{
    memcpy(type,
0,sizeof(type));
    weight 
= 0;
    memcpy(food,
0,sizeof(food));
}


Dog::
~ Dog( void )
{
}

Dog::Dog(
char *  ctype, int  w, char *  cfood)
{
//    Animal(i);
    strcpy(type,ctype);
    strcpy(food,cfood);
    weight 
= w;
    Dog::animalType
=2;

}

void  Dog::ShowMe()
{
    cout
<<"The Dog' properties"<<endl;
    cout
<<"Type: "<<getType()<<endl;
    cout
<<"Weight: "<<getWeight()<<endl;
    cout
<<"Food: "<<getFood()<<endl;
}

char *  Dog::getType()
{
    
return type;
}

int  Dog::getWeight()
{
    
return weight;
}

char *  Dog::getFood()
{
    
return food;
}



#pragma  once
#define  MAX 12
#include 
" Animal.h "

class  shelves
{
public:
    shelves(
void);
    
~shelves(void);
    
void addElement(Animal* animal);
    
void deleteElement(int index);
    
void searchType();
    
int getTotal();
    
void getTypeTotal();


protected:
    Animal
* element[MAX];
    
int total;
//    int typeTotal;
}
;


#include 
" StdAfx.h "
#include 
" .shelves.h "
using   namespace  std;

shelves::shelves(
void )
{
    
for(int i=0;i<MAX;i++)
    
{
        element[i]
=NULL;
    }

    total
=0;
//    typeTotal=0;
}


shelves::
~ shelves( void )
{

    
for(int i=0;i<MAX;i++)
    
{
//        delete element[i];
        element[i]=NULL;
    }


}

void  shelves::addElement(Animal *  animal)
{
    
if(total<MAX)
    
{
        element[total] 
= animal;
        total
++;
    }

}

void  shelves::deleteElement( int  index)
    
{
        
if(index >= 0 && index < MAX)
        
{
            element[index] 
= NULL;
            total
--;
        }

    }

void  shelves::searchType()
    
{
        
for(int i=0;i<MAX;i++)
        
{
            element[i]
->ShowMe();
        }

    }

int  shelves::getTotal()
    
{
        
return total;
    }

void  shelves::getTypeTotal()
    
{
        
int catTotal=0;
        
int dogTotal=0;
        
int snakeTotal=0;
        
for(int i=0;i<MAX;i++)
        
{
            
if(element[i]->getAnimalType() == 1)
            
{
                catTotal 
++;
            }

            
else if(element[i]->getAnimalType() == 2)
            
{
                dogTotal 
++;
            }

            
else 
            
{
                snakeTotal 
++;
            }

        }

//        return typeTotal;
        cout<<"Cat total: "<<catTotal<<endl;
        cout
<<"Dog Total: "<<dogTotal<<endl;
        cout
<<"Snake Total: "<<snakeTotal<<endl;
    }


#pragma  once
#include 
" animal.h "

class  Snake :
    
public  Animal
{
public:
    Snake(
void);
    
~Snake(void);
    Snake(
char* ctype,int w,char* cfood);
    
void ShowMe();
    
char* getType();
    
int getWeight();
    
char* getFood();
protected:
    
char type[20];
    
int weight;
    
char food[20];
}
;

#include 
" StdAfx.h "
#include 
" .snake.h "
using   namespace  std;

Snake::Snake(
void )
{
    memcpy(type,
0,sizeof(type));
    weight 
= 0;
    memcpy(food,
0,sizeof(food));
}


Snake::
~ Snake( void )
{
}

Snake::Snake(
char *  ctype, int  w, char *  cfood)
{
//    Animal(i);
    strcpy(type,ctype);
    strcpy(food,cfood);
    weight 
= w;
    Snake::animalType
=3;
}

void  Snake::ShowMe()
{
    cout
<<"The Snake' properties"<<endl;
    cout
<<"Type: "<<getType()<<endl;
    cout
<<"Weight: "<<getWeight()<<endl;
    cout
<<"Food: "<<getFood()<<endl;
}

char *  Snake::getType()
{
    
return type;
}

int  Snake::getWeight()
{
    
return weight;
}

char *  Snake::getFood()
{
    
return food;
}


//  test0825.cpp : Defines the entry point for the console application.
//

#include 
" stdafx.h "

#include 
" iostream "
#include 
" Animal.h "
#include 
" at.h "
#include 
" Dog.h "
#include 
" Snake.h "
#include 
" shelves.h "

using   namespace  std;



int  _tmain( int  argc, _TCHAR *  argv[])
{
    shelves shel;
     Cat
* cat;
      Dog
* dog;
      
//Dog* dog1=new Dog();
      
//cin>>dog1;
      Snake* snake;
    
char* name= new char[20];
    
char* food=new char[20];
    
int weight = 0;
    cout
<<"请输入3个动物的类型,动物喜欢的食物,动物的重量"<<endl;
    
for(int i = 0;i<3;i++)
    
{
       cin
>>name>>food>>weight;
       
if(i==0)
       
{
           cat
=new Cat(name,weight,food);
           shel.addElement(cat);
       }

       
if(i==1)
       
{
           dog
=new Dog(name,weight,food);
           shel.addElement(dog);
       }

       
if(i==2)
       
{
           snake
= new Snake(name,weight,food);
           shel.addElement(snake);
       }

    }

    cat
->ShowMe();
    dog
->ShowMe();
    snake
->ShowMe();
    cout
<<"显示笼子的动物数量: "<<shel.getTotal()<<endl;
    cout
<<"显示笼子的动物类型: ";
    shel.searchType();
    cout
<<endl;
    cout
<<"显示笼子的动物类型数量: ";
    shel.getTypeTotal();
    cout
<<endl;

//void addElement(Animal* animal);
//    void deleteElement(int index);
//    void searchType();
//    int getTotal();
//    void getTypeTotal();
//    

//    cat.ShowMe();
    cin>>weight;
    delete name;
    delete food;
    delete cat;
    delete dog;
    delete snake;
    name 
= NULL;
    food 
= NULL;
    
    
return 0;
}








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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值