C++ structure (struct) and class (class) comparison

C++ structure (struct) and class (class) comparison

In C++, both struct and class are user-defined data types for encapsulating data and methods. Structs are usually used to define some simple data structures, while classes are more used in object-oriented programming.

The differences between the two are as follows:

A structure (struct) is a simple data collection that can contain member variables of different types, but by default the member variables are public (public). Structures are often used to represent a group of related data, such as describing a student's name, age, and grades.

Sample code:

#include <iostream> 
#include <cstring>
using namespace std;

struct Person {
//public:	
    int age;
    char name[20];
};

int main() {
    Person p;
    p.age = 30;
    strcpy(p.name, "John");

    cout << "Name: " << p.name << ", Age: " << p.age << endl; //输出:Name: John, Age: 30
 

    return 0;
}

Class (class) is a more advanced data encapsulation method, which can contain variables (member variables) and functions (member functions). The default access type of a class is private (private), that is, data members can only be accessed and manipulated through public member functions. Classes also support object-oriented programming features such as inheritance and polymorphism.

Sample code:

#include <iostream> 
using namespace std;

class Person {
private:
    int age;
    string name;

public:
    void setAge(int newAge) {
        age = newAge;
    }

    void setName(string newName) {
        name = newName;
    }

    void printInfo() {
        cout << "Name: " << name << ", Age: " << age << endl;
    }
};

int main() {
    Person p;
    p.setAge(30);
    p.setName("John");

    p.printInfo();

    return 0;
}

Both structures (struct) and classes (class) in C++ can use access modifiers (access specifiers, access specifiers). Although structs and classes have some differences in syntax, their member access is handled in the same way.

By using the public, protected, and private access modifiers, we can control the visibility and accessibility of members of structures and classes to the outside world. Specifically:

Public members: Members modified by public can be directly accessed outside the structure or class.

Protected members: Members modified by protected can only be accessed inside the structure or class and in derived classes.

Private members: Members modified by private can only be accessed inside the structure or class, and are not visible to the outside.

It should be noted that the access rights of structures and classes are different by default. In a structure, the default access right is public, while in a class, the default access right is private.

Summary : Structures and classes are very useful tools in C++ for organizing and encapsulating data and behavior. They mainly differ in default access restrictions and semantic differences. Structures are suitable for operations such as simple data containers and queues; classes provide more advanced features, such as inheritance, encapsulation, and polymorphism, which are more scalable, flexible, and complex than structures.

Guess you like

Origin blog.csdn.net/cnds123/article/details/131918979