?? vc0903.cpp
字號(hào):
// Example 9.3: 抽象寵物類
#include <iostream.h>
#include <string.h>
class Pet //基類
{
char m_strName[20];
int m_nAge;
char m_strColor[12];
public:
char m_strType[10];
Pet(char *,int ,char *);
char* GetName() { return m_strName; }
int GetAge() { return m_nAge; }
char* GetColor() { return m_strColor; }
virtual void Speak() = 0;
virtual void GetInfo() {}
};
Pet::Pet(char *name,int age,char *color)
{
strcpy(m_strName,name);
m_nAge=age;
strcpy(m_strColor,color);
strcpy(m_strType,"pet");
}
class Cat : public Pet //派生類
{
public:
Cat(char *name,int age, char *color):Pet(name,age,color)
{ }
void Speak()
{ cout<<" Sound of speak : miao!miao!"<<endl<<endl; }
void GetInfo();
};
void Cat::GetInfo()
{
cout<<" The cat's name : "<<GetName()<<endl;
cout<<" The cat's age : "<<GetAge() <<endl;
cout<<" The cat's color: "<<GetColor()<<endl;
}
class Dog : public Pet //派生類
{
public:
Dog(char *name,int age, char *color):Pet(name,age,color)
{ }
void Speak()
{ cout<<" Sound of speak : wang!wang!"<<endl<<endl; }
void GetInfo();
};
void Dog::GetInfo()
{
cout<<" The dog's name : "<<GetName()<<endl;
cout<<" The dog's age : "<<GetAge() <<endl;
cout<<" The dog's color: "<<GetColor()<<endl;
}
void main()
{
Pet *p1; //基類對(duì)象指針p1
p1 = new Cat("MiKey",1,"Blue"); //動(dòng)態(tài)生成Cat類對(duì)象
p1->GetInfo();
p1->Speak();
delete p1;
p1 = new Dog("BenBen",2,"Black");//動(dòng)態(tài)生成Dog類對(duì)象
p1->GetInfo();
p1->Speak();
delete p1;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -