C++ questions...early C++ (console stuff)

sjn78

Junior Contributor
Joined
May 4, 2003
Messages
255
Location
Australia
I'm taking a C++ subject for uni and have a couple of questions if anyone canhelp me out.

Don't worry, its not an assignment question!! I don't have any troubles with the syntax.

What's the purpose of makefile's. Does it clean up your source code that you have lying around and produce a better exe or am I talking dribble?

I would ask the lecturer, but I am studying by correspondence and it makes it hard to ask questions.


I must say, I have never actually 'learnt' about classes before and just 2 weeks into the subject, I have learnt so much about them and how they are used. Can't wait until I get back into .NET to try using this stuff properly.
 
Well I was way off. lol

I have also noticed the exe's built are huge. I have a small main procedure and then a separate .h file that I use which is only a screen full of code, but when compiled it is nealry .5mb

Whats the deal with that?
 
Debug builds are often much larger than release builds (no debug stuff plus code optimisation takes place). Also most C++ code will involve linking to various runtime libraries - will makea big difference if you link to the dll version or the static version.
 
OK thanks.

I have a pratice exercise that is not working. Its just to make a class and well, you will probably understand when you see it. The problem is I cant get the getCount() to work. I think it is returning the address of the variable not the value. It is suppose to be an int that keeps count of how many objects have been created. I have tried several diffferent things but nothing is working.

C#:
#include <iostream>
#include <string>

using namespace std;

class Tool {
    public:
        Tool(const string& t);
        const string& name() const;  // Gets ToolName
        const int getCount() const;  // Gets ToolCount
    private:
        string toolName;
        int toolCount;
};

// Tool constructor
Tool::Tool(const string& t) {
    toolName = t;
    toolCount ++;
}

// Retrieve Tool Name
const string& Tool :: name() const {
    return toolName;//.c_str();
}

// Retrieve Tool Count
const int Tool :: getCount() const{
    return toolCount;
}


int main() {

    //Testing the class

    Tool t1("hammer");
    cout << "Tool Name - " << t1.name() << endl;
    cout << "Tool Count - " << t1.getCount() << endl;
    Tool t2("spade");
    cout << "Tool Name - " << t2.name() << endl;
    cout << "Tool Count - " << t2.getCount() << endl;
    Tool t3("rake");
    cout << "Tool Name - " << t3.name() << endl;
    cout << "Tool Count - " << t3.getCount() << endl;

}
If anyone can have a look and see what is going on, I would be very grateful for your help.
 
I wouldn't imagine you'd want either of the "const" keywords on your definition of getCount. Just make it:
int Tool::getCount()
{
return toolCount;
}

-nerseus
 
I tried that firstly and this is my output:

Tool Name - hammer
Tool Count - 1
Tool Name - spade
Tool Count - 863866
Tool Name - rake
Tool Count - 794197



Thanks again
 
Back
Top