In C++, both structures (struct) and classes (class) are user-defined data types used to group related data and functions into a single unit. They support almost the same features, including member functions, constructors, inheritance, and polymorphism. The primary differences between them lie in their default access control and inheritance behavior.
- Both can contain data members and member functions.
- Both support constructors, inheritance, and virtual functions.
- The main differences are the default member access and default inheritance mode.
Structure
A structure is a user-defined data type that groups related variables under a single name. In C++, structures can also contain member functions, constructors, and other class-like features.
- Declared using the struct keyword.
- Members are public by default.
- Supports functions, constructors, and inheritance.
- Commonly used for storing and organizing data.
#include <iostream>
using namespace std;
struct Student{
int id;
string name;
};
int main(){
Student s;
s.id = 101;
s.name = "Rahul";
cout << s.id << " " << s.name;
return 0;
}
Output
101 Rahul
Explanation
- Student is a structure containing two data members.
- Members are public by default.
- Therefore, s.id and s.name can be accessed directly outside the structure.
Class
A class is a user-defined data type that encapsulates data and functions into a single unit. It is one of the fundamental building blocks of Object-Oriented Programming (OOP).
- Declared using the class keyword.
- Members are private by default.
- Supports encapsulation, inheritance, and polymorphism.
- Widely used for implementing object-oriented programs.
#include <iostream>
using namespace std;
class Student{
private:
int id;
public:
void setId(int i) {
id = i;
}
void display() {
cout << id;
}
};
int main(){
Student s;
s.setId(101);
s.display();
return 0;
}
Output
101
Explanation
- id is a private data member.
- It cannot be accessed directly outside the class.
- Public member functions are used to access and modify the data.
Structure Members 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
Explanation
- The member x is declared inside a structure without an explicit access specifier.
- Since structure members are public by default, x can be accessed directly outside the structure.
- Therefore, assigning and printing t.x works successfully.
Key Differences Between Structure and Class
Although structures and classes are functionally very similar, they differ in a few default behaviors.
Class | Structure |
|---|---|
Members of a class are private by default. | Members of a structure are public by default. |
It is declared using the class keyword. | It is declared using the struct keyword. |
Inheritance is private by default. | Inheritance is public by default. |
It is normally used for Object Oriented Programming. | It also allows almost all features of a class, but is normally used for the grouping of different datatypes. |