Classes:

Class is a template / blueprint which stores the propeties/attributes and behaviors of objects.
Class is a user-defined data type.

Objects:

Object is an entity which has its properties and behavior. For example, car is an object. It has properties like its color, weight, number etc. and behavior like it can stop or it can accelerate etc.
  • Properties of objects: data members declared inside the class.
  • Behavior of objects: members functions declared inside the class.
So, properties and behaviors are members of class and declared inside the class.

Now we will practically implement the concept of classes and objects.

Class Layout:

Class can have data members or members functions. They can be public, private or protcted which we will discuss later. Keep in mind that public members can be accessed everywhere.
class className{
    public:
    //data members
    int weight;
    string name;

    //member functions
    void sayHi(){
        cout<<"Hi"<<endl;
    }
};

int main(){
  //Object initialization
  className objName; 
}

Note:
Do not forget to put semi-colon at the end.
Have you noticed that the syntax of class is almost same as that of struct.


Exploring a simple basic example:

class.cpp
#include<iostream>
using namespace std;

class Vehicles{
	  public:
	  string color;
	  int weight;
	  int num;
	
};
int main(){
	Vehicles supra;  
	
	supra.color = "White";
	supra.weight = 200;
	supra.num = 514;

	//accessing public properties
	cout<<"Color = "<<supra.color<<endl;
	cout<<"Weight = "<<supra.weight<<endl;
	cout<<"Number = "<<supra.num<<endl;
}


Member Functions:

Members functions are defined inside the class and they show the behavior of objects. They are also called methods.
If I say, "My brother can say hello". This is the behavior of my brother.

class.cpp
#include<iostream>
using namespace std;

class Brothers{
	  public:
	  void sayHello(){
		    cout<<"Hello"<<endl;
	  }
};

int main(){
	  Brothers Bro;
	  Bro.sayHello();
}

Now we will make objects which can do addition, subtraction, multiplication and division.

class.cpp
#include<iostream>
using namespace std;

class Calculator{
	  public:
	  int a,b;
	  int sum(){
		  return a+b;
	  }
	  int diff(){
		  return a-b;
	  }
	  int mul(){
		  return a*b;
	  }
	  int div(){
		  return a/b;
	  }
};
int main(){
  //declaring object1
	Calculator Calc1;
	
	//initialize data members for Calc1
	Calc1.a = 10;
	Calc1.b = 20;
	
	//Behavior of Object Calc1
	cout<<"Sum = "<<Calc1.sum()<<endl;
	cout<<"Difference = "<<Calc1.diff()<<endl;
	cout<<"Multiplication = "<<Calc1.mul()<<endl;
	cout<<"Division = "<<Calc1.div()<<endl;
	
	
	cout<<endl<<endl;
	
  //declaring object 2
	Calculator Calc2;
	
	//initialize data members for Calc2
	Calc2.a = 20;
	Calc2.b = 30;
	
	//Behavior of Object Calc2
	cout<<"Sum = "<<Calc2.sum()<<endl;
	cout<<"Difference = "<<Calc2.diff()<<endl;
	cout<<"Multiplication = "<<Calc2.mul()<<endl;
	cout<<"Division = "<<Calc2.div()<<endl;
}

Scope Resolution Operator:

If we want to keep the code inside the class clean, we can declare functions inside class and define them outside the class using scope resolution operator(::).

class.cpp
#include <iostream>
using namespace std;

class Sum{
	  private:
		  int a, b;
	  public:
		  Sum(int a, int b){
			  this->a = a;
			  this->b = b;
		  }
		  int sum();
};

//defining method of class outside the class

Sum :: sum(){
	return a+b;
}
int main(){
	  Sum sum1(10, 20);
	  cout<<sum1.sum();
}


Have you noticed? the behavior of objects changes as data members changes. If data members of two objects are same, their behavior is same.


Local Variables:
Local Variables are declared and initialized inside the methods, constructors or blocks. They will be destroyed when method has completed.
Instance Variables:
Instance variables are defined within a class but outside methods. They are initialized when we instantiate the class.
Class Variables:
These are declared within a class outside methods with static keyword.