Classes and Objects

C++ is an Object Oriented Programming language. The main concepts of the Object Oriented Programming (OOP) are classes and objects.

A class is a blueprint for creating objects. It provides the description of member variables and implements the behavior of the objects of this class (member functions). Classes and objects can be compared with the real world objects. There are a lot of individual entities of the same kind in real life. For example, a description of Person which consists of first name, last name and date of birth can be compared with the class. In the same time there are a lot of individual persons that can be compared with objects. A class is abstract data structure which provides only template for creating objects. But an object is an individual entity with its own characteristics.

You can define a class by using class keyword. The definition of class is just a template for the creation of new objects. Class definition describes the structure of the new data type, its behavior. Here is an example of creating Person class:

class Person
public://access control
	string firstName;//first name of a person
	string lastName;//last name of a person
	tm dateOfBirth;//date of birth

This is a simple example of a class with 3 data members. It can be described in the following way:

Access type specifies the possibility to access data members and member functions. Public access type allows to access data members from the outside of the class. There are 2 other access types: protected and private. Private access type restricts access to data members and member functions from the outside of a class. Access types are described in details in “Access control in classes”.

The definition of objects is done in the same way as definition of variables of different types. It’s done by using name of class and identifier for the object:

Person person1;//declare a Person object with name "person1"

A member function is declared inside of a class. Any non-static member function can access all the non-static data members and member functions of this class. This is an example of creating a member function of class Person:

void printPerson()
	cout << "First name " << firstName << endl;
	cout << "Last name " << lastName << endl;
	cout << "Year of Birth " << dateOfBirth.tm_year << endl;

This member function prints information about an individual person.

Public data members of a class can be accessed directly by using dot operator (.):

Person person1;//declare a Person object with name "person1"
person1.firstName = "Smith";//set first name of person1 to Smith
person1.lastName = "James";//set last name of person1 to James
person1.dateOfBirth.tm_year = 1980;//set  year of birth of person1 to 1980

Public member functions are called by using dot operator too:

//call printPerson member function

This program provides the following output:

First name Smith

Last name James

Year of Birth 1980

The creation of objects is done by using class constructors. A constructor can be compared with a member function that has a goal to create an individual object. The name of the constructor always is the same as the name of the class:

//constructor of individual person
Person(string fName, string lName, int yearOfBirth)
	firstName = fName;//set data members
	lastName = lName;//according to constructor's
	dateOfBirth.tm_year = yearOfBirth;//parameters

Constructors are discussed in details in the “Constructor and Destructor” topic.

Every object of a class has a hidden pointer to itself – this. C++ uses this pointer to access data members and member functions of objects.

Classes can contain static data members and member functions. Only one copy of the data is stored for the static data member for all objects of the class. Static member functions have class scope. They can use only static members of the class:

static int x;//declare static data member
static int getX()//static function to get x value
	return x;

Now you can access static data member and static member function from the outside of class:

Person::x = 10;//set value of x
cout << Person::getX() << endl;//call static function

Static members functions do not have implicit this argument. That’s why they can use only static members of class.

You can use pointers to work with objects.  Definition of a pointer to an object is done in the same way as for other data types:

Person* person2;

In this case you need to allocate memory for this object. It’s done by calling a constructor of this class:

person2 = new Person("Moore", "Thomas", 1972);

You can access data members in the case of using pointers by using arrow operator (->)

Translate »