Difference Between Structure and Class in C++

structure and class difference

In C++, both structures and classes are used to define custom data types that can hold multiple data members and member functions. While they share some similarities, there are a few key differences between structures and classes. Here in this article, we are going to understand the key differences between them and which one we should use in which conditions. But before discussing the difference we should have the basic idea of structure and class in C++.


What is Structure in C++?

In C++, a structure is a user-defined data type that allows you to group related data elements together. It provides a way to create a composite data structure that can hold multiple variables of different types. 


Structure Syntax:

struct StructureName {
    // Member declarations
    DataType1 member1;
    DataType2 member2;
    // ...
};

The struct keyword is used to define a structure and StructureName is the name given to the structure. Inside the curly braces {} you declare the member of the structure.

What is Class in C++?

In C++, a class is a user-defined data type that encapsulates data and functions together. It provides a blueprint for creating objects and defines their behavior and properties.

Class Syntax:
class ClassName {
    // Member declarations
    AccessSpecifier1:
        DataType1 member1;
        DataType2 member2;
        // ...

    AccessSpecifier2:
        FunctionReturnType functionName1(ParameterList);
        FunctionReturnType functionName2(ParameterList);
        // ...
};

The class keyword is used to define a Class and the ClassName is the name given to the class. Inside the curly braces {}, you declare the members of the class. Members can include data members (variables) and member functions (methods). AccessSpecifier is to determine the accessibility of the members within the class.  

Difference Between Structure and Class.

While they have some similarities, there are a few key differences between structures and classes and we are going to discuss each of them in detail.

Structure Class
Members are public by default. Members are private by default.
Structure does not support inheritance. A class supports single and multiple inheritances.
Does not have access specifiers (private, protected). Access specifiers can be used (public, private, protected).
A structure cannot have member functions. A class can have member functions.
A structure cannot have constructors or destructors. A class can have constructors and destructors.
Memory is allocated for each instance separately. Memory is allocated once and shared among instances.
Used for simple data structures or data containers. Used for complex objects with behavior and properties.

1. Default Member Accessibility.

In a structure, by default, all members (data and functions) are public. This means that they can be accessed from outside the structure without any restrictions. 

C++ Structure Example Code:
//C++ code Structure default Accessibility
#include <iostream>
using namespace std;

// Define a structure named Point
struct Point {
    int x;
    int y;
};

int main() {
    // Declare a variable of type Point
    Point p1;

    // Access and assign values to the members
    p1.x = 10;
    p1.y = 20;

    // Display the values of the members
    cout << "x: " << p1.x << endl;
    cout << "y: " << p1.y << endl;

    return 0;
}
Output:
x: 10
y: 20

In contrast, in a class, by default, all members are private. This means that they can only be accessed within the class itself and its friend functions.

C++ Class Example Code:
//C++ code class defualt Accessibility
#include <iostream>
using namespace std;

// Define a class named Point
class Point {
    //private by default
    int x;
    int y;
};

int main() {
    // Declare a variable of type Point
    Point p1;

    //Error because members are private
    p1.x = 10;
    p1.y = 20;

    // Display the values of the members
    cout << "x: " << p1.x << endl;
    cout << "y: " << p1.y << endl;

    return 0;
}
Output:

2. Inheritance.

In C++, classes support inheritance, which is the ability to derive new classes from existing ones. Inheritance allows for code reuse and the creation of hierarchical relationships between classes. Structures, on the other hand, do not support inheritance by default. Although you can technically use inheritance with structures, it is more common to use classes for that purpose.

3. Object-Oriented Features.

Classes are primarily used in object-oriented programming (OOP) and provide features like encapsulation, data hiding, and polymorphism. They are suitable for creating complex data types with associated behaviors. Structures, on the other hand, are traditionally used for simple data structures that group related data together. They do not support advanced OOP features like inheritance and access specifiers.

When to use Structure in C++?

  • When you need a simple data container to group related data together, such as representing a point with x and y coordinates.
  • When you want all members to be public by default, without the need for strict encapsulation.
  • When you don't need to use advanced OOP features like inheritance and access specifiers.

When to use Class in C++?

  • When you want to create complex data types with associated behaviors and encapsulation.
  • When you need to define private members and provide controlled access through member functions.
  • When you want to use inheritance to derive new classes and establish hierarchical relationships.

It's important to note that while structures and classes have some differences in their default behavior, you can often achieve similar functionality using either of them.

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS