![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
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
|
![]() |
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.
|