CPP学习之——单一继承(11.4)

一 概述

本文主要讲述单一继承,基类是Father,子类是Son,来演示单一继承特性

二 示例演示及结果输出

2.1 代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<iostream>
using namespace std;
class father
{
private:
int fatherHeight,fatherWeight;
public:
void setFatherHeight(int fathersHeight){fatherHeight=fathersHeight;}
void setFatherWeight(int fathersWeight){fatherWeight=fathersWeight;}
void showFatherHeightWeight()
{
cout<<"父亲身高="<<fatherHeight<<"\t"<<"父亲体重="<<fatherWeight<<endl;
}
};
class Son:public father
{
private:
int SonWidth,SonLength;
public:
void setSonWidth(int sonsWidth){SonWidth=sonsWidth;}
void setSonLength(int sonsLength){SonLength=sonsLength;}
void showSonData()
{
cout<<"儿子肩宽="<<SonWidth<<"\t"<<"儿子臂长="<<SonLength<<endl;
}

};
using namespace std;
int main()
{
Son a;
a.setFatherHeight(160);
a.setFatherWeight(60);
a.setSonWidth(60);
a.setSonLength(80);
a.showFatherHeightWeight();
a.showSonData();
return 0;
}

2.2 输出结果

1
2
父亲身高=160	父亲体重=60
儿子肩宽=60 儿子臂长=80