Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




C++ - CLASS AND OBJECT


Object Oriented Programming is completely based on Objects and Classes.


C++ Class


Anything and everthing you see around can be said to be a class. Be it a Car or a Tree or even a Human.


A class can be described as a blueprint which says how an object is going to behave once it is created. And a class has a behaviour and a state.


Sounds Scary ?


Let us simplify with the below example :


Example :



class Human
{
	private :
    	string name;
		string food;
		string language;
        
	public :
		void eat()
		{
			cout << "Eats " << food;
		}

		void speak()
		{
			cout << "Speaks " << language;
		}
};




Caution : Do not run the above code for now.


Note : In the above code there is something called private and public. They are access specifiers. We would cover them in Access Specifiers topic.

We have a class named Human. And all of us know humans have a common behavior of speaking and eating. And as said, a class has behaviour and state. We find the two behaviors/methods.


Eat behaviour/method


void eat()
{
	cout << "Eats " << food;
}

Speak behaviour/method


void speak()
{
	cout << "Speaks " << language;
}

But the language and food varies from person to person. Thus we have the states/variables as : food, language and name as it varies from person to person.


string name;
string food;
string language;

Now, just wait, think for a while. Does the above class Human have any physical existence? I mean Tom, Dick, Harry all are humans and have a physical existance but the above class Human is just a kind of blueprint tellin us how Tom, Dick, Harry is going to behave or react once they are created.


Tom, Dick, Harry are all Objects created from the Human class.


Now, does it ring a bell as in why objects are created from a class? Just imagine God has prepared a Human class stating all the behaviours of Human and thus we are behaving right now.


So, class is just a blueprint stating how an object will behave once it is created.


How do we create an Object from a class?


Below is the way we do in C++ :


Example :



#include <iostream>
using namespace std;

class Human
{
	private :
    	string name;
		string food;
		string language;
        
	public :
		void eat()
		{
			cout << "Eats " << food;
		}

		void speak()
		{
			cout << "Speaks " << language;
		}
};

int main() {	
	Human human;
	return 0;
}



We have the main method where objects are created.


int main() {
	Human human;
	return 0;
}

The first line is :


Human human;

This is how we create an object. The name of the object is human(Off course you can give it any name).


Note : In technical terms, Human human is going to create an instance or object of Human class and, human is reference variable which is holding the instance/object of Human class.
java_Collections

So, after we create the human object, a block of memory will be allocated to it. And you are all set to insert values to name, food and language.

Say you are a Car designer. You have drawn a model of the Car. Based on the model you have drawn the actual Cars are created by workers. Here, the Car model you have drawn is the Class and the actual Cars made by the workers are the Objects.

How do we access the behaviors/methods and properties/state of an Object?


So, we know the human object has three properties/state i.e. name, food and language. And two behaviors/methods eat() and speak(). But how do we access those?


The dot(.) Operator


Behaviors/methods and properties/state of a class can only be accessed using the dot(.) operator of an object.


Accessing the 'eat()' behaviors/method


human.eat();

Where human is the object of Human class. And we have used the dot(.) to access eat().


But at first, we need to know that how can we assign values to an object


And we will be learning next, how to assign values to an Object.