|

楼主 |
发表于 2013-3-20 20:59:22
|
显示全部楼层
- #include <iostream>
- #include <string>
- class Animal
- {
- public:
- Animal(std::string theName);
- void eat();
- void eat(int eatCount);
- void sleep();
- void swim();
- protected:
- std::string name;
- };
- class Pig : public Animal
- {
- public:
- Pig(std::string theName);
- void climb();
- };
- void Animal::eat()
- {
- std::cout <<"I'm eating!" << std::endl;
- }
- void Animal::eat(int eatCount)
- {
- std::cout <<"我吃了" << eatCount <<"碗馄饨" << std::endl;
- }
- void Animal::sleep()
- {
- std::cout <<"I'm sleeping!" <<std::endl;
- }
- void Animal::swim()
- {
- std::cout <<"我是"<<name <<"I'm swiming!" << std::endl;
- }
- Animal::Animal(std::string theName)
- {
- name=theName;
- }
- Pig::Pig(std::string theName) : Animal(theName)
- {
- name=theName;
- }
- void Pig::climb()
- {
- std::cout <<"我是"<<name <<"正在追龟" <<std::endl;
- }
- int main()
- {
- Animal animal("动物");
- animal.eat();
- animal.eat(15);
- animal.sleep();
- animal.swim();
- Pig pig("小猪猪");
- pig.climb();
- return 0;
- }
复制代码 |
|