Objects in C++ Programming



Introduction to Objects in C++

Objects are instances of a class. They are created with a definition statement after the class has been declared. A class declaration is similar to the blueprint for a house. The blueprint itself is not a house, but is a detailed description of a house.

When we use the blueprint to build an actual house, we could say we are constructing an instance of the house described by the blueprint. Class objects are created with simple definition statements, just like variables.

For example the following statement defines circle1 and circle2 to be two objects of the Circle class

Syntax:


   Circle circle1,
           circle2;

Defining a class object is called the instantiation of a class. The objects circle1 and circle2 are two distinct instances of the Circle class, with different memory assigned to hold the values stored in their member variables.

Accessing an object’s members

Public members of a class object are accessed with the dot operator. The following statements call the setRadius function of circle1 and circle2.


    circle1.setRadius(0.9); //This sets circle1's radius to 0.9 
    circle2.setRadius(1.5); // This sets circle2's radius to 1.5
   

The following program is a complete program that demonstrates the Circle class. Notice that the statements to create and use Circle objects are in main, not in the class declaration.


    #include <iostream>
    #include <cmath>
    using namespace std;
    // Circle class declaration
    class Circle
    { 
    private:
    double radius;
    public:
    void setRadius(double r) { 
    radius = r; 
    }
    double getArea() { 
    return 3.14 * pow(radius, 2); 
    } };
    int main()
    {
    // Define 2 Circle objects
    Circle circle1,
    circle2;
    // Call the setRadius function for each circle
    circle1.setRadius(1); // This sets circle1's radius to 1
    circle2.setRadius(2.5); // This sets circle2's radius to 2.5
    // Call the getArea function for each circle and
    // display the returned result
    cout << "The area of circle1 is " << circle1.getArea() << endl;
    cout << "The area of circle2 is " << circle2.getArea() << endl;
    return 0;
    }

    Output:
    The area of circle1 is 3.14
    The area of circle2 is 19.625

Notice how the class member functions setRadius and getArea use the member variable radius.

They do not need to use the dot operator to reference it because member functions of a class can access member variables of the same class like regular variables, without any extra notation.

Notice also that the class member function getArea only uses, but does not modify, the member variable radius. A function like this, that uses the value of a class variable but does not change it, is known as an accessor.

The function setRadius, on the other hand, modifies the contents of radius. A member function like this, which stores a value in a member variable or changes its value, is known as a mutator.


PROMOTIONS