#pragma warning(disable:2243)
#pragma warning(disable:4996)
#include<stdio.h>
#include<stdbool.h>
class Pet{
public:
virtual void speack() {//本章知识点 虚函数
/*
目前虚函数的知识 我只在基类指针指向派生类对象时用到过
*/
printf("How does a pet spack?\n");
return;
};
virtual void move() {
printf("How does a pet move?\n");
return;
}
protected:
private:
};
class Cat:public Pet
{
public:
Cat() {};
~Cat() {};
void speack() {
printf("喵喵~\n");
return;
};
void move() {
printf("walk...walk..walk...\n");
}
private:
protected:
};
class Dog:public Pet
{
public:
Dog() {};
~Dog() {};
void speack() {
printf("wangwang!\n");
return;
};
void move() {
printf("walk...walk..walk...\n");
}
private:
protected:
};
class Bird :public Pet
{
public:
Bird() {};
~Bird() {};
void speack() {
printf("啾啾!\n");
return;
};
void move() {
printf("fly...fly...fly...\n");
// printf("walk...walk..walk...\n");
}
private:
protected:
};
void solve() {
printf("初始宠物选择~请输入宠物序号~\n1.狗 2.猫 3.鸟\n");
int a = 0;
bool flag = true;
Pet* p = nullptr;
scanf("%d", &a);
switch (a)
{
case 1:
p = new Dog();
break;
case 2:
p = new Cat();
break;
case 3:
p = new Bird();
break;
default:
printf("只能选择1 2 3 ,不要耍系统,小伙子!\n");
flag = false;
break;
}
if (flag) {
p->speack();
p->move();
delete p;//不要忘了释放!!!
}
putchar('\n');
return;
}
int main() {
int w = 3;
while (w--) {
solve();
}
return 0;
}
C++课程作业之 宠物类的创建
最新推荐文章于 2023-08-28 19:46:12 发布