鱼C论坛

 找回密码
 立即注册
查看: 4511|回复: 5

C++string字符串、类的问题

[复制链接]
发表于 2013-3-20 15:50:25 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include <iostream>
  2. #include <string>

  3. class Animal
  4. {
  5. public:

  6. void eat();
  7. void sleep();
  8. void swim();
  9. std::string name();
  10. };


  11. void Animal::eat()
  12. {
  13. std::cout <<"I'm eating!" << std::endl;
  14. }

  15. void Animal::sleep()
  16. {
  17. std::cout <<"I'm sleeping!" <<std::endl;
  18. }

  19. void Animal::swim()
  20. {
  21. std::cout <<"I'm swiming!" << std::endl;
  22. }


  23. int main()
  24. {
  25. Animal animal;
  26. animal.name("小猪猪");
  27. animal.eat();
  28. animal.sleep();
  29. animal.swim();
  30. return 0;
  31. }
复制代码

animal.name("小猪猪");错误。

小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-3-20 15:53:22 | 显示全部楼层
1 name这个函数么有实现
2 name这个函数没有带参数的重载 你不能传入参数
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2013-3-20 15:59:05 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-3-20 16:01:00 | 显示全部楼层
我只提示你哪里不对
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-3-20 20:43:37 | 显示全部楼层
  1. #include <iostream>

  2. #include <string>


  3. class Animal

  4. {
  5.        
  6. public:
  7.        
  8.        
  9.         void eat();
  10.        
  11.         void sleep();
  12.        
  13.         void swim();
  14.        
  15.         //std::string name();
  16.         std::string name;
  17.        
  18. };



  19. void Animal::eat()

  20. {
  21.        
  22.         std::cout <<"I'm eating!" << std::endl;
  23.        
  24. }


  25. void Animal::sleep()

  26. {
  27.        
  28.         std::cout <<"I'm sleeping!" <<std::endl;
  29.        
  30. }


  31. void Animal::swim()

  32. {
  33.        
  34.         std::cout <<"I'm swiming!" << std::endl;
  35.        
  36. }



  37. int main()

  38. {
  39.        
  40.         Animal animal;
  41.        
  42.         //animal.name("小猪猪");
  43.         animal.name = "小猪猪";
  44.        
  45.         animal.eat();
  46.        
  47.         animal.sleep();
  48.        
  49.         animal.swim();
  50.        
  51.         return 0;
  52.        
  53. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2013-3-20 20:59:22 | 显示全部楼层
  1. #include <iostream>
  2. #include <string>

  3. class Animal
  4. {
  5. public:

  6.     Animal(std::string theName);
  7.     void eat();
  8.     void eat(int eatCount);
  9.     void sleep();
  10.     void swim();

  11. protected:
  12.     std::string name;

  13. };

  14. class Pig : public  Animal
  15. {
  16. public:

  17.     Pig(std::string theName);
  18.     void climb();
  19. };
  20. void Animal::eat()
  21. {
  22.     std::cout <<"I'm eating!"  << std::endl;
  23. }

  24. void Animal::eat(int eatCount)
  25. {
  26.     std::cout <<"我吃了" << eatCount <<"碗馄饨"  << std::endl;
  27. }

  28. void Animal::sleep()
  29. {
  30.     std::cout <<"I'm sleeping!" <<std::endl;
  31. }

  32. void Animal::swim()
  33. {
  34.     std::cout <<"我是"<<name <<"I'm swiming!" << std::endl;
  35. }

  36. Animal::Animal(std::string theName)
  37. {
  38.     name=theName;
  39. }

  40. Pig::Pig(std::string theName) : Animal(theName)
  41. {
  42.     name=theName;
  43. }

  44. void Pig::climb()
  45. {
  46.     std::cout <<"我是"<<name <<"正在追龟" <<std::endl;
  47. }

  48. int main()
  49. {
  50.     Animal animal("动物");

  51.     animal.eat();
  52.     animal.eat(15);
  53.     animal.sleep();
  54.     animal.swim();

  55.     Pig pig("小猪猪");
  56.     pig.climb();
  57.     return 0;
  58. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-8-5 10:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表