Download presentation
Presentation is loading. Please wait.
Published byEthan Randall Modified over 9 years ago
2
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 * * *
3
Edit Source Code Start Compile Errors yes no Link Errors Link yes no Link yes Run Time Errors Done no * * *
4
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
5
A First Program 1.#include 2.void main() 3.{ 4.cout 2.void main() 3.{ 4.cout <<"Hello World\n"; 5.}
6
#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 *
7
Including Shared Code source code preprocessor expanded source code C++ compiler #include #include
8
#include<iostream.h> void main( ) { } A Simple Program Parts of a simple program * statements Header Body
9
int main ( ) int main ( ) Function Header Line main() is called by & returns to OS function name type of returned value argument * *
10
header void main( ) { } { } Parts of all Functions There must be exactly one main() note alignment * body { Function:
12
<< << 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 *
13
cout <<"Here is 5: "<<5<<"\n"; Here is 5: 5 Output Here is 5: 5 cout Example Prints two strings and one literal integer *
14
cout <<"A big number: \t "<<70000<<endl; Output A big number: 70000 Note: endl does the same thing as “\n” Another cout Example Additional space from the TAB character *
15
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 *
16
cout <<"Big # : "<< 7000*7000<<endl; Big # : 4.9e+07 Output Big # : 4.9e+07 A Look at cout 49000000 is bigger than default space * 4.9 *10 7 Scientific notation
17
cout <<"A fraction: "<<5/8 <<endl; A fraction: 0.625 Output A fraction: 0.625 A Look at cout A decimal fraction *
18
>> >>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
19
cout > num1; The contents of the address named num1 is... A Look at cin Parts of a simple program
20
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
21
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 *
22
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. *
23
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 #
24
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”; }
25
Comments These are important parts of a program. Two types Two types // /* */or/* */ *
26
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. * *
27
Programming Style Always list function return type. Void is used to indicate no value is returned void main ( void ) { statements; } optional *
28
Programming Style group declarations at the beginning void main ( ) { declaration statements; other statements; }
29
Programming Style blank lines before and after control structures void main () { statements; if (expression) { statement; statement; } statements; } * * *
31
Slide 30 of 32
33
Slide 32 of 32
34
Success comes before work only in the dictionary.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.