Top Banner
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 1 Haresh Jaiswal Rising Technologies, Jalna.
23
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 01. introduction to C++

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 1

Haresh Jaiswal

Rising Technologies, Jalna.

Page 2: 01. introduction to C++

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 2

Agenda Introduction

Input/Output

Manipulators

Reference Variables

C++ Data Types

Page 3: 01. introduction to C++

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 3

Origin of C++ C++ began as an expanded version of C.

The C++ extensions were first invented by BjarneStroustrup in 1979 at Bell Laboratories.

Initially called the new language "C with Classes”

Later in 1983 the name was changed to “C++”

Page 4: 01. introduction to C++

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 4

Origin of C++ Over the years, computer programs have become larger

and more complex.

Even though C is an excellent programming language, ithas its limits. In C, once a program exceeds from 25,000to 100,000 lines of code, it becomes so complex that it isdifficult to grasp as a totality.

Page 5: 01. introduction to C++

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 5

Origin of C++ Although C was one of the most liked and widely used

professional programming languages in the world, theinvention of C++ was necessitated by one majorprogramming factor: increasing complexity.

The essence of C++ is to allow the programmer tomanage larger, more complex programs.

Page 6: 01. introduction to C++

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 6

Input/Output in C++ The most common way in which a program

communicates with the outside world is through simple,character oriented Input/Output (IO) operations.

C++ provides two useful operators for this purpose:

>> for input and

<< for output.

Page 7: 01. introduction to C++

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 7

Input/Output in C++ Every C++ Program must include the header ‘iostream.h’

It contains declarations for the stream objects

cout & cin

stream insertion & stream extraction operators.

<< (Stream insertion)

>> (Stream Extraction)

Page 8: 01. introduction to C++

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 8

Input/Output in C++ An Example of console output using cout & << operator

cout << “Hello”;

Causes the string in quotation mark to be displayed onthe screen/console.

Page 9: 01. introduction to C++

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 9

Input/Output in C++

cout << “Hello…”

VariableInsertion operator

Object

Hello…

Page 10: 01. introduction to C++

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 10

Input/Output in C++ An Example of console input using cin & >> operator.

cin >> myVariable;

Will receive input from keyboard and store it inmyVariable

Page 11: 01. introduction to C++

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 11

Input/Output in C++

cin >> 45.6

Object

Extraction Operator

myVariable

Page 12: 01. introduction to C++

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 12

Manipulators Manipulators are operators that are used to format the

output data display. The most commonly usedmanipulators are…

endl

setw

setprecision

Page 13: 01. introduction to C++

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 13

Manipulators The endl manipulator, when used in an output statement,

causes a line feed to be inserted. It has the same effect asusing the newline ‘\n’ in ‘C’.

cout << “Rising Technologies” << endl;

Page 14: 01. introduction to C++

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 14

Manipulatorsint a = 2314, b = 28, c = 327;

cout << “A = ” << a << endl;

cout << “B = “ << b << endl;

cout << “C = “ << c << endl;

Above code will print 3 lines of output as follows

A = 2314

B = 28

C = 327

Page 15: 01. introduction to C++

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 15

ManipulatorsA = 2314

B = 28

C = 327

But above form is not ideal, it should rather appear likethis,

A = 2314

B = 28

C = 327

Here output is right aligned.

Page 16: 01. introduction to C++

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 16

ManipulatorsA = 2314

B = 28

C = 327

Above form of output is possible only if we specify acommon field width for all the numbers and force themto print right aligned.

setw manipulator can do this job.

Page 17: 01. introduction to C++

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 17

Manipulators

int myVariable = 372;

cout << setw(5) << myVariable;

The manipulator setw(5) specifies a field width 5 forprinting the value of myVariable, this value is rightjustified within the field.

3 7 2

Page 18: 01. introduction to C++

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 18

Manipulators

setprecision(argument);

The setprecision manipulator sets the floating-pointprecision to the specified argument.

float myVariable = 1.123456;

cout << setprecision(2) << myVariable;

Will print following output

1.12

Page 19: 01. introduction to C++

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 19

Reference Variable. A ‘Reference Variable’ provides an Alias (Alternate Name)

for a previously defined variable.

int a = 10;

int &r = a;

// in above statement „r‟ is declared as

reference to „a‟, hence

r++;

Will increase both ‘a’ & ‘r’ by 1

Page 20: 01. introduction to C++

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 20

Reference Variable.

int &r = a;

After this declaration ‘a’ and ‘r’ both refer to the samememory location, as if they were the same variable.

int a = 10;

10

Variable in memory

int &r = a;

One location two names

Page 21: 01. introduction to C++

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 21

Reference Variable. Notice that a reference does not create a copy of an

object/variable, it simply provides an alias/alternatename for it, Hence after following declaration…

int &r = a;

Both ‘a’ and ‘r’ will refer to same memory location.

Page 22: 01. introduction to C++

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 22

Reference Variable. A Reference must be initialized when it is declared, it

must be an alias for something upon declaration.

It would be illegal to declare a reference variable andthen initialize it later.

int a;

int &r; // illegal : reference without initializer

r = a;

int a;

int &r = a; // legal

Page 23: 01. introduction to C++

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 23

C++ Data Types

Derived Types

ArrayFunctionPointerReference

User defined Types

StructureUnionClassEnumeration

Built In Types

Empty Type Floating TypesIntegral Types

Int char float double

C++ Data Types

void