Overview
EXERCISE 16
Write a class called Distance which contains the declarations for two private variables, an integer called feet and a float called inches.
Declare three public function members:
- A function called set, that has an int argument called ft, a float argument called in and no return.
- A function called get that has no arguments and no return
- A function called show that has no arguments and no return
Write code for the functions using the scope resolution operator :: to show to which class the function belongs:
Write the set function so that the private data feet and inches will be set to the values passed in the arguments.
Write the get function so that user is asked for a value for feet and for inches. The values must be input directly to the class members (in the same statement).
Write the show function so that the values will be output to the screen.
Within the main() function declare three instances of the class called dist1, dist2 and dist3. dist3 will be used later. Use the class function member set to set dist1 to 11 feet 6.25 inches. Use the class function member get to obtain the values for dist2.
I'll forgive the fact that in C++ there is no such thing as a screen...
Print all of the values to the screen using the class member function show.
Make sure that you save your program as it will be used again later.
CONSTRUCTOR AND DESTRUCTORS
Does this mean that if you have int m the compiler reserves space but if you have int m = 0 the compiler first reserves the space and then puts 0 into that space or is it that the compiler reserves the space and magically does something with the 0?
When an instance of a built-in type of variable (such as int) is defined, the compiler creates storage space for that variable. If a value is assigned when reserving space for the variable, the compiler does that too. In effect, the compiler constructs the variable.
When a variable goes out of scope, the memory it occupied is freed.
When a variable of a built-in type goes out of scope, the compiler cleans up the storage for that variable by freeing it; in effect, it destroys the variable.
When an instance of a class is declared, some values may have to be defined.
For example:
What's a data member? It's not been defined!
A bank account class has a data member called "balance" that has to be initialised to zero, when an instance is created. This ensures that when a new account is opened, the customers balance starts at zero and not some arbitary figure.
No it doesn't. A constructor creates an instance of the class - when it is created, the value for balance has to be defined
A constructor is used to do this.
A constructor is a member function that is executed automatically whenever an object is created.
The destructor is used when the class instance goes out of scope. This de-allocates memory that was allocated for the object by the constructor.
#include <iostream.h>
class ExClass
{
private:
int x,y,z;
public:
ExClass() { x = 0; y = 0; z = 0; cout << "Constructing....\n"; }
~ExClass() { cout << "Destructing...\n"; }
};
void main()
{
ExClass a; // constructor called automatically when
// 'a' is defined of type 'ExClass'
{
ExClass b; // called again
}
// b goes out of scope. Destructor called
} // a goes out of scope. Destructor called again
Things to notice:
The constructor member has exactly the same name as the class.
The constructor is called automatically when a variable of the class type is defined.
The destructor member has the same name as the class but is prefixed with the '~' (tilde) character.
There is no such thing as a command in C++
The destructor need not have any commands within it and is called when a variable of the class type goes out of scope.
Both the constructor and destructor can never return a value so the use of void is not needed.
A constructor can have parameters passed to it, a destructor can't.
Both constructors and destructors become more important when using dynamic memory allocation. This will be discussed later.
EXERCISE 17
Add a constructor to the measurement program in the previous exercise which will initialise the class values feet and inches to zero. Add a destructor to the program, before the end of main() to release the occupied memory.
We're dealing with classes, so we have methods (or method functions). This is exercise 16 and the instructions call for int class_name::set(int ft, float in) and two void methods