Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 1 BY ADEEL ANJUM...

26
Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 Chapter No: 01 1 BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)

Transcript of Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 1 BY ADEEL ANJUM...

Page 1: Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 1 BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)

Course Title: Introduction to C++

Course Instructor:

ADEEL ANJUM

Chap

ter N

o: 0

1

Chap

ter N

o: 0

1

1BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)

Page 2: Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 1 BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)

Chapter No Chapter Name

1. Introduction to C++

2. Variables,constants,expressions

3. Loops and decisions

4. Arrays

5. Functions

6. Classes

7. Inheritance

2

Course Contents

BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)

Reco

mm

ende

d bo

ok:o

op in

c++

by

Robe

rt la

fore

Page 3: Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 1 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.

Page 4: Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 1 BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)

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.

Page 5: Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 1 BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)

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.

Page 6: Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 1 BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)

Conti….

• C++ supports the polymorphism concept.

• Polymorphism means to create multiple definitions for operators and functions.

Page 7: Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 1 BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)

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.

Page 8: Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 1 BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)

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.

Page 9: Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 1 BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)

Structure of c++ program

• A c++ program consists of three main parts. These are:

• Preprocessor directives

• The main () function

• C++ statements

Page 10: Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 1 BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)

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.

Page 11: Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 1 BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)

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.

Page 12: Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 1 BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)

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.

Page 13: Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 1 BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)

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.

Page 14: Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 1 BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)

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….

}

Page 15: Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 1 BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)

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

Page 16: Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 1 BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)

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.

Page 17: Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 1 BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)

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.

Page 18: Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 1 BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)

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.

Page 19: Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 1 BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)

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.

Page 20: Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 1 BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)

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;

Page 21: Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 1 BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)

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();}

Page 22: Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 1 BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)

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;

Page 23: Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 1 BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)

Program to print a message on screen by using endl manipulator

#include<iostream.h>

#include<conio.h>

main()

{

cout <<“I am a”<<endl<<“student”;

}

Page 24: Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 1 BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)

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.

Page 25: Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 1 BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)

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();

}

Page 26: Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 1 BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)

The escape sequence