Skip to content

Classes and objects (1)

Course: C++ Part 2 - chapter 5

C++ is an object oriented language. The 2 fundamental concepts are:

  1. Classes - A class is a blueprint or template for creating objects. It defines the properties (data members) and behaviors (member functions) that objects of the class will have. Think of classes as user defined types.
  2. Objects - An object is an instance of a class. It represents a specific realization of the class blueprint, with its own set of data members and methods.

Example, consider you are building a Student management system. Now, for each student you want to store their name, roll no, address and CGPA. One approach is creating different lists for storing each of them. However, that makes things difficult to manage. The better approach is to create a type Student that holds all these values and also exposes methods that can mutate these values and perform certain operations for each student. This idea is called Encapsulation.

class Student {
string name;
string address;
int roll_no;
int attendance;
float cgpa;
public:
void mark_attendance() {
this.attendance += 1;
}
}
Student ram = new Student();

this keyword is used inside a class, that is used to represent the instance of the class calling the method. For example, if we have 2 students, Ram and Shyam and we call ram.mark_attendance() then this keyword represents the object ram.

Constructors are the first method that is called when an object of the class is created. It is for assigning initial values to the data members. There are 3 types of constructors:

  1. Default constructor - This is by default present, even if we don’t manually define a constructor. It does not take any argument and assigns default values to the data members.
  2. Parameterised constructor - This takes parameters of the initial values and assigns the data members these values.
  3. Copy constructor - Copies the data member initial values from another object.
class Student {
string name;
string address;
int roll_no;
int attendance;
float cgpa;
public:
// default constructor
Student() {
this.name = "";
this.address = "";
this.roll_no = 0;
this.attendance = 0;
this.cgpa = 0.0;
}
// Parameterised constructor
Student(string name, string address, int roll, float cgpa) {
this.name = name;
this.address = address;
this.roll_no = roll;
this.attendance = 0;
this.cgpa = cgpa;
}
// Copy constructor
Student(Student &s) {
this.name = s.name;
this.address = s.address;
this.roll_no = s.roll_no;
this.attendance = 0;
this.cgpa = s.cgpa;
}
void mark_attendance() {
this.attendance += 1;
}
}

Q - Why do we pass reference in copy constructor?

Control the visibility of class members (variables and functions) to other parts of the program

  1. Private: Only methods within the classes can access these data members and member functions.
  2. Protected: Only methods within the class and in derived class can access these.
  3. Public: Accessible from anywhere, inside or outside the class.

Q - Where should the constructor of a class be?

We generally will not need concepts like Inheritance, Polymorphism, Data hiding, Abstractions, Friend classes for DSA so we will skip them. However, these are important Software Engineering concepts and you should definitely learn them to be able to ship good quality code.

  1. **Defining new data type for making the task easier and code readable **Sometimes we would come across the need of creating a custom data type. For example, let’s say we want to store 2 or 3 values for each array element. An example can be a LinkedList. In LinkedList, a node stores the value of the current node and also a pointer to the next node. We can do this without classes as well, but the code quality takes a hit, and also it becomes difficult to manage. With classes, we can do like this:
class Node {
int data;
Node *next; // A pointer to the next node
}
  1. **Encapsulating some common functionalities to make the code cleaner **Sometimes, when our code becomes significantly bigger, and let’s say we are using some utility data structure to solve a part of it (like graph), we can encapsulate it into a class and expose some methods that the rest of the code can interact with. Let’s say, we need an utility that calculates minimum distance between 2 nodes, we can do something like this:
class Graph {
// data members needed for graph representation
public:
int calculate_minimum_distance(int node1, int node2) {}
}
  1. **Avoiding global variables **In some problems like graph based, DP based and some other problems, we might need to maintain some global variables. Generally, it is okay wrt solving DSA problems, but treated as a bad pattern in interviews. Ideally in these cases, you should encapsulate the variables and the functions in classes. The global variables becomes data members, and the functions using them becomes methods. For example,
bool found = false; // Global variable
void f() {
if(some condition) {
found = true;
return;
}
}
// Instead we can do
class Solve {
bool found;
public:
Solve() {
this.found = false;
}
void f() {
if (some condition) {
found = true;
return;
}
}
}