雨中漫步~ 发表于 2022-1-8 22:27:47

继承protected

#include <iostream>
#include <string>

class Animal
{
public:
        std::string mouth;
       
        Animal(std::string theName);//构造器
       
        void eat();
        void sleep();
protected:
                std::string name;       

};

class Pig : public Animal
{
public:
        void climb();
        Pig(std::string theName1); //构造器
//protected:
//        std::string name;
};

class Turtle : public Animal
{
public:
        void swim();
        Turtle(std::string theName);//构造器
};

Animal::Animal(std::string theName)
{
        name = theName;
}

Pig::Pig(std::string theName):Animal(theName)
{
}

Turtle::Turtle(std::string theName) : Animal(theName)
{
}

void Animal::eat()
{
        std::cout << "I am eating!" << std::endl;
}

void Animal::sleep()
{
        std::cout << "I am sleeping!" << std::endl;
}


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

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

int main(void)
{
        Pig pig("B");
        Turtle turtle("老乌龟");
//        pig.name = "A";
       
        std::cout << "这只猪的名字是:" << pig.name << std::endl;
        std::cout << "小甲鱼的名字是:" << turtle.name << std::endl;
        pig.eat();
        turtle.eat();
        pig.climb();
        turtle.swim();
       
        return 0;
}

我想将Animal基类中name保护起来不被修改,请问应该怎么对name进行赋值?

雨中漫步~ 发表于 2022-1-8 22:49:29

#include <iostream>
#include <string>

class Animal
{
public:
        std::string mouth;
        Animal(std::string theName);//构造器
        void eat();
        void sleep();
        void drool();
protected:
        std::string name;       
};

class Pig : public Animal
{
public:
        void climb();
        Pig(std::string theName); //构造器
};

class Turtle : public Animal
{
public:
        void swim();
        Turtle(std::string theName);//构造器
};

Animal::Animal(std::string theName)
{
        name = theName;
}

Pig::Pig(std::string theName) : Animal(theName)
{
}

Turtle::Turtle(std::string theName) : Animal(theName)
{
}

void Animal::eat()
{
        std::cout << "I am eating!" << std::endl;
}

void Animal::sleep()
{
        std::cout << "I am sleeping!" << std::endl;
}

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

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

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

int main(void)
{
        Pig pig("B");
        Turtle turtle("老乌龟");
//        pig.name = "A";
       
        //std::cout << "这只猪的名字是:" << pig.name << std::endl;
        //std::cout << "小甲鱼的名字是:" << turtle.name << std::endl;
        pig.eat();
        turtle.eat();
        pig.climb();
        turtle.swim();
       
        return 0;
}

我该怎么访问我所命名的被保护的name呢?

傻眼貓咪 发表于 2022-1-9 00:09:21

#include <iostream>
#include <string>

class Animal
{
public:
      std::string mouth;
      Animal(std::string theName);//构造器
      void eat();
      void sleep();
      void drool();
      std::string getName(); // 这里 <-----------------------------------------------
protected:
      std::string name;         
};

class Pig : public Animal
{
public:
      void climb();
      Pig(std::string theName); //构造器
};

class Turtle : public Animal
{
public:
      void swim();
      Turtle(std::string theName);//构造器
};

Animal::Animal(std::string theName)
{
      name = theName;
}

Pig::Pig(std::string theName) : Animal(theName)
{
}

Turtle::Turtle(std::string theName) : Animal(theName)
{
}

void Animal::eat()
{
      std::cout << "I am eating!" << std::endl;
}

void Animal::sleep()
{
      std::cout << "I am sleeping!" << std::endl;
}

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

std::string Animal::getName() // 这里 <-----------------------------------------------
{
    return name;
}

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

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

int main(void)
{
      Pig pig("B");
      Turtle turtle("老乌龟");
//      pig.name = "A";
      
      std::cout << "这只猪的名字是:" << pig.getName() << std::endl; // 打印 <-----------------------------------------------
      std::cout << "小甲鱼的名字是:" << turtle.getName() << std::endl;// 打印 <-----------------------------------------------
      pig.eat();
      turtle.eat();
      pig.climb();
      turtle.swim();
         
      return 0;
}

雨中漫步~ 发表于 2022-1-9 18:43:06

傻眼貓咪 发表于 2022-1-9 00:09


std::string getName();
请问这句怎么理解呢,它和Animal里面的属性mouth,方法eat()、sleep()、drool()有什么区别么

雨中漫步~ 发表于 2022-1-9 18:49:11

傻眼貓咪 发表于 2022-1-9 00:09


我好像明白了,getName()和eat()、sleep()都是类的方法,只不过它的返回值是string,是这个意思吧

傻眼貓咪 发表于 2022-1-9 18:53:34

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

没错,只有类方法,才能用自己的 protected 变量
页: [1]
查看完整版本: 继承protected