What's new

C++ Tutorial

  • Thread starter samster97
  • Start date
  • Views 1,058
samster97

samster97

Enthusiast
Messages
183
Reaction score
35
Points
85
Sin$
0
ok well i'm gonna give you guys who are new to c++ :wink: a quick little tut
I decided I will put on some C++ tuts I made a long time ago there made with a begginer in mind

PS: if u say I leeched i can prove that i am the poster and creator of all of the tuts that i am going to post
also yes these are on 2 other sites

included:
4 tutorials

Classes/structs:
ok well if you have been stuying C++ for a while its time that you finally get into true OOP
Object Oriented Programming
now C++ is an OOP
and the point of OOP is so that like other programmers can easily and quickly see whats going on what the variables do and much easier to edit and work with
this is one reason C++ has scopes of course there are other reasons
such as how many of you do this
Code:
int main(){

int a;
int b;
int c; 
int d;
bool e;
float f;

cout << a;
}

and so on and make all of your variables gloabal inside of main
this is not clean OOP
however technicly the code above is clean because there is only the global scope and the local scope main

however classes were made for C++ so that programmers could take an object and make features

now a class and structures could be used to maybe hold the values of players and always should be
now the syntax for the class is this
Code:
class name {
}name(optional);
[php]
now so if i were to make a class for a player i would do this

Code:
[php]
class Player {
private :
int health;
int strength;
int speed;
public :
void attack();
void defend();
void move();
};


now i am going to explain this
first im making a class named Player
then im using a new command
private :
i will go into private later
then public :
i will go over public later
then you see some function definitions
the same clean C++ rule applies that you should make a function definition in a class then make the actual function later
now in order to implement this into a code you could do this

Code:
#include <windows.h>
#include <iostream>

using namespace std;

class Player {
private:
int health;
int strength;
int speed;
public:
void attack();
void defend();
void move();
};

int main(){

Player guy;

guy.attack();

}
ok now if you compiled this you would get an error because i used attack without defining it but besides that
you know the class so lets go to int main now
you see Player guy;
this is making guy have value of Player Player is now acting like a variable type
ect saying the C++ programmers made int using a class or struct they would have done

Code:
class int {
++(increment)
--(decrement)
value can be from 10000 to 1321432
};

now dont get the idea that will work they didnt use clasees and dealt with binary when making the types but it gives you the idea
then you see this
guy.attack();

things made from classes use the dot operator to access the things in the class
so lets say attack made health decrease
then it would guys health would decrease when guy.attack(); was called
now
lets say i tired to do this
guy.health = 100;

i would get an error however not for what you think
this is where public and private come in
private means that what ever is in there cannot be accessed directly
however they can be access by the things in the public and public can be accessed directly
now all of your ints bools and floats and all the variables should be private in the class
this is because we dont want programmers to worry about the details
then have them accessed throught the fuctions in public

so what if we did this
Code:
class test{
private:
int health;
public:
int getHealth();
void SetHealth();
};

int main(){

test itest;
thealth = test.getHealth();
test.setHealth();
}
now assumming this was the code for getHealth
Code:
int test::getHealth(){
return health;
}
then thealth would get the value of health in the class
now lets say setHealth was this
Code:
void test::setHealth(){
health = 100;
}
this would make the value of tests health = 100

understand them a bit more now?
now why is there a ; after the closing bracket for a class this is because
you can declare your class variables without making them in the main
Code:
ex
class superman {
int health;
}SUPERMAN;
you could also make multiple ones at a time
Code:
class superman {
int health;
}SUPERMAN,HELLO,THISGUY;

now the diffrences about structs and classes is data structures dont have public or private thats it same declarations and everything
Code:
struct hi {
stuff
};
now i have spent an hour on this and i will go back and proof read please reply

Variables:
ok varaibles are very important in C++ and are a must learn no matter what!
the main variables are
Code:
int - interger
float - floating point number
double - double.....
then there are two others
bool - boolean
char - character
but in mind these are more speccial but if you must know
char holds 1 symbol or 1 letter
bool holds true or false

now lets learn about ints
now it's very important that you know that through this part i am not stating the real value that a varaible can hold
ex - int can hold a number from -20,000 to 20,000 that is not true it can hold more





now lets learn about variables
a variable can hold up to a certain value in which it then overlaps


ex
lets SAY int can hold from -100 to 100 and during your program an int gets a value of 101 well then it is overflowed and it overlays or loops and its value would become -99 understand?


now i have never actually told you how to make your variable yet have I hmmmm well here you go


Code:
type name = value;


type can be the variable type
name is anything you want
C++ IS CASE-SENSITIVE
and value......is its value



EX..

Code:
int MyVariable = 98;

now i should tell you now that you do not have to declare the variable when you make it you could do this


int MyVariable;
but bewarned you should only do this if the variable will get a value somewhere in the program else you will get an error


also you could declare your variables value later in a program because as you should know a compilier reads line by line
now im gonna give you a lesson what i mean!




Code:
#include <iostream>
#include <windows.h>


using namespace std;


int main(){


int iFnum;
int iSnum;
int iSum;

cout << "welcome to TheFallens calculator!\n";
cout << please input your first number\n";
cin >> iFnum;
cout << "please input your second number\n";
cin >> iSnum;
iSum = iFnum + iSnum;
cout << "The sum is" << " " << iSum << "\n";
}


this above is the correct method! the incorrect method! below


Code:
#include <iostream>
#include <windows.h>

using namespace std;

int main(){

int iFnum;
int iSnum;
int iSum = iFnum + iSnum;

cout << "welcome to TheFallens calculator!\n";
cout << please input your first number\n";
cin >> iFnum;
cout << "please input your second number\n";
cin >> iSnum;
cout << "The sum is" << " " << iSum << "\n";

}


why would this generate an error? this would generate and error because remember a C++ compilier runs line by line and as you see in the beging i give iSum the value of iFnum + iSnum...... welll they havnt been declared yet so it has nothing to add thus creating an error

well im not finished with the tutorial theres more but i cant think right now just woke up anyways yeah leave comments and check back i might add more when i rememeber

loops:
this one was made when i was a begginner so code isnt very... clean

i made this a while ago on a diffrent forum and when i was a begginer it is meant to teach multiple things so its not exactly "clean code"


i suggest you install MSVisualC++ that way you can copy and paste the code run and experiment with it, also comments are easier to read
[color]

i guess i should porb tell you what the program does

It starts and asks you how your day was you choose one then press enter if it reconizes that command then it will respond else it will skip to the password,
the password has two passwords 1 and 2 one will send you into a never ending matrix...until you close the porgram
2 will send a loop of 500 TheFallen ROCKS! and then ask you if you want to redo
if you put it 0 it will return 0
else it will make iLoopt = 2 which will cause the While( iLoopt == 2) to go off and restart =]








// I added comments to help you understand whats going on

Code:
#include <iostream>
#include <windows.h>
using namespace std;
// This makes you namespace iD hold the integer iDay
namespace iD{
int iDay;
}
/* void Check is a fuction i used to hold the if staments please note it does not return a value thus
it is labeled VOID Check() */
void Check(){
/* in This function i did not enter using namespace iD so i have to type iD(the namespace)::iDay
(the varible in namespace iD */

Code:
 if(iD::iDay == 1){
system("CLS");
cout << "Im glad to here that\n";
}
else if(iD::iDay == 2){
system("CLS");
cout << "Hey it's better than a bad day\n";
}
else if(iD::iDay == 3){
system("CLS");
cout << "Awww im sorry hope this will cheer yah up\n";
}
else if(iD::iDay == 4){
system("CLS");
cout << "Well don't let it get yah down! theres always tomorrow\n";
}
else if(iD::iDay == 5){
system("CLS");
cout << "Well thats no good, thats not good at all, well try to cheer up buddy it will be ok\n";
}
else if(iD::iDay > 5){
cout << "Sorry code not reconized\n";
cout << "Skipping Welcome response"; 

}
}// it goes back to main() and executes the line that came after
Code:
Check()
int main(){
using namespace iD;
int iPasswordCheck;
int iLoop = 0;
int iLoopt = 2;//this equals 2 and starts as 2 to start the loop
int iLoopTF = 0;//this controls a varible at the bottom which changes iLoopt
while (iLoopt == 2)//
this checks to see if iLoopt = 2 if so then do the loop below
Code:
{

cout << "Hello *** Member how was your day?\n";
cout << "1 = good\n2= ok\n3= eh\n4= bad\n5= horrible\n";
cin >> iDay;// After this line it goes to the function Check()
Check();//Runs the values in function Check
Sleep(2000);// once Check has been completed it comes back here and executes the lines below=]
system("CLS");
cout << "PASSWORD PROTECTED!\n";
cin >> iPasswordCheck;
if (iPasswordCheck == 1){
cout << "CHECKING.\n";
Sleep(2000);
cout << "Accepted!\n";
Sleep(200);
system("CLS");
while(1) {
cout << " 101001101001";
}

}
else if (iPasswordCheck == 2){
cout << "CHECKING.\n";
Sleep(2000);
cout << "Accepred!\n";
Sleep(200);
system("CLS");
Code:
while(iLoop < 500)//here iLoop = 0 if iLoop is less than 500 then redo the loop
{
iLoop++;//this adds one everytime the loop is done
cout << "TheFallen ROCKS!\n";
}

}

else{
cout << "Password invalid\n";
Sleep(5000);
cout << "EXITING\n";
Sleep(2000);
cout << "Good bye exiting with 0x0";
Sleep(1000);
return 0;
}
iLoopt = 0;//this sets iLoopt to 0 so it doesn't repeat again without asking the question
system("CLS");
cout << "Would you like to do this again? 1 or 0 \n";
cin >> iLoopTF;// you choose here what iLoopTF equals
if (iLoopTF == 1)/*if iLoopTF = 1 then it will set iLoopt to 2 (to let it loop start again)*/
{
iLoopt = 2;
iLoop = 0;/* I do this because iLoop is still at a value of 500 So when you got to that step again
and it wasn't 0 it would say oops its already 500 lets skip this step
so with it back at 0 it restarts*/

}
else{//so if they didn't put down 1 then the iLoopt would not become 2 and i couldn't restart
s
Code:
ystem("CLS");
cout <<"Ok ill cya laterz\n";
Sleep(200);
cout <<"EXITING\n";
Sleep(200);
cout << "0x0\n";
Sleep(200);
return 0;// returns 0 and turns off the program
}

}

}
RAM:frown:COMPUTER)
I hope this gave you a better understanding of while loops in C++ :happy:[/b]

i have been on the site for a while but kinda quit so now im releasing some tutorials i have made over the years

this is a tutorail for people getting started in any programming language really but i learned it while learnign C++ soo

R.A.M. or RAM stands for Random Access Memory the more you have the better

now this tutotial isnt going to teach a thing of code but just sorta how variables and addresses work

ok well first im sure you all have a hard disk or hard drive w/e

this is were all of your programs are stored but they are not active

once you start a program its attributes such as variables get loaded in to the RAM

and is givin an adress

now think of RAM like a bunch of cubby holes each with its own unique label like

0x01BC4A2 or sumthing

when you create an integer in C++ it is likely to take up 4 cubby holes in RAM as each cubby hole can hold 1 byet

this is also y you dont just use double because its one of the biggest variables because it takes up unecessary space in the RAM and could cause some lag

so lets say in my program i have variable x

int x= 1;

then i run the program and it uses 4 spots of the cubbys and assigns it its addres such as 0x0000001

0x0000001 is the variable's address

this is how when you call x in your program the computer says go into RAM and get the value of address 0x0000001

this is also very useful for hacking as you can make a variable, pointer, or refrence of an address such as if the variable you want had the addr 0x00002A then you could just make a variable that gets that value and you can then manipulate it

Thanks guys <3 hope this helped ya and yes I will put these in spoilers later but right now I gotta go PEACE! :rolleyes:
5TH tut<3 hope i get my award and tried to place spoilers best i could hope this gets pinned:smile: <3 this is great for many starters
Credits go to ManUnited aswell:smile:
 
Lace Up

justintime810

Enthusiast
Messages
585
Solutions
1
Reaction score
28
Points
95
Sin$
0
Long... but it deserves +rep

Thanks, when I start with C++ im coming to this tut, it has a lot of details :smile:

Edit: but maybe put the codes in the code thing. Ex. [*code]code here[*/code]

Code:
without the *'s
 
SotG Caboose

SotG Caboose

Getting There
Messages
1,448
Reaction score
687
Points
230
Sin$
0
You don't need to bump the thread twice. Besides, I can tell you just copied those tutorials from somewhere else.
 
samster97

samster97

Enthusiast
Messages
183
Reaction score
35
Points
85
Sin$
0
You don't need to bump the thread twice. Besides, I can tell you just copied those tutorials from somewhere else.
Really I did tell me where? I most certainly did not and this took me a very long time And I don'y appreciate this BS when everyone else here thought this should have been stickied because it is more detailed than the other sticky so hop off and flame somewhere else I bumped twice because I can so chill. :blink:
 
SotG Caboose

SotG Caboose

Getting There
Messages
1,448
Reaction score
687
Points
230
Sin$
0
Really I did tell me where? I most certainly did not and this took me a very long time And I don'y appreciate this BS when everyone else here thought this should have been stickied because it is more detailed than the other sticky so hop off and flame somewhere else I bumped twice because I can so chill. :blink:

No you can't.
 
Top Bottom
Login
Register