Difference Between Class and Structure in C++

What is a Structure in C++?

In C++, structures have more versatility and features than in C programming. In addition to member variables, they can contain member functions, have static data members, support access specifiers (public, protected, and private) and support inheritance. In addition to this, there are some additional features in C++ structure:

  • The ability to create constructors.
  • The ability to directly initialize data members.
  • The ability to have both pointers and references to the struct.
  • The ability to implement data hiding, which restricts access to certain structure members.

Following is the syntax of structure definition in C++.

struct StructureName 
{
    //attributes and methods
};

Example

struct Student 
{ 
    string first_name;
    int roll_number
    
    // Constructore to initialize data member
    Student(string name, int roll) 
    { 
        first_name = name;
        roll_number = roll;
    }
    
    // Member function inside structure to get first name
    string get_name() 
    { 
        return first_name;
    }
    
    // Member function inside structure get roll number 
    string get_roll()
    {
        return roll_number;
    }
};

// Driver Program
int main()
{
    Student s("mohan", 37);
    cout << s.roll_number;
    cout << s.get_roll();
    cout << s.get_name();
    return 0;
}

Since we can have structure members as user-defined variables, therefore, we can have structures within the structure.

Code example: Inheritance with structures in C++

struct Base 
{
    int v1;
};

struct Derived : Base 
{
	int v2;
};

int main()
{
	Derived V;
	// Works good because inheritance is public.
	V.v1 = 1;
	cout << V.v1;
	return 0;
}

What is a Class in C++?

In C++, a class is a user-defined template or blueprint for an object that specifies the characteristics and behavior of a specific type of object. Essentially, it is a collection of data variables and data methods that define a specific object. For example, a mobile phone is an object with its storage capacity as a data variable and the ability to make calls as a data method.

To create a class in C++, the keyword "struct" is replaced with "class". Here is an example:

class ClassName 
{
    private:
    // private attributes and methods
     
    public:
    // public attributes and methods
};

C++ classes have several features in common with structures, including the ability to hold member functions and member variables, support access specifiers, and support inheritance. However, they also support additional object-oriented programming (OOP) concepts.

class Student 
{ 
private:    
    string first_name;
    int roll_number
public:    
    // Constructore to initialize attributes
    Student(string name, int roll) 
    { 
        first_name = name;
        roll_number = roll;
    }
    
    // Member function inside class to get first name
    string get_name() 
    { 
        return first_name;
    }
    
    // Member function inside class get roll number 
    string get_roll()
    {
        return roll_number;
    }
};

// Driver Program
int main()
{
    Student s("mohan", 37);
    cout << s.get_roll();
    cout << s.get_name();
    return 0;
}

What is difference Structure and Class in C++?

In C++, there is technically a difference between structures and classes, although the two are syntactically similar. Some of the main differences include:

  • The default access level for members and base classes of a structure is public, while the default access level for members and base classes of a class is private.
  • Many programmers use structures to store only data members because the default access level is public, making all data members accessible from the outside. This means that structures offer limited data hiding and functionality by default, although the private keyword can be used to restrict access to certain data members and methods. On the other hand, classes offer better encapsulation and a more defined abstraction, as they are intended for more advanced scenarios that require more methods and proper data hiding.
  • In general, structures are suitable for cases where a class has few methods and public data, while classes are preferred for more complex scenarios that require better encapsulation and abstraction.

Enjoy learning, Enjoy oops!

Share feedback with us

More blogs to explore

Our weekly newsletter

Subscribe to get weekly content on data structure and algorithms, machine learning, system design and oops.

© 2022 Code Algorithms Pvt. Ltd.

All rights reserved.