--> Frank Masingill wrote to Cliff Rhodes <--
FM>Hey, Cliff and gang. How's this?
Frank, it looks pretty good. You've got a functioning class!
FM>Too Borland spcific?
The only Borlandism is clrscr(), so that's not too bad.
FM>#include
FM>#include
FM>#include
FM>class crazy
FM>{
FM> private:
FM> int pooky, kooky;
FM> float smoky;
FM> public:
FM> void install(int p, int k, float f)
FM> {
FM> pooky = p; kooky = k; smoky = f;
FM> }
FM> void display()
FM> {
FM> cout << "Pooky = " << pooky << endl;
FM> cout << "Kooky = " << kooky << endl;
FM> cout << "Smoky = << $"
FM> << setiosflags(ios:: showpoint)
FM> << setprecision(2)
FM> << setw(2)
FM> << smoky;
FM> }
FM>};
FM>
FM>void main()
FM>{
FM> crazy kookie, lookie, smooky;
FM> kookie.install (100, 200, 4.50);
FM> clrscr();
FM> kookie.display();
FM>}
Here is my version:
#include
#include
#include
class crazyII
{
private:
int pooky, kooky;
float smoky;
public:
// Instead of an Install() function, I used a constructor. It is
// just like your Install() except it is automatically called when
// a crazyII object is created--so no c.Install(x, y, z); is needed.
// You should use constructors for this reason.
// Also notice I have given default values for each of the
// arguments. C++ allows this in functions. That means you can
// use this constructor to create crazyII objects by giving 0, 1,
// 2, or 3 arguments. For the ones not given, the default value is
// used. Note that you can only drop arguments from right to left.
// You could not skip an int value and put a float for instance.
crazyII(int p = 0, int k = 0, float f = 0.0f)
{
pooky = p; kooky = k; smoky = f;
}
// Instead of a Display() function, I overloaded the << operator.
// This is much more convenient. The notation is a little
// complicated to do this, but using the result is easy.
friend ostream & operator << (ostream & os, crazyII & c)
{
// Given the output stream os, use its << operator to
// display object c's members.
os << "Pooky = " << c.pooky << endl;
os << "Kooky = " << c.kooky << endl;
os << "Smoky = "
<< setiosflags(ios:: showpoint)
<< setprecision(2)
<< setw(2)
<< c.smoky << endl;
return os; // Return the reference to the ostream
}
};
int main(void)
{
crazyII c1; // Create a crazyII with all default values.
crazyII c2(25, 50, 3.1415); // crazy II object with my values.
clrscr();
cout << c1; // Use overloaded << to display c1
cout << c2;
return 0;
}
Cliff Rhodes
cliff.rhodes@juge.com
crhodes@flash.net
X CMPQwk 1.42 1692 X"The wisest men follow their own direction." - Euripides
--- Maximus/2 3.01
---------------
* Origin: COMM Port OS/2 juge.com 204.89.247.1 (281) 980-9671 (1:106/2000)
|