• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C语言 > C++中关键字Struct和Class的区别

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

作者:果冻想 字体:[增加 减小] 来源:互联网 时间:2017-05-28

果冻想 通过本文主要向大家介绍了c++struct,c++ struct用法,c++中struct,c++ typedef struct,c++中struct的用法等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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.weikejianghu.com
*/
 
#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;
}
</div>

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

Struct有自己的构造函数吗?

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

/*
** FileName     : StructAndClassDiffDemo
** Author       : Jelly Young
** Date         : 2013/12/7
** Description  : More information, please go to http://www.weikejianghu.com
*/
 
#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;
}
</div>

Struct可以有析构函数么?

让我来验证一下:

/*
** FileName     : StructAndClassDiffDemo
** Author       : Jelly Young
** Date         : 2013/12/7
** Description  : More information, please go to http://www.weikejianghu.com
*/
 
#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;
}
</div>

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

Struct支持继承么?

再让我写代码验证一下:
/*
** FileName     : StructAndClassDiffDemo
** Author       : Jelly Young
** Date         : 2013/12/7
** Description  : More information, please go to http://www.weikejianghu.com
*/
 
#include <iostream>
using namespace std;
 
struct A
{
    int a;
    A()
    {
     

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • C++使struct对象拥有可变大小的数组(详解)
  • 详解C++程序中定义struct结构体的方法
  • 深入剖析C++中的struct结构体字节对齐
  • C++中声明类的class与声明结构体的struct关键字详解
  • c++中struct使用注意事项
  • C++中关键字Struct和Class的区别
  • 浅析c与c++中struct的区别
  • 深入C++中struct与class的区别分析

相关文章

  • 2017-05-28C++的static关键字及变量存储位置总结
  • 2017-05-28c++中虚函数的实现详解
  • 2017-05-28C语言中二维数组指针的简要说明
  • 2017-05-28循环队列详解及队列的顺序表示和实现
  • 2017-05-28C++链表倒序实现方法
  • 2017-05-28linux c多线程编程实例代码
  • 2017-05-28快速学习C语言中for循环语句的基本使用方法
  • 2017-05-28C语言中变量与其内存地址对应的入门知识简单讲解
  • 2017-05-28详解C++中StringBuilder类的实现及其性能优化
  • 2017-05-28详解C++中的一维数组和二维数组

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • C++实现简单的信息管理系统
    • MFC控件大小随窗体大小而改变
    • c++显式类型转换示例详解
    • C语言究竟是一门怎样的语言?
    • 线程池的原理与实现详解
    • 常用C/C++预处理指令详解
    • 详解C语言中getgid()函数和getegid()函数的区别
    • C语言从txt文件中逐行读入数据存到数组中的实现方法
    • hdoj 1533 && poj 2195 Going Home
    • C++中用指向数组的指针作函数参数

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有