标签:code 数据 面积 display space 访问权限 影响 类成员 注意
c++继承与派生
课题目标
学习声明和使用类的继承关系,声明派生类。 熟悉不同继承方式下对基类成员的访问控制。
课题引入
如何让一个类能直接调用另一个类的数据和函数。
我们常用类的友元来实现,但是友元关系并不能继承。基类的友元对派生类的成员没有特殊访问权限。
如果基类被授予友元关系,则只有基类具有特殊访问权限,该基类的派生类不能访问授予友元关系的类。
概念引入:
继承:保持已有类的特性而构成新类的过程。
派生:在已有类的基础上新增自己的特性而产生新类的过程。
基类被继承的已有类(父类)
派生类派生出的新类称为(子类)
class 派生类名:继承方式 基类名{
成员声明;
}
继承方式种类:
1.公有继承(public )
公有继承,基类的公有成员和保护成员在派生类中属性不变,但私有成员不可直接访问。也就是说通过派生类对象只能访问基类的public成员。
#include<iostream>
using namespace std;
class point{
private: int x, y;
public:
void initpoint(float x1 = 0, float y1 = 0) {
x = x1;
y = y1;
}
void move(float xx=0, float yy=0) {
x += xx;
y += yy;
}
float getx() const{
return x;
}
float gety() const {
return y;
}
};
class Rectangle :public point { //公有继承
private:
int w, h;
public:
void initRectangle(float x, float y, float w, float h) {
initpoint(x, y);
this->w = w;
this->h = h;
}
float geth() const {
return h;
}
float getw() const {
return w;
}
};
int main() {
Rectangle rect;
rect.initRectangle(2, 3, 20, 30);
rect.move(3, 2);
cout << rect.getx() << endl;
cout << rect.gety() << endl;
cout << rect.getw() << endl;
cout << rect.geth() << endl;
return 0;
}
运行结果:

2.私有继承(private)
私有继承,基类的公有成员和保护成员都以私有成员出现在派生类中,同样私有成员在派生类中不可直接访问。也就是说通过派生类对象能访问基类的public成员和protected成员。
#include<iostream>
using namespace std;
class point {
private: int x, y;
public:
void initpoint(float x1 = 0, float y1 = 0) {
x = x1;
y = y1;
}
void move(float xx = 0, float yy = 0) {
x += xx;
y += yy;
}
float getx() const {
return x;
}
float gety() const {
return y;
}
};
class Rectangle :private point { //私有继承
private:
int w, h;
public:
void initRectangle(float x, float y, float w, float h) {
initpoint(x, y);
this->w = w;
this->h = h;
}
float getx() const {
return point::getx();
}
float gety() const {
return point::gety();
}
float geth() const {
return h;
}
float getw() const {
return w;
}
void move(float offx, float offy) {
point::move(offx, offy);
}
};
int main() {
Rectangle rect;
rect.initRectangle(2, 3, 20, 30);
rect.move(1, 1);
cout << rect.getx() << endl;
cout << rect.gety() << endl;
cout << rect.getw() << endl;
cout << rect.geth() << endl;
return 0;
}
运行结果:

3.保护继承(protected)
在保护继承中,基类公有成员和保护成员都以保护成员出现在派生类中,但基类私有成员仍然不能被直接访问。也就是说通过派生类对象能访问基类的public成员和protected成员但都以protected成员形式出现。
#include<iostream>
using namespace std;
class A {
protected:
int x;
};
class B :protected A {
public:
void fuc() {
x = 5;
cout << x;
}
};
int main() {
B b;
b.fuc();
return 0;
}
注意:如果B是A的派生,B中的成员函数只能通过B的对象访问A中定义的protected成员,而不能通过A的对象访问A中的protected成员。
运行结果:

| 继承方式/基类成员 | public成员 | private成员 | protected成员 |
| public继承 | public | 不可见 | protected |
| protected继承 | protected | 不可见 | protected |
| private继承 | private | 不可见 | private |
友元与继承的区别
友元关系不能继承。基类的友元对派生类的成员没有特殊访问权限。如果基类被授予友元关系,则只有基类具有特殊访问权限,该基类的派生类不能访问授予友元关系的类。
实验例题
定义一个图形类,其中有保护类型的成员数据:高度和宽度,一个公有的构造函数由该图形类建立两个派生类:矩形类和等腰三角形类。在每个派生类中area(),分别用来计算矩形和等腰三角形的面积。
#include<iostream>
using namespace std;
class figure
{
public:
figure();
figure(double width,double height);
protected:
double kuan,gao;
};
figure::figure(){}
figure::figure(double width,double height)
{
kuan=width;
gao=height;
}
class rectangle:public figure
{
public:
rectangle(double width,double height):figure(width,height){}
double area();
void display();
// private:
// double kuan,gao;
};
double rectangle::area()
{
double m;
m=kuan*gao;
return(m);
}
void rectangle::display()
{
cout<<"I am a rectangle. The value of width is "<<kuan<<", the height is "<<gao<<‘.‘;
}
class triangle:public figure
{
public:
triangle(double width,double height):figure(width,height){}
double area();
void display();
// private:
// double kaun,gao;
};
double triangle::area()
{
double m;
m=(kuan*gao)/2;
return(m);
}
void triangle::display()
{
cout<<"I am a triangle. The value of width is "<<kuan<<", the height is "<<gao<<‘.‘;
}
int main()
{
double width,height;
cin>>width>>height;
triangle tri(width,height);
rectangle rect(width,height);
tri.display();
cout<<"The area is "<<tri.area()<<endl;
rect.display();
cout<<"The area is "<<rect.area()<<endl;
return 0;
}

标签:code 数据 面积 display space 访问权限 影响 类成员 注意
原文地址:https://www.cnblogs.com/LGboy/p/11667942.html