Well I've been in a C++ programming class for the last 5 months. If you've coded Java it's not very different but it is : ). Well anyways here is the notes I've taken this year : ).
.:C++ PROGRAMMING BASICS:.[/CENTER]
Well first off you're going to need a program to compile the c++ right? Well I suggest "[COLOR=orange]Tclite Borland Turbo C++" (will post a link later).
To create a new c++ program the format is:
**There must be a semicolon after each command line! (
*The command lines (lines that don't show up in a program can start with // or /* this is for multiple lines */ (must have that at the end)
the includes are compiler directives like - #include <something.h>. Make sure that bracket after main() there is a tab (i hear its to make the program look neater which interms can sell your program better).
A few includes:
SAMPLE CODE
Math Operators
add = (+)
subtract = (-)
multiply = (*)
divide = (/)
ex. 2(x+1) 2(x-1) 2(x*1) 2(x/1)
Incrementing Variables
i=i+1;
i=i+5;
x=x*3;
A=A-1;
i=1++;
i=1;
Legal Variables
int Average;
float Mess;
Illegal Variables
char Last Name;
^space
int int;
^c++ reserved word
Code Format (updated)
New Command
cin = Input command (console input is what it stands for)
(use) cin>>Num1;
**Remember to always prompt the user as to what they should enter!
Identifiers and output
Indentifiers / Variables - Values held in ram while the program is running
char = characters
int = integers from -32,768 to 32,767
float = decimals -3.4x10(-3
to 3.4x10(3
Expansions
long = integers from -2, 147, 483, 648 to 2, 147, 483, 648
double = decimal from 1.7x10(-308) to 1.7x10(308)
long double = 3.4x10(-4932) to 1.7x10(4932)
Declaring Variables
1. Must start with a letter or the underscore char
2. After the first character, you may use any characters or numbers that are on your keyboard except the minus sign
3. Use a name that describes the use of the variable
4. There van be no spaces in your variables name (First_Name, an underscore must be used as a space).
5. You may not use C++ reserved words as variable names
For Loops
Use "for" loops when you know or can determine easily the exact number of times you want code to be repeated
Structure:
the variable that you use to control how many times the loop repeated is called the "loop control variable"
Example - Lets write a program that will add up to 10 numbers
While and do-while loops
While Structure:
Do-While Structure:
Example - Suppose you ask a user to ask a number less than 100 you can use this structure to make the program repeat the question until the user enters the appropriate #
Error Trapping
SUBROUTINES
Subroutine - A mini program inside another program
Use - Simplifies the writing of big programs (they come in very handy with menu driven programs)
STRUCTURE
Yay time for an example
EXAMPLE
[I]
**REMEMBER TO ALWAYS FLOW CHART YOUR PROGRAMS
If you have any questions, ask. I'll try to help the best I can.
.:C++ PROGRAMMING BASICS:.[/CENTER]
Well first off you're going to need a program to compile the c++ right? Well I suggest "[COLOR=orange]Tclite Borland Turbo C++" (will post a link later).
To create a new c++ program the format is:
| Code: |
|
//James //Period 1 //Cool //Prints out a brief statement #include <iostream.h> main() { cout<<"Computer Science"; cout<<"is cool"; return 0; } |
**There must be a semicolon after each command line! (
*The command lines (lines that don't show up in a program can start with // or /* this is for multiple lines */ (must have that at the end)
the includes are compiler directives like - #include <something.h>. Make sure that bracket after main() there is a tab (i hear its to make the program look neater which interms can sell your program better).
A few includes:
- Iostream.h - input output screen
- Conio.h - Mainly used for the clrscr; command
SAMPLE CODE
| Code: |
|
#include <whatever.h> main() { float Num1, Num2, Avg; //variables cout<<"Please Enter First Number"; //basic output cin>>Num1; //cin is the user input command, it is used to assign a value to a variable cout<<"Please enter second number"; cin>>Num2; Avg=(Num1+Num2)/2; cout<<"The Average of "<<Num1<<" and "<<Num2<<" is "<<Avg; getch(); return 0; } |
Math Operators
add = (+)
subtract = (-)
multiply = (*)
divide = (/)
ex. 2(x+1) 2(x-1) 2(x*1) 2(x/1)
Incrementing Variables
i=i+1;
i=i+5;
x=x*3;
A=A-1;
i=1++;
i=1;
Legal Variables
int Average;
float Mess;
Illegal Variables
char Last Name;
^space
int int;
^c++ reserved word
Code Format (updated)
| Code: |
|
//James //Period 1 // Program Name //Program Description and function #include <whatever.h> main() { variable declerations; code; return 0; } |
New Command
cin = Input command (console input is what it stands for)
(use) cin>>Num1;
**Remember to always prompt the user as to what they should enter!
Identifiers and output
Indentifiers / Variables - Values held in ram while the program is running
char = characters
int = integers from -32,768 to 32,767
float = decimals -3.4x10(-3
Expansions
long = integers from -2, 147, 483, 648 to 2, 147, 483, 648
double = decimal from 1.7x10(-308) to 1.7x10(308)
long double = 3.4x10(-4932) to 1.7x10(4932)
Declaring Variables
1. Must start with a letter or the underscore char
2. After the first character, you may use any characters or numbers that are on your keyboard except the minus sign
3. Use a name that describes the use of the variable
4. There van be no spaces in your variables name (First_Name, an underscore must be used as a space).
5. You may not use C++ reserved words as variable names
For Loops
Use "for" loops when you know or can determine easily the exact number of times you want code to be repeated
Structure:
| Code: |
|
For (beginning value ; range of values ; increment) { code to be repeated; } |
the variable that you use to control how many times the loop repeated is called the "loop control variable"
Example - Lets write a program that will add up to 10 numbers
| Code: |
|
#include <whatever.h> main() { int i, num, sum; for (i=1; i<=10; i=i+1) { cout<<"Please Enter numnber "<< i; cin>>num; sum=sum+num; } cout<<"The sum of your ten numbers is "<<sum; return 0; } |
While and do-while loops
While Structure:
| Code: |
|
while (condition) //for the loop to procede { code } |
Do-While Structure:
| Code: |
|
do { code } while (condition) |
Example - Suppose you ask a user to ask a number less than 100 you can use this structure to make the program repeat the question until the user enters the appropriate #
Error Trapping
| Code: |
|
do { cout<<"Enter a number less than 100"; cin>>num; }while (num>=100) |
SUBROUTINES
Subroutine - A mini program inside another program
Use - Simplifies the writing of big programs (they come in very handy with menu driven programs)
STRUCTURE
| Code: |
|
#include <whatever.h> void Name-of-subroutine(); Global Variables //Variables used in any routine (hence global) main(); { variables; code; //include calls to subroutines return 0; } void Name-of-subroutine() { local variables; code; } |
Yay time for an example
EXAMPLE
| Code: |
|
#include <whatever.h> void add(); void subtract(); void endit(); main(); { int choice; do { cout<<"1 - Addition\n"; cout<<"2 - Subtraction\n"; cout<<"3 - Quit"; cin>>choice; if (choice == 1) { add(); } if (choice == 2) { subtract(); } if (choice == 3 { endit(); } } while (choice !=3) return 0; } void add() { int x, y, ans, userans, count; x=rand()%10; //will choose a random number 1-9 y=rand()%10; ect. ect. (you should be able to code the rest) } |
[I]
**REMEMBER TO ALWAYS FLOW CHART YOUR PROGRAMS
If you have any questions, ask. I'll try to help the best I can.
