PDA

View Full Version : void n00b_question;


c:\>format c:
12-10-2006, 02:48 AM
Hello everyone,

1st post here.
Quick background::: I'm taking Computer Programming II courses right now. Mostly C++. I do not expect you guys to do my homework, and I think that's great. I've just got some apparently 'basic' questions.

Last class lab, I was so and so lost, that I picked-up my stuff and left in total dismay. :(
I just couldn't crack it. :(
I dunno what happened.


MY PROBLEM::::
I coded last semester C++ fine, very basic stuff. The most complicated we got was with classes and destructors.
But I did everything under MS Visual C++ or a similar program.
Now in this CompProgrammingII class, we're using MS Visual Studio 2005.
This is where I'm completely at loss.

Do I have to create a separate "name1.h" file?
What do I consider the "code1.cpp" file?
I thought that the *.cpp was a program.
I remember we called the libraries (*.h) at the start of my .cpp files.
But... now I create a new .h file? :confused:

SIDE-QUESTION::: Would you guys suggest a specific software for coding?
The school uses MS Visual Studio... I've got the latest version I can install.
I also have MS C++ Express (I think that's the name).
I know hardcore would be using VIMeditor, but... I'm nowhere near there.

As you can see, it is definitely a super n00b question.
Once I've got figured these out, I can start hammering away my buggy code.

Thanks in advance for the help guys,
I hope I can become an active and helpful member of these forums :)

-AJ

Gox
12-10-2006, 03:47 AM
Alright, let's talk header files (.h files)

.h files or header files generally include just the definitions for methods, and variable declarations that another file will implement. The implementation for these method definitions generally go in a file that has the extension .cpp. The .cpp file is where all the 'real' code goes.

Let's look at a small example.

//Sample.h file
#include <string>
#include <iostream>
using namespace std;
class Sample{

//Public methods
public:
//Constructors / Deconstructors
Sample();
virtual ~Sample();

//Methods
void printName();

//Protected methods
protected:
//Methods
void initialize(); //A method to initialize state variables of the class


//Private state, and methods
private:
//State
string _name;
};


//Sample.cpp file
#include "Sample.h"

/**
* The Constructor for the Sample Class
**/
Sample::Sample()
{
this->initialize();
}

//Deconstructor
Sample::~Sample()
{
//Deconstructor
}

/**
* A method to initialize all necessary state for the Sample Class
**/
void Sample::initialize()
{
this->_name = "Gox";
}

/**
* A method to print the name variable to the screen
**/
void Sample::printName()
{
cout << this->_name << endl;
}


//main.cpp file
#include "Sample.h"
using namespace std;

int main(int argc, char **argv) {
Sample s;
s.printName();

return 0;
}

You'll notices that Sample.h declares the class Sample and it's State (variables) and methods, but nothing more. Sample.cpp adds the code that implements the method declarations that are in Sample.h. SampleMain.cpp is the main file that uses the class Sample in some manner. In this example, the main file simply creates an object Sample and then calls the printName method which prints Gox to the screen.

As for which IDE to use, my only suggestion is to use one that you're comfortable with. For me that means the following; if I'm coding in Windows I usually use DevCpp which is free. If I'm coding on Unix it's usually XEmacs. The only reason I use those two is because I know my way around them not because they are necessarily better than any other.

Good Luck.

c:\>format c:
12-10-2006, 04:02 AM
Gox,

Thank you very much for taking the time to write that.
I read your text, now I'm going to give some study to your code.

I really appreciate this.
THANK YOU!

I'll keep this up-to-date with my questions.
Again, thanks man... I feel like now I've got a chance. :)

I'm gonna hit the nitty-gritty for a while, and I'll scream when I encounter more problems. :) :)
Thanks,
Peace
AJ

c:\>format c:
12-17-2006, 11:54 PM
I'm here... I'm trying... :)