The Wayback Machine - https://web.archive.org/web/20220130082811/https://www.geeksforgeeks.org/structure-vs-class-in-cpp/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Structure vs class in C++

  • Difficulty Level : Easy
  • Last Updated : 26 Jan, 2022

In C++, a structure is the same as a class except for a few differences. The most important of them is security. A Structure is not secure and cannot hide its implementation details from the end-user while a class is secure and can hide its programming and designing details. Following are some differences between a class and a structure.

Class

Structure

Members of a class are private by default.Members of a structure are public by default. 
Memory allocation happens on the heap.Memory allocation happens on a stack.
It is a reference type data type.It is a value type data type.
It is declared using the class keyword.It is declared using the struct keyword.

Some of the points 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, 

CPP




// Program 1
// CPP Program to demonstrate that Members of a class are
// private by default
#include <bits/stdc++.h>
using namespace std;
 
class Test {
    // x is private
    int x;
};
 
// Driver Code
int main()
{
    Test t;
    t.x = 20;
    // compiler error because x is private
    getchar();
    return 0;
}

 
 

Output

 

prog.cpp: In function ‘int main()’:
prog.cpp:8:9: error: ‘int Test::x’ is private
    int x;
        ^
prog.cpp:13:7: error: within this context
    t.x = 20;
      ^

 

CPP




// Program 2
// CPP Program to demonstrate that members of a structure
// are public by default. 
#include <bits/stdc++.h>
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;
    getchar();
    return 0;
}

 
 

Output
20

 

2) Class is declared using the class keyword, and 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 in class, and in structures.

 

For example, program 3 and 4 works fine, 

 

C++




// Program 3
// C++ program to demonstrate Inheritance in class
#include <bits/stdc++.h>
using namespace std;
 
// Base class
class Parent {
public:
    int x;
};
 
// Sub class inheriting from Base Class(Parent)
class Child : public Parent {
public:
    int y;
};
 
// Driver Code
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

 

C++




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

 
 

Must Read: Difference between C structures and C++ structures
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 

 

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!