![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
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 dont 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 wouldnt 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.
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.
Steps In this example, the actions of constructing an object and invoking its member functions are done in the main body of the program.
|
![]() |
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.
|