纵有疾风起
人生不言弃

[C++学习]继承

[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();

}

原文链接:https://blog.csdn.net/w605283073/article/details/44246519

本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。

未经允许不得转载:起风网 » [C++学习]继承
分享到: 生成海报

评论 抢沙发

评论前必须登录!

立即登录