A simple program to demonstrate how to create and use objects in C++ programming language.




Program Listing

#include <iostream>

 using namespace std;

class Person {       // The class
  public:             // Access specifier
    int age;        // Attribute (int variable)
    string persons_name;  // Attribute (string variable)
    string address;  // Attribute (string variable)

int main() {
  Person Male; // Create an Object

  cout << "\n\n";
  cout << "How To Create an Object in C++";
  cout << "\n\n";
  // Access attributes and set values
  Male.age = 43;
  Male.persons_name = "John Dela Cruz";
  Male.address = "Silay City, Negros Occidental";
  // Print attribute values
  cout << "Persons Name    : " << Male.persons_name << "\n";
  cout << "Persons Age     : " << Male.age << " years old. \n";
  cout << "Persons Address : " << Male.address << "\n";
  return 0;

Leave a Reply

Your email address will not be published.