Learn Why class and object Is Important & What Is Object In C++?

class is a user-defined data type. class is defined by class keyword. Class And Object are the most important  And common concepts in object-oriented programming languages. class is widely used in C++. you can declare multiple classes in one C++ program

class and object in c++

What is Class?

Class is a box. class does hold member methods functions and data members (variables) in the class body. The class uses a data-hiding concept. the class has no limit for creating a function and variable. member functions and data member declared by curly braces

basic structures of class

CLASS class_name
{
 
 // declare  variable
 
 // access specifier
 
 // function code
 
};

the class has 3 access specifiers.

  1. Public
  2. Private
  3. Protected

What is an Object?

when you define a class then not used memory but when you create class objects then memory will be allocated.

you can not operate class without objects. when you created multiple objects then memory will separate allocated. if you want to call a method of a class, first you write the object name after write dot(.) and write the method name.

Example of class and object

#include <iostream>
using namespace std;

class item
{
    int item_no; // Private Member
    float cost;  // Private Member

public:
    void getdata(int a, int b)
    {
        item_no = a;
        cost = b;
    }

    void putdata()
    {
        cout << "Item_no = " << item_no << endl;
        cout << "Cost = " << cost << endl;
    }
};

int main()
{
    item i1, i2;

    i1.getdata(101, 5000);
    i2.getdata(102, 2000);

    cout << " I1 Object : " << endl;
    i1.putdata();

    cout << " I2 Object : " << endl;
    i2.putdata();
    return 0;
}

output

I1 Object : 
Item_no = 101
Cost = 5000
 I2 Object : 
Item_no = 102
Cost = 2000