[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();
}
文档来源:51CTO技术博客https://blog.51cto.com/u_15316467/3309497
页:
[1]