C++的类访问修饰符有public、private、protected
一个类可以有多个 public、protected 或 private 标记区域。
public:
公有成员在程序中类的外部是可访问的
上代码
/*================================================================
* Copyright (C) 2020 Sangfor Ltd. All rights reserved.
*
* 文件名称:3.cpp
* 创 建 者:陈 丁
* 创建日期:2020年05月03日
* 描 述:
*
================================================================*/
#include<iostream>
using namespace std;
class Line{
public:
double length;
void setLength(double len);
double getLength(void);
};
double Line::getLength(void){
return length;
}
void Line::setLength(double len){
length =len;
}
int main()
{
Line line;
line.setLength(6.0);
cout<<line.getLength()<<endl;
line.length=10.0;
cout<<line.length<<endl;
return 0;
}
private:
私有成员变量或函数在类的外部是不可访问的,甚至是不可查看的。只有类和友元函数可以访问私有成员。
默认情况下,类的所有成员都是私有的。
一般会在私有区域定义数据,在公有区域定义相关的函数,以便在类的外部也可以调用这些函数
上代码:
/*================================================================
* Copyright (C) 2020 Sangfor Ltd. All rights reserved.
*
* 文件名称:4.cpp
* 创 建 者:陈 丁
* 创建日期:2020年05月03日
* 描 述:
*
================================================================*/
#include<iostream>
using namespace std;
class Box{
public:
double length;
void setWidth(double wid);
double getWidth(void);
private:
double width;
};
double Box::getWidth(void){
return width;
}
void Box::setWidth(double wid){
width=wid;
}
int main()
{
Box box;
box.length=10.0;
cout<<"the length of box:"<<box.length<<endl;
box.setWidth(10.0);
cout<<"the width of box:"<<box.getWidth()<<endl;
return 0;
}
protected:
保护成员变量或函数与私有成员十分相似,但有一点不同,保护成员在派生类(即子类)中是可访问的。
上代码:
/*================================================================
* Copyright (C) 2020 Sangfor Ltd. All rights reserved.
*
* 文件名称:5.cpp
* 创 建 者:陈 丁
* 创建日期:2020年05月03日
* 描 述:
*
================================================================*/
#include<iostream>
using namespace std;
class Box{
protected:
double width;
};
class SmallBox:Box{
public:
void setSmallwidth(double wid);
double getSmalllwidth(void);
};
double SmallBox::getSmalllwidth(void){
return width;
}
void SmallBox::setSmallwidth(double wid){
width=wid;
}
int main()
{
SmallBox box;
box.setSmallwidth(5.0);
cout<<box.getSmalllwidth()<<endl;
return 0;
}
-
1.public 继承:基类 public 成员,protected 成员,private 成员的访问属性在派生类中分别变成:public, protected, private
-
2.protected 继承:基类 public 成员,protected 成员,private 成员的访问属性在派生类中分别变成:protected, protected, private
-
3.private 继承:基类 public 成员,protected 成员,private 成员的访问属性在派生类中分别变成:private, private, private
public继承:
上代码:
/*================================================================
* Copyright (C) 2020 Sangfor Ltd. All rights reserved.
*
* 文件名称:6.cpp
* 创 建 者:陈 丁
* 创建日期:2020年05月03日
* 描 述:
*
================================================================*/
#include<iostream>
#include<assert.h>
using namespace std;
class A{
public:
int a;
A(){
a1=1;
a2=2;
a3=3;
a=4;
}
void fun(){
cout<<a<<endl;
cout<<a1<<endl;
cout<<a2<<endl;
cout<<a3<<endl;
}
public:
int a1;
protected:
int a2;
private:
int a3;
};
class B:public A{
public:
int a;
B(int i){
A();
a=i;
}
void fun(){
cout<<a<<endl;
cout<<a1<<endl;
cout<<a2<<endl;
// cout<<a3<<endl;
}
};
int main()
{
B b(10);
cout<<b.a<<endl;
cout<<b.a1<<endl;
// cout<<b.a2<<endl;
// cout<<b.a3<<endl;
system("pause");
return 0;
}