When to use a class and a struct in C++?

In C++, both classes and structs are used to define custom data types that can hold data members and member functions. To know when to use classes or when to use struct we need to know some key differences between them.

To know more about classes refer to the following Answers:

Difference between a class and a struct

Classes and structs share many similarities, however, there are a few key differences between them:

Default member access

  • The default access level for members in a struct is public, while the default for a class is private. This means that members of a struct can be accessed directly from outside the struct, while members of a class cannot unless they are explicitly declared as public or accessible through public member functions.

Example

Defining a class

class MyClass {
private:
int myPrivateData;
public:
void myPublicFunction() {
// Access and modify myPrivateData
}
};

Defining a struct

struct MyStruct {
int myPublicData;
void myPublicFunction() {
// Access and modify myPublicData
}
};
  • Classes are often used for encapsulation, where data and the operations on that data are tightly bound together, and access to the data is controlled through public interfaces (public member functions). While structs are commonly used for data structures where the emphasis is on holding data rather than providing encapsulated behavior.

Inheritance

The second property on which classes and structs differ is inheritance. A class can be inherited from another class or struct using the class keyword, while a struct can only be inherited from another struct.

Member functions

A class can have member functions, which are functions that operate on the data members of the class. A struct can also have member functions, but they are not as commonly used.

Constructors and destructors

A class can have constructors and destructors, which are special member functions that are called when an object is created or destroyed. A struct can also have constructors and destructors, but they are not as commonly used.

When to use class and when to use struct

In modern C++, structs are often used similarly to classes, and the decision to use one over the other is more a matter of convention and readability. So the choice between using a class or struct in C++ depends on how we want to organize our data and behavior. So we can:

  • Use a class if we want to encapsulate data and behavior, and control access through public interfaces.

  • Use a struct if we want a simple data structure with public access to its members or when working with legacy code or C libraries that predominantly use structs for similar purposes.

Example

#include <iostream>
class MyClass {
private:
int privateData;
public:
void setPrivateData(int value) {
privateData = value;
}
int getPrivateData() {
return privateData;
}
};
int main() {
MyClass obj;
obj.setPrivateData(42);
std::cout << "Private data: " << obj.getPrivateData() << std::endl;
return 0;
}

In the example above, of using class, the member privateData is explicitly marked as private, and access to it is provided through public member functions (setPrivateData and getPrivateData).

#include <iostream>
struct MyStruct {
int publicData;
};
int main() {
MyStruct obj;
obj.publicData = 42;
std::cout << "Public data: " << obj.publicData << std::endl;
return 0;
}

In the above example, using a struct, we can directly access the publicData member without getters and setters. However, if we choose to use getters and setters for the publicData member in the struct, there will be no functional difference, but it would be considered redundant and unnecessary in this case.

Conclusion

In general, classes are used for more complex data types that require encapsulation and data abstraction, while structs are used for simpler data types that do not require as much abstraction or encapsulation. It is now common to use structs for small, simple, data-oriented types, and classes for more complex types with behavior and encapsulation. However, the choice between using a class or a struct ultimately depends on the specific needs of the program and the personal preferences of the programmer.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved