评论

收藏

[C++] [C++学习]继承

编程语言 编程语言 发布于:2021-08-08 13:11 | 阅读数:547 | 评论:0

[C++学习]继承
#include <iostream>
#include <string>
using namespace std;
class Father
{
public:

  void setHeight(int height)
    {
    
        this->height= height;
    }
    int getHeight()
    {
    return height;
    }

    void setWeight(int weight)
    {
        this->weight = weight;
    }

  int getWeight()
    {
   return weight;
    }


    void toString()
    {
        
   cout<<"体重:"<<this->weight<<"身高:"<<this->height<<endl;
    }
private:
    int height;
    int weight;

};
class Son : public Father
{
public:
    void setAge(int age)
    {
     this->age = age;
    }

    int getAge()
    {
    return age;
    }

    void toString()
    {
        
        cout<<"姓名:\t"<<this->name<<"\t体重:\t"<<Father::getWeight()<<"\t身高:\t"<<Father::getHeight()<<"\t年龄:\t"<<this->age<<endl;
    }

    void setName(string name)
    {
        this->name=name;
    }
    string getName()
    {
    return name;
    }


private:
    int age;
    string name;
};
void main()
{



    Son f ;
   f.setName("张三丰");
   f.setHeight(175);
     f.setWeight(140);
     f.setAge(25);


      f.toString();

}

关注下面的标签,发现更多相似文章