Human has a very strong ability to recognize object When we see an object we immediately categorize them. The class defines common states and behavior of objects. For example Tables, Chairs Desks are instance of Class furniture. Same as it is physics chemistry math English are instance of Class Books.


The objects belonging to the same class may have different states but their behavior remains the same. In the above example, table size may be different but it remains a part of the furniture,


For solving any problem in c++ we must follow these steps:

  • Identify the objects that comprise the problem.
  • Categorize the objects into classes.
  • Establish the relationship between classes.
  • Create an instance of objects as per the requirements.
  • Manage objects to solve the problem.

Declaration of classes:

In c++ class can be defined by keyword class.
The format of the declaration of a class is given below:
class Student
{
private:
char name[10];
int rollno;
int marks;
public:
void put_data()
}
In this diagram class is a keyword Student that is the user-defined name of its class. Private and public both are keyword which can be used to hide or public the data.
The syntax of structure and class are quite similar. However, the difference is structure is open and accessible but the class has two sections private and public. The member of the private section can only accessible in class on the other hand public members access all over the program.

Abstraction And Encapsulation:

The visible part of the program which can provide only minimum detail of the object is called Abstraction.
The data information and implementation are hidden users. On the other hand, we can say that data information and implementation are hidden in the capsule 💊 is called Encapsulation.


By default, the computer 🖥 contain all data members and members' function is private. Thus the class consulate data information and implementation. All the members who can write in the public section is an abstraction.

Creating Objects of class:

An object is an instance of the class it can be created of its class type in the main function:

Syntax of creating an object is:

 class name     object name;

  Book     b1;

The above declaration says that b1 is the object of Book.

Example program:

Write a menu-driven program p that tests the class for following services
Read the data of students
Display the grade of students

#include<iostream>
using namespace std;
class student
{
private:
char name[10];
int rollno;
int marks;
public:
void put_data()
{
cout<<"Enter Name:";
cin>>name;
cout<<"Enter Roll number:";
cin>>rollno;
cout<<"Enter marks:";
cin>>marks;
}
void display()
{
cout<<"Name:"<<name<<endl;
cout<<"Roll no:"<<rollno<<endl;
cout<<"Marks:"<<marks<<endl;
}
};
int main()
{
student s1;
s1.put_data();
s1.display();
}