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

One of the most common problems encountered by beginning C++ programmers is that they end up using far too many variables. This problem is transferred to object-oriented programs, and the beginner immediately finds that he has variables in the main program and the same variables in the class. Obviously, the two sets of variables are totally different because they have different scope. The programmer finds the only solution is to create public or global variables that can be accessed from anywhere within the program. The whole idea of individual, separate class modules is thrown out the window and the user is virtually back to a linear style of programming.

The correct way to encapsulate data is to use private data members in the class and utilize a constructor to pass by value local variables from main into the class. The member functions can then see and use the data as it is now in scope. In addition to passing data to an object at instantiation, it is also possible to read data into an object using the iostream member function get() and to write data out using the member function set().

The following is a full program listing of the correct method.

// Correct use of Private Data Members

#include <iostream.h>

class Test    {
        private:
            float Area;
            int Height;    //Private DataMembers
            int Length;
public:
            void CalcArea();
            void ShowArea();
                Test(int H, int L);    //Constructor
        };

void Test::CalcArea()
{
    Area = Height*Length;    //Find Area
}

void Test::ShowArea()
{
    cout << “The Area is “ << Area << endl;
}

Test::Test(int H, int L)
{
    Height = H;        //Pass parameters
    Length = L;
}


void main(void)
{
    int Height,Length;

    cout << “Enter a Height “;        //Gather input
    cin >> Height;
    cout << “Enter a Length “;
    cin >> Length;

    Test Square(Height,Length);        //Create instance
    Square.CalcArea();        //Call Member Functions
    Square.ShowArea();

    cout << “Enter a Height “;    //Gather input
    cin >> Height;
    cout << “Enter a Length “;
    cin >> Length;

    Test Box(Height,Length);    //Create Another Instance
    Box.CalcArea();
    Box.ShowArea();

Comments

If you are using global variables, the class becomes useless. The idea of good class design is that each instance of a class is its own single unique entity. Each instance has its own data and member functions to manipulate that data. By using global variables, each instance of the class will be overwritten and use only one piece of data. Therefore, the information used is not necessarily the correct data because another instance of the class might have altered it.

5.3 Use the scope resolution operator?

Problem

Object-oriented programming is very different from standard linear programming. The use of objects requires the programmer to use member functions. Each class will have its own member functions. In standard C++ programs, the programmer uses a prototype to declare a function and then writes a function definition to accompany it. That function is public to the whole program. However, member functions are different. They are functions that belong to a class, making them separate from the rest of the program. In order to use these functions, the programmer must use the scope resolution operator to tell the compiler that this function definition belongs to a particular class. A major problem for programmers new to OOP is knowing where and when to use the scope resolution operator.

Technique

The scope resolution operator (::) can be used in two distinct ways. Both techniques relate to using global members or variables. For example, you might have a program that uses a global variable named Length. However, if you declare a new variable also named Length in a local fashion, you have to use the scope resolution operator to access the global variable.

In relation to classes, the scope resolution operator is used to tie a function to a certain class. For example, you might have two functions called Area with each existing within a different class. You use the scope resolution operator during the definition of each function to specify which class it belongs to. Therefore, Alpha::Area() and Beta::Area() are two different functions located in two different classes.

Steps

1.  You will first examine the use of the scope resolution operator with global variables.
You can tell the compiler to use a global variable rather than a local variable by using the scope resolution operator.
The following example has two variables called Length. One is global and the second is local to function main.
// The Scope Resolution Operator

#include <iostream.h>

int Length  = 789; //Global Variable

void main()
{
    int Length = 123; //Local Variable

    cout << ::Length; //Output Global using ::
    cout << endl;
    cout << Length;   //Output Local;
}

When you run the program both values are output to the screen. For the first value, you have told the compiler to output the global variable by prefixing the variable name with the scope resolution operator. For the second value, you have simply used the variable name. Therefore, the compiler refers back to the local variable Length within the main program.
2.  This example focuses on the use of the scope resolution operator with member functions.
During the construction of a member function definition, you have to specify in the header which class this function belongs to. To achieve this, you must use the scope resolution operator. This tells the compiler the name of the class associated with the function. By doing this, you can have several functions with the same name but attached to different classes.
The following program shows how you can have multiple functions with the same signature. However, they are actually methods of unique classes.
This program has two classes: Alpha and Beta. Alpha takes two integers, adds them together, and then shows the result.
Beta is similar to Alpha except that Beta multiplies the two integers together.
3.  Create the following class named Alpha.
//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);
        };
4.  Now add another class named Beta.
class Beta{
    private:
        int Val1,Val2;
        int total;
    public:
        void Calc();
        void Show();
        Beta(int x,int y);
    };

The classes are similar in that both have private member variables named Val1, Val2, and total. They also have the same member functions (Calc and Show). The constructors accept two integer values as input.


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.