C++ programming Help

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
TreyOne Games
Posts: 18
Joined: Wed Dec 14, 2005 12:44 am

C++ programming Help

Post by TreyOne Games »

I figured i start this for all the Noob's still learning 2 program :)

this is just a reference 2 help you better under stand some of the more advanced tut's and demo's :)

1- C++ Structure
(found this at http://homepages.ius.edu/JDRUIN/html_pa ... ucture.htm )



--------------------------------------------------------------------------------

C ++ Structure


--------------------------------------------------------------------------------



C++ is still one of the most powerful languages in use today. This, like anything, has a trade off. C++ can be difficult to learn because it is not very forgiving; especially not to beginners. This page is designed to help beginners by providing examples. We are all teachers and learners of something. Feel free to copy some or all of the functions. Help others when they need help.
This is a reference page for beginners to see sample code. I recommend an instructional book such as Sam's Teach Yourself C++ in 21 days from Liberty Publishing to supplement self study. Files available on this site are currently kept in PDF. Download a free viewer from Adobe if needed:
Download Adobe Acrobat Reader©


--------------------------------------------------------------------------------

Basic Structure of Examples:

A C++ program is usually made up of three files, the header, the driver, and the implementation file. Some of the reasons include:

Reusability of the code is easier if:


only functions are kept in one file (implementation)

only headers are kept in another (header file)

all other code is kept separate in yet another file (driver)



If functions and headers are kept separate, these files can be "included" in any number of projects without ever looking at the code again. This is where the #include statement comes into play. When the #include statement is used to call <iostream.h>, you are just using code some other programmer has already written and compiled, then you are inserting that code into your project to serve some purpose.



Implementation and Header Files can even be compiled in advance (in nearly any language) and then called from any program that wishes to use them.



If a C++ implementation file is compiled in advance, even another programming language could call those functions. VB could call the quick sort of C++ for example. VB cannot normally solve problems recursively but has fantastic user interfaces. Programmers can get the best of both worlds with precompiled, well-filed code.




Code is easier to debug if "like code" is kept separate from "unlike code".




When one programmer wants to reuse another programmer's code, having to cut and paste is inefficient. Keeping functions separate from "main()" makes life easier for everyone involved.



In simple programs, these C++ program files are generally:

Header File: Function prototypes are kept here. A detailed discussion of header files is in the first example (PDF). A prototype is sometimes called a signature.

Implementation File: Functions that are "called" are defined here except "main()". Main is a special function that is called by the operating system.

Driver File: Also called the client file. "main()" lives here. The operating system looks for main() inside this file as soon as execution begins. The header files need to be "included " (#include) in your driver file, just above main().



You can imagine these files to look something like this:



Header.h

I am the Header File:
I store function prototypes. I tell main() what to expect from function that are stored in the implementation file. When main() calls one of these functions, I have already told main() what kind of memory to allocate. I have to be included in the driver file above main(). I am saved in the same folder as the driver and implementation files.


Driver.cpp

I am the Driver File:
Main() lives here. I might look something like this:

#include "the_header.h" // for my own functions
#include <iostream.h> // for cout and cin
void main()
{
call functions in the implementation file. I can do that since Header.h already told me how to use them.;
do stuff;
}


Implementation.cpp

I am the Implementation File:
Any functions called from main are stored here. I only contain functions. If you want to reuse functions, just include me in your next project. You will save lots of time and avoid cutting and pasting.




A note on #include:

#include basically cuts whatever is in your header.h file and paste it in place of the #include statement.


This explains why you get an error on the driver file when you leave a ";" off of the last statement in the header file. The header file is stuck, line by line, in the exact place where the #include statement resides. The error is "transferred" into the area where the #include was.


This also explains why the #include statements must be in very particular locations. #including the header is exactly the same as if you simply wrote the entire header file right there where that #include statement is located.


Why not just write the code and skip the #include stuff? This C++ code is already hard enough to read as it is. Keep the code separate and work on one file at a time. There a plenty of reasons why C++ is hard. Do not make it anymore difficult by stuffing all the code into one huge file.


A header file is necessary. When different people are working on other functions, they need your header to know how your functions work so they can write there code while yours is still in development. Having separate files is a necessity, even if it seems strange now.



When Classes are present, files are broken down even further.

Class Header File: The class is defined here.



Class Implementation File: The functions that belong to the class are defined here. They are called by objects using the "." (dot) operator. The programmer who wrote this code is usually not the one currently using the code. The programmer who wrote the class code is the author of the class and the programmer using the class code is the client. Both people are programmers. The client is using the code of another programmer so that the client does not have to rewrite code already in existence. You are your own client when you write both the class and then write a driver to demonstrate your class.



Client Driver File: "main()" lives here. The programmer that is using the class is accessing the class object through their own program. This programmer is the client. The class was (usually) written by some other programmer long before. The user of the class writes his or her code here in the client driver file. The client uses objects just like they would use and int or a float. The client declares the object then uses the object but the object invokes its own functions. This is the same as a programmer adding a float to another float. The programmer just uses the floats but does not call the add function. C++ knows when two floats have a plus sign between them, it needs to call the add function for two floats.



Client Header File: Function prototypes for the driver (client) are kept here. Class definitions and class headers are NOT kept here.



Client Implementation File: Functions that are called by the client for use in the clients driver file are defined here except "main()". Class functions are NOT kept here.


--------------------------------------------------------------------------------
TreyOne Games
Posts: 18
Joined: Wed Dec 14, 2005 12:44 am

Post by TreyOne Games »

2 - Organizing Code Files in C and C++

This is a must read !!!!!
While many simple programs fit into a single C or CPP source file, any serious project is going to need splitting up into several source files in order to be manageable. However, many beginning programmers may not realize
Found this here http://www.gamedev.net/reference/articl ... le1798.asp
zenaku
Posts: 212
Joined: Tue Jun 07, 2005 11:23 pm

Post by zenaku »

The books you want are:

The C programming language
Teach yourself C++ by Al Stevens,as recommended by Dr Dobbs Journal and lots of other big names in programming. (NOT Teach yourself C++ in 21 days!)

First, learn C and procedural programming. Once you know C, you know most of C++.
TreyOne Games
Posts: 18
Joined: Wed Dec 14, 2005 12:44 am

Post by TreyOne Games »

3 - C Programming

this has been posted many times b4 on the forum ..but this is usefull also
Cprogramming.com is a web site designed to help you learn the C or C++ programming languages, and provide you with C and C++ programming language resources
(found at http://www.cprogramming.com/)
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

thanks for sharing, i found this very useful. helped me sort out some cyclic dependencies that were giving me hell
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
TreyOne Games
Posts: 18
Joined: Wed Dec 14, 2005 12:44 am

Post by TreyOne Games »

thanks for sharing, i found this very useful.
glad it was usefull :D
TreyOne Games
Posts: 18
Joined: Wed Dec 14, 2005 12:44 am

Post by TreyOne Games »

4 - What is a flag and so much more?

_____________________________________________________________________
What is a flag ?


A flag is an algorithm that informs the program that a certain condition has occured.

example-9

main()
{
int temp;
float celsius;
char repeat;
char flag;
do
{
flag='n";
do
{
if(flag=='n')
printf("Input a valid temperature :");
else
printf("input a valid temperature,stupid:");
scanf("%d",&temp);
flag='y';
}
while (temp<0||temp >100);
celsius=(5.0/9.0)*(temp-32);
printf("%d degrees F is %6.2f degrees celsius\n",temp,celsius);
printf("Do you have another temperature?");
repeat=getchar();
putchar('\n');
}
while (repeat=='y' || repeat=='Y");
}

That was an example of how flags work.


What is the break command?



The break command ends the loop in which it is placed just as if the while condition, or the condition in a for loop becomes false.


How to declare an array?


An array can be defined as follows:

int temp[5]={45,56,12,98,12};

This would mean the following:

temp[0]=45....temp[4]=12

This was a single dimension array with 5 elements of the integer type.If you wanted to depict float variables just use float temp instead of int temp.


Let us now see an example of using an array for two tasks.


main()
{
int temps[31];
int index,total;
float average,celsius;
total=0.0;
for(index=0;index<31;index++)
{
printf("enter temperature #%d:",index);
scanf("%d",&temps[index]);
}
for(index=0;index<31;index++)
total+=temps[index];
average=total/31.0
printf("average is:%f\n\n", average);
puts9"fahrenheit\tcelsius\n");
for(index=0;index<31;index++)
{
celsius=(5.0/9.0)*(temps[index]-32);
printf("%d\t\t%6.2f\n",temps[index],celsius);
}
}



Now I am going to show you how to pass an array. When you pass an array you are actually passing the address of the array.

example-10

#define count 31
main()
{
int temps[count];
int index;
float celsius;
for(index=0; index< count;index++)
{
celsius=(5.0/9.0)*(heat[index]-32);
printf("%d\t\t%6.2f\n",heat[index],celsius);
}
}




--------Below is more from the original writer------------------------------------------------------------------------

Now we are in the fourth hour of our tutorial.We are now going to look at 1)comparing strings 2)determining string lengths. 3) combining strings 4)structures.

Comparing 2 strings:>> In c it is not possible to directly compare two strings so a statement like if (string1==string2) is not valid.


Most c libraries contain a function called the strcmp().This is used to compare two strings in the following manner.

if(strcmp(name1,name2)==0)
puts("The names are the same");
else
puts("The names are not the same.");


Determining string length.:>> This is done using the strlen() function.



a simple programming bit showing this function looks like this:



gets(name);
count=strlen(name);
printf("the string %s has %d characters",name,count);



Combining strings:>>We use the function strcpy() an example follows:

Example-11



strcpy(name,"Adam");
strcpy(name1,"and eve");
strcat(name,name1);
puts(name);



The assumption being that adam and eve are two values of the variables name1 and name2. The end result is the combination of the 2 names.


What are structures?


A structure variable is a collection of other variables comprising different types.


What are pointers?



Ponters are variables which refer to the memory locations of other variables.

This is how a structure is defined.

example-12



struct cd
{
char name[20];
char description[40];
char category[12];
float cost;
int number;
};
main()


Notice how the main function comes after the definition of the structure. In the example above the cd was a cd disk and I was writing the definition of a cd collection program.




--------------------------------------------------------------------------------
Now in the fifth hour I will show you how to output your data onto a disk.After all what is the use of the program if you can't save output to a disk.


Inorder to do this we have to use a pointer. The pointer in this case is FILE. The syntax to declare a file is :FILE*file_ponter;


The link between your program, the file and the computer is established with the fopen() function using the syntax shown below:
pointer=fopen("FILENAME","mode");



For example to create a file by the name cd.dat we do the following:

FILE*cdfile;
cdfile=fopen("CD>DAT","w");
If you will be reading from the file above use "r" instead "w" in the
second sentence.


In order to rpint information use the following command:
FILE*cdfile;
cdfile=fopen("PRN","w");
A file is closed by using the fclose() command.Next we will look at an exam ple of reading from a file.

example-13


#include "stdio.h"
main()
{
FILE*fp;
int letter;
if((fp=fopen("MYFILE","r"))==NULL)
{
puts("Cannot oepn the file");
exit();
}
while((letter=fgetc(fp)) !=eof)
printf("%c",letter);
fclose(fp);
}

The eof statement means end of file and this is included in the stdio.h header file which was declared at the start of the example. The stdio.h header file is one of many that comes with your compiler. So check your compiler specifics for other header files which will help perform other functions.


Now that you went through this tutorial you should be in a position to write simple programs and save it to a disk so you can give it your friends or even your boss. In no way the depth of c can be done in 5 hours but the nut and bolts can be learned that fast.Wher e you go from there depends upon your ambitions and hard work.

This page is maintained by Francis Thottungal.
Visitor.


Visit the Falcon page.

( i found this here http://members.tripod.com/%7Ejohnt/c.html )
Post Reply