It appears you have not yet registered with our community. To register please click here.

Origin XT RPG Network Home

XT Project Introduction
Blah blah intro to this place. To be composed later.


C++ - Tutorial, Lesson 6, A slight change of plans...

May 10 2007, 04:55 AM (Post #1)
Well why can't we do the shuffle?!
* * * * * *
Posts: 1,679
Cash: 183,455 / 0
Group: Nobility
Joined: 7/12/03 08:49 PM

Lesson 6 - Header files, constructors/destructors, and other fun stuff

In this lesson I was supposed to be teaching you about header files and constructors/destructors. However, I'm not going to because they're so easy that I cannot devote a whole lesson to them, so I'll just cover them really quickly.

What is a header file?

A header file is a file that keeps all of your classes separated from your main.cpp file. This makes things neat and pretty. To be able to use your classes from a header file, in your main.cpp file just do this at the top:
#include "myheader.h"

Constructors—what are they?

These go inside of the class. So let's make a sample class to demonstrate.

Example One


class myclass
myclass(); //constructor, must have same name as class
int* a; //pointers, we'll discuss later
int* b;
int* c;
cout<<"We are born"<<endl; //to see when program is in constructor
delete a;
delete b;
delete c;
cout<<"We are dead."<<endl; //to see when it is in destructor

Basically constructors set values to memory and destructors destroy them when they are not being used any longer. This won't make sense until we do pointers, and I'm not so sure I'm ready for that to be honest. They are extremely complicated and difficult to explain. Pointers and dynamic memory allocation are the bread and butter of C++, and no other language is as good at them as C++, not even JAVA. JAVA has a dumbed down version of pointers that do not have the same power, which is one of the reasons why JAVA should not be used to make an OS.

Command Line Prompting

Now that's done, we can move onto another thing that I find to be particularly cool, but didn't deserve its own lesson. We move onto command line prompting. You know how we've been making DOS programs and some of them ask you for inputs, and when you're checking and checking to see if a program works, you must keep answering these questions and inputing values. Well, no more! With command line prompting, a very simple tool to use, you can input the name of the program in the command line and corresponding values and let the program do the rest. No more nagging questions, no more having to use if, then statements or switches for choices, and no more hastle. So here is how it is done:

Example Two


#include <iostream>
using namespace std;

int main(int argc, char ** argv)
//could also be: int main(int argc, char * argv[]) they are the same

//we want to have 2 inputs in this

if (argc < 2)
cout<<"Incorrect amount of inputs!"<<endl;
//we'll actually have three inputs - program, integer, integer
//but the counter starts at 0 so
//program = 0, integer = 1, integer = 2
//the placeholders are like an array because argv is an array
//or rather it is a pointer of a pointer, a dynamic array
//we'll discuss this in detail later

int a, b;

a = atoi(argv[1]); //atoi is ascii to integer, we must do this because
b = atoi(argv[2]); //argv is a character array

cout<<a<<" "<<b<<endl;

return 0;

Ok, so compile this program and load up your command prompt. Go to the directory this file is in and type in: filename (without .exe at the end) number number and it should return the numbers to you. Pretty cool huh?


So, I know this was a quick lesson, but C++ often includes various little topics that just do not take that long to explain. Remember, if you have any questions just post here or PM me or something, I'll be happy to explain. The next lesson will be optional, but it's very interesting, if you still don't wish to participate in it you can skip to lesson 8.

Next Lesson:
Lesson 7 - Project: Making a File Splitter/Merger Program
Lesson 8 - Pointers, Linked Lists, and Dynamic Arrays

Have a lovely day!


This post has been edited by Jinghao: Jun 16 2007, 12:59 AM
Post Options