index:   hello1  hello2  hello3  hello4    source:  hello.zip

hello3.cpp

hello3.cpp is identical to hello2.cpp except that we now use private static variables to hold the planet names.


#include <iostream>
#include <string>
using namespace std;

class theWorld {
public:
	theWorld() { instance = count++; cout << "Hello from " + worlds[instance] << endl; }
	~theWorld() { cout << "Goodbye from " + worlds[instance] << endl; }
	int instance;
private:
	static string worlds[];
	static int count;
};

string theWorld::worlds[] =
	{"Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune"};
int theWorld::count = 0;

theWorld myWorld;

int main()
{
	cout << "Hello from main program.\n";
	theWorld *pworld = new theWorld();
	for (int i=0; i<2; i++) {
		theWorld loopWorld;
	}
	delete pworld;
	cout << "Goodbye from main program.\n";
	return 0;
}


Results

C:\classes\ece538\work\hello>hello3
Hello from Mercury
Hello from main program.
Hello from Venus
Hello from Earth
Goodbye from Earth
Hello from Mars
Goodbye from Mars
Goodbye from Venus
Goodbye from main program.
Goodbye from Mercury


Maintained by John Loomis, updated Tue Jan 23 20:45:07 2007