C++ 类的定义和访问修饰符

前沿

类的定义,以及类访问修饰符。

类的定义

#include <iostream>
using namespace std;

class Box
{
    public:
        double length; // 盒子的长度
        double breadth; // 盒子的宽度
        double height; // 盒子的高度

        // member function
        double get(void);
        void set(double len, double bre, double hei);
};
// 成员函数定义
double Box::get(void)
{
    return length * breadth * height;
}
void Box::set(double len,double bre,double hei)
{
    length = len;
    breadth = bre;
    height = hei;
}
int main()
{
    // 声名类对象
    Box Box1;
    Box Box2;
    Box Box3;
    double volume = 0.0; // store volume

    //Box1
    Box1.height = 5.0;
    Box1.breadth = 6.0;
    Box1.length = 7.0;

    // Box2
    Box2.height = 10.0;
    Box2.length = 12.0;
    Box2.breadth = 13.0;

    // the volume of Box1
    volume = Box1.height * Box1.breadth * Box1.length;
    cout << "the volume of the Box1 is :  " << volume << endl;

    // the volume of Box2
    volume = Box2.height * Box2.breadth * Box2.length;
    cout << "the volume of the Box2 is : " << volume << endl;

    // Box3
    Box3.set(16.0,8.0,12.0);
    volume = Box3.get();
    cout << "the volume of Box3 is : " << volume << endl;

    return 0;
}

可以注意到,类中定义的public 变量,可以在类外的实现的函数中使用。

所有的属性最好设置为私有 (private)
然后提供公共的接口(public)

二、访问修饰符

访问修饰符: publicprivateprotected

Note:

  1. 一个类可以有多个 public 、 protected 和private 标记区域
  2. 标记作用范围: 每个标记区域在下一个标记区域开始之前或者在遇到 类主体 结束右括号之前,都是有效的。
  3. 成员和类的默认访问修饰符是private

2.1 公有成员 public

公有成员在程序中类的外部是可访问的。您可以不使用任何成员函数来设置和获取公有变量的值。

#include <iostream> 

using namespace std;

class Line
{
    public:
        double length;
        void setLength(double len);
        double getLength(void);
};

// define member function
void Line::setLength(double len)
{
    length = len;
}

double Line::getLength(void)
{
    return length;
}

int main()
{
    Line line;

    // set length
    line.setLength(6.0);
    cout << "Length of line is: " << line.getLength() << endl;

    // set length without using member function
    line.length = 10.0;
    cout << "Length of line is :" << line.length << endl;

    return 0;
}

公有变量可以通过直接成员访问运算符 . 来访问。

2.2 私有变量 private

私有成员变量或函数在类的外部是不可访问的,甚至不可查看的。

只有类和友元函数可以访问私有成员

默认情况,类的所有成员都是私有的。

对于私有变量,一般会在私有区域定义数据,在公有区域定义函数,以便在类的外部也可以调用这些函数。

#include <iostream>

using namespace std;

class Box
{
    private:
        double width;

    public:
        double length;
        void setWidth(double wid);
        double getWidth(void);
};

// define member function
double Box::setWidth(double wid)
{
    width = wid;
}
double Box::getwidth(void)
{
    return width;
}

int main()
{
    Box box;

    // set length without using member function
    box.length = 10.0; // length is public
    cout << "Length of box: " << box.length << endl;

    // set width without using member function
    box.width = 10.0;

    return 0;
}

报错:因为width 是私有的。

修改:

    box.setWidth(10.0);
    cout << "width of box : " << box.getWidth() << endl;

2.3 受保护的成员 protected

protected 和 private 相似, 不同的是: protected 成员 在派生类(即子类)中是可以访问的。


示例:从父类Box中派生了一个子类smallBox。其中,width 成员可以被派生类 samllBox中的任何成员函数访问。

#include <iostream>
using namespace std;
class Box
{
protected:
    double width;
};

class SmallBox : Box // SmallBox is derived class
{
public:
    void setSmallWidth(double wid);
    double getSmallWidth(void);
};

// the member function of child class
double SmallBox::getSmallWidth(void)
{
    return width;
}

void SmallBox::setSmallWidth(double wid)
{
    width = wid;
}

int main()
{
    SmallBox box;

    // set width with member function
    box.setSmallWidth(5.0);
    cout << "Width of box : " << box.getSmallWidth() << endl;

    return 0;
}

注: 继承用:,类外实现用::

2.4 访问修饰符在继承中的特点。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值