Object and Class

Object

An object is something which has some definite properties. In computing, an object is a memory location having a value and referenced by an identifier. An object is an instance of a class.

Class

Class is the logical structure of an object. A class is a blueprint where an object can build from this blueprint. In addition, a class is the collection of data and methods or functions under a single class name.

In C++, a program can have many classes. When a class is declared, no memory is allocated until the object is defined. All the variables in the class are called member variable and all the functions are called member function.

Structure of a class

class ClassName

{

// member variables

// member functions

};

class Rect

{

//memeber varibales

public:

int width;

int height;

// member functions

int area(void);

};

 

This is a class of Rect where two member variables width and height and a member function area.

#include<iostream>

using namespace std;

class Rect {

//memeber varibales

public:

int width;

int height;

// member functions

int area(void);

};

int Rect::area(void){

return width * height;

}

int main( ) {

Rect r;

r.height=5;

r.width=4;

cout<<"Area of a rectangle: ";

cout<<r.area();

return 0;

}

Output

Area of a rectangle: 20

In the main function Rect is a class and r is the object.