Objects
In C++, an object is an instance of a class. A class is a blueprint or template that defines the structure and behavior of objects. Objects are used to encapsulate data and functions (methods) that operate on that data. Here's an example of defining a class and creating objects in C++:
In the code above:
We define a class called
Person
with a constructor, a member functionDisplayInfo
, and private member variablesname_
andage_
. The constructor initializes the object with a name and age.In the
main
function, we create two objects,person1
andperson2
, of thePerson
class using the constructor.We then call the
DisplayInfo
member function on each object to display their information.
Objects encapsulate data (in this case, name_
and age_
) and the functions that operate on that data (e.g., DisplayInfo
). Objects of the same class share the same methods but have their own unique data.
Last updated
Was this helpful?