Jump to content
  • Advertisement
Sign in to follow this  

destructor problem (round 2)

This topic is 4725 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

first off, thank you to the people of this forum for helping me, but it seems i ran into another problem! i have commented my questions. from listing 12-4.h

#include <iostream>
using namespace std;

enum BREED { GOLDEN, CAIRN, DANDIE, SHETLAND, DOBERMAN, LAB };

class Mammal
{
public:
	Mammal();
	Mammal(int age);
	virtual ~Mammal();

	int GetAge() const { return itsAge; }
	void SetAge(int age) { itsAge = age; }
	int GetWeight() const { return itsWeight; }
	void SetWeight(int weight) { itsWeight = weight; }

	void Speak() const { cout << "Mammal sound!\n"; }
	void Sleep() const { cout << "shhh. I'm sleeping.\n"; }
protected:
	int itsAge;
	int itsWeight;
};

class Dog : public Mammal
{
public:
	Dog();
	Dog(int age);
	Dog(int age, int weight);
	Dog(int age, BREED breed);
	Dog(int age, int weight, BREED breed);
	virtual ~Dog();


	BREED GetBreed() const { return itsBreed; }
	void SetBreed(BREED breed) { itsBreed = breed; }

	void WagTail() const { cout << "Tail wagging...\n"; }
	void BegForFood() const { cout << "Begging for food...\n"; }
private:
	BREED itsBreed;
};


Mammal::Mammal():
itsAge(1),
itsWeight(5)
{
	cout << "Mammal constructor...\n";
}

Mammal::Mammal(int age):
itsAge(age),
itsWeight(5)
{
	cout << "Mammal(int) constructor...\n";
}

Mammal::~Mammal()
{
	cout << "Mammal destructor...\n";
}

Dog::Dog():
Mammal(),
itsBreed(GOLDEN)
{
	cout << "Dog constructor...\n";
}

Dog::Dog(int age):
Mammal(age),
itsBreed(GOLDEN)
{
	cout << "Dog(int) constructor...\n";
}

Dog::Dog(int age, int weight):
Mammal(age),
itsBreed(GOLDEN)
{
	itsWeight = weight;
	cout << "Dog(int, int) constructor...\n";
}

Dog::Dog(int age, int weight, BREED breed):
Mammal(age),
itsBreed(breed)
{
	itsWeight = weight;
	cout << "Dog(int int, BREED) constructor...\n";
}

Dog::Dog(int age, BREED breed):
Mammal(age),
itsBreed(breed)
{
	cout << "Dog(int Breed) constructor...\n";
}

Dog::~Dog()
{
	cout << "Dog destructor...\n";
} //i tried this to try to get this destructor to show, but it didn't work
//how do i get this to show, i've tried the pointers, but 


and for listing 12-4.cpp

#include "listing 12-4.h"

/*
Mammal::~Mammal()
{
	cout << "Mammal destructor...\n";
}

Dog::~Dog()
{
	cout << "Dog destructor...\n";
}
*/
//i commented this out to avoid ambiguous invocation 


int main()
{
	/*
	Dog *fido = new Dog;
	Dog *rover = new Dog;
	Dog *buster = new Dog;
	Dog *yorkie = new Dog;
	Dog *dobbie = new Dog;
	rover->SetAge(5);
	buster->SetAge(6);
	buster->SetWeight(8);
	yorkie->SetAge(3);
	yorkie->SetBreed(GOLDEN);
	dobbie->SetAge(4);
	dobbie->SetWeight(20);
	dobbie->SetBreed(DOBERMAN);
	fido->Speak();
	rover->WagTail();
	cout << "Yorkie is " << yorkie->GetAge() << " years old\n";
	cout << "Dobbie weighs ";
	cout << dobbie->GetWeight() << " pounds\n";
	*/

	Dog fido;
	Dog rover(5);
	Dog buster(6,8);
	Dog yorkie(3,GOLDEN);
	Dog dobbie(4,20,DOBERMAN);
	fido.Speak();
	rover.WagTail();
	cout << "Yorkie is " << yorkie.GetAge() << " years old\n";
	cout << "Dobbie weighs ";
	cout << dobbie.GetWeight() << " pounds\n";
	system("PAUSE");
	return 0;
}


i need to know what i am doing wrong now...

Share this post


Link to post
Share on other sites
Advertisement
Well, you didn't post exactly what your problem was. However, if the issue is that you aren't getting the messages that the destructors are being called, there is an issue with that. Those objects won't be destroyed (and their destructors won't be called) until the program exits, which happens AFTER you call system("PAUSE"), so you won't see the output. In fact, if you are running this in the windows environment, they'll show up and disappear nearly instantly. You need to create your own console window, navigate to the executable manually, and run it that way.

Share this post


Link to post
Share on other sites
Quote:
Original post by Tibre
Well, you didn't post exactly what your problem was. However, if the issue is that you aren't getting the messages that the destructors are being called, there is an issue with that. Those objects won't be destroyed (and their destructors won't be called) until the program exits, which happens AFTER you call system("PAUSE"), so you won't see the output. In fact, if you are running this in the windows environment, they'll show up and disappear nearly instantly. You need to create your own console window, navigate to the executable manually, and run it that way.


Tibre is correct. You can simply make an artifical scope so the objects get destroyed before the program exits like this:


{//ooh yay artificial scope
Dog fido;
Dog rover(5);
Dog buster(6,8);
Dog yorkie(3,GOLDEN);
Dog dobbie(4,20,DOBERMAN);
fido.Speak();
rover.WagTail();
cout << "Yorkie is " << yorkie.GetAge() << " years old\n";
cout << "Dobbie weighs ";
cout << dobbie.GetWeight() << " pounds\n";
}//no more artificial scope :[
system("PAUSE");
return 0;

Share this post


Link to post
Share on other sites
Or better yet, forget about system("PAUSE"), and run your program from the command line (e.g. under Windows XP: Start->Run... "cmd", then locate your program and execute it at the DOS prompt), as console apps are intended to be run.

Or, if you'd like double-clickiness, make a batch file for it:

(myprogram.bat - goes in same folder as the compiled exe, here assumed to be named myprogram.exe - adjust according to whatever you compiled it as)

myprogram
pause

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!