Skip to main content
Engineering LibreTexts

7.4: Structures in C++ (cont'd)

  • Page ID
    34674
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    1. Member functions inside structure: Structures in C cannot have member functions inside structure but Structures in C++ can have member functions along with data members.
    2. Direct Initialization: We cannot directly initialize structure data members in C but we can do it in C++.
    // CPP program to initialize data member in c++ 
    #include <iostream>
    using namespace std;

    struct Record {
     int x = 7;
    };

    // Driver Program
    int main()
    {
     Record s;
     cout << s.x << endl;
     return 0;
    }
    // Output
    // 7
     
    1. Using struct keyword: In C, we need to use struct to declare a struct variable. In C++, struct is not necessary. For example, let there be a structure for Record. In C, we must use “struct Record” for Record variables. In C++, we need not use struct and using ‘Record‘ only would work.
    2. Static Members: C structures cannot have static members but is allowed in C++.
    // C++ program with structure static member

    struct Record {
       static int x;
    };

    // Driver program
    int main()
    {
        return 0;
    }

    1. Constructor creation in structure: Structures in C cannot have constructor inside structure but Structures in C++ can have Constructor creation.
    // C program to illustrate empty structure 
    #include <stdio.h>

    // empty structure
    struct Record {


    };
    1. sizeof operator: This operator will generate 0 for an empty structure in C whereas 1 for an empty structure in C++.
    // Driver program 
    int main()
    {
       struct Record s;
       printf("%d\n", sizeof(s));
       return 0;
    }

    Output in C++:

    1
    1. Data Hiding: C structures do not allow concept of Data hiding but is permitted in C++ as C++ is an object oriented language whereas C is not.
    2. Access Modifiers: C structures do not have access modifiers as these modifiers are not supported by the language. C++ structures can have this concept as it is inbuilt in the language.

     

    Adapted from: "Difference between C structures and C++ structures" by Gyanendra Singh Panwar is licensed under CC BY-SA 4.0


    This page titled 7.4: Structures in C++ (cont'd) is shared under a CC BY-SA license and was authored, remixed, and/or curated by Patrick McClanahan.

    • Was this article helpful?