• 01 浅谈c++及面向对象编程


    参考链接:

    学习完c++但是对c++面向对象编程还是比较模糊,现在花时间总体来总结一下:

    c++中的对象是使用来定义的,下面先重点讲一下类的概念。

    说到类就要先说一下类的三种特性:封装,继承,多态。

    封装性:事物的封闭性。

    作用:

    它防止函数直接访问类类型的内部成员,他是通过类主体内部对各个区域标记 public、private、protected 来指定的。

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

    public:公有成员

    在程序中类的外部是可访问的。例如:

     1 #include <iostream>
     2  
     3 using namespace std;
     4  
     5 class Line
     6 {
     7    public:
     8       double length;
     9       void setLength( double len );
    10       double getLength( void );
    11 };
    12  
    13 // 成员函数定义
    14 double Line::getLength(void)
    15 {
    16     return length ;
    17 }
    18  
    19 void Line::setLength( double len )
    20 {
    21     length = len;
    22 }
    23  
    24 // 程序的主函数
    25 int main( )
    26 {
    27    Line line;
    28  
    29    // 设置长度
    30    line.setLength(6.0); 
    31    cout << "Length of line : " << line.getLength() <<endl;
    32  
    33    // 不使用成员函数设置长度
    34    line.length = 10.0; // OK: 因为 length 是公有的
    35    cout << "Length of line : " << line.length <<endl;
    36    return 0;
    37 }
    View Code

    私有成员:private

    成员变量或函数在类的外部是不可访问的,甚至是不可查看的。只有类和友元函数可以访问私有成员。默认情况下,类的所有成员都是私有的。struct中默认是公有的。

    1 //代码中width就是私有的
    2 class Box
    3 {
    4    double width;
    5    public:
    6       double length;
    7       void setWidth( double wid );
    8       double getWidth( void );
    9 };
    View Code

    一般在私有区域定义数据,公有区域定义函数,方便在类外也可以调用这些函数。

     1 #include <iostream>
     2  
     3 using namespace std;
     4  
     5 class Box
     6 {
     7    public:
     8       double length;
     9       void setWidth( double wid );
    10       double getWidth( void );
    11  
    12    private:
    13       double width;
    14 };
    15  
    16 // 成员函数定义
    17 double Box::getWidth(void)
    18 {
    19     return width ;
    20 }
    21  
    22 void Box::setWidth( double wid )
    23 {
    24     width = wid;
    25 }
    26  
    27 // 程序的主函数
    28 int main( )
    29 {
    30    Box box;
    31  
    32    // 不使用成员函数设置长度
    33    box.length = 10.0; // OK: 因为 length 是公有的
    34    cout << "Length of box : " << box.length <<endl;
    35  
    36    // 不使用成员函数设置宽度
    37    // box.width = 10.0; // Error: 因为 width 是私有的
    38    box.setWidth(10.0);  // 使用成员函数设置宽度
    39    cout << "Width of box : " << box.getWidth() <<endl;
    40  
    41    return 0;
    42 }
    View Code

    保护成员:protected

    保护成员变量或函数与私有成员十分相似,但有一点不同,保护成员在派生类(即子类)中是可访问的。

     1 #include <iostream>
     2 using namespace std;
     3  
     4 class Box
     5 {
     6    protected:
     7       double width;
     8 };
     9  
    10 class SmallBox:Box // SmallBox 是派生类
    11 {
    12    public:
    13       void setSmallWidth( double wid );
    14       double getSmallWidth( void );
    15 };
    16  
    17 // 子类的成员函数
    18 double SmallBox::getSmallWidth(void)
    19 {
    20     return width ;
    21 }
    22  
    23 void SmallBox::setSmallWidth( double wid )
    24 {
    25     width = wid;
    26 }
    27  
    28 // 程序的主函数
    29 int main( )
    30 {
    31    SmallBox box;
    32  
    33    // 使用成员函数设置宽度
    34    box.setSmallWidth(5.0);
    35    cout << "Width of box : "<< box.getSmallWidth() << endl;
    36  
    37    return 0;
    38 }
    View Code

    友元函数将封装性打破了,使用要慎重。

    继承中的特点

    有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

    但无论哪种继承方式,上面两点都没有改变:

    • 1.private 成员只能被本类成员(类内)和友元访问,不能被派生类访问;

    • 2.protected 成员可以被派生类访问。

  • 相关阅读:
    Markdown基本语法
    Linux之更好的使用Bash
    博客园-使用的模板
    井字棋的最优策略竟是先占角!
    Highcharts入门+Highcharts基础教程,【非常值得学习的资料】
    Python4周 入职培训的过程
    Linux系统入门学习:在CentOS上安装phpMyAdmin
    WeCenter程序安装
    js正则表达式语法
    Sublime text2如何设置快捷键让编写的HTML文件在浏览器预览?
  • 原文地址:https://www.cnblogs.com/fanhua666/p/11512681.html
Copyright © 2020-2023  润新知