260 likes | 428 Views
Course Title: Introduction to C++. Course Instructor: ADEEL ANJUM. Chapter No: 01. Chapter No: 01. BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER). 1. Course Contents. Recommended book:oop in c++ by Robert lafore. 2. BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER). Introduction to C++ :.
E N D
Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 Chapter No: 01 BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER) 1
Course Contents Recommended book:oop in c++ by Robert lafore 2 BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)
Introduction to C++ : Differences between C and C++: • C++ is derived from the C language. C is a structured language , while C++ is an object-oriented language. • Dennis Ritchie developed C in 1970, while Bjarne stroustrup developed C++ in the early 1980 at Bell Laboratories.
Conti….. • C++ is an object-oriented version of C. Object-oriented programming involves concepts that are new to programmers of traditional languages such as BASIC, Pascal and C. • It goals clear, more reliable and more easily maintained programs.
Conti… As compare to C, C++ has many features like improved approach to input/output and a new way to write comments. • C++ inherits much of the efficiency of C language , as well as the readability and ease of writing programs. • C++ supports the concept of encapsulation and data hiding through the creation of user-define types, called Classes.
Conti…. • C++ supports the polymorphism concept. • Polymorphism means to create multiple definitions for operators and functions.
Writing c++ program • The source code of c++ program is stored on the disk with extension cpp (cpp stand c plus plus). The program is stored in a text file on the disk. any text editor can be used to write and edit c++ source code. • The c++ compiler translates a c++ program into the machine code. the machine code is called object code.it is stored in a new file with extension obj.
Conti….. • The object code is then linked to the libraries. After linking the object code to the libraries, an executable file with extension .exe is created. The executable program is then run from operating system command line. • For example a source code of program in c++ is written and stored in file first.cpp.after compilation the object code is saved in file first.obj and executable code is stored in file first .exe.
Structure of c++ program • A c++ program consists of three main parts. These are: • Preprocessor directives • The main () function • C++ statements
Preprocessor directives • The instruction that are given to the compiler before the beginning of the actual program are called preprocessor directives. These are also known as compiler directives. • These directives normally start with a number sign # and keyword “include” or “define “.for example preprocessor directives are used to include header files in the program.
Header file • Header file is a ++ source file that contains definition of library functions.Header files are added into program at the time of compilation of the program. A header file is added if the function /object defined in it is to be used in the program. • The preprocessor directive “include” is used to add a header file into the program. The name of file is written in angle bracket <> after “# include” directive. it can also be written in double quotes.
Conti…….. • C++ has a large number of header files in which library functions are defined. The header file must be included in the program before calling its functions in the program.A single header file may contain a large number of built in library functions. • For example ,the header file iostream has definition of different built in input output objects and functions.
main function…. • The “main ()” function indicates the beginning of a C++ program. • The “main()” must be included in every C++ program. • When a C++ program is executed, the control goes directly to the main() function. • The statements within this function are the main body of the C++ program.
Conti…. • If main() function is not included, the program is not compiled and an error message is generated . The syntax of main() function is : main () { program statement…. }
Basic input/output • The statements that are used to get data and then assign it to variables are known as input statements • The statements that are used to receive data from the computer memory and then to send it to the output devices are called output statements
The “cout” object-output stream • The cout is pronounced as ‘see out’. The ’cout’ stand for console output . The console represents the computer display screen. The ‘cout’ is a c++ predefined object. It is used as an output statement to display output on the computer screen . It is a part of iostream header file. • Flow of data from one location to another is called stream. The ‘cout’ is the standard output stream in c++. This stream sends the output to the computer screen.
Syntax of cout • Cout<<constant/variable; where ‘cout ‘ name of output stream object. ‘<<‘ insertion operator it separate variable ,constants. The string constant are given in a double quotation marks. numeric constants , variable and expression are without quotation marks.
A program to print a message on screen using cout object. # include <iostream.h> #include<conio.h> main() { clrscr(); cout<<“c++ is a powerful programming language”; } Note: clrscr() function is part of conio.h header file.
The “cin “ object-input stream • The “cin” stands for console input. It is pronounced as “see in” . It is an input stream. It is used as input statement to get input from the keyboard during execution of the program. • When an input statement is executed, the computer waits to receive an input from keyboard . When a value is typed and enter key is pressed ,the value is assigned to the variable and control shifts to the next statement . • The cin object is also part of iostream header file.
Syntax of cin • Cin >> var1>>var2…; where cin represent the object name used as input stream. “>>” extraction operator or get from operator . It gets an input from the input device and assign it to the variable. each variable is separated by extraction.”>>”. for example. cin>>a>>b>>c;
Calculate sum and product get value from keyboard during program execution. #include <iostream.h> #include <conio.h> main () { int n1,n2,s,p; clrscr(); cout<<“enter first number “; cin>>n1; cout<<“enter second value”; cin>>n2; s=n1+n2; p=n1*n2; cout<<“sum of two number”<<s<<endl; cout<<“product of two number “<<p<<endl; getch(); }
The endl manipulator • The “endl” stands for end of line. It has same effect as the escape sequence “\n”. This is a predefined iostream manipulator. For example cout<<“c++\ n”<<“programming \n“; is equivalent to: cout<<“c++”<<endl<<“programming” <<endl;
Program to print a message on screen by using endl manipulator #include<iostream.h> #include<conio.h> main() { cout <<“I am a”<<endl<<“student”; }
The “setw” manipulator • The “setw” stand for set width. This manipulator is used to set width of the output on the output device. syntax is : setw(n) where n represent the width of the output field. It is an integer value. The setw manipulator is a part of iomanip.h header file. If the manipulator is to be used in the program ,this header file must be included at the beginning of the program using include statement.
Program that uses “setw” manipulator #include<iostream.h> #include<conio.h> # include<iomanip.h> main() { clrscr(); cout<<setw(5)<<63<<setw(5)<<8<<endl; cout<<setw(5)<<100<<setw(5)<<77<<endl; getch(); }