Class and Structure in C++
Structure in C++
In C++, structures are data types that combine various sorts of objects into a single data type. Moreover, the structure is a user-defined data type created using the struct keyword. The structure's members are its elements, and any valid data type may be used to represent them.
Syntax:
struct structure_name {
member1;
member2;
...
memberN;
};
For example, the following code defines a Student structure with three members: name, roll_no, and height.
struct Student {
char name[50];
int roll_no;
float height;
};
After a structure is declared, a variable of that type can be created. For instance, the code below creates a Student type variable and gives it the values George, 15, and 5.6:
Student student;
strcpy(student.name, "George");
student.roll_no = 15;
student.height=5.6;
You can access the elements with this code:
cout << student.name << endl;
Class in C++
A class is an essential component of OOPs in C++. Examples of the class created as objects using it act as a blueprint. A class encapsulates data (attributes) and functions (methods) that manipulate the data, defining the structure and behavior of the objects it creates. Here is a brief explanation of how classes work in C++:
Syntax:
class class_name {
attribute1;
attribute2;
...
attributeN;
method1;
method2;
...
methodN;
};
For example, the following code defines a Student structure with three members: name, roll_no, and height, and two methods: setName() and getHeight():
Code:
class Student {
public:
char name[50];
int roll_no;
float height;
void setName(const char* name) {
strcpy(this->name, name);
}
float getHeight() {
return height;
}
};
After a class is declared, a variable of that type can be created. For instance, the code below creates a Student type variable and gives it the values George, 15, and 5.6:
Student student;
student.setName("George");
student.roll_no = 15;
student.height = 5.6;
You can access the elements with this code:
cout << student.name << endl;
Distinguish between Classes and Structure
In C++, both classes and structures are used to create custom data types that can hold numerous variables of different data types. However, there are several significant distinctions between classes and structures:
1.Default Member Access:
- In a class, Members are, by default, private. Unless specifically stated otherwise, the characteristics and methods that form a class cannot be accessed outside the class.
- In a structure, members are public by default. This means that the members of a structure can be accessed directly from outside the structure.
2.Inheritance:
- Classes can serve as base classes for inheritance. Using inheritance to build more complex hierarchies, you can derive new classes from existing classes.
- Structures cannot be used as base classes for inheritance.
3. Access Specifiers:
- Classes allow you to specify access specifiers such as private, protected, and public for members, controlling their visibility and access from outside the class.
- Structures do not allow access specifiers. All structure members are public or private based on the default access level.
4. Member Functions (Methods):
- Classes can have member functions (methods) associated with them, which can operate on their data members.
- Structures cannot have member functions in C++ (though in C++20, you can use lambdas inside structures).
5. Usage Convention:
- Historically, structures in C were meant to hold data, and classes were introduced in C++ to extend structures by allowing them to hold data and member functions (methods).
- However, in modern C++, the distinction between classes and structures is more flexible. You can use structures with methods using the struct keyword or classes with all public members using the class keyword.
Here's a simple comparison example based on usage:
Code:
#include <iostream>
// Using class
class MyClass {
private:
int privateMember;
public:
MyClass(int value) : privateMember(value) {}
void printValue() {
std::cout << "Class Private Member Value: " << privateMember << std::endl;
}
};
// Using struct
struct MyStruct {
int publicMember;
MyStruct(int value) : publicMember(value) {}
void printValue() {
std::cout << "Struct Public Member Value: " << publicMember << std::endl;
}
};
int main() {
// Using class
MyClass classObj(42);
classObj.printValue();
// Using struct
MyStruct structObj(23);
structObj.printValue();
return 0;
}
Output:
![Class and Structure in C++](https://static.tutorialandexample.com/cpp/class-and-structure-in-cpp1.png)
Though historical differences exist between classes and structures, C++ has evolved and reduced the gap. In modern C++, classes are often preferred due to their added flexibility, encapsulation, and ability to define data and behavior in a single unit. Structures can still be useful for simpler data containers or when interacting with code that follows C conventions.