Difference Between Class and Structure in C++

What is a Structure?

In C language, a structure is a collection of various types of variables. These variables can either be built-in datatype or user-defined datatype. However, structures in C++ are very versatile and different from that in C.

The C++ structures can hold member functions along with the member variables. They can have static data members, access specifiers (public, protected, and private), and support inheritance too.

Structure in C++

Following is the syntax of structure definition in C++. Here structure definition is a specification for how the structure variables will look when they are declared.

struct StructureName {
     //structure members 
};

Example:

struct name { 
     string first_name;
     string second_name;  
     string sign;
};
//declaring structre variable
name person;
//here person is the variable name of the name structure

To access the data members of the variable person, we use the dot operator ‘.’. For example, if we need to set the first_name field of a person to “Mohan”, then we will write:

person.first_name = "Mohan"; 

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

What is a Class in C++?

The C language does not have the concept of class and objects. That’s why it is a procedural-oriented language. Moreover, C++ is a successor of the C language, which has classes and objects, and it is an object-oriented language.

In C++, a class is a user-defined layout or blueprint of an object that describes what a specific kind of object will look like. In simpler words, it is a collection of data variables and data methods that depict a specific object. For example, a mobile phone is an object with its storage as a data variable and calling as a data method.

The C++ classes hold member functions along with the member variables; they have access specifiers, support inheritance, and support many other OOP concepts.

In C++, classes are syntactically very similar to the structures. Only the keyword struct needs to be replaced with class. Here is an example:

class name {
     string sign;
   public:
     string first_name;
     string last_name;
};
name person;

Again, just like structures, a class definition is of no use unless we define objects/variables of that class. We use the dot operator to access the data members of the objects of the class. We can have user-defined data members too.

What is the difference?

In C++, intuitively, there’s no difference between structure and class. But, technically, there are a few differences based on their definition and use cases: 

  • The members and base classes of a struct are public by default, and the members and base classes of a class are private by default. So in the case of a class, we can make our base classes explicitly public, private, or protected.
  • As a convention, many of us use structure to store only data members because the default access level is public, i.e., all the data members are accessible from the outside world. So by default, it offers limited data hiding and functionality. Although, we can use the private keyword over desirable data members and methods. On another side, a class is like an advanced structure version that offers proper encapsulation and a well-defined abstraction.
  • We can use the structure if a class has very few methods and has public data, but otherwise, we can use the class.

Enjoy learning, Enjoy oops!

More From EnjoyAlgorithms

Our weekly newsletter

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

Follow Us:

LinkedinMedium

© 2020 EnjoyAlgorithms Inc.

All rights reserved.