220 likes | 391 Views
In the Name of Allah The Most Merciful The Most Compassionate Programming In C++ http://vustudents.ning.com/. COURSE CONTENTS. Week 1: Basics Week 2: Loops and Decisions Week 3: Arrays and Strings Week 4: Functions Week 5: Structures and Intro to Classes
E N D
In the Name of Allah The Most Merciful The Most CompassionateProgramming In C++http://vustudents.ning.com/
COURSE CONTENTS • Week 1: Basics • Week 2: Loops and Decisions • Week 3: Arrays and Strings • Week 4: Functions • Week 5: Structures and Intro to Classes • Week 6: Intro to Pointers and Final Test • Students passing the final test will only be awarded certificates
Recommended Books • Object Oriented Programming in C++ by Robert Lafore • C++ How to Programby Deital & Deital
Other Resources • All displayed slides will be available in academics folder • Net Tutorials • http://www.tutorialbox.com/tutors/c++/C++tutor.htm • http://www.intap.net/~drw/cpp/ • http://www.olagam.com/tech1/c-tutorial.htm • www.freeprogrammingresources.com/cpptutor.html
History of Programming • 1st Generation LanguagesMachine Language • 2nd GenerationLanguages Assembly Language • 3rd Generation Languages Procedural Programming Languages like Basic, Pascal, Fortran, C etc. • 4th Generation Languages OOP e.g. Java ,C++, VB.
C++ History • C was developed at AT & T’s Bell Laboratories of USA in 1972 by Dennis Ritchie • C++ an extension of C was developed by Bjarne Stroustrup in early 1980s at Bell Laboratories. C++ provides capabilities for object-oriented programming.
Getting Started Alphabets Words Sentences Paragraph English Language Alphabets,Digits, Special- Symbols Constants,Variables,Keywords Program/ Code Instructions C++ Language
Structure of C Program # include < iostream.h > int main ( ) { cout << “Welcome to C++” ; return 0; } Preprocessor Directives Main Function void Function Body
Preprocessor Directives • The first line of the program # include < iostream.h > is called a Preprocessor Directive, The instruction that are given to the compiler before beginning of the actual program. • It begins with the # sign and a keyword (reserve word) “include” followed by < name of header file>.
Header Files • Header files are a part of the C++ Compiler and contain definitions of standard library functions. • Each header file has an extension “.h”. The preprocessor “include” is used to add a header file into the source code. The name of the file is written in angle brackets ”< >”. Syntax is # include < name of header file> # include “ name of header file” • For example; • iostream.h:The source file thatcontains declarations needed for input and output statements like cout/cin. • math.h : This header file contains definitions for mathematical functions
Functions • Functions are the fundamental building blocks of C++. The first program consists of a single function called main ( ) . The Main() Function • The main function indicates the beginning of a C++ program. It must be included in every program.When a C++ program is executed, the control goes directly to main () function.The statements within this function are the main body of a program.If the main function is not included, the program is not compiled and an error is generated.
Syntax of the main() function is: int / void main () { body of main function (program statements) ; } Terminated by a semi colon “;”
The Function Body Braces: The braces ( Curly brackets “{ }“ ) indicate Begin and End of a function. Every function must use this pair of braces. Program Statements: The basic unit of a C++ program. In our program we’ve only one program statement i.e. cout << “Welcome to C++” ; This statement tells the compiler to display the quoted phrase. A semicolon (;) indicates the end of the statement. Each statement has to be followed by a semicolon otherwise, a compilation error will occur.
OUTPUT USING cout • The identifier cout( pronounced as “ C out” ) is a predefined object in C++, called as Standard Output Stream. It follows data to be displayed. • The operator <<is called the Insertion or Put to operator. • In our example, the phrase “Welcome to C++” is an example of a String Constant or String Literal,is directed to cout by the insertion operator << and is to be displayed on the output.
Escape Sequence or Backslash Character Constant • In C++, backslash (\) causes an “escape” from the normal way of output. An escape sequence is combination of a backslash ‘\’ i.e; control character and a code character. Symbol Meaning Symbol Meaning \a beep \” Double / alarm quotes \r return \’ Single quote \n new line \? ? \t tab \\ \ \b backspace \xdd Hexadecimal
The endl Manipulator • Manipulators are instructions to the output stream that modify the output in various ways. • This causes a linefeed to be inserted into the stream, so that the subsequent text is displayed on the next line. It has the same effect as the ‘\n’ character. cout<<"PAKISTAN”<<endl; cout<<“OUR HOMELAND"<<endl; cout<<“PAKISTAN\nOUR HOMELAND\n”;
The setw Manipulator • It modifies or manipulates the way in which data is displayed • setw(n) : causes the string or no. that follows it in the stream to be printed within a field n characters wide.The value is right justified within the field. cout<<"PAKISTAN\tIS\tTHE BEST"<<endl; cout<<"PAKISTAN" <<setw(10)<< "IS"<<setw(14) <<"THE BEST"; • Both have the same output
Comments • A comment statement is a non-executable statement. It is used to add remarks in a program.The compiler ignores the comments given in the program.Comments are used to explain, what a program is doing. Comments can be written anywhere in a program. Syntax: • // comments for single-line comments • /* comments…… for multi-line comments ……..*/
# include<iostream.h> int main() //beginning of main function { cout<<“\n \”NU \t ST\” ”; //printing outputs cout<<“\n \’MCS\’ ”; return 0; } Output ??? “NU ST” ‘MCS’
# include<iostream.h> void main() { /*This is a program to find errors cout<<“\n NUST ; cout<<“\n PAKISTAN” return 0; } //end of program Errors ???
Steps of Programming in C++ • Editing • Editing is done in an editor like • BORLAND C / TURBO C / TC BIN • To write and save the code -filename.cpp • Preprocessing • To include required library files (headers etc) • e.g # include • Compiling and Linking • To create Object file -filename.obj • Lib is linked to make an exe file -filename.exe • Execution • To view the output