Class in c++: Simple explanation

Class is an inevitable part of object-oriented programming.

In simple definitions :

  • A class is a user-defined data type
  • It contains its own data members and data functions.
  • Data members /functions of a class can be accessed by creating an instance of the class.


A class can be considered as a blueprint of some particular object.


For example, we can take a BUS.
There are several manufacturers are there building buses.
They all have some common properties like the number of tires, length, number of doors, and number of passenger seats.
The values of these properties may change according to manufacturers but for a normal bus, all these properties will be there.

So for a class(blueprint)of BUS, the number of tires, length, number of doors, and number of passenger seats are its properties/data members.

For any bus, there will be certain action parts like, apply_ break, stop, increase  speed, turn ON  A/C, These are the Data functions of the class.

Object :

An object is an instance of a class .when a class is defined it won't be using any memory until its object is created.

Defining a class in c++:  

Class ClassName // user-defined name (eg:Bus) {
Access specifier ;// can be private ,public , protected Data members;// Variables to be used (eg: number of tires) Member Functions(){// method to access data members (eg: stop_bus( ) )
    //do something
}

};

Accessing Data Members:

The public data members are also accessed in the same way given however the private data members are not allowed to be accessed directly by the object. Accessing a data member depends solely on the access control of that data member.

There are three access modifiers: public, private, and protected.

 Example program based on the explanation :

#include <iostream>
using namespace std; class Bus{ public: string bus_name;
int bus_length; int number_of_seats; int number_of_people_in_the_bus; void print_bus_name(){     cout<<"Bus name : "<< bus_name<<endl;
}
int number_of_empty_seats(){ cout <<"seat available:"<<number_of_seats-number_of_people_in_the_bus<<endl;
    return number_of_seats-number_of_people_in_the_bus;
}

};
 
int main() { Bus state_bus;
state_bus.bus_name="road king "; state_bus.bus_length=3; state_bus.number_of_seats=40; state_bus.number_of_people_in_the_bus=10; state_bus.print_bus_name(); state_bus.number_of_empty_seats(); return 0;
}
Output

Bus name: road king

seat available: 30


In the above script 

   void print_bus_name()

   int number_of_empty_seats()

These Are the member functions that are responsible for certain actions of the class.

And 

   string bus_name;

   int bus_length;

   int number_of_seats;

   int number_of_people_in_the_bus;

These are the data members of the class.

 

There are 2 ways to define member function.

  • We can define inside the class definition 
  • Define outside the class definition using the scope operator.

To define a member function outside the class definition we have to use the 

scope resolution:: operator along with the class name and function name.

Example : 

We have modified the above BUS class. Now the print_bus_name() member function we are defining outside the class using 

the scope operator, and defining  number_of_empty_seats() inside the class itself.

 

#include <iostream>

using namespace std;

class Bus{

   public:

   string bus_name;

   int bus_length;

   int number_of_seats;

   int number_of_people_in_the_bus;

  

   void print_bus_name();//not defined inside class

  

   int number_of_empty_seats(){ //is defined inside class definition

   cout <<"seat available:"<<number_of_seats-number_of_people_in_the_bus<<endl;

   return number_of_seats-number_of_people_in_the_bus;

   }

  

};

 

///////////////////////////////////////////////////////////////////////////// 

////////////

void Bus::print_bus_name(){

   cout<<"Bus name : "<< bus_name<<endl;

}

 

////////////////////////////////////////////////////////////////////////////// 

int main() {

   Bus state_bus;

   state_bus.bus_name="road king ";

   state_bus.bus_length=3;

   state_bus.number_of_seats=40;

   state_bus.number_of_people_in_the_bus=10;

  

   state_bus.print_bus_name();

   state_bus.number_of_empty_seats();

 

   return 0;

}

 

Output :

Bus name : road king
seat available:30

output will be same as it won't affect the logic of the program!

Constructors :

Constructors are special members of a class.

  • The constructor function will have the same name as the class.
  • The constructor function will automatically be called every time when 
    an object of the class is created/initialized.
  • A constructor can be defined inside or outside the class definition.


Types of the constructors :

  1. Default constructors.

  2. Parameterized constructors. 

  3. Copy constructors.

Example - Parameterized constructor.

We have modified the above Bus class and added parameterized constructor in it.

So now when we create an instance of the class we have to pass

the values for the parameters defined in the constructor.

#include <iostream>

using namespace std;

class Bus{

   public:

   int number_of_people_in_the_bus;

   int number_of_seats;

   int bus_length;

   string bus_name;

   //Parameterized constructor.

   Bus(int people,int seat,int bus_length,string bus_name)

{

   number_of_people_in_the_bus=people;

   number_of_seats=seat;

   bus_length=bus_length;

   bus_name=bus_name;

   }

   

   void print_bus_name();// defined outside  class-->

  

   int number_of_empty_seats(){ //is defined inside class definition

   cout <<"seat available:"<<number_of_seats-number_of_people_in_the_bus<<endl;

 

   return number_of_seats-number_of_people_in_the_bus;

   }

  

};

 

void Bus::print_bus_name(){

   cout<<"Bus name : "<< bus_name<<endl;

}

int main() {

  //instance creation by passing values to the constructor

   Bus state_bus(10,40,3,"road king");

   state_bus.print_bus_name();

   state_bus.number_of_empty_seats();

 

   return 0;

}

 

Example for Default constructors:

We have modified the above Bus class and added Default constructor in it.
So now when we create an instance of the class we don't have to pass the values as it is already defined inside the constructor.

#include <iostream>

using namespace std;

class Bus{

   public:

   int number_of_people_in_the_bus;

   int number_of_seats;

   int bus_length;

   string bus_name;

   //default constructor.

   Bus(){

   number_of_people_in_the_bus=10;

   number_of_seats=40;

   bus_length=3;

   bus_name="road king";

   }

   

   void print_bus_name();// defined outside  class-->

  

   int number_of_empty_seats(){ //is defined inside class definition

   cout <<"seat available:"<<number_of_seats-number_of_people_in_the_bus<<endl;

   return number_of_seats-number_of_people_in_the_bus;

   }

  

};

 

void Bus::print_bus_name(){

   cout<<"Bus name : "<< bus_name<<endl;

}

int main() {

   Bus state_bus; //don’t use ()

   state_bus.print_bus_name();

   state_bus.number_of_empty_seats();

 

   return 0;

}