Click Here!
home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
[an error occurred while processing this directive]
Previous Table of Contents Next


How It Works

To get information into an object, you must state the name of the object and the member function within that object that you are referring to.

test.Gather();

First, set up the class definition. The class is called Greatest and has the following capabilities:

1.  Gathering two integer values and storing these values in the private data members first and second.
2.  Calculating which of these two integer values is greater and storing the greater value in the private data member result.
3.  Showing result to the screen.
4.  The constructor initializes result to 0 as an object is created and issues the comfort message New test object created just to prove that the constructor has done its job.
5.  The destructor issues the comfort message Test object destroyed to prove that the object no longer exists.

The main() function contains a while loop that continually creates a new object called test using the class constructor. The user is invited to enter two integer values using the member function Gather(). Those two values are compared using the member function Calc() and the greater value is stored in the data member result. The member function Show() simply displays the result to the screen.

There is an valid point to be made here. Note that you have a definition of the destructor ~Greatest. That destructor is never called into action, but you will see its message on the screen every time the while loop does its job. Destructors are automatically invoked every time an object goes out of scope, and your test object goes out of scope at the end of the while loop.

Here is the full program listing.

// Accessing member functions using dot notation.

#include <iostream.h>

class Greatest {
                private:
                    int first;
                    int second;
                    int result;
                public:
                    Greatest();
                    ~Greatest();
                    void Gather(void);
                    void Calc(void);
                    void Show(void);
                };

Greatest::Greatest()
{
    result = 0;
    cout << “New test object created” << endl << endl;
}

Greatest::~Greatest()
{
    cout << “Test object destroyed” << endl << endl;
}


void Greatest::Gather(void)
{
    cout << “Enter the first number : “;
    cin >> first;
    cout << “Enter the second number : “;
    cin >> second;
}

void Greatest::Calc(void)
{
    if (first > second)
        result = first;
    else
        result = second;
}

void Greatest::Show(void)
{
    cout << “The greatest number is : “ << result
<< endl << endl;
}

main()
{
    char again;

    while(1)
    {
        Greatest test;
        test.Gather();
        test.Calc();
        test.Show();
        cout << “Again : “;
        cin >> again;
        if (again != ‘y’)
            break;
    }
    return(0);
}

Comments

The jargon used in object-oriented programming is difficult to pick up and doesn’t help the novice.

The terms instance and object are used interchangeably. When an object is constructed, it is said to be an instance of the class. In other words, you instantiate a class to create an object. This process can be repeated many times to create many objects from the same class. Don’t forget that a class is the specification, whereas an object is the physical memory within the computer used to create an instance of the class.

In the early days of OOP, the code that manipulated the member variables was called either member functions or methods. Depending upon which camp you came from, only one naming methodology was correct. To laymen, methods and member functions are exactly the same thing. They are computer code used to manipulate data members. Don’t be confused: Member functions and methods are the same thing.

5.5 Know which constructor to use when there are several to choose from?

Problem

In this How-To, you will develop and study a class named Shape. The Shape class finds the area or volume of the following geometric shapes: circle, rectangle, and cube. To do this, you can use the same class but build the object with a different constructor. Depending upon how the constructor definitions are written, you can impose actions upon the class as it instantiates the object.

Technique

A constructor always has the same name as its class. However, you can have as many constructors as you want within the same class as long as they have different signatures. The word signature simply means the constructors have different numbers or data types of input parameters. The technique of using constructors with different signatures is known as overloading. Because of the different signatures, the C++ compiler can differentiate between the constructors and select the one most appropriate for the task of building the object.

Steps

1.  When following constructor is invoked
Shape circle(5);

the compiler spots that only one input parameter is of type integer. The only signature that fits this call is the constructor that has only one input parameter of type integer. Therefore, that constructor is the one used to instantiate the object.
Shape::Shape(int a)
{
    result = 3.143 * a * a;
}
2.  The object is built and given the unique (within this program) variable name of circle (see Figure 5.14).


Figure 5.14  The Shape constructor called with a single integer input leads to this object.

3.  When the following alternative constructor is invoked
Shape square(3,4);

the compiler spots that two input parameters are of type integer. The only signature that fits this call is the constructor that has two input parameters of type integer. Therefore, that constructor is the one used to instantiate the object.
Shape::Shape(int a, int b)
{
    result = a*b;
}
4.  The second object is built and given the unique name of square (see Figure 5.15).


Figure 5.15  The Shape constructor called with two integer input leads to this object.

5.  Finally, when the following constructor is invoked
Shape box(3,4,5);

the compiler spots that three input parameters are of type integer. The only signature that fits this call is the constructor that has three input parameters of type integer. Therefore, that constructor is the one used to instantiate the object.
Shape::Shape(int a, int b, int c)
{
    result = a*b*c;
}
6.  The third object is built and given the name box (see Figure 5.16).


Figure 5.16  The Shape constructor called with three integer inputs leads to this object


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.