c++类的三种继承方式的区别
来源:互联网 发布:破获特大网络售假药案 编辑:程序博客网 时间:2024/06/05 09:20
几种继承的区别
1.共有继承
基类的public和protected成员的访问属性在派生类中保持不变,但基类的private成员不可访问。
//Rectangle.h
class Point //基类Point类的声明
{public: //公有函数成员
void InitP(float xx=0, float yy=0)
{X=xx;Y=yy;}
void Move(float xOff, float yOff)
{X+=xOff;Y+=yOff;}
float GetX( ) {return X;}
float GetY( ) {return Y;}
private: //私有数据成员
float X,Y;
};
class Rectangle: public Point //派生类声明
{
public: //新增公有函数成员
void InitR(float x, float y, float w, float h)
{InitP(x,y);W=w;H=h;}//调用基类公有成员函数
float GetH( ) {return H;}
float GetW( ) {return W;}
private: //新增私有数据成员
float W,H;
};
#include<iostream.h>
#include<math.h>
#include“Rectangle.h”
int main( )
{ Rectangle rect;
rect.InitR(2,3,20,10);
//通过派生类对象访问基类公有成员
rect.Move(3,2);
cout<<rect.GetX( )<<','
<<rect.GetY( )<<','
<<rect.GetH( )<<','
<<rect.GetW( )<<endl;
return 0;
}
2.私有继承
基类的public和protected成员都以private身份出现在派生类中,但基类的private成员不可访问。
派生类中的成员函数可以直接访问基类中的public和protected成员,但不能访问基类的private成员。
通过派生类的对象不能访问基类中的任何成员。
举例:
例1、#include <iostream>
using namespace std;
class father
{
public:
void room(){cout<<"父亲的大房子我可以享受\n";}
};
class son:private father
{
public:
void enjoy(){room();}
};
int main()
{
son a;
a.enjoy();
return 0;
}
例2、#include<iostream.h>
#include<math.h>
class Point //基类Point类的声明
{public: //公有函数成员
void InitP(float xx=0, float yy=0)
{X=xx;Y=yy;}
void Move(float xOff, float yOff)
{X+=xOff;Y+=yOff;}
float GetX() {return X;}
float GetY(){return Y;}
private: //私有数据成员
float X,Y;
};
class Rectangle: private Point //派生类声明
{public: //新增外部接口
void InitR(float x, float y, float w, float h)
{InitP(x,y);W=w;H=h;}//访问基类公有成员
void Move(float xOff, float yOff) {Point::Move(xOff,yOff);}
float GetX(){return Point::GetX();}
float GetY(){return Point::GetY();}
float GetH(){return H;}
float GetW() {return W;}
private: //新增私有数据
float W,H;
};
int main()
{
//通过派生类对象只能访问本类成员
Rectangle rect;
rect.InitR(2,3,20,10);
rect.Move(3,2);
cout<<rect.GetX()<<','
<<rect.GetY()<<','
<<rect.GetH()<<','
<<rect.GetW()<<endl;
return 0;
}
3.保护继承(protected)
基类中的public和protected成员都以protected身份出现在派生类中,但基类的private成员不可访问。
派生类中的成员函数可以直接访问基类中的public和prtected成员,但不能访问基类的private成员
通过派生类的对象不能访问基类中的任何成员
protected成员的特点与作用
对建立其所在类的对象的模块来说(水平访问时),它与private成员的性质相同。
对于其派生类来说(垂直访问时),它与public成员的性质相同。
既实现了数据隐藏,又方便继承,实现代码的重用。(解释:这是面向对象
1、面向对象的四大特性:封装、继承、多态、抽象
2、面向对象的基本的数据结构:类和对象
封装是将数据隐藏在类中的方法:
private:
int a;//此时的a是类的私有成员,该类的对象无法访问,这实现了数据的隐藏(封装)
继承可以大大提高代码的重用性(复用))
例1、
class A {
protected:
int x;
}
class B: public A{
public:
void Function( );
};
void B::Function( )
{
X=5; //正确
}
1.共有继承
基类的public和protected成员的访问属性在派生类中保持不变,但基类的private成员不可访问。
派生类中的成员函数可以直接访问基类中的public和protected成员,但不能访问基类的private成员。
通过派生类的对象只能访问基类的public成员。
共有继承举例
//Rectangle.h
class Point //基类Point类的声明
{public: //公有函数成员
void InitP(float xx=0, float yy=0)
{X=xx;Y=yy;}
void Move(float xOff, float yOff)
{X+=xOff;Y+=yOff;}
float GetX( ) {return X;}
float GetY( ) {return Y;}
private: //私有数据成员
float X,Y;
};
class Rectangle: public Point //派生类声明
{
public: //新增公有函数成员
void InitR(float x, float y, float w, float h)
{InitP(x,y);W=w;H=h;}//调用基类公有成员函数
float GetH( ) {return H;}
float GetW( ) {return W;}
private: //新增私有数据成员
float W,H;
};
#include<iostream.h>
#include<math.h>
#include“Rectangle.h”
int main( )
{ Rectangle rect;
rect.InitR(2,3,20,10);
//通过派生类对象访问基类公有成员
rect.Move(3,2);
cout<<rect.GetX( )<<','
<<rect.GetY( )<<','
<<rect.GetH( )<<','
<<rect.GetW( )<<endl;
return 0;
}
2.私有继承
基类的public和protected成员都以private身份出现在派生类中,但基类的private成员不可访问。
派生类中的成员函数可以直接访问基类中的public和protected成员,但不能访问基类的private成员。
通过派生类的对象不能访问基类中的任何成员。
举例:
例1、#include <iostream>
using namespace std;
class father
{
public:
void room(){cout<<"父亲的大房子我可以享受\n";}
};
class son:private father
{
public:
void enjoy(){room();}
};
int main()
{
son a;
a.enjoy();
return 0;
}
例2、#include<iostream.h>
#include<math.h>
class Point //基类Point类的声明
{public: //公有函数成员
void InitP(float xx=0, float yy=0)
{X=xx;Y=yy;}
void Move(float xOff, float yOff)
{X+=xOff;Y+=yOff;}
float GetX() {return X;}
float GetY(){return Y;}
private: //私有数据成员
float X,Y;
};
class Rectangle: private Point //派生类声明
{public: //新增外部接口
void InitR(float x, float y, float w, float h)
{InitP(x,y);W=w;H=h;}//访问基类公有成员
void Move(float xOff, float yOff) {Point::Move(xOff,yOff);}
float GetX(){return Point::GetX();}
float GetY(){return Point::GetY();}
float GetH(){return H;}
float GetW() {return W;}
private: //新增私有数据
float W,H;
};
int main()
{
//通过派生类对象只能访问本类成员
Rectangle rect;
rect.InitR(2,3,20,10);
rect.Move(3,2);
cout<<rect.GetX()<<','
<<rect.GetY()<<','
<<rect.GetH()<<','
<<rect.GetW()<<endl;
return 0;
}
3.保护继承(protected)
基类中的public和protected成员都以protected身份出现在派生类中,但基类的private成员不可访问。
派生类中的成员函数可以直接访问基类中的public和prtected成员,但不能访问基类的private成员
通过派生类的对象不能访问基类中的任何成员
protected成员的特点与作用
对建立其所在类的对象的模块来说(水平访问时),它与private成员的性质相同。
对于其派生类来说(垂直访问时),它与public成员的性质相同。
既实现了数据隐藏,又方便继承,实现代码的重用。(解释:这是面向对象
1、面向对象的四大特性:封装、继承、多态、抽象
2、面向对象的基本的数据结构:类和对象
封装是将数据隐藏在类中的方法:
private:
int a;//此时的a是类的私有成员,该类的对象无法访问,这实现了数据的隐藏(封装)
继承可以大大提高代码的重用性(复用))
例1、
class A {
protected:
int x;
}
class B: public A{
public:
void Function( );
};
void B::Function( )
{
X=5; //正确
}
- 三种继承方式的区别
- 三种继承方式的区别
- C++三种继承方式的区别
- c++类的三种继承方式的区别
- private,public,protect三种继承方式的区别
- 派生类的三种继承方式
- 三类继承的区别
- 共有继承 私有继承 保护继承 派生类的三种继承方式小结
- 类的三种继承方式的访问权限.
- C++——三种继承方式与三种访问权限的相互组合
- C++——三种继承方式与三种访问权限的相互组合
- C++——三种继承方式与三种访问权限的相互组合
- C++的三种继承方式
- hibernate 实现继承的三种方式
- c++的三种继承方式
- Hibernate继承映射的三种方式
- c++的三种继承方式
- c++的三种继承方式
- 破解红蜘蛛 (破解红蜘蛛多媒体网络教室) 解除老师控制
- 补充 查看python安装位置
- Servlet高级应用----缓存设置
- Java定时器
- 自定义el函数
- c++类的三种继承方式的区别
- 网络安全基础篇之<十五>
- 基于堆实现的优先级队列:PriorityQueue 解决 Top K 问题
- DLL编写(VC,孙鑫MFC教程笔记)
- UIScrollView的属性总结
- orace 命令
- git blame
- 最原始网站回归网络
- Iphone应用开发之五: UIScrollView的详细讲解