Notices

Overclockers Forums > Software > Programming Tips and Tricks
Programming Tips and Tricks
Forum Jump

Learn it Yourself: C++ Course

Post Reply New Thread Subscribe Search this Thread
 
 
Thread Tools
Old 04-22-07, 01:25 AM   #61
Stavros
Member

 
Stavros's Avatar 

Join Date: Jun 2005
Location: Mississippi

 
Having almost finishing C++ class, I really wish I had taken WuChild's advice and taking Visual Basic first. I must say, though, that I thoroughly enjoyed my C++ class.

Aside from learning order (Like learning Functions before Arrays) This is pretty much the way our class was structured.

Overall, if you've got the guts, C++ really isn't all that difficult.

__________________
CPU-i7 950 Stock
GPU- 580 GTX (sock clocks)
Mobo- ASUS Sabertooth X58
PSU- FSP Group Everest 800 800W
RAM- G.SKILL 6GB (3 x 2GB) DDR3 1600 Tri-Channel
Heatsink- COOLER MASTER Hyper 212 Plus 120mm
HD1- Western Digital Raptor X 300GB (for Game installs)
HD2- Seagate Barracuda 500GB 7200RPM (for other programs)
HD3- Western Digital 2TB 7.2K Green (storage)
Case- Antec P180
Stavros is offline   QUOTE Thanks
Old 05-06-07, 06:53 PM   #62
maddog39

 
maddog39's Avatar 

Join Date: May 2006
Location: Somerville, MA

 
You should probably also note that the first few chapters wont work on any type of unix, because the system command PAUSE doesnt exist in a unix shell or bash for that matter.

__________________
My Rig:
Windows 7 64
EVGA nForce 780i SLI
Intel Core 2 Quad Q6600 @ 3.21GHz VCORE 1.325
4x1GB Corsair XMS2 DDR2 800
EVGA GTX 260 C216 SSC 896MB
maddog39 is offline   QUOTE Thanks
Old 05-06-07, 07:01 PM   #63
ShadowPho
Member

 
ShadowPho's Avatar 

Join Date: Jun 2005
Location: I am in your stack, SUBbing your registers!

 
Somebody please send me a PM in exactly 2 weeks and I will write a sequel to this. I am just afraid that I will forget about it.

__________________
"Take only that which you can give"
Need Help? IM me! I want to help the OC community!

MainComputer:
E6400@2.69-4.0 gig RAM-8800GT
ASUS P5N-E SLI

ShadowPho is offline Heatware Profile   QUOTE Thanks
Old 05-07-07, 04:48 AM   #64
Shelnutt2
Overclockers Team Content Editor

 
Shelnutt2's Avatar 

Join Date: Jun 2005
Location: /home/

 
Quote:
Originally Posted by ShadowPho
Somebody please send me a PM in exactly 2 weeks and I will write a sequel to this. I am just afraid that I will forget about it.
You got it!

__________________
Build!.Borg!.Recruit!.Folding.for.Team.32!
Gigabyte P35-DS3R, E6300, 2x512MB Team Group DDR2 667, EVGA 7800GT Heat

Want to run the folding@home gpu client in linux?
www.linuxfah.info

Your teacher is a dumbass. The equation is exact. ~Frodo Baggins
Damn....It's 12:00 AM and I thought my 805 would be a magic pumkin and turn into a conroe.........boy was I fooled:) ~Stilletto
This is what I get for not only being a newb, but also disregarding instructions! ~ShadowPho
I hate women... but im not gay lol. ~MrCrowley
Love can fade but knowledge stays forever (unless you develop alzheimers). ~Constantinos
Shelnutt2 is online now Folding Profile   QUOTE Thanks
Old 06-08-07, 04:14 PM   #65
ShadowPho
Member

 
ShadowPho's Avatar 

Join Date: Jun 2005
Location: I am in your stack, SUBbing your registers!

 
- I love C/C++, so here is a continuation of the tutorial.
Warm-Up lesson
If your program has a loop, then it might take up 100% of the CPU. To avoid that use
Code:
sleep(how long, in unsigned long, ms);
That command tells the CPU to forget about your program for ms time and then come back. So if I do sleep(1000); then your program will pause at that place for ~1 second while the CPU goes ahead and either idles or works on other programs. It is a good idea to have a sleep command in a loop if your program does not need full use of the CPU.
For example, I am working on a cryptographic instant messenger ( so I could chat with my friends without being afraid of RIAA listening in). We don't want it to eat all the CPU resources. So we are telling it to check for incoming messages every 50 ms. (sleep(50)

Random Tidbit
NULL or null is actually 0 in C++. They are exactly the same thing, which is why it is safe to use 0/NULL interchangeably. It is actually declared in one of the header files that NULL is 0. (or is it (void*) 0? )

In java it is NOT the same thing. I learned it the hard way...
Lesson 8 – structures.
What if we are coding a game and want to have some kind of a place to store the status of lets say.... players in a game? We would use a structure. A structure is simply a combination of different types of variables. Here is an example:
Code:
 struct player {  char name[24]; int life; int numberOfPiecesLeft; float location}
The above code will create a structure player that has the specified variables in it. It groups them together. After we declare the structure, we can now declare the actual data storages. You can have structures inside of structures, functions and classes.
So we will do:
Code:
player pl1; player pl2;
Then we will have pl1 and pl2. Now, in order to access a variable in a structure we use periods. So if we want to initialize the top code, we will do:
Code:
pl1.life=100; pl2.life=99; pl1.numberOfPiecesLeft=1;...
This is also used in a lot of headers by different headers/OSes. For example,
Code:
//from winsock2.h
struct sockaddr_in {
        short   sin_family;
        u_short sin_port;
        struct  in_addr sin_addr;
        char    sin_zero[8];
};
So, if we want to have a sockaddr_in, we would do something like this:
Code:
 sockaddr_in mySocket;
mySocket.sin_family=2;
mySocket.sin_port=122;
mySocket..sin_addr.WhateverIsInsideOfSIn_addrStruct;
Lesson 9 – memory/pointers, in theory
Just what is memory? Computers' current memory is a sequence of 1's and 0s that are represented differently. You probably knew this already. So... what does that mean for you? Well, lets look at something you got familiar with: the integer. If you do int myInty = 0, then what does the PC see? It sees 4 bytes (so 32 bits). It sees 0000 0000 0000 0000 0000 0000 0000 0000. Now, you do not need to learn how to convert, because if you will use it then you will remember it.
Now if we have a char ohMyChar. Thats one byte, so it would be 0000 0000.
Pointer. They come next. A pointer is just that – it POINTS to the memory address. In itself, its usually an int size. So, on my system all my pointers are 4 bytes. Example: if I declare this...
Code:
 int realInt=200;  
int *ptr = &realInt;
realInt is just a location that contains 200. ptr is a number (like 0x22ff6c) that the CPU reads to get to that number. *ptr will give back 200.
Pointers are actually numbers that hold a value of the memory they are accessing. But in order to have the memory to use we have to either tie a pointer to somewhere (like above) or ask the OS to give us RAM.
Lesson 10 – The Big Ones, lesson 9 in practice
Normally if you define data for your program, it gets taken from stack. But if you want to be able to allocate RAM while your program is running, you would need to do that manually.
Here is what you do need to learn: malloc/calloc/realloc , and free.
Believe me, asking OS for RAM is extremely easy. The hard part is giving it back appropriately and actually using the RAM. The easiest way to explain this would be to use an example.
So, relax and enjoy the story.
You are the lead programmer of google. You are given an assignment: read some information from a file. So you go ahead and google (oh, the irony!) for C++ file handling. Then you open the file and think about where you are going to store all that information to. You could do something like
Code:
char buffer[1024];
But what if the file is 1025 bytes in length? So you decide that you need to malloc some RAM from Windows/ Linux. Requesting memory from OS is really easy, all you do is
Code:
 malloc(howMuchRamYouWantInBytes);
and the OS will gladly return a pointer to that RAM. If the pointer equals NULL/0 then you know that you wanted too much or the OS did not like you well enough (not enough RAM).
Now, in C++ (unlike JAVA) OS will attempt its hardest to give you all you ask it for. If you accidentally ask the OS for 1 gig of RAM, then it won't ask you what you need it for. It will happily allocate 1 gig of ram and send a pointer that points to one gig of RAM your way.
If you want to see the effect that it can have then try the following code while looking at your task manager RAM usage. And be ready to kill the program once it eats some RAM. After you kill it, Windows will attempt to clean up all wasted RAM.
Code:
while(1)
    {
    malloc(1000*1000); // eat some RAM
    Sleep(100);              // But not too fast
    }
Now that you have seen the following example, make sure never to code the above way. . There a couple ways of rules you have to follow. Here is what good malloc usage has to be:
Code:
int/char/long/struct...   *myPtr  = malloc(amountOfStuff * sizeof(int/char/long...));
if(NULL== myPtr) // if malloc returns 0/NULL, then something bad happened
{ // throw an out of RAM error with report of how much ram you tried to eat 
}
//Use myPtr data here.
free(myPtr); // Frees the RAM. A MUST step
So, by now you know how to ask OS for RAM and how to receive it. You also know the free(ptr), which gives memory back to OS.
The only thing left for the lesson is calloc and realloc.
Code:
 ptr = calloc(number of stuff to do, bytes of each stuff);
That is then initialized to 0.
Code:
 ptr = realloc(ptr, size- same as malloc);
Realloc is a much more interesting function it allows for us to increase or decrease the RAM usage as needed. So, lets say that we have a pointer that points to 20 Mbs of RAM. Now we only need 10 Mbs. A call to realloc will free the last 10 mbs without touching the first 10

Lesson 11, or what we actually do with those
Soon to come. - memset, memcpy, memmove and the usage of RAM by malloc.

__________________
"Take only that which you can give"
Need Help? IM me! I want to help the OC community!

MainComputer:
E6400@2.69-4.0 gig RAM-8800GT
ASUS P5N-E SLI

ShadowPho is offline Heatware Profile   QUOTE Thanks
Old 11-12-07, 11:15 PM   #66
sreeja
New Member



Join Date: Nov 2007

 
The information about programming in c++ and java is very useful. C++ is a compiler oriented language and java can be complied or interpreted.Therefore java is more suitable than c++.Is it?
sreeja is offline   QUOTE Thanks
Old 11-13-07, 01:15 AM   #67
Adak
Senior Member

 
Adak's Avatar 

Join Date: Jan 2006

 
Java has it's uses, and C++ has it's strengths. As a programmer, you need to learn the strengths of the languages, and use the one that is most appropriate for the program you intend to write.

C++ would be a good choice for a computationally intensive program, that had to execute with maximum speed, and had an OOP design. It has a power that even compiled Java, just can't match.

Java is a good choice for a program that is meant to be easily deployed across several different platforms.

Although C++ has it's "ease of use" features, it unfortunately also has an incredible complexity as you get deeper into it.
Adak is offline Author Profile Folding Profile   QUOTE Thanks
Old 01-14-08, 10:00 AM   #68
loornlee
New Member



Join Date: Jan 2008

 
Hello everyone

im reading this c++ tutorial, now im doing the #4 of the first assignment, manipulating strings.

but i cant get my program to work properly...

this is my code so far

Code:
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int i, j, n;
    char frase[100]=" ", a[10][10]={' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
    cout <<"\n\n\nmax 100 characters and 10 words:\n\n";
    gets(frase);
    cout <<"\n";
    for (i=0;i<10;i++)
    {
            for (j=0;j<10;j++)
            {
                n=i*10+j;
                if (frase[n]==' ')
                {
                                break;
                }
                a[i][j]=frase[n];       
            }
    }
    for (i=0;i<10;i++)
    {
        cout<<"\n"<<a[i];
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}
cant realize whats wrong whit it >.<

i was hoping someone can point me in the right direction...

thx in advance ^^
loornlee is offline   QUOTE Thanks
Old 01-14-08, 11:59 AM   #69
Midnight Dream
OSPF Loving Member

 
Midnight Dream's Avatar 

Join Date: Mar 2004
Location: Lawrenceville, Georgia

 
Quote:
Originally Posted by loornlee View Post
Hello everyone

im reading this c++ tutorial, now im doing the #4 of the first assignment, manipulating strings.

but i cant get my program to work properly...

this is my code so far

Code:
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int i, j, n;
    char frase[100]=" ", a[10][10]={' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
    cout <<"\n\n\nmax 100 characters and 10 words:\n\n";
    gets(frase);
    cout <<"\n";
    for (i=0;i<10;i++)
    {
            for (j=0;j<10;j++)
            {
                n=i*10+j;
                if (frase[n]==' ')
                {
                                break;
                }
                a[i][j]=frase[n];       
            }
    }
    for (i=0;i<10;i++)
    {
        cout<<"\n"<<a[i];
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}
cant realize whats wrong whit it >.<

i was hoping someone can point me in the right direction...

thx in advance ^^
Well, some error output would be helpful, but just so you know, the proper way to return "EXIT_SUCCESS" is to simply do "return 0;" 0 implies success, anything else implies otherwise.

__________________
Gigabyte GA-EP43-UD3l - 6GB Patriot 6400 DDR2
Intel Core 2 Duo E7500 2.93GHz - XFX HD 6850 Black Edition

Learn to Fold - T32

A little bit of Heat


Still hate JoT

But I love you! And so does OSPF! Why do you turn us away!
Midnight Dream is offline   QUOTE Thanks
Old 01-14-08, 01:18 PM   #70
YellowDart
Member

 
YellowDart's Avatar 

Join Date: Sep 2003
Location: Mesa, AZ

 
My first guess would be your return value. I don't see anywhere in your code where you define EXIT_SUCCESS. Your main method is typed as int, so it's expecting you to return an int.

You might want to just try returning 0 and see if that fixes your error.

__________________
XBL Gamertag: Cr0wN0 .:. Steam ID: yell0wdart

Powdered Toast Man
~
p4 2.6c @ 3.3 ducted to TT SF2 .:. 1g corsiar xms 400mhz .:. DFI PS83-BL .:. (RIP)HIS 9800pro IceQ @ 425/370 (now running nV 7300 256) .:. SB Audigy MP3+

yell0wdart ~
Dell XPS 720 .:. Core 2 Duo 2.13g (@ 2.4) .:. 4g DDR2 677 .:. 2x 160g HDD .:. 7900 GS (@ 558/751)

Heat
YellowDart is offline   QUOTE Thanks
Old 01-14-08, 11:47 PM   #71
loornlee
New Member



Join Date: Jan 2008

 
well that part of the code is done by default in Dev-C++ 4.9.9.2 and it works in everything else i've done, anyways i tried changeing that to 0, but still doesnt works =(
loornlee is offline   QUOTE Thanks
Old 01-15-08, 12:22 AM   #72
technoViking
Member

 
technoViking's Avatar 

Join Date: Nov 2007
Location: San Jose, CA

 
Is the issue you can't compile it or output is wrong (aka you did somthing wrong in the logic)?

I just compiled and ran it fine, of course the output doesn't look right but it compiles.

Quote:
1>------ Build started: Project: C program, Configuration: Debug Win32 ------
1>Compiling...
1>test.cpp
1>.\test.cpp(11) : warning C4996: 'gets' was declared deprecated
1> C:\Program Files\Microsoft Visual Studio 8\VC\include\stdio.h(270) : see declaration of 'gets'
1> Message: 'This function or variable may be unsafe. Consider using gets_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.'
1>Linking...
1>Embedding manifest...
1>Build log was saved at "file://c:\Documents and Settings\Mr. Coffee\My Documents\Visual Studio 2005\Projects\C program\C program\Debug\BuildLog.htm"
1>C program - 0 error(s), 1 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
Also you might think about using a more up to date tutorial, as you can see its using things that are deprecated ex: gets.

__________________
Videocard: BFG nvidia/8800gt/512 OC @ 725/1816/2000
Processor: E8400 @ 3.6 [1.26V]
Mobo: ...... Gigabyte P35-DS3L
Memory: ...2x1gig G. Skill DDR2 8500: 5-5-5-15
Sound
: .....Sound Blaster X-Fi Titanium Fata1ity
PSU: .........Enermax 460W
Monitor: ...22" HP w2207 @ 1680x1050 && 40" 1080p @ 1920x1080
Mouse: .....Logitech mX518

One must remember...
The techno viking doesn't dance to the music.
The music dances to the TECHNO VIKING!
technoViking is offline   QUOTE Thanks
Old 01-15-08, 12:30 AM   #73
loornlee
New Member



Join Date: Jan 2008

 
yeap it compiles, but the output its all wrong =P

do you know some good updated tutorial? =D

thx for the help ^^
loornlee is offline   QUOTE Thanks
Old 01-18-08, 04:44 PM   #74
technoViking
Member

 
technoViking's Avatar 

Join Date: Nov 2007
Location: San Jose, CA

 
I thought you could add this to the list of C++ tutorials, I wrote this when I was 16 hah, I googled my name, Cory Sanchez and it came up!


http://library.thinkquest.org/C0111571/

__________________
Videocard: BFG nvidia/8800gt/512 OC @ 725/1816/2000
Processor: E8400 @ 3.6 [1.26V]
Mobo: ...... Gigabyte P35-DS3L
Memory: ...2x1gig G. Skill DDR2 8500: 5-5-5-15
Sound
: .....Sound Blaster X-Fi Titanium Fata1ity
PSU: .........Enermax 460W
Monitor: ...22" HP w2207 @ 1680x1050 && 40" 1080p @ 1920x1080
Mouse: .....Logitech mX518

One must remember...
The techno viking doesn't dance to the music.
The music dances to the TECHNO VIKING!
technoViking is offline   QUOTE Thanks
Old 01-19-08, 03:16 PM   #75
loornlee
New Member



Join Date: Jan 2008

 
ty! ill check it out, hope i can get my problem solved ^^
loornlee is offline   QUOTE Thanks
Old 01-19-08, 09:39 PM   #76
ShadowPho
Member

 
ShadowPho's Avatar 

Join Date: Jun 2005
Location: I am in your stack, SUBbing your registers!

 
Quote:
Originally Posted by technoViking View Post
Is the issue you can't compile it or output is wrong (aka you did somthing wrong in the logic)?

I just compiled and ran it fine, of course the output doesn't look right but it compiles.



Also you might think about using a more up to date tutorial, as you can see its using things that are deprecated ex: gets.
gets is deprecated?

__________________
"Take only that which you can give"
Need Help? IM me! I want to help the OC community!

MainComputer:
E6400@2.69-4.0 gig RAM-8800GT
ASUS P5N-E SLI

ShadowPho is offline Heatware Profile   QUOTE Thanks
Old 01-19-08, 10:25 PM   #77
technoViking
Member

 
technoViking's Avatar 

Join Date: Nov 2007
Location: San Jose, CA

 
Quote:
Originally Posted by ShadowPho View Post
gets is deprecated?
Yes.

__________________
Videocard: BFG nvidia/8800gt/512 OC @ 725/1816/2000
Processor: E8400 @ 3.6 [1.26V]
Mobo: ...... Gigabyte P35-DS3L
Memory: ...2x1gig G. Skill DDR2 8500: 5-5-5-15
Sound
: .....Sound Blaster X-Fi Titanium Fata1ity
PSU: .........Enermax 460W
Monitor: ...22" HP w2207 @ 1680x1050 && 40" 1080p @ 1920x1080
Mouse: .....Logitech mX518

One must remember...
The techno viking doesn't dance to the music.
The music dances to the TECHNO VIKING!
technoViking is offline   QUOTE Thanks
Old 02-15-08, 12:36 PM   #78
SauronofMordor
New Member



Join Date: Feb 2008

 
Hey Baggins!


Good to see you've posted my notes, as they existed in '03. Some things are a little out of date already. Sorry to see you only posted the first two weeks! You should post the rest - advanced functions, pointers, the OOP stuff, java. That's when it gets interesting.

Have you earned your Fields medal yet?
SauronofMordor is offline   QUOTE Thanks
Old 03-19-08, 03:36 PM   #79
technoViking
Member

 
technoViking's Avatar 

Join Date: Nov 2007
Location: San Jose, CA

 
Did Frodo baggins steal your stuff?

__________________
Videocard: BFG nvidia/8800gt/512 OC @ 725/1816/2000
Processor: E8400 @ 3.6 [1.26V]
Mobo: ...... Gigabyte P35-DS3L
Memory: ...2x1gig G. Skill DDR2 8500: 5-5-5-15
Sound
: .....Sound Blaster X-Fi Titanium Fata1ity
PSU: .........Enermax 460W
Monitor: ...22" HP w2207 @ 1680x1050 && 40" 1080p @ 1920x1080
Mouse: .....Logitech mX518

One must remember...
The techno viking doesn't dance to the music.
The music dances to the TECHNO VIKING!
technoViking is offline   QUOTE Thanks
Old 08-25-09, 06:48 PM   #80
iaacp
Member

 
iaacp's Avatar 

Join Date: Mar 2005
Location: Glendora CA

 
Hi guys, very late to the party, and this looked like an okay thread to ask. I just now started C++ and am trying to do my own hello world program. I am using Dev-C++. So, I compile with no errors, and as it runs it, the box that I believe would have the "hello world" in it shows up but instantly closes before I can read it.

Can anyone help a mega noob here?

This is what I have:

#include <iostream>

int main()
{
std::cout << "Hello World!\n";
return 0;
}

Edit:If I try to just "Run" the file instead of compile and run, I get the "source file not compiled error". Not sure if that is helpful or not

__________________
PSU: Corsair 650W T2 V2
GPU: EVGA GTX 560 Ti SC (1GB)
CPU: i5 2500K
RAM: 8GB (4x2) G.Skill Ripjaws 1600
Storage: SanDisk Extreme 240GB SSD - OCZ Vertex 3 120GB - WD Caviar Blue 500GB 7200 RPM
Mobo: Gigabyte Z68

Steam - iaacp
iaacp is offline   QUOTE Thanks

Post Reply New Thread Subscribe


Overclockers Forums > Software > Programming Tips and Tricks
Programming Tips and Tricks
Forum Jump

Thread Tools Search this Thread
Search this Thread:

Advanced Search


Mobile Skin
All times are GMT -5. The time now is 11:23 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
You can add these icons by updating your profile information to include your Heatware ID, Benching Profile ID or your Folding/SETI profile ID. Edit your profile!
X

Welcome to Overclockers.com

Create your username to jump into the discussion!

New members like you have made this the best community on the Internet since 1998!


(4 digit year)

Why Join Us?

  • Share experience
  • Max out your hardware
  • Best forum members anywhere
  • Customized forum experience

Already a member?