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


5.  Now add the function definitions for Alpha’s member functions. At this point, the scope resolution operator is used.
void Alpha::Calc()
{
    total = 0;
    total = Val1 + Val2;
}

void Alpha::Show()
{
    cout << “The total is “ << total << endl;
}

Alpha::Alpha(int x, int y)
{
    Val1 = x;
    Val2 = y;
}

Note how the scope resolution operator works. You specify the name of the class to which the member function belongs. Next, insert the scope resolution operator, and then insert the name of the function (see Figure 5.10).


Figure 5.10  The scope resolution operator.

6.  Now, insert the definitions for the Beta member functions.
void Beta::Calc()
{
    total = 0;
    total = Val1 * Val2;
}

void Beta::Show()
{
    cout << “The Product is “ << total << endl;
}

Beta::Beta(int x, int y)
{
   Val1 = x;
   Val2 = y;
}

As you can see, the definitions are virtually identical but are differentiated by the use of the scope resolution operator to relate them to a unique class.
7.  Finally, you must add a main function that will call instances of the two classes using the member functions.
void main()
{
    int Num1,Num2;

    cout << “Enter Number 1 “;
    cin >> Num1;
    cout << “Enter Number 2 “;
    cin >> Num2;

    Alpha First(Num1,Num2);
    First.Calc();
    First.Show();

    Beta Second(Num1,Num2);
    Second.Calc();
    Second.Show();
}

This function has two local variables named Num1 and Num2. They are passed to the constructors of the two classes when the instance is created. The member functions are then used to manipulate the data.

How It Works

The private member variables and public member functions are declared in the class declarations. The member functions become methods of the class they belong to.

In the definitions of the member functions, you use the scope resolution operator to specify the class to which the member function belongs. By doing this, you can have member functions that have the same names but are, in fact, individual methods of their class.

In the preceding program, you created an instance of Alpha named First. You then used dot notation to invoke the member functions of Alpha. Note that only the member functions of class Alpha are being used at this point. In reality, the member functions for Beta don’t exist yet; they are not created until the class is instantiated.

Finally, an instance of class Beta is created and its member functions are used to perform another calculation using the same two pieces of data.

//The Scope Resolution Operator

#include <iostream.h>

class Alpha{
            private:
                int Val1,Val2;
                int total;
            public:
                void Calc();
                void Show();
                Alpha(int x,int y);
};
class Beta{
            private:
                int Val1,Val2;
                int total;
            public:
                void Calc();
                void Show();
                Beta(int x,int y);
        };

void Alpha::Calc()
{
    total = 0;
    total = Val1 + Val2;
}

void Alpha::Show()
{
    cout << “The total is “ << total << endl;
}

Alpha::Alpha(int x, int y)
{
    Val1 = x;
    Val2 = y;
}

void Beta::Calc()
{
    total = 0;
    total = Val1 * Val2;
}

void Beta::Show()
{
    cout << “The product is “ << total << endl;
QQ again, change sum to “product”.  9/28 DM
}

Beta::Beta(int x, int y)
{
    Val1 = x;
    Val2 = y;
}
void main()
{
    int Num1,Num2;

    cout << “Enter Number 1 “;
    cin >> Num1;
    cout << “Enter Number 2 “;
    cin >> Num2;

    Alpha First(Num1,Num2);
    First.Calc();
    First.Show();

    Beta Second(Num1,Num2);
    Second.Calc();
    Second.Show();
}

Comments

In this example, you created two classes with member functions using the same name. This is the easiest way to show how to use the scope resolution operator in order to distinguish the member functions from each other. In reality, you wouldn’t create two classes like this (one to add two numbers together and another to multiply them). The easiest way would be to create one member function for each different calculation.

5.4 Use dot notation to access the member functions of an object?

Problem

Anyone used to working with traditional-style programs, whether those programs are linear or procedural, finds it potentially confusing to work with objects. An object is a mini-program in its own right and carries its own member variables and functions. Member functions are accessed with dot notation, whereas no dot is used when constructing the object. Why is this so?

Technique

The constructor is used to create an instance of the class and give that instance a unique name. Dot notation is not used to achieve this action. This has the effect of allocating enough computer memory to hold all the member variables and the code for the member functions of the class. After it has been created, or instantiated, that memory block is known as an object. Figure 5.11 shows the constructor in action.


Figure 5.11  The constructor allocates memory and attaches a unique name to an object.

After an object is created, you need to access individual member functions within it. This is done using what is known as dot notation. Dot notation tells the compiler which member function is being called. At machine level, the member function name is a symbolic reference that specifies how far the member function code is removed from the start of the object memory block. This is shown in Figures 5.12 and 5.13.


Figure 5.12  The member function Gather is removed by this number of bytes from the starting base address of the object test.


Figure 5.13  The member function Show is removed by this number of bytes from the starting base address of the object test.

Steps

In this example, the actions of constructing an object and invoking its member functions are done in the main body of the program.

1.  The first step is to create the object with this line of code. Do not use dot notation to create an object. This line is used to allocate computer memory.
Greatest test;
2.  Then invoke the Gather() member function using dot notation.
3.  Invoke the Calc() member function, again using dot notation to perform an action.
test.Calc();
4.  The Show() member function is invoked to display the data member result to the screen. Like all member functions, it is invoked using dot notation to separate the object name and the required member function.
test.Show();


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.