Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,234 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,632 people online right now. Registration is fast and FREE... Join Now!




Using Destructors... Most likely very easy

2 Pages V  1 2 >  
Reply to this topicStart new topic

Using Destructors... Most likely very easy

oat
12 Mar, 2007 - 04:38 PM
Post #1

New D.I.C Head
*

Joined: 30 Jan, 2007
Posts: 20


My Contributions
These are the instructions, any help would be appreciated...

QUOTE
Create a class named Car. The Car class contains a static integer field named count. Create a constructor that adds 1 to the count and displays the count each time a Car is created. Create a destructor that subtracts 1 from the count and displays the count each time a Car goes out of scope. Write a Main() function that declares an array of five Car objects. Output consists of five constructor messages and five destructor messages, each displaying the current count.


This is what I have so far...

CODE
// Car.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>

using namespace std;

class Car
{
private:
    int count;
public:
    Car();
    ~Car();
};
Car::Car()
{
    count = count + 1;
    cout<<count<<" Car Objects Exist"<<endl;
    
}
Car::~Car()
{
    count = count - 1;
    cout<<count<<" Car Objects Exist"<<endl;
}

int main()
{
    Car aCar[5];
}

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Using Destructors... Most Likely Very Easy
12 Mar, 2007 - 05:07 PM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
Can you specify the problem you are encountering?
User is online!Profile CardPM
+Quote Post

oat
RE: Using Destructors... Most Likely Very Easy
12 Mar, 2007 - 05:14 PM
Post #3

New D.I.C Head
*

Joined: 30 Jan, 2007
Posts: 20


My Contributions
I was having a problem where I couldn't figure out how to get count to = 1 and have it add and subtract from it, but I figured it out biggrin.gif
User is offlineProfile CardPM
+Quote Post

GWatt
RE: Using Destructors... Most Likely Very Easy
12 Mar, 2007 - 05:16 PM
Post #4

human inside
Group Icon

Joined: 1 Dec, 2005
Posts: 2,199



Thanked: 20 times
Dream Kudos: 450
My Contributions
You probably want to use the keyword static and then you'll have to find some way to initialize it, because you can't do it in the class definition;
User is online!Profile CardPM
+Quote Post

oat
RE: Using Destructors... Most Likely Very Easy
12 Mar, 2007 - 05:25 PM
Post #5

New D.I.C Head
*

Joined: 30 Jan, 2007
Posts: 20


My Contributions
actually, I have a problem again... This is my code, it worked until I put in the system("Pause"); at the end... It has to be there for my instructor, he says it won't open on his system if it isn't there...

This is the error I get

QUOTE
error C2447: '{' : missing function header (old-style formal list?)


CODE
// Car.cpp : Defines the entry point for the console application.
// Michael Otis - DMACC C++
// Chapter 8 - Exercise 8

#include "stdafx.h"
#include <iostream>
using namespace std;

class Car
{
    private:
        static int count;
    public:
        Car();
        ~Car();
};
Car::Car()
{
    count = count + 1;
    cout<<count<<" Car Objects Exist"<<endl;
}
Car::~Car()
{
    count = count - 1;
    cout<<count<<" Car Objects Exist"<<endl;
}
int Car::count = 0;

int main()
{
    Car aCar[5];
    
}
{
system("Pause");
}


This post has been edited by oat: 12 Mar, 2007 - 05:26 PM
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Using Destructors... Most Likely Very Easy
12 Mar, 2007 - 05:39 PM
Post #6

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
CODE

int main()
{
    Car aCar[5];
system("Pause");
}

You had placed your system call after the closure of the main function - it should be in the main function.
User is online!Profile CardPM
+Quote Post

oat
RE: Using Destructors... Most Likely Very Easy
12 Mar, 2007 - 07:18 PM
Post #7

New D.I.C Head
*

Joined: 30 Jan, 2007
Posts: 20


My Contributions
I had that, but it didn't come out the way I wanted it too.... it came out like this... I don't want it split in two like that...

IPB Image
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Using Destructors... Most Likely Very Easy
13 Mar, 2007 - 04:18 AM
Post #8

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
Please post your updated code.
User is online!Profile CardPM
+Quote Post

oat
RE: Using Destructors... Most Likely Very Easy
13 Mar, 2007 - 04:52 AM
Post #9

New D.I.C Head
*

Joined: 30 Jan, 2007
Posts: 20


My Contributions
CODE
// Car.cpp : Defines the entry point for the console application.
// Michael Otis - DMACC C++
// Chapter 8 - Exercise 8

#include "stdafx.h"
#include <iostream>
using namespace std;

class Car
{
    private:
        static int count;
    public:
        Car();
        ~Car();
};
Car::Car()
{
    count = count + 1;
    cout<<count<<" Car Objects Exist"<<endl;
}
Car::~Car()
{
    count = count - 1;
    cout<<count<<" Car Objects Exist"<<endl;
}
int Car::count = 0;

int main()
{
    Car aCar[5];
    system("Pause");
}

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Using Destructors... Most Likely Very Easy
13 Mar, 2007 - 05:12 AM
Post #10

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
Well, you should never be explicitly calling destructors - they are called automatically as the object goes out of scope. In your case, apparently your instructor wishes to see a message from the destructor. The simplest workaround would be to refine the scope by judiciously using braces to force the object out of scope before your program ends. Try this:
CODE

int main()
{
    {
    Car aCar[5];
    }
    system("Pause");
    return 0;
}

You may also wish you have a look at the following:

http://www.parashift.com/c++-faq-lite/dtors.html

User is online!Profile CardPM
+Quote Post

AmitTheInfinity
RE: Using Destructors... Most Likely Very Easy
13 Mar, 2007 - 05:16 AM
Post #11

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,026



Thanked: 35 times
Dream Kudos: 125
My Contributions
QUOTE(oat @ 13 Mar, 2007 - 06:22 PM) *

CODE

// Car.cpp : Defines the entry point for the console application.
// Michael Otis - DMACC C++
// Chapter 8 - Exercise 8

int main()
{
    Car aCar[5];
    system("Pause");
}




In the code above you are creating array of 5 car objects so the constructor for class car gets called successively 5 times.
and after pause the program finishes where scope of all 5 objects also finishes so all are destroyed successively, that's why you see that kind of output there.

if you want to see some kind of output where there is a mix of creation and destruction of objects then you can do this.

* create one object of car object in main.

* then call some function from main and create a car object inside it [if you want print it also smile.gif ]

* then have some internal block [maybe a for loop or something] where you create objects inside it. [the declaration and definition both should be inside].

* create one more instance of a car in main at the end.

* pause.

this will create good mixture of creation and destruction of objects.

Hope this will help you.
else....
We will meet here again. biggrin.gif
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Using Destructors... Most Likely Very Easy
13 Mar, 2007 - 05:23 AM
Post #12

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
Any form of scope termination will work - a closing brace will force scope termination.
User is online!Profile CardPM
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 12/4/08 04:02PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month