In C++, a field is a data member of a class that can hold the state or attributes of an object of that class. Fields are declared within the class definition, and can have different access levels (public, private, or protected) which determine the visibility of the field outside the class.
Here's an example of how to declare and use fields in a class in C++:
`` c++
class Person {
public:
string name; // a public field
private:
int age; // a private field
public:
void setAge(int newAge) {
age = newAge;
}
void printDetails() {
cout << "Name: " << name << "\n";
cout << "Age: " << age << "\n";
}
};
c++Person
In this example, we have aclass with two fields -nameandage.nameis declared as a public field, which means it can be accessed from outside the class.ageis a private field, which means it can only be accessed from within the class.setAge()The class also has two methods -
andprintDetails().setAge()is a public method that allows us to modify the value of theagefield.printDetails()is also a public method, and it prints out the values of both fields.PersonTo use this class, we can create an object of type
and set its fields:
Person john;
john.name = "John"; // set the name field directly
john.setAge(30); // call the setAge() method to set the age field
c++printDetails()
We can then call themethod to print out the values of both fields:
john.printDetails(); // prints "Name: John\nAge: 30\n"
`
Note that we were able to access the
name field directly since it was declared as a public field. However, we had to use the setAge() method to modify the age` field, since it was declared as a private field.