Classes & Objects In C++

Classes and objects in C++

C++ is an object oriented programming language. Classes and objects are the way through which it implements its "object orientedness".

Classes are used to make user defined data types which we can use in our programs.

A class generally contains data members and member functions. A class is a blueprint from which any number of objects (which have certain attributes and functions) can be made. Data members (variables of class) are the attributes of the objects and member functions (functions declared / defined in the class) are the functions which define the behavior of the object.

Defining a Class

when a class is defined no memory is allocated. Memory is allocated for the object we create.

A class is used to make an object which resembles to real life objects. Following is a basic example of a class.

 1 
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
// person.h
#pragma once
using namespace std;
class person {	    // defining a class named "person"
public:			// public access specifier
	string name;		// two string variables 
	string gender;		// which are the attributes of the objects of this class.

	void speak();	// declaring the function which defines the behavior of the object
	
};

void person::speak() {	// defining "speak()" function
		// prints the name and gender of the person.
	cout << "My name is " << name << " and I am a " << gender << "." << endl;
}

Above is the header file in which, from line 4, we define a class named person.

Class definition starts with the keyword class. On line 5, we specify the public access_specifier and on line 6 and 7, (like a person has a name and a gender) we define two string variables, name and gender (attributes of the objects) which will respectively indicate the name and gender of the object created from the class person, simply said, when we create a person.

And as a person can speak, from line 9, we declare a function named speak(). This function does not return anything (void function) and does not take any parameters.

void speak();	// declaring the function which defines the behavior of the object

We define the speak() function outside the class, we did that by using the scope operator (::). On line 13, We first wrote the return type of the function, then the class name followed by the scope operator to access the member functions of the class, then comes the function body.

void person::speak() {		// defining a speak() function
		// prints the name and gender of the person.
	cout << "My name is " << name << " and I am a " << gender << "." << endl;
}

The function prints the value of the name and gender variables as if a person tells his/her name and gender.

In the above code we declared the speak() function inside the class and defined that function outside the class. If we were to directly define the speak() function inside the class, the code would have been like below.

 1 
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
// person_1.h
#pragma once
using namespace std;
class person {			// defining a class named "person"
public:				// public access specifier
	string name;		// two string variables 
	string gender;		// which are the attributes of the objects of this class.

	void speak() {		// defining a speak() function
			// prints the name and gender of the person.
		cout << "My name is " << name << " and I am a " << gender << "." << endl;
	}

};

Above is the header file in which we have directly defined the speak() function in the class from line 9. 

void speak() {		// defining a speak() function
	// prints the name and gender of the person.
	cout << "My name is " << name << " and I am a " << gender << "." << endl;
}

Here we do not need to write the class name and the scope operator (::) before the function name.

Creating Objects from Class

Now, every time we create an object from a class, each object will have it's own copy of class members, in this case each object of the class person will have the name and gender variables and also the speak() function.

 1 
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
// program.cpp
#include<iostream>
#include "person.h"

using namespace std;

int main() {
	person obj1;           // creating a person
	obj1.name = "Jack";   // assigning value to string variable "name"
	obj1.gender = "Male";	// assigning value to string variable "gender" 
	obj1.speak();		// calling the speak function

	person obj2;		// same process for another person
	obj2.name = "Hallie";
	obj2.gender = "Female";
	obj2.speak();

	return 0;
}

To create an object, we specify the class name and the name for the object to be created. In the above code, on line 8, we created an object named obj1 of the class person.

As in the class, the string variables are public, so we can access those string variables through the object obj1 by using the dot operator (.).

obj1.name = "Jack";        // assigning value to string variable "name"
obj1.gender = "Male";      // assigning value to string variable "gender"

We assigned the value "Jack" to name variable and "Male" to gender variable and on line 11,

obj1.speak();	// calling the speak function

we call the speak() function which prints the value of both the string variables of the class.

Similarly on line 13, we create another object named obj2, and also assign values to the name and gender variable and then call the speak() function for obj2.

We can create as many objects we want and all will have their own attributes and functions.

Comments