35
Computer Project Computer Project Work Work Getting started with C++ Getting started with C++ Guided by:- Guided by:- Prepared Prepared by:- by:- M. Ravi Kiran (sir) M. Ravi Kiran (sir) K. Durga Prasad K. Durga Prasad T.G.T computer. T.G.T computer. X1 class X1 class M.P.C.Computers M.P.C.Computers

Getting started with c++

Embed Size (px)

Citation preview

Page 1: Getting started with c++

Computer Project Computer Project WorkWork

Getting started with C++Getting started with C++

Guided by:-Guided by:- Prepared by:- Prepared by:- M. Ravi Kiran (sir)M. Ravi Kiran (sir) K. Durga Prasad K. Durga Prasad

T.G.T computer.T.G.T computer. X1 class X1 class

M.P.C.ComputersM.P.C.Computers

Page 2: Getting started with c++

In 1980s In 1980s bjarne Stroustrup bjarne Stroustrup decided to extend decided to extend the C language by adding some features the C language by adding some features from his favourite language from his favourite language Simula 67.Simula 67. Simula 67 was one of the earliest object Simula 67 was one of the earliest object oriented language. Bjarne Stroustrup called it oriented language. Bjarne Stroustrup called it “C with classes”.“C with classes”.Later Later Rick Mascitti Rick Mascitti renamed as C++. Ever renamed as C++. Ever since its birth, C++ evolved to cope with since its birth, C++ evolved to cope with problems encountered by users, and though problems encountered by users, and though discussions.discussions.

INTRODUCTIONINTRODUCTION

Page 3: Getting started with c++

C++ CHARACTER SETC++ CHARACTER SET

Character set is a set of valid characters that a language Character set is a set of valid characters that a language can recognise. A character represents any letter, digit, or can recognise. A character represents any letter, digit, or any other sign.any other sign.

Letters Letters A-Z, a-zA-Z, a-z Digits Digits 0-90-9 Special SymbolsSpecial Symbols Space + - * / ^ \ ( ) [ ] { } = != Space + - * / ^ \ ( ) [ ] { } = !=

< > . ‘ “ $ , ; : % ! & ? _(underscore) # <= >= @< > . ‘ “ $ , ; : % ! & ? _(underscore) # <= >= @ White SpacesWhite Spaces Blank spaces, Horizontal tab, Carriage Blank spaces, Horizontal tab, Carriage

return, New line, Form feed.return, New line, Form feed. Other CharactersOther Characters C++ can process any of the 256 C++ can process any of the 256

ASCII characters as data or as literals.ASCII characters as data or as literals.

Page 4: Getting started with c++

TOKENS(LEXICAL UNITS)TOKENS(LEXICAL UNITS)

The smallest individual unit in aThe smallest individual unit in a program is program is known asknown as a a Token Token oror lexical unit. lexical unit.

Types of TokensTypes of Tokens KeywordsKeywords IdentifiersIdentifiers LiteralsLiterals PunctuatorsPunctuators OperatorsOperators

Page 5: Getting started with c++

KEYWORDSKEYWORDS

Keywords are the words that convey a Keywords are the words that convey a special meaning to the language compiler. special meaning to the language compiler. These are reserved for special purpose These are reserved for special purpose and must not be used as normal identifier and must not be used as normal identifier names.names.

Page 6: Getting started with c++

Some of the keywords in c++Some of the keywords in c++

asm continue float new signed tryasm continue float new signed try

auto default for operator sizeof typedefauto default for operator sizeof typedef

break delete friend private static unionbreak delete friend private static union

case do goto protected struct unsignedcase do goto protected struct unsigned

catch double if public switch virtualcatch double if public switch virtual

char else inline register template voidchar else inline register template void

class enum int return this volatileclass enum int return this volatile

const extern long short throw whileconst extern long short throw while

Page 7: Getting started with c++

IdentifiersIdentifiers

Identifiers are names of the program Identifiers are names of the program given by user.given by user.

Rules to write identifiersRules to write identifiers1.1. Do not start with digits.Do not start with digits.2.2. No special symbols are used except No special symbols are used except

_(underscore)._(underscore).3.3. No spaces are used.No spaces are used. Examples:- myfile , date9_2_7_6Examples:- myfile , date9_2_7_6

Page 8: Getting started with c++

LiteralsLiterals

Literals (constants) are data items that Literals (constants) are data items that never change their value during a never change their value during a program run.program run.

Types of Literals:Types of Literals:

1.1. Integer constantInteger constant

2.2. Floating constantsFloating constants

3.3. Character constantCharacter constant

4.4. String literalString literal

Page 9: Getting started with c++

Integer constantInteger constant

Integer constants Integer constants are whole numbers are whole numbers without any fractional part.without any fractional part.

ThreeThree types of Integer constants types of Integer constants

1.1. Decimal Integer constantDecimal Integer constant

2.2. Octal Integer constantOctal Integer constant

3.3. Hexadecimal Integer constantHexadecimal Integer constant

Page 10: Getting started with c++

Decimal Integer constantDecimal Integer constant

An integer constant consisting of a An integer constant consisting of a sequence of digits is taken to be decimal sequence of digits is taken to be decimal integer constant unless it begins with 0 integer constant unless it begins with 0 (digit zero).(digit zero).

Example:- 1296, 5642, 12, +69,- Example:- 1296, 5642, 12, +69,- 23,etc., 23,etc.,

Page 11: Getting started with c++

Octal Integer constantOctal Integer constant

A sequence of digits starting with0(digit A sequence of digits starting with0(digit zero) is taken to be an octal integer.zero) is taken to be an octal integer.

Example:-123, 456, etc.,Example:-123, 456, etc.,

Page 12: Getting started with c++

Hexadecimal Integer constantHexadecimal Integer constant

A sequence of digits preceded by 0x or A sequence of digits preceded by 0x or 0X is taken to be an hexadecimal integer.0X is taken to be an hexadecimal integer.

Example:-4B6, A43,etc.,Example:-4B6, A43,etc.,

Page 13: Getting started with c++

Floating ConstantsFloating Constants

Floating constants are also called as Floating constants are also called as Real Real constantsconstants

Real constantsReal constants are numbers having are numbers having fractional parts. These may be written in fractional parts. These may be written in one of the two forms called one of the two forms called fractional formfractional form or the or the exponent form. exponent form.

Examples:-2.0, 3.5, 8.6, etc., Examples:-2.0, 3.5, 8.6, etc.,

Page 14: Getting started with c++

Character constantsCharacter constants

A A Character constantCharacter constant is one character is one character enclosed in single quotes, as in ‘z’. enclosed in single quotes, as in ‘z’.

Examples:- ‘a’, ‘b’, etc.,Examples:- ‘a’, ‘b’, etc.,

Page 15: Getting started with c++

Escape sequencesEscape sequences

\a Audible sound\a Audible sound\b back space\b back space\f Formfeed\f Formfeed\n Newline or Linefeed\n Newline or Linefeed\r Carriage return\r Carriage return\t Horizontal tab\t Horizontal tab\v Vertical tab\v Vertical tab\\ Backslash\\ Backslash\’ single quote\’ single quote\” double quote\” double quote\? Question mark\? Question mark\on Octal number\on Octal number\xHn Hexadecimal number\xHn Hexadecimal number\0 Null\0 Null

Page 16: Getting started with c++

String LiteralsString Literals

Multiple character Multiple character constants are treated constants are treated as string literals.as string literals.

Examples:-”a” , “ade”, etc.,Examples:-”a” , “ade”, etc.,

Page 17: Getting started with c++

PunctuatorsPunctuators

The following characters are used as The following characters are used as punctuators.punctuators.

[ ] ( ) { } , ; : * … = #[ ] ( ) { } , ; : * … = # BracketsBrackets [ ] opening and closing brackets [ ] opening and closing brackets

indicate single and multidimensional array indicate single and multidimensional array subscripts.subscripts.

Parenthesis Parenthesis ( ) ( ) these indicate function these indicate function calls and function parameters.calls and function parameters.

Page 18: Getting started with c++

BracesBraces { } { } these indicates the start and end of these indicates the start and end of a compound statement.a compound statement.

Comma Comma , , it is used as separator in a function it is used as separator in a function argument list.argument list.

Semicolon Semicolon ; ; it is used as statement it is used as statement terminator.terminator.

CollonCollon : : it indicates a labeled statement.it indicates a labeled statement. Asterisk Asterisk * it is used for pointer declaration.* it is used for pointer declaration.

Page 19: Getting started with c++

Ellipsis Ellipsis …… Ellipsis (...) are used in the formal Ellipsis (...) are used in the formal argument lists of the function prototype to argument lists of the function prototype to indicate a variable number of argument.indicate a variable number of argument.

Equal to signEqual to sign = = It is used for variable It is used for variable initialisation and an assignment operator in initialisation and an assignment operator in expressions.expressions.

Pound signPound sign # # this sign is used for this sign is used for preprocessor directive.preprocessor directive.

Page 20: Getting started with c++

OperatorsOperators

Operators are tokens that trigger some Operators are tokens that trigger some computation when applied to variables and computation when applied to variables and other objects in an expression.other objects in an expression.

Types of operatorsTypes of operators

1.1. Unary operatorsUnary operators

2.2. Binary operatorsBinary operators

3.3. Ternary operatorsTernary operators

Page 21: Getting started with c++

Unary operatorsUnary operators

Unary operators are those operators that Unary operators are those operators that require one operator to operate upon.require one operator to operate upon.

Examples :- +45, 5, -636,etc.,Examples :- +45, 5, -636,etc.,

Page 22: Getting started with c++

Some unary operatorsSome unary operators

& & Addresser operator Addresser operator

* Indirection operator* Indirection operator

+ Unary plus+ Unary plus

- Unary minus- Unary minus

~ Bitwise complement~ Bitwise complement

++ increment operator++ increment operator

-- decrement operator-- decrement operator

! Logical negation! Logical negation

Page 23: Getting started with c++

Binary operatorsBinary operators

Binary operators are those operators that Binary operators are those operators that require two operands to operate upon.require two operands to operate upon.

Types of Binary operatorsTypes of Binary operators Arithmetic operatorsArithmetic operators

+(+(addition) –(subtraction) *(multiplication) addition) –(subtraction) *(multiplication) /(division) %(reminder/modulus)/(division) %(reminder/modulus)

Logical operatorsLogical operators

&& (Logical AND) || (Logical OR)&& (Logical AND) || (Logical OR)

Page 24: Getting started with c++

Relational operatorsRelational operators

< (Less than)< (Less than)

<=(Less than or equal to)<=(Less than or equal to)

>(Greater than)>(Greater than)

>=(greater than or equal to)>=(greater than or equal to)

== (equal to)== (equal to)

!= (not equal to)!= (not equal to)

Page 25: Getting started with c++

A First look at C++ ProgramA First look at C++ Program

Why include iostream.h ?Why include iostream.h ?

The header file The header file iostream.h iostream.h is included in is included in every C++ program to implement every C++ program to implement input/output input/output facilities. Input/output facilities. Input/output facilities are not defined within C++ facilities are not defined within C++ language, but rather are implemented in a language, but rather are implemented in a component of C++ standard library,component of C++ standard library, iostream.h iostream.h which is I/O library. which is I/O library.

Page 26: Getting started with c++

Predefined streams in I/O LibraryPredefined streams in I/O Library

A stream is simply a sequence of A stream is simply a sequence of bytes.bytes.

The predefined stream objects for input, The predefined stream objects for input, output, error as follows:output, error as follows:

1.1. Cin Cin cin stands for console input.cin stands for console input.

2.2. CoutCout cout stands for console output. cout stands for console output.

3.3. Cerr Cerr cerr stands for console error.cerr stands for console error.

Page 27: Getting started with c++

Comments in a C++ ProgramComments in a C++ Program

Comments are pieces of codes that the Comments are pieces of codes that the compiler discards or ignores or simply compiler discards or ignores or simply does not execute.does not execute.

Types of comments:Types of comments:

1.1. Single line commentsSingle line comments

2.2. Multiline or block commentsMultiline or block comments

Page 28: Getting started with c++

These comments begins with // are single These comments begins with // are single line comments. The compiler simply line comments. The compiler simply ignores everything following // in that same ignores everything following // in that same lineline

Example:-Example:-

#include<iostream.h>#include<iostream.h>

Void main() // the program about addition. Void main() // the program about addition.

Single line commentSingle line comment

Page 29: Getting started with c++

Multi line commentsMulti line comments

The block comments, mark the beginning The block comments, mark the beginning of comment with /* and end with */. That of comment with /* and end with */. That means, everything that falls between/* means, everything that falls between/* and*/ is considered as comment.and*/ is considered as comment.

Example:-Example:-

#include<iostream.h>#include<iostream.h>

Void main() Void main() /*the program is about addition*//*the program is about addition*/

Page 30: Getting started with c++

Using I/O operatorsUsing I/O operators

Output operatorOutput operator “ << ““ << “ The output operator (“<<“), also called The output operator (“<<“), also called

stream insertion operatorstream insertion operator is used to direct is used to direct a value top standard output.a value top standard output.

Input operator Input operator “ >> ”“ >> ” The input operator(“>>“), also known as The input operator(“>>“), also known as

stream extraction operatorstream extraction operator is used to read is used to read a value from standard input.a value from standard input.

Page 31: Getting started with c++

VariableVariable A A variable variable refers to a storage area whose refers to a storage area whose

contents can vary during processing.contents can vary during processing.

Cascading of I/O operatorsCascading of I/O operators The multiple use of input or output The multiple use of input or output

operators(“>>”or”<<“) in one statement is called operators(“>>”or”<<“) in one statement is called cascading of I/O operators.cascading of I/O operators.

Page 32: Getting started with c++

Role of compilerRole of compiler

A part of the compiler’s job is to analyze the A part of the compiler’s job is to analyze the program code for ‘correctness’. If the meaning program code for ‘correctness’. If the meaning of the program is correct, then a compiler can of the program is correct, then a compiler can not detect errors.not detect errors.

Types of errors:Types of errors:1.1. Syntax ErrorsSyntax Errors2.2. Semantic ErrorsSemantic Errors3.3. Type ErrorsType Errors4.4. Run-time ErrorsRun-time Errors5.5. Logical ErrorsLogical Errors

Page 33: Getting started with c++

Syntax ErrorsSyntax Errors are occurred when rules of are occurred when rules of the program is misusedthe program is misused i.e., i.e., when when grammatical rule of C++ is violated.grammatical rule of C++ is violated.

Ex:- int a, b (semicolon missing) Ex:- int a, b (semicolon missing) Semantic ErrorsSemantic Errors are occur when are occur when

statements not meaningful.statements not meaningful.

Ex:- x*y=z;Ex:- x*y=z; Type ErrorsType Errors are occurred when the data are occurred when the data

types are misused.types are misused. Ex:-int a; a=123.56;Ex:-int a; a=123.56;

Page 34: Getting started with c++

Run-time ErrorsRun-time Errors are occurred at the time are occurred at the time of execution.of execution.

Logical ErrorsLogical Errors are occurred when the are occurred when the logic of program is not proper.logic of program is not proper.

Ex:- ctr=1;Ex:- ctr=1; While (ctr>10)While (ctr>10) {{ cout<<n*ctr;cout<<n*ctr; ctr=ctr+1;ctr=ctr+1; }}

Page 35: Getting started with c++

Thank YouThank You

Thank YouThank You Thank Thank

YouYouThank YouThank You Thank YouThank YouThank YouThank You Thank YouThank You Thank YouThank You