Objects
#include <iostream>
// Define a simple class called "Person"
class Person {
public:
// Constructor to initialize the object
Person(std::string name, int age) : name_(name), age_(age) {}
// Member function to display information about the person
void DisplayInfo() {
std::cout << "Name: " << name_ << ", Age: " << age_ << " years" << std::endl;
}
private:
// Private member variables
std::string name_;
int age_;
};
int main() {
// Create objects of the Person class
Person person1("Alice", 30);
Person person2("Bob", 25);
// Call member functions on objects
person1.DisplayInfo();
person2.DisplayInfo();
return 0;
}Last updated