zoukankan      html  css  js  c++  java
  • struct与class的区别(分别在c和c++中)

    注:以下内容未经博主同意,不得转载。


    解析:
    这里有两种情况下的区别。
    (1)C的struct与C++的class的区别。
    (2)C++中的struct和class的区别。
    在第一种情况下,struct与class有着非常明显的区别。C是一种过程化的语言,struct只是作为一种复杂数据类型定义,struct中只能定义成员变量,不能定义成员函数。例如下面的C代码片断:

     1 struct Point
     2 {
     3    int x; // 合法
     4    int y; // 合法
     5    void print()
     6     {
     7       printf("Point print
    "); //编译错误
     8    };
     9 };
    10  

    这里第7行会出现编译错误,提示如下的错误消息:“函数不能作为Point结构体的成员”。因此大家看到在第一种情况下struct只是一种数据类型,不能使用面向对象编程。现在来看第二种情况。首先请看下面的代码:

    #include <iostream>
    using namespace std;
    class CPoint
    {
          int x;                  //默认为private
          int y;                  //默认为private
          void print()            //默认为private
          {
              cout << "CPoint: (" << x << ", " << y << ")" << endl;
          }
        public:
            CPoint(int x, int y)      //构造函数,指定为public
              {
              this->x = x;
              this->y = y;
              }
           void print1() //public
           {
              cout << "CPoint: (" << x << ", " << y << ")" << endl;
           }
    };
    
    struct SPoint
    {
          int x;              //默认为public
          int y;              //默认为public
          void print()         //默认为public
          {
              cout << "SPoint: (" << x << ", " << y << ")" << endl;
          }
          SPoint(int x, int y)  //构造函数,默认为public
          {
              this->x = x;
              this->y = y;
          }
        private:
          void print1()      //private类型的成员函数
          {
             cout << "SPoint: (" << x << ", " << y << ")" << endl;
          }
    };
    
    int main(void)
    {
       CPoint cpt(1, 2);  //调用CPoint带参数的构造函数
       SPoint spt(3, 4);  //调用SPoint带参数的构造函数
           
       cout << cpt.x << " " << cpt.y << endl;  //编译错误
       cpt.print();       //编译错误
       cpt.print1();      //合法
    
       spt.print();      //合法
       spt.print1();     //编译错误
       cout << spt.x << " " << spt.y << endl;  //合法
    
       return 0;
    }

    在上面的程序里,struct还有构造函数和成员函数,其实它还拥有class的其他特性,例如继承、虚函数等。因此C++中的struct扩充了C的struct功能。那它们有什么不同呢?
    main函数内的编译错误全部是因为访问private成员而产生的。因此我们可以看到class中默认的成员访问权限是private的,而struct中则是public的。在类的继承方式上,struct和class又有什么区别?请看下面的程序:

     1 #include <iostream>
     2 using namespace std;
     3 class CBase
     4 {
     5     public:
     6       void print()                //public成员函数
     7        {
     8          cout << "CBase: print()..." << endl;
     9         }
    10 };
    11 class CDerived1 : CBase        //默认private继承
    12 {
    13 };
    14 
    15 class CDerived2 : public Cbase   //指定public继承
    16 {
    17 };
    18 
    19 struct SDerived1 : Cbase        //默认public继承
    20 {
    21 };
    22 
    23 struct SDerived2 : private Cbase  //指定public继承
    24 {
    25 };
    26 
    27 int main()
    28 {
    29   CDerived1 cd1;
    30   CDerived2 cd2;
    31   SDerived1 sd1;
    32   SDerived2 sd2;
    33 
    34   cd1.print();    //编译错误
    35   cd2.print();
    36   sd1.print();
    37   sd2.print();    //编译错误
    38 
    39   return 0;
    40 }

    可以看到,以private方式继承父类的子类对象不能访问父类的public成员。class继承默认是private继承,而struct继承默认是public继承。
    另外,在C++模板中,类型参数前面可以使用class或typename,如果使用struct,则含义不同,struct后面跟的是“non-type template parameter”,而class或typename后面跟的是类型参数。
    事实上,C++中保留struct的关键字是为了使C++编译器能够兼容C开发的程序。


    答案:
    分以下所示两种情况。
    C的struct与C++的class的区别:struct只是作为一种复杂数据类型定义,不能用于面向对象编程。
    C++中的struct和class的区别:对于成员访问权限以及继承方式,class中默认的是private的,而struct中则是public的。class还可以用于表示模板类型,struct则不行。
     

  • 相关阅读:
    (AIDE)Android Eclipse JNI 调用 .so文件加载问题
    VMware10.06精简版安装后台运行
    RAID磁盘阵列笔记
    高科固定电话机铃声
    嵌入式Linux驱动学习之路(十八)LCD驱动
    嵌入式Linux驱动学习之路(十七)驱动程序分层分离概念-平台设备驱动
    嵌入式Linux驱动学习之路(十六)输入子系统
    嵌入式Linux驱动学习之路(十五)按键驱动-定时器防抖
    嵌入式Linux驱动学习之路(十四)按键驱动-同步、互斥、阻塞
    嵌入式Linux驱动学习之路(十三)按键驱动-异步通知
  • 原文地址:https://www.cnblogs.com/tenjl-exv/p/7468977.html
Copyright © 2011-2022 走看看