面向对象的封装、继承和多态

#include <iostream>
const int N = 1e5 + 7;
// 面向对象:封装、继承、多态
class Animal {
	int age;
public:
	int x, y;
	// 封装
	void setAge(int a) { 
		if (a > 100 || a < 1) return;
		age = a; 
	}
	int getAge() { return age; }
	virtual void yell() { puts("haha"); }
} node;
// 继承
class Cat : public Animal {
public:
	// 重载函数
	void yell() { setAge(10); puts("miao"); }
	void eat() { puts("fish"); }
} leaf;
int main() {
	Animal *a[3] = {&node, &leaf};
	// 多态
	for (int i = 0; i <= 1; ++i)
		a[i]->yell();
	printf("%d\n", leaf.getAge());
}
posted @ 2024-09-17 09:05  飞花阁  阅读(28)  评论(0)    收藏  举报
//雪花飘落效果