The Wayback Machine - https://web.archive.org/web/20240813140426/https://www.geeksforgeeks.org/structure-vs-class-in-cpp/
Open In App

Difference Between Structure and Class in C++

Last Updated : 22 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.



Previous Article
Next Article

Similar Reads

Difference between Single Bus Structure and Double Bus Structure
1. Single Bus Structure: In a single bus structure, one common bus is used to communicate between peripherals and microprocessors. It has disadvantages due to the use of one common bus. 2. Double Bus Structure: In a double bus structure, one bus is used to fetch instructions while other is used to fetch data, required for execution. It is to overco
2 min read
Android Studio Project Structure VS Eclipse Project Structure
Eclipse is a standard package that is ideal for java and plug-in development, as well as introducing new plugins. Eclipse is mostly used for Java because it is basically developed in Java. One of the reasons Eclipse is well-known for its Java IDE is because of this. To personalize the environment, Eclipse includes its own workspace and a flexible p
4 min read
Difference between Structure chart and Flow chart
1. Structure Chart : Structure Chart represents the hierarchical structure of modules. It represents the software architecture that means the various modules making up the system and the dependency. Structure chart representation can be easily implemented using some common programming language. The main focus in the structure chart is on the module
2 min read
Difference between Web Content, Web Structure, and Web Usage Mining
Web mining is an application of the Data Mining technique that is used to find information patterns from the web data. Web Mining helps to improve the power of web search engines by identifying the web pages and classifying web documents. Types of Web Mining : 1. Web Content Mining - Web Content Mining can be used for the mining of useful data, inf
2 min read
Difference between Database and Data Structure
1. Database : A Database is a collection of data that is stored in an organized fashion in a table containing rows and columns using a software package known as Database Management System (DBMS). DBMS is used to modify, define, manipulate and manage data. Some examples of DBMS are : MySQL, Oracle Database and Microsoft Access. 2. Data Structure : I
2 min read
Difference between Control Structure and Control Statement
1. Control Structure :Control Structure, as name suggests, is basically a set of statements and control statements that are controlling their execution. 2. Control Statement :Control Statement, as name suggests, is basically a statement that is used to determine control flow of set of statements. It makes decision on basis of condition provided by
2 min read
Difference between Swift Structures and C Structure
Swift structures are a basic building block in the Swift programming language. They are used to group related data together in a single unit. They are similar to classes, but differ in several key ways, including: Value Types: Structures are value types, meaning that when you pass a structure to a function or assign it to a constant or variable, a
4 min read
Difference between data type and data structure
Data Type A data type is the most basic and the most common classification of data. It is this through which the compiler gets to know the form or the type of information that will be used throughout the code. So basically data type is a type of information transmitted between the programmer and the compiler where the programmer informs the compile
4 min read
Difference between Abstract Class and Concrete Class in Java
Abstract Class: An abstract class is a type of class in Java that is declared by the abstract keyword. An abstract class cannot be instantiated directly, i.e. the object of such class cannot be created directly using the new keyword. An abstract class can be instantiated either by a concrete subclass or by defining all the abstract method along wit
5 min read
Difference between Base class and Derived class in C++
Base Class: A base class is a class in Object-Oriented Programming language, from which other classes are derived. The class which inherits the base class has all members of a base class as well as can also have some additional properties. The Base class members and member functions are inherited to Object of the derived class. A base class is also
2 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg