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