index:   hello1  hello2  hello3  hello4    source:  hello.zip

hello4.cpp

hello4.cpp introduces a member function and tests the copy operation.



#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; }
	void tell() { cout << "I am " + worlds[instance] << endl; }
	int instance;
public:
	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();
	{
		theWorld localWorld;
	}
	theWorld copy = *pworld;
	delete pworld;
	copy.tell();
	cout << "Goodbye from main program.\n";
	cout << "We named " << theWorld::count << " worlds\n";
	return 0;
}


Results

C:\classes\ece538\work\hello>hello4
Hello from Mercury
Hello from main program.
Hello from Venus
Hello from Earth
Goodbye from Earth
Goodbye from Venus
I am Venus
Goodbye from main program.
We named 3 worlds
Goodbye from Venus
Goodbye from Mercury


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