You are on page 1of 1

CPA: Programming

Essentials in C++ C++ INSTITUTE - PROGRAM YOUR FUTURE

Lab 5.1.1 Classes and Objects in C++


Objectives
Familiarize the student with:

C++ class and object syntax;


creating objects;
accessing object members.

Scenario
Before we start doing anything useful with classes, objects and the like, we should familiarize ourselves a little with these concepts.

So for now, let's represent a person in terms of classes!

Run the code below and then experiment with it. Add some more members to the class and try accessing them.

#include <iostream>
#include <string>

using namespace std;

class Person
{
public:
string name;
int age;
// Your code here
};

void print(Person* person)


{
cout << person->name << " is " person->age << " years old" << endl;
}

int main()
{
Person person;
person.name = "Harry";
person.age = 23;

cout << "Meet " << person.name();


print(&person);

// Your code here

return 0;
}

© 2017 C++ Institute. All rights reserved. Last updated: March 07, 2017 | www.cppinstitute.org Page 1 of 1

You might also like