Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie) C was developed in 1970s at AT&T (Richie) C is a procedure-oriented language C++ early 80s (Bjarne Stroustrup) C++ is an object-oriented superset of C Should I learn C first? Learn procedural programming before OO * * *
Edit Source Code Start Compile Errors yes no Link Errors Link yes no Link yes Run Time Errors Done no * * *
Source to Executable * source code compiler - h-files used object code linked to libraries a.out To ensure compilation was successful: 1.rm a.out 2.g++ myprog.cc 3.ls a.out
A First Program 1.#include 2.void main() 3.{ 4.cout 2.void main() 3.{ 4.cout <<"Hello World\n"; 5.}
#include Inserts the contents of a system file iostream.h the particular file to include Can include user files, too Breaking it Down Preprocessor can include a file *
Including Shared Code source code preprocessor expanded source code C++ compiler #include #include
#include<iostream.h> void main( ) { } A Simple Program Parts of a simple program * statements Header Body
int main ( ) int main ( ) Function Header Line main() is called by & returns to OS function name type of returned value argument * *
header void main( ) { } { } Parts of all Functions There must be exactly one main() note alignment * body { Function:
<< << insertion operator (“put to”) \t \n endl \ttab \n endlhard return - begin a new line Intro to cout (Console OUTput) Prints values to the screen Intro to cout (Console OUTput) Prints values to the screen *
cout <<"Here is 5: "<<5<<"\n"; Here is 5: 5 Output Here is 5: 5 cout Example Prints two strings and one literal integer *
cout <<"A big number: \t "<<70000<<endl; Output A big number: Note: endl does the same thing as “\n” Another cout Example Additional space from the TAB character *
cout <<"The sum of 8 & 5 is "<<8 + 5<<“\n”; Output The sum of 8 & 5 is 13 Expressions & cout The value of the expression is displayed *
cout <<"Big # : "<< 7000*7000<<endl; Big # : 4.9e+07 Output Big # : 4.9e+07 A Look at cout is bigger than default space * 4.9 *10 7 Scientific notation
cout <<"A fraction: "<<5/8 <<endl; A fraction: Output A fraction: A Look at cout A decimal fraction *
>> >>insertion operator (get from) Paired with cout. cout accesses and displays values cin reads and assigns values cin >> age; /*causes the program to stop and wait for user input*/ A Look at cin cin allows data to be input into variables
cout > num1; The contents of the address named num1 is... A Look at cin Parts of a simple program
cout << “Enter 3 numbers: “; cin >> num1 > num1 << num2 << num3; The contents of the address named num1 is … num2 is … num3 is... A Look at cin Parts of a simple program
Reserved Words Words that have special meanings in the language. They must be used only for their specified purpose. Using them for any other purpose will result in a error. e.g. coutdoifswitch cinwhileelsereturn *
Reserved Words C++ is case-sensitive. Thus: coutCOUTCoutcOut all have different meanings. The reserved words are all in lowercase. Don’t use reserved words other ways, even with different capitalization. *
Statements A statement controls the sequence of execution, evaluates an expression, or does nothing, and ends with a semicolon. Preprocessor statements do not end with semicolon #include They do always start with #
4 Statements 4 Statements { cout <<"A fraction: "<<5/8 <<endl; cout <<"Big # : "<< 7000*7000<<endl; cout <<8 + 5 <<" is the sum of 8 & 5\n"; cout << “Hello world”; }
Comments These are important parts of a program. Two types Two types // /* */or/* */ *
Comments Do: Add comments to source code. Keep comments up to date. Use comments to explain sections of code. Don't: Use comments for code that is self-explanatory. * *
Programming Style Always list function return type. Void is used to indicate no value is returned void main ( void ) { statements; } optional *
Programming Style group declarations at the beginning void main ( ) { declaration statements; other statements; }
Programming Style blank lines before and after control structures void main () { statements; if (expression) { statement; statement; } statements; } * * *
Slide 30 of 32
Slide 32 of 32
Success comes before work only in the dictionary.