Classes and Objects in C++ and real life

Class. Object. They are used frequently in the land of object oriented programming (OOP). Classes and objects are nothing mysterious. Look around you, there are so many objects - table, chair, mobile, TV, fridge, etc. All of these are real life objects.

Manufacturers use blueprints to create these objects. The blueprints define how every object of a particular type of product (say iPhone 7) will look like and how they'll behave. Since, all the iPhone 7 objects are created from a single iPhone 7 blueprint, they all look and behave the same, for example - they all have a camera at the back, they can make calls, etc. In our OOP land, these blueprints are known as Class. A class defines how an object will be created, the properties it'll have and the actions that can be performed on that object.

Now let's observe an object. Each object has its own properties, for example, think of your mobile phone - it has properties like "the phone is switched on/off", "the phone's brightness level is set to 5", "the phone is in vibration mode" and so on. Apart from the properties that your mobile has, you can also do stuffs (perform some action) with it - "switch on/off the phone", "increase brightness level", "make a call", "text to someone", etc. This is exactly what an object in OOP is. An object has properties and functions/methods (which perform an action).

Now that we know what an object and a class is, let's see how they're defined in code by creating a Mobile class:

#include <iostream>
using namespace std;

class Mobile {
 // name of the mobile
 string mobile_name;
 // volume level of the mobile
 int volume_level;
 // brightness level of the mobile
 int brightness_level;
 // by default the phone is off
 bool phone_is_on = false;
};

We defined a Mobile blueprint (class) which has properties - mobile_name, volume_level, brightness_level and phone_is_on (which denotes the phone is either on/off). Now, let's define some actions for the Mobile class.

class Mobile {
 // name of the mobile
 string mobile_name;
 // volume level of the mobile
 int volume_level;
 // brightness level of the mobile
 int brightness_level;
 // by default the phone is off
 bool phone_is_on = false;

public:
 void toggle_phone_state() {
  if (phone_is_on == false) {
   cout << "Phone is switched on." << endl;
   phone_is_on = true;
  } else {
   cout << "Phone is switched off." << endl;
   phone_is_on = false;
  }
 }

 void set_volume(int vol) {
  volume_level = vol;
 }

 void set_brightness(int brightness) {
  brightness_level = brightness;
 }
};

Now, we have defined actions for the class. Notice, how actions (methods/member functions) are just plain ordinary functions. Let's use this blueprint (class) to create an object.

#include <iostream>
using namespace std;

class Mobile {
 // name of the mobile
 string mobile_name;
 // volume level of the mobile
 int volume_level;
 // brightness level of the mobile
 int brightness_level;
 // by default the phone is off
 bool phone_is_on = false;

public:
 void toggle_phone_state() {
  if (phone_is_on == false) {
   cout << "Phone is switched on." << endl;
   phone_is_on = true;
  } else {
   cout << "Phone is switched off." << endl;
   phone_is_on = false;
  }
 }

 void set_volume(int vol) {
  cout << "New volume is: " << vol << endl;
  volume_level = vol;
 }

 void set_brightness(int brightness) {
  cout << "New brightness is: " << brightness << endl;
  brightness_level = brightness;
 }
};

int main() {
 // Create an object of class Mobile named - my_android
 Mobile my_android;
 // perform actions on the my_android object by using a dot and the action name
 // my_android . action_name();
 my_android.toggle_phone_state();
 my_android.set_volume(5);
 my_android.set_brightness(3);

 return 0;
}

NOTE: You may have noticed a public keyword. Its okay to not understand it for now, just write it as it is.
Now, you can create as many Mobiles as you want. Go and run the program!

No comments:

Post a Comment

Power function

In C/C++ (and also other languages), we have the built in power function to raise a number to a given power. What if we need to calculate ...