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

作者:果冻想 时间:2022-12-23 11:25:06 

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 https://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 https://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 https://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 https://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 https://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 https://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&lt;&lt;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。

标签:C++,Struct,Class
0
投稿

猜你喜欢

  • C#设置页面单位和缩放的方法

    2023-06-09 12:59:27
  • Android实现手机震动抖动效果的方法

    2023-01-25 07:27:43
  • Java Comparable 和 Comparator 的详解及区别

    2023-07-05 10:41:44
  • Android动画 实现开关按钮动画(属性动画之平移动画)实例代码

    2023-01-02 23:06:30
  • Java Redis Redisson配置教程详解

    2022-10-13 06:32:39
  • android图片转换器示例

    2022-02-16 14:27:01
  • java基础之数组常用操作总结(必看篇)

    2022-12-09 03:32:17
  • 【C#基础】Substring截取字符串的方法小结(推荐)

    2021-12-21 06:22:32
  • unity 文件流读取图片与www读取图片的区别介绍

    2023-08-30 09:10:37
  • C# 3DES加密详解

    2022-12-01 05:58:55
  • Android实战APP启动速度优化

    2023-03-21 15:34:18
  • spring boot整合RabbitMQ实例详解(Fanout模式)

    2022-08-18 18:52:30
  • SpringBoot如何实现分离资源文件并打包

    2023-02-18 12:01:49
  • Android实现简单的自定义ViewGroup流式布局

    2022-03-16 10:21:23
  • Java如何在 Word 中设置上、下标

    2023-10-15 21:04:10
  • Android webview和js互相调用实现方法

    2022-06-03 13:11:59
  • Android使用VideoView出现无法播放此视频问题的解决方法

    2023-01-16 18:01:41
  • Java 堆内存溢出原因分析

    2023-06-27 08:34:23
  • 在Java中判断两个Long对象是否相等

    2022-09-01 11:22:10
  • Java单例模式实现静态内部类方法示例

    2021-08-03 00:36:51
  • asp之家 软件编程 m.aspxhome.com