C++中关键字Struct和Class的区别

 更新时间:2020年4月25日 17:40  点击:1540

Struct和Class的区别

今天这篇博文主要讲解在C++中关键字struct和class的区别。这篇博文,将会系统的将这两个关键字的不同面进行详细的讲解。

从语法上来讲,class和struct做类型定义时只有两点区别:

1.默认继承权限,如果不指定,来自class的继承按照private继承处理,来自struct的继承按照public继承处理;

2.成员的默认访问权限。class的成员默认是private权限,struct默认是public权限。以上两点也是struct和class最基本的差别,也是最本质的差别;

但是在C++中,struct进行了扩展,现在它已经不仅仅是一个包含不同数据类型的数据结构了,它包括了更多的功能。

Struct能包含成员函数吗?

是的,答案是肯定的。现在就让我写一段代码验证一下:

复制代码 代码如下:

/*
** FileName     : StructAndClassDiffDemo
** Author       : Jelly Young
** Date         : 2013/12/7
** Description  : More information, please go to //www.jb51.net
*/
 
#include <iostream>
using namespace std;
 
struct Test
{
    int a;
    int getA()
    {
        return a;
    }
 
    void setA(int temp)
    {
        a = temp;
    }
};
 
int main(int argc, char* argv[])
{
    Test testStruct;
    testStruct.setA(10);
    cout<<"Get the value from struct:"<<testStruct.getA()<<endl;
 
    Test *testStructPointer = new Test;
    testStructPointer->setA(20);
    cout<<"Get the value from struct again:"<<testStructPointer->getA()<<endl;
    delete testStructPointer;
 
    return 0;
}

以上的代码会很正确的运行,是的;没错,struct能包含成员函数的。

Struct有自己的构造函数吗?

是的,可以的。看以下测试代码:

复制代码 代码如下:

/*
** FileName     : StructAndClassDiffDemo
** Author       : Jelly Young
** Date         : 2013/12/7
** Description  : More information, please go to //www.jb51.net
*/
 
#include <iostream>
using namespace std;
 
struct Test
{
    int a;
 
    Test()
    {
        a = 100;
    }
 
    int getA()
    {
        return a;
    }
 
    void setA(int temp)
    {
        a = temp;
    }
};
 
int main(int argc, char* argv[])
{
    Test testStruct;
    testStruct.setA(10);
    cout<<"Get the value from struct:"<<testStruct.getA()<<endl;    
        Test *testStructPointer = new Test;
    testStructPointer->setA(20);
    cout<<"Get the value from struct again:"<<testStruct.getA()<<endl;
    delete testStructPointer;
 
    // test the constructor
    Test testConstructor;
    cout<<"Set the value by the construct and get it:"<<testConstructor.getA()<<endl;
 
    return 0;
}

Struct可以有析构函数么?

让我来验证一下:

复制代码 代码如下:

/*
** FileName     : StructAndClassDiffDemo
** Author       : Jelly Young
** Date         : 2013/12/7
** Description  : More information, please go to //www.jb51.net
*/
 
#include <iostream>
using namespace std;
 
struct Test
{
    int a;
 
    Test()
    {
        a = 100;
    }
 
    int getA()
    {
        return a;
    }
 
    void setA(int temp)
    {
        a = temp;
    }
 
    ~Test()
    {
        cout<<"Destructor function called."<<endl;
    }
};
 
int main(int argc, char* argv[])
{
    Test testStruct;
    testStruct.setA(10);
    cout<<"Get the value from struct:"<<testStruct.getA()<<endl;    
        Test *testStructPointer = new Test;    
        testStructPointer->setA(20);
    cout<<"Get the value from struct again:"<<testStruct.getA()<<endl;
    delete testStructPointer;
 
    // test the constructor
    Test testConstructor;
    cout<<"Set the value by the construct and get it:"<<testConstructor.getA()<<endl;
 
    return 0;
}

是的,完全支持析构函数。

Struct支持继承么?

再让我写代码验证一下:

复制代码 代码如下:

/*
** FileName     : StructAndClassDiffDemo
** Author       : Jelly Young
** Date         : 2013/12/7
** Description  : More information, please go to //www.jb51.net
*/
 
#include <iostream>
using namespace std;
 
struct A
{
    int a;
    A()
    {
        a = 10;
    }
    void print()
    {
        cout<<"I am from A"<<endl;
    }
};
 
struct B : A
{
    int b;
    B()
    {
        a = 30; // set a to 30
        b = 20;
    }
    /*void print()
    {
    cout<<"I am from B"<<endl;
    }*/
};
 
int main(int argc, char* argv[])
{
    B b1;
    cout<<b1.a<<endl;
    cout<<b1.b<<endl;
    b1.print();
 
    A a1;
    cout<<a1.a<<endl;
    a1.print();
 
    return 0;
}

运行上述代码,struct支持继承。

Struct支持多态么?

写代码测试一下便知:

复制代码 代码如下:

/*
** FileName     : StructAndClassDiffDemo
** Author       : Jelly Young
** Date         : 2013/12/7
** Description  : More information, please go to //www.jb51.net
*/
 
#include <iostream>
using namespace std;
 
struct A
{
    virtual void print() = 0;
};
 
struct B : A
{
    void print()
    {
        cout<<"I am from B"<<endl;
    }
};
 
struct C : A
{
    void print()
    {
        cout<<"I am from C"<<endl;
    }
};
 
int main(int argc, char* argv[])
{    
    A *a1;    
    B *b1 = new B;    
    C *c1 = new C;    
    a1 = b1;    
    a1->print(); // call B, not A
 
    a1 = c1;
    a1->print(); // call C, not A
 
    return 0;
}

Struct支持Private、Protected和Public关键字么?

复制代码 代码如下:

/*
** FileName     : StructAndClassDiffDemo
** Author       : Jelly Young
** Date         : 2013/12/7
** Description  : More information, please go to //www.jb51.net
*/
 
#include <iostream>
using namespace std;
 
struct A
{
private:
    int b;
 
protected:
    int c;
 
public:
    A()
    {
        b = 10;
        c = 20;
        d = 30;
    }
 
    int d;
};
 
struct B : A
{
    void printA_C()
    {
        cout<<A::c<<endl;
    };
 
    // private member can not see
    /*void printA_B()
    {
    cout<<A::b<<endl;
    }*/
 
    void printA_D()
    {
        cout<<A::d<<endl;
    }
};
 
int main(int argc, char* argv[])
{
 
    A a1;
    B b1;
 
    // private member can not see
    //cout<<a1.b<<endl;
 
    // protected member can not see
    //cout<<a1.c<<endl;
 
    // public member can see
    cout<<a1.d<<endl;
 
    return 0;
}

写了这么多了,那么会出现这种一个状况,如果是class的父类是struct关键字描述的,那么默认访问属性是什么?

当出现这种情况时,到底默认是public继承还是private继承,取决于子类而不是基类。class可以继承自struct修饰的类;同时,struct也可以继承自class修饰的类,继承属性如下列描述:

复制代码 代码如下:

class B:A{}; // private 继承
 
class A{};
struct B:A{}; // public 继承

最后,那么到底是使用struct,还是使用class呢?这个看个人喜好,但是这里有一个编程规范的问题,当你觉得你要做的更像是一种数据结构的话,那么用struct,如果你要做的更像是一种对象的话,那么用class。

[!--infotagslink--]

相关文章

  • C++ STL标准库std::vector的使用详解

    vector是表示可以改变大小的数组的序列容器,本文主要介绍了C++STL标准库std::vector的使用详解,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2022-03-06
  • C++中取余运算的实现

    这篇文章主要介绍了C++中取余运算的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
  • 详解C++ string常用截取字符串方法

    这篇文章主要介绍了C++ string常用截取字符串方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
  • C++调用C#的DLL程序实现方法

    本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
  • C++中四种加密算法之AES源代码

    本篇文章主要介绍了C++中四种加密算法之AES源代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。...2020-04-25
  • PHP Fatal error: Cannot use object of type stdClass as array in错误

    下面一起来看看在php开发中碰到PHP Fatal error: Cannot use object of type stdClass as array in错误问题的解决办法吧。 普通的数组出现如下错误 代码...2016-11-25
  • C++ 整数拆分方法详解

    整数拆分,指把一个整数分解成若干个整数的和。本文重点给大家介绍C++ 整数拆分方法详解,非常不错,感兴趣的朋友一起学习吧...2020-04-25
  • C++中 Sort函数详细解析

    这篇文章主要介绍了C++中Sort函数详细解析,sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变...2022-08-18
  • C++万能库头文件在vs中的安装步骤(图文)

    这篇文章主要介绍了C++万能库头文件在vs中的安装步骤(图文),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
  • 详解C++ bitset用法

    这篇文章主要介绍了C++ bitset用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
  • 浅谈C++中的string 类型占几个字节

    本篇文章小编并不是为大家讲解string类型的用法,而是讲解我个人比较好奇的问题,就是string 类型占几个字节...2020-04-25
  • C++ Eigen库计算矩阵特征值及特征向量

    这篇文章主要为大家详细介绍了C++ Eigen库计算矩阵特征值及特征向量,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-04-25
  • 浅析C# 结构体struct

    这篇文章主要介绍了C# 结构体struct 的相关资料,帮助大家更好的理解和学习c#,感兴趣的朋友可以了解下...2020-11-03
  • C++ pair的用法实例详解

    这篇文章主要介绍了C++ pair的用法实例详解的相关资料,需要的朋友可以参考下...2020-04-25
  • VSCode C++多文件编译的简单使用方法

    这篇文章主要介绍了VSCode C++多文件编译的简单使用方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-03-29
  • C++中的循环引用

    虽然C++11引入了智能指针的,但是开发人员在与内存的斗争问题上并没有解放,如果我门实用不当仍然有内存泄漏问题,其中智能指针的循环引用缺陷是最大的问题。下面通过实例代码给大家介绍c++中的循环引用,一起看看吧...2020-04-25
  • C++随机点名生成器实例代码(老师们的福音!)

    这篇文章主要给大家介绍了关于C++随机点名生成器的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
  • C++如何删除map容器中指定值的元素详解

    map容器是C++ STL中的重要一员,删除map容器中value为指定元素的问题是我们经常与遇到的一个问题,下面这篇文章主要给大家介绍了关于利用C++如何删除map容器中指定值的元素的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。...2020-04-25
  • C++ 约瑟夫环问题案例详解

    这篇文章主要介绍了C++ 约瑟夫环问题案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下...2021-08-15
  • C++中cin的用法详细

    这篇文章主要介绍了C++中cin的用法详细,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25