C++之类访问修饰符

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值