龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > C/C++开发 >

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

时间:2014-10-01 02:20来源:网络整理 作者:网络 点击:
分享到:
这篇文章主要介绍了C++中关键字Struct和Class的区别,本文用大量实例讲解了Struct和Class的区别,需要的朋友可以参考下

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 http://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 http://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 http://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支持继承么?

精彩图集

赞助商链接