14週間プロジェクト1.2(動物の名前)
2464 ワード
/*
*
* Copyright (c)2013,
* All rightsreserved.
* :Animal.cpp
* :
* :2013 6 4
* : v1.0
* :
* :
* :
* :
#include<iostream>
using namespace std;
class Animal
{
public:
virtual void cry()=0;
};
class Mouse:public Animal
{
public:
Mouse(string nam,char s):name(nam),sex(s){}
virtual void cry()
{
cout<<" "<<name<<", "<<((sex=='m')?" ":" ")<<" , : !"<<endl;
}
private:
string name;
char sex;
};
class Cat:public Animal
{
public:
Cat(string nam):name(nam){}
virtual void cry()
{
cout<<" "<<name<<", , : !"<<endl;
}
private:
string name;
};
class Dog:public Animal
{
public:
Dog(string nam):name(nam){}
virtual void cry()
{
cout<<" "<<name<<", , : !"<<endl;
}
private:
string name;
};
class Giraffe:public Animal
{
public:
Giraffe(string nam,char s):name(nam),sex(s){}
virtual void cry()
{
cout<<" "<<name<<", "<<((sex=='M')?" ":" ")<<" , , !"<<endl;
}
private:
string name;
char sex;
};
int main( )
{
Animal *p;
//p = new Animal();
//p->cry();
Mouse m1("Jerry",'m');
p=&m1;
p->cry();
Mouse m2("Jemmy",'f');
p=&m2;
p->cry();
Cat c1("Tom");
p=&c1;
p->cry();
Dog d1("Droopy");
p=&d1;
p->cry();
Giraffe g1("Gill",'m');
p=&g1;
p->cry();
return 0;
}
*実行結果: