Open In App
Related Articles

Difference Between Structure and Class in C++

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

In C++, a structure works the same way as a class, except for just two small differences. The most important of them is hiding implementation details. A structure will by default not hide its implementation details from whoever uses it in code, while a class by default hides all its implementation details and will therefore by default prevent the programmer from accessing them. The following table summarizes all of the fundamental differences.

Class

Structure

1. Members of a class are private by default.1. Members of a structure are public by default. 
2. An instance of a class is called an ‘object’.2. An instance of structure is called the ‘structure variable’.
3. Member classes/structures of a class are private by default but not all programming languages have this default behavior eg Java etc.3. Member classes/structures of a structure are public by default.
4. It is declared using the class keyword.4. It is declared using the struct keyword.
5. It is normally used for data abstraction and further inheritance.5. It is normally used for the grouping of data
6. NULL values are possible in Class.6. NULL values are not possible.

7. Syntax:

 class class_name{

         data_member;

         member_function;

  };

7. Syntax:

   struct structure_name{

         type structure_member1;

         type structure_member2;

   };

Some examples that elaborate on these differences:

1) Members of a class are private by default and members of a structure are public by default. 

For example, program 1 fails in compilation but program 2 works fine, 

Program 1:

C++

// C++ Program to demonstrate that
// Members of a class are private
// by default
#include <iostream>
 
using namespace std;
 
class Test {
    // x is private
    int x;
};
 
int main()
{
    Test t;
    // compiler error because x
    // is private
    t.x = 20;
 
    return t.x;
}

                    

Time Complexity: O(1)
Auxiliary Space: O(1)

 
 Output:

./cf03c8d1-d4a3-43ea-a058-fe5b5303167b.cpp: In function 'int main()':
./cf03c8d1-d4a3-43ea-a058-fe5b5303167b.cpp:10:9: error: 'int Test::x' is private
     int x;
         ^
./cf03c8d1-d4a3-43ea-a058-fe5b5303167b.cpp:18:7: error: within this context
     t.x = 20;
       ^
./cf03c8d1-d4a3-43ea-a058-fe5b5303167b.cpp:10:9: error: 'int Test::x' is private
     int x;
         ^
./cf03c8d1-d4a3-43ea-a058-fe5b5303167b.cpp:20:14: error: within this context
     return t.x;
              ^

Program 2: 

C++

// C++ Program to demonstrate that
// members of a structure are public
// by default
#include <iostream>
 
using namespace std;
 
struct Test {
    // x is public
    int x;
};
 
int main()
{
    Test t;
    t.x = 20;
 
    // works fine because x is public
    cout << t.x;
}

                    

Output
20


Time Complexity: O(1)
Auxiliary Space: O(1)

2) A class is declared using the class keyword, and a structure is declared using the struct keyword.

Syntax: 

class ClassName {
private:
    member1;
    member2;

public:
    member3;
    .
    .
    memberN;
};

Syntax: 

struct StructureName {
    member1;
    member2;
    .
    .
    .
    memberN;
};

3) Inheritance is possible with classes, and with structures

For example, programs 3 and 4 work fine.

Program 3:

C++

// C++ Program to demonstrate
// Inheritance with classes.
#include <iostream>
 
using namespace std;
 
// Base class
class Parent {
public:
    int x;
};
 
// Subclass inheriting from
// base class (Parent).
class Child : public Parent {
public:
    int y;
};
 
int main()
{
    Child obj1;
 
    // An object of class Child has
    // all data members and member
    // functions of class Parent.
    obj1.y = 7;
    obj1.x = 91;
    cout << obj1.y << endl;
    cout << obj1.x << endl;
 
    return 0;
}

                    

Output
7
91


Time Complexity: O(1)
Auxiliary Space: O(1)

Program 4:

C++

// C++ Program to demonstrate
// Inheritance with structures.
#include <iostream>
 
using namespace std;
 
struct Base {
public:
    int x;
};
 
// is equivalent to
// struct Derived : public Base {}
struct Derived : Base {
public:
    int y;
};
 
int main()
{
    Derived d;
 
    // Works fine because inheritance
    // is public.
    d.x = 20;
    cout << d.x;
    cin.get();
    return 0;
}

                    

 
Output

20


Time Complexity: O(1)
Auxiliary Space: O(1)

To know about the Difference between C structures and C++ structures refer to this article.



Last Updated : 22 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads