c++ 类访问修饰符
c++ 类 & 对象
数据封装是面向对象编程的一个重要特点,它防止函数直接访问类类型的内部成员。类成员的访问限制是通过在类主体内部对各个区域标记 public、private、protected 来指定的。关键字 public、private、protected 称为访问修饰符。
一个类可以有多个 public、protected 或 private 标记区域。每个标记区域在下一个标记区域开始之前或者在遇到类主体结束右括号之前都是有效的。成员和类的默认访问修饰符是 private。
class base {
public:
protected:
private:
};
公有(public)成员
公有成员在程序中类的外部是可访问的。您可以不使用任何成员函数来设置和获取公有变量的值,如下所示:
实例
#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 << "length of line : " << line.getlength() <<endl;
line.length = 10.0; cout << "length of line : " << line.length <<endl;
return 0;}
当上面的代码被编译和执行时,它会产生下列结果:
length of line : 6
length of line : 10
私有(private)成员
私有成员变量或函数在类的外部是不可访问的,甚至是不可查看的。只有类和友元函数可以访问私有成员。
默认情况下,类的所有成员都是私有的。例如在下面的类中,width 是一个私有成员,这意味着,如果您没有使用任何访问修饰符,类的成员将被假定为私有成员:
实例
class box { double width;
public:
double length;
void setwidth( double wid );
double getwidth( void );};
实际操作中,我们一般会在私有区域定义数据,在公有区域定义相关的函数,以便在类的外部也可以调用这些函数,如下所示:
实例
#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 << "length of box : " << box.length <<endl;
box.setwidth(10.0); cout << "width of box : " << box.getwidth() <<endl;
return 0;}
当上面的代码被编译和执行时,它会产生下列结果:
length of box : 10
width of box : 10
protected(受保护)成员
protected(受保护)成员变量或函数与私有成员十分相似,但有一点不同,protected(受保护)成员在派生类(即子类)中是可访问的。
在下一个章节中,您将学习到派生类和继承的知识。现在您可以看到下面的实例中,我们从父类 box 派生了一个子类 smallbox。
下面的实例与前面的实例类似,在这里 width 成员可被派生类 smallbox 的任何成员函数访问。
实例
#include <iostream> using namespace std;
class box { protected:
double width;};
class smallbox:box { public:
void setsmallwidth( double wid );
double getsmallwidth( void );};
double smallbox::getsmallwidth(void) { return width ;} void smallbox::setsmallwidth( double wid ) { width = wid;} int main( ) { smallbox box;
box.setsmallwidth(5.0);
cout << "width of box : "<< box.getsmallwidth() << endl;
return 0;}
当上面的代码被编译和执行时,它会产生下列结果:
width of box : 5
继承中的特点
有public, protected, private三种继承方式,它们相应地改变了基类成员的访问属性。
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 继承
实例
#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;}
protected 继承
实例
#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 : protected 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;}
private 继承
实例
#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 : private 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;}
【说明】:
本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!