hey all,

im workin on a text rpg that has become very long and i have all the code on one page. basically i wanna break it down so its more organized and easier to understand. I read some stuff here and gathered that I need to do some research in classes. i havent dipped into classes yet.. mostly just fucntions and loops so far. does anyone know of a good tutorial on classes for beginners that will break it down barney style? i wanna be able to do it without jacking up all my code cus right now my program is working fine i just want to make it more neat. thanks for the help.

http://www.cplusplus.com/doc/tutorial/
http://cplus.about.com/od/learning1/ss/cppobjects.htm
http://www.codersource.net/c/c-tutorials/c-tutorial-class.aspx
Fetched from:
http://www.google.com/search?q=c%2B%2B+classes

I also recommend C++ for Dummies then C++ in a Nutshell (O'Reilly Book)

Since you have a lot written I wouldn't start out writing classes. I would break up the functions so that each one does an individual task. Repeating code can be sorted into one function. Then worry about writing what you've learned.

I'm assuming based on your wording that your program is currently all procedural.

Since it's currently working, I think separating it into header and implementation files would be a good organizational start. That would allow you to re-use your existing code, you would just be putting it in different files. Converting it to OOP will most likely require a major re-write.

Start by creating a header that has your constant definitions and function prototypes in it.
You would go from this:

//function prototypes
void function1(int);
int function2(int, int);

const double MULTIPLIER 2.0;

int main() {
  // ...
}

void function1(int intArg1) {
  //...
}

int function2(int intArg2, int intArg3) {
  //...
  return someInt;
}

To this:

//header.h

#ifndef HEADER_H
#define HEADER_H

//function prototypes
void function1(int);
int function2(int, int);

const double MULTIPLIER 2.0;

#endif //HEADER_H
//main.cpp
#include "header.h"

int main() {
  // ...
}

void function1(int intArg1) {
  //...
}

int function2(int intArg2, int intArg3) {
  //...
  return someInt;
}

Then take it a step further and move your function implementations to their own file:

//header.cpp

#include "header.h"

void function1(int intArg1) {
  //...
}

int function2(int intArg2, int intArg3) {
  //...
  return someInt;
}

Now, you have your code spread over 3 files and you don't have as much code to wade through to find what you're looking for.

To take it a step further, add easily-located comment sections:

//header.cpp

#include "header.h"


/***** IMPLEMENT function1() *****/

void function1(int intArg1) {
  //...
} //END function1()


/***** IMPLEMENT function2() *****/

int function2(int intArg2, int intArg3) {
  //...
  return someInt;
} //END function2()

Edited 5 Years Ago by Fbody: n/a

thanks to both of you.

nice example fbody thats what i was looking for. i'm gonna try seperating it into a bunch of organized smaller files instead of one massive file.

...separating it into header and implementation files would be a good organizational start. That would allow you to re-use your existing code...

Indeed. That's necessary virtually every time I code.

.Start by creating a header that has your constant definitions and function prototypes in it.

I prefer my declarations and definitions in the same file. That way they're easy to keep track of, and there's no confusion as to what's expectable from a header file. It's my understanding that's not standard, but I may have a unique approach to modularization.

i tried breaking it down using the example.. however now i am getting an error in the new .cpp files when i try to compile them. its not recognizing anything, not even cout.. its saying undeclared and erroring out on every line. i assume im missing something in the header of my new .cpp files. do i need to link it to the main.cpp somehow?

yes i am using

using namespace std;

in main.cpp right after the headers

It's usually one of the first couple of errors. What are they? Are you using your compiler properly? For example, the compiler is told about every file it needs to know about.

Edited 5 Years Ago by seanbp: n/a

any variables i declared in main.cpp are saying undeclared in my 2nd .cpp file. i think the problem lies in my 2nd .cpp not being linked to the main at all.. but how do i do so? do i need to make a header file for main.cpp also?

I'm no expert, but I'm thinking you're right about there being a linkage problem. The extern keyword defines external linkage, or something from another file. However, "global variables are evil". source

extern int g_nValue;
extern void foo();

Reading :on the linker, and source hyper-link above.

yea i think i got it figured out.. i just moved all the variables i declared in main.cpp to their respective places in the new .cpp files. hopefully it works out and thanks for the help

The description of your issue is a little cryptic. It sounds like you're missing some headers. Perhaps my example wasn't complete enough, let me expand the header part of it a little:

//original code (expanded)
#include <iostream>
#include <fstream>

using namespace std;

//function prototypes
void function1(int);
int function2(int, int);

const double MULTIPLIER 2.0;

int main() {
  // ...
}

void function1(int intArg1) {
  //...
}

int function2(int intArg2, int intArg3) {
  //...
  return someInt;
}

The revised header file:

//header.h (REVISED)
#ifndef HEADER_H
#define HEADER_H

#include <iostream>
#include <fstream>

using namespace std;

//function prototypes
void function1(int);
int function2(int, int);

const double MULTIPLIER 2.0;

#endif //HEADER_H

The *.cpp files wouldn't change.

Did you remember to #include the header in BOTH *.cpp files?

This article has been dead for over six months. Start a new discussion instead.