C++ - Difference Structure and Class

In C++, both structures and classes are used to define user-defined types that encapsulate data and related operations. While they share many similarities, there are a few key differences between structures and classes.

Default Member Accessibility:

In a structure, the default member accessibility is public, which means all members (data and functions) are accessible from outside the structure without any restrictions. On the other hand, in a class, the default member accessibility is private, which means members are not accessible outside the class by default.

Data Member Accessibility:

In a structure, data members are by default public, allowing direct access and modification. In contrast, in a class, data members are by default private, and access to them is restricted to member functions or through the use of getter and setter functions.

Member Functions:

Both structures and classes can have member functions (methods) associated with them. The main difference is that in a structure, member functions can only be declared but not defined inside the structure itself. They need to be defined separately outside the structure. In a class, member functions can be declared and defined within the class itself.

Inheritance:

Classes support inheritance, allowing one class to inherit the properties and behaviors of another class. Inheritance allows code reuse and the creation of class hierarchies. Structures, on the other hand, do not support inheritance.

Here's an example that demonstrates the differences between structures and classes:

// Structure
struct Employee {
    int empId;     // Public by default
    float salary;  // Public by default
    void display() {
        cout << "Employee ID: " << empId << endl;
        cout << "Salary: " << salary << endl;
    }
};
// Class
class Student {
private:
    int rollNumber;    // Private member
    string name;       // Private member
public:
    void setRollNumber(int roll) {
        rollNumber = roll;
    }
    int getRollNumber() {
        return rollNumber;
    }
    void setName(string n) {
        name = n;
    }
    string getName() {
        return name;
    }
};

In this example, we define a structure Employee and a class Student. The Employee structure has public data members empId and salary, along with a public member function display().

The Student class has private data members rollNumber and name, along with public member functions setRollNumber(), getRollNumber(), setName(), and getName() to manipulate and access the private data members.

By default, the data members in the structure are accessible directly, while in the class, they can only be accessed through the member functions.

To use these structures and classes, you can create objects and access their members accordingly:

int main() {
    // Structure
    Employee emp;
    emp.empId = 101;      // Direct access to data members
    emp.salary = 5000.0;  // Direct access to data members
    emp.display();        // Accessing member function
    // Class
    Student stu;
    stu.setRollNumber(1);     // Accessing member function
    stu.setName("John Doe");  // Accessing member function
    cout << "Roll Number: " << stu.getRollNumber() << endl;  // Accessing member function
    cout << "Name: " << stu.getName() << endl;              // Accessing member function
    return 0;
}

In this example, we create an object of the Employee structure and access its data members and member function directly. For the Student class, we use the member functions to set and get the values of the private data members.