鱼C论坛

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

[已解决]继承protected

[复制链接]
发表于 2022-1-8 22:27:47 | 显示全部楼层 |阅读模式

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

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

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

  3. class Animal
  4. {
  5. public:
  6.         std::string mouth;
  7.        
  8.         Animal(std::string theName);//构造器
  9.        
  10.         void eat();
  11.         void sleep();
  12. protected:
  13.                 std::string name;         

  14. };

  15. class Pig : public Animal
  16. {
  17. public:
  18.         void climb();
  19.         Pig(std::string theName1); //构造器
  20. //protected:
  21. //        std::string name;
  22. };

  23. class Turtle : public Animal
  24. {
  25. public:
  26.         void swim();
  27.         Turtle(std::string theName);//构造器
  28. };

  29. Animal::Animal(std::string theName)
  30. {
  31.         name = theName;
  32. }

  33. Pig::Pig(std::string theName):Animal(theName)
  34. {
  35. }

  36. Turtle::Turtle(std::string theName) : Animal(theName)
  37. {
  38. }

  39. void Animal::eat()
  40. {
  41.         std::cout << "I am eating!" << std::endl;
  42. }

  43. void Animal::sleep()
  44. {
  45.         std::cout << "I am sleeping!" << std::endl;
  46. }


  47. void Pig::climb()
  48. {
  49.         std::cout << "我会上树,没想到吧!" << std::endl;
  50. }

  51. void Turtle::swim()
  52. {
  53.         std::cout << "我是一只小甲鱼,当有人要捉我的时候,我就缩头。。。" << std::endl;
  54. }

  55. int main(void)
  56. {
  57.         Pig pig("B");
  58.         Turtle turtle("老乌龟");
  59. //        pig.name = "A";
  60.        
  61.         std::cout << "这只猪的名字是:" << pig.name << std::endl;
  62.         std::cout << "小甲鱼的名字是:" << turtle.name << std::endl;
  63.         pig.eat();
  64.         turtle.eat();
  65.         pig.climb();
  66.         turtle.swim();
  67.          
  68.         return 0;
  69. }
复制代码


我想将Animal基类中name保护起来不被修改,请问应该怎么对name进行赋值?
最佳答案
2022-1-9 00:09:21
  1. #include <iostream>
  2. #include <string>

  3. class Animal
  4. {
  5. public:
  6.         std::string mouth;
  7.         Animal(std::string theName);//构造器
  8.         void eat();
  9.         void sleep();
  10.         void drool();
  11.         std::string getName(); // 这里 <-----------------------------------------------
  12. protected:
  13.         std::string name;         
  14. };

  15. class Pig : public Animal
  16. {
  17. public:
  18.         void climb();
  19.         Pig(std::string theName); //构造器
  20. };

  21. class Turtle : public Animal
  22. {
  23. public:
  24.         void swim();
  25.         Turtle(std::string theName);//构造器
  26. };

  27. Animal::Animal(std::string theName)
  28. {
  29.         name = theName;
  30. }

  31. Pig::Pig(std::string theName) : Animal(theName)
  32. {
  33. }

  34. Turtle::Turtle(std::string theName) : Animal(theName)
  35. {
  36. }

  37. void Animal::eat()
  38. {
  39.         std::cout << "I am eating!" << std::endl;
  40. }

  41. void Animal::sleep()
  42. {
  43.         std::cout << "I am sleeping!" << std::endl;
  44. }

  45. void Animal::drool()
  46. {
  47.         std::cout << "我想挑战一下我的极限!" << std::endl;
  48. }

  49. std::string Animal::getName() // 这里 <-----------------------------------------------
  50. {
  51.     return name;
  52. }

  53. void Pig::climb()
  54. {
  55.         std::cout << "我会上树,没想到吧!" << std::endl;
  56. }

  57. void Turtle::swim()
  58. {
  59.         std::cout << "我是一只小甲鱼,当有人要捉我的时候,我就缩头。。。" << std::endl;
  60. }

  61. int main(void)
  62. {
  63.         Pig pig("B");
  64.         Turtle turtle("老乌龟");
  65. //        pig.name = "A";
  66.         
  67.         std::cout << "这只猪的名字是:" << pig.getName() << std::endl; // 打印 <-----------------------------------------------
  68.         std::cout << "小甲鱼的名字是:" << turtle.getName() << std::endl;  // 打印 <-----------------------------------------------
  69.         pig.eat();
  70.         turtle.eat();
  71.         pig.climb();
  72.         turtle.swim();
  73.          
  74.         return 0;
  75. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-1-8 22:49:29 | 显示全部楼层
  1. #include <iostream>
  2. #include <string>

  3. class Animal
  4. {
  5. public:
  6.         std::string mouth;
  7.         Animal(std::string theName);//构造器
  8.         void eat();
  9.         void sleep();
  10.         void drool();
  11. protected:
  12.         std::string name;         
  13. };

  14. class Pig : public Animal
  15. {
  16. public:
  17.         void climb();
  18.         Pig(std::string theName); //构造器
  19. };

  20. class Turtle : public Animal
  21. {
  22. public:
  23.         void swim();
  24.         Turtle(std::string theName);//构造器
  25. };

  26. Animal::Animal(std::string theName)
  27. {
  28.         name = theName;
  29. }

  30. Pig::Pig(std::string theName) : Animal(theName)
  31. {
  32. }

  33. Turtle::Turtle(std::string theName) : Animal(theName)
  34. {
  35. }

  36. void Animal::eat()
  37. {
  38.         std::cout << "I am eating!" << std::endl;
  39. }

  40. void Animal::sleep()
  41. {
  42.         std::cout << "I am sleeping!" << std::endl;
  43. }

  44. void Animal::drool()
  45. {
  46.         std::cout << "我想挑战一下我的极限!" << std::endl;
  47. }

  48. void Pig::climb()
  49. {
  50.         std::cout << "我会上树,没想到吧!" << std::endl;
  51. }

  52. void Turtle::swim()
  53. {
  54.         std::cout << "我是一只小甲鱼,当有人要捉我的时候,我就缩头。。。" << std::endl;
  55. }

  56. int main(void)
  57. {
  58.         Pig pig("B");
  59.         Turtle turtle("老乌龟");
  60. //        pig.name = "A";
  61.        
  62.         //std::cout << "这只猪的名字是:" << pig.name << std::endl;
  63.         //std::cout << "小甲鱼的名字是:" << turtle.name << std::endl;
  64.         pig.eat();
  65.         turtle.eat();
  66.         pig.climb();
  67.         turtle.swim();
  68.          
  69.         return 0;
  70. }
复制代码


我该怎么访问我所命名的被保护的name呢?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-9 00:09:21 | 显示全部楼层    本楼为最佳答案   
  1. #include <iostream>
  2. #include <string>

  3. class Animal
  4. {
  5. public:
  6.         std::string mouth;
  7.         Animal(std::string theName);//构造器
  8.         void eat();
  9.         void sleep();
  10.         void drool();
  11.         std::string getName(); // 这里 <-----------------------------------------------
  12. protected:
  13.         std::string name;         
  14. };

  15. class Pig : public Animal
  16. {
  17. public:
  18.         void climb();
  19.         Pig(std::string theName); //构造器
  20. };

  21. class Turtle : public Animal
  22. {
  23. public:
  24.         void swim();
  25.         Turtle(std::string theName);//构造器
  26. };

  27. Animal::Animal(std::string theName)
  28. {
  29.         name = theName;
  30. }

  31. Pig::Pig(std::string theName) : Animal(theName)
  32. {
  33. }

  34. Turtle::Turtle(std::string theName) : Animal(theName)
  35. {
  36. }

  37. void Animal::eat()
  38. {
  39.         std::cout << "I am eating!" << std::endl;
  40. }

  41. void Animal::sleep()
  42. {
  43.         std::cout << "I am sleeping!" << std::endl;
  44. }

  45. void Animal::drool()
  46. {
  47.         std::cout << "我想挑战一下我的极限!" << std::endl;
  48. }

  49. std::string Animal::getName() // 这里 <-----------------------------------------------
  50. {
  51.     return name;
  52. }

  53. void Pig::climb()
  54. {
  55.         std::cout << "我会上树,没想到吧!" << std::endl;
  56. }

  57. void Turtle::swim()
  58. {
  59.         std::cout << "我是一只小甲鱼,当有人要捉我的时候,我就缩头。。。" << std::endl;
  60. }

  61. int main(void)
  62. {
  63.         Pig pig("B");
  64.         Turtle turtle("老乌龟");
  65. //        pig.name = "A";
  66.         
  67.         std::cout << "这只猪的名字是:" << pig.getName() << std::endl; // 打印 <-----------------------------------------------
  68.         std::cout << "小甲鱼的名字是:" << turtle.getName() << std::endl;  // 打印 <-----------------------------------------------
  69.         pig.eat();
  70.         turtle.eat();
  71.         pig.climb();
  72.         turtle.swim();
  73.          
  74.         return 0;
  75. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-1-9 18:43:06 | 显示全部楼层

std::string getName();
请问这句怎么理解呢,它和Animal里面的属性mouth,方法eat()、sleep()、drool()有什么区别么
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-1-9 18:49:11 | 显示全部楼层

我好像明白了,getName()和eat()、sleep()都是类的方法,只不过它的返回值是string,是这个意思吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-9 18:53:34 | 显示全部楼层
雨中漫步~ 发表于 2022-1-9 18:49
我好像明白了,getName()和eat()、sleep()都是类的方法,只不过它的返回值是string,是这个意思吧


没错,只有类方法,才能用自己的 protected 变量
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-4-25 03:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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