C++ Class and Object

A class is a template for an object, and an object is an instance of a class. Once a class is defined, this new type can be used to create objects of that type. The class is at the core of C++.

Class is the logical construct upon which the entire C++ language is built because it defines the shape and nature of an object. As such, the class forms the basis for object-oriented programming in C++.

 

Defining a class

The general form of class defination is:

class classname {
  private: 
    type instance-variable1; 
    type instance-variable2; 
    // ... 
    type instance-variableN; 
    type methodname1(parameter-list) { 
        // body of method 
    }

  public:
    type instance-variableM;
    //....
     
    type methodname2(parameter-list) { 
        // body of method 
    } 
    // ... 
     type methodnameN(parameter-list) { 
        // body of method 
    } 
}
  • The definition starts with the keyword class, followed by the class name. The body of the class is delimited by braces and terminated by a semicolon (;).
  • private and public are access specifier. Private data or functions can only be accessed from within the class. Public data or functions, on the other hand, are accessible from outside the class.
  • Collectively, the functions and variables defined within a class are called members of the class. The data items
    within a class are called data members (or sometimes member data). Functions that are included within a class are member function. In most classes, the instance variables are acted upon and accessed by the methods defined for that class.
  • Thus, as a general rule, it is the methods that determine how a class’ data can be used.

The following figure represents the class and it's members.

class object

Figure: Class and it's members

Example:

class Box
{
private:
    double width;
    double height;
    double depth;
    float volume;
public:
    void readDimensions()
    {
        cout<<"Enter width, height and depth: ";
        cin>>width>>height>>depth;
    }
    void calculateVolume()
    {
        volume = width * height * depth;
    }

    void displayVolume()
    {
        cout<<"The volume of object is : " << volume << " m^3";
    }
};

 

Declaring Objects

Defining objects in means creating them. This is also called instantiating them. Objects are sometimes called instance variables.

It has this general form:

class_name class_var1, class_var2, ... , class_varN;

Example:

Box box; // declare a Box object whose name is box

Defining an object is similar to defining a variable of any data type i.e. space is set aside for it in memory.

So far, we've discussed that

  • A class creates a new data type that can be used to create objects. That is, a class creates a logical framework that defines the relationship between its members.
  • When you declare an object of a class, you are creating an instance of that class. Thus, a class is a logical construct. An object has physical reality.

 

Calling a member function

The syntax used to call a member function that is associated with a specific object is as below:

object.member_function();

For example,

    Box box;                    //creating an object box of type Box
    box.readDimensions();       //calling member functions
    box.calculateVolume();

Because readDimensions() is a member function of the Box class, it must always be called in connection with an object of this class.

Now, lets look at the complete program to illustrates all the points mentioned above.

//example of class

#include<iostream>
using namespace std;

//declaring a class
class Box
{
private:
    //data members
    double width;
    double height;
    double depth;
    float volume;
public:
    //member functions
    void readDimensions()
    {
        cout<<"Enter width, height and depth: ";
        cin>>width>>height>>depth;
    }
    void calculateVolume()
    {
        volume = width * height * depth;
    }

    void displayVolume()
    {
        cout<<"The volume of object is : " << volume << " m^3";
    }
};

int main()
{
    Box box;                    //creating an object box of type Box
    box.readDimensions();       //calling member functions
    box.calculateVolume();
    box.displayVolume();
    return 0;
}

The ouput of the program is:

Enter width, height and depth: 10 20 30
The volume of object is : 6000 m^3

 

Member Functions Defined Outside the Class

A member function can be declared inside class and defined outside a class.

So, a function declared inside the class, with the statement

void displayVolume();

This tells the compiler that this function is a member of the class but that it will be defined outside the class declaration, someplace else in the listing.

A syntax to define a function outside a class is:

return_type class_name :: function_name(params)
{
     //body
}

For example:

//Member Function Defined Outside the Class
void Box:: displayVolume()
{
    cout<<"The volume of object is : " << volume << " m^3";
}

So, the earlier program can be modified as

//example of class

#include<iostream>
using namespace std;

//declaring a class
class Box
{
private:
    //data members
    double width;
    double height;
    double depth;
    float volume;
public:
    //member functions
    void readDimensions()
    {
        cout<<"Enter width, height and depth: ";
        cin>>width>>height>>depth;
    }
    void calculateVolume()
    {
        volume = width * height * depth;
    }
    //member function declaration
    void displayVolume();

};

//Member Function Defined Outside the Class
void Box:: displayVolume()
{
    cout<<"The volume of object is : " << volume << " m^3";
}

int main()
{
    Box box;                    //creating an object box of type Box
    box.readDimensions();       //calling member functions
    box.calculateVolume();
    box.displayVolume();
    return 0;
}

The output of the program is exactly same as earlier program.

C++ Recursion
C++ Constructors and Destructors