Broad Network


Pointers to Classes in C++

Object Oriented Programming in C++ – Part 2

Forward: In this part of the series, I show you how pointers work with objects derived from classes.

By: Chrysanthus Date Published: 23 Aug 2012

Introduction

This is part 2 of my series, Object Oriented Programming in C++. An object instantiated from a class is an object similar to the fundamental objects. Such instantiated object can have a pointer pointing to it. In this part of the series, I show you how pointers work with objects derived from classes. What I show you here is a bit more of what you should have learned about pointers and fundamental objects.

You also need basic knowledge in C++ Dynamic Objects. If you do not have that knowledge then read the article I wrote titled, Dynamic Objects in C++. To access the article, type the title and my name, Chrys in the Search box of this page and click Search.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Example
Consider the following code:

#include <iostream>
using namespace std;

class Calculator
    {
        public:
        int num1;
        int num2;

        int add ()
            {
                int sum = num1 + num2;
                return sum;
            }
    };


int main()
    {
        Calculator theObj;
        Calculator *myObject = &theObj;
        myObject->num1 = 2;
        myObject->num2 = 3;
        int result = myObject->add();
        cout << result;

        return 0;
    }

You have the calculator class, which is one of the calculator class descriptions we saw in the previous part of the series. Let us turn our attention to what is in the main function. The first statement instantiates an object of type Calculator, in a way that we saw in the previous part of the series. The next statement creates a pointer object of type, Calculator. Note that the class (Calculator) has been used in these two statements just as an object type. The second statement also assigns the address of the object, theObj, to the pointer, myObject. myObject now holds the address the of the object, theObj.

The really new thing comes in the next three statements. In order to use the pointer to a class object to access the members of the class object, you have to use the arrow operator, -> (negative sign followed by greater than sign) as the three statements show. The syntax to use a pointer to an object (class object) to access a member of the object is,

    pointer->member

            Or

    (*pointer).member

In the second syntax, we have used the dot operator, because we are using the value of the object pointed to by the pointer.

Dynamic Objects
Objects created from classes can be dynamic objects in free store. The following example illustrates this:

#include <iostream>
using namespace std;

class Calculator
    {
        public:
        int num1;
        int num2;

        int add ()
            {
                int sum = num1 + num2;
                return sum;
            }
    };


int main()
    {
        Calculator *myObject = new Calculator;
        myObject->num1 = 2;
        myObject->num2 = 3;
        int result = myObject->add();
        cout << result;

        return 0;
    }

Read through the above code. There are three syntax statements to create dynamic objects with fundamental objects. The three syntax statements are applicable to the class in the same way as follows:


    Class *pointerToClass = new Class;

    Class *pointerToClass = new Class(constructorValues);

    Class *pointerToClass = new Class[noOfElements];

The above code uses the first syntax.

If you have not understood anything in this article, then go back and learn C++ pointers and dynamic object (free store).

Let us end here and continue in the next part of the series.

Chrys

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message