Getting Started with C++

Category: Entertainment

Presentation Description

No description available.


Presentation Transcript


Getting started with C++ Computer Project Work


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


C++ Character S et Character set is a set of valid characters that a language can recognise . A character represents any letter, digit, or any other sign. Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] { } = != < > . ‘ “ $ , ; : % ! & ? _(underscore) # <= >= @ White Spaces Blank spaces, Horizontal tab, Carriage return, New line, Form feed. Other Characters C++ can process any of the 256 ASCII characters as data or as literals.


Tokens(Lexical U nits) The smallest individual unit in a program is known as a Token or lexical unit. Types of Tokens Keywords Identifiers Literals Punctuators Operators


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


Some of the keywords in C ++


Identifiers/Variables Names In any program we typically do lots of calculations. The results of these calculations are stored in computer memory. The calculated values are stored in the memory. To make the retrieval and usage of these values easily, these memory cells (also called memory locations) are given names. Since the value stored in each location may change the names given to these locations are called variable names.


Rules for forming Identifier An identifier as a long sequence of letters & digits. Identifier forming rule of C ++ states the following:- First letter should be an alphabet No special character is allowed except underscore It is case sensitive, Upper & lower case letter treat differently. No blank space is allowed Keywords can not be used as identifiers/ variables names. Examples:- myfile , date9_2_7_6

Definition of Literals:

9 Definition of Literals Literals (often referred as constant) are data items that never change their value during a program run. C++ allows several kind of literals: Integer –Constant Floating constant Character constant String constant


Integer constant Integer constants are whole numbers without any fractional part. Three types of Integer constants Decimal Integer constant Octal Integer constant Hexadecimal Integer constant

Decimal Integer constant:

Decimal Integer constant An integer constant consisting of a sequence of digits is taken to be decimal integer constant unless it begins with 0 (digit zero). Example:- 1296,5642,12,+ 69 ,-23,etc .,

Octal Integer constant:

Octal Integer constant A sequence of digits starting with 0(digit zero) is taken to be an octal integer. Example:- 123, 456 , etc.,

Hexadecimal Integer constant:

Hexadecimal Integer constant A sequence of digits preceded by 0x or 0X is taken to be an hexadecimal integer. Example:-4B6, A43,etc.,


Rules for constructing integer constants :- An integer constant must have at least one digit. It must not have a decimal point. It can be either positive or negative. If no sign precedes an integer constant it is assumed to be positive. No commas or blanks are allowed within an integer constant. Examples:-1234, 41, +97, -45

Floating Constants:

Floating Constants Floating constants are also called as Real constants Real constants are numbers having fractional parts. These may be written in one of the two forms called fractional form or the exponent form. Examples:-2.0, 3.5, 8.6, etc.,

Character constants:

Character constants A Character constant is one character enclosed in single quotes, as in ‘z’. Examples:- ‘a’, ‘ b’, etc.,

Escape sequences:

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

String Literals:

String Literals Multiple character constants are treated as string literals. Examples:-”a” , “ ade ”, etc.,


A real constant must have at least one digit after decimal. It must have a decimal point. It could be either positive or negative. Default sign is positive. No commas or blanks are allowed within a real constant . Examples : 2.0, 17.5, -13.05 7, 7. are invalid real constant Rules for constructing real constants :-


OPERATORS Operators are the symbols that define the actions that computer can perform on various operands. All executable statements involve the use of operators and operands. Operands are the items of data--- variable or constant Examples:- A + B A & B are operand + is an operator

Classification :

Classification The various operators in C++ are as follows :- Arithmetic operators Relational operators Logical operators Assignment operators Conditional operators Increment and decrement operators Unary. Binary, ternary operators Special operators

Arithmetic Operators:

Arithmetic Operators Arithmetic operators- These are the operators used for performing various arithmetic operations on operands. It is a binary operator i.e it requires two operands to operate upon. There are five arithmetic operators available in C++

Arithmetic Operators:

Arithmetic Operators Operator Purpose + Addition - Subtraction * Multiplication / Division % Modulus (Remainder after division) The operands for arithmetic operators can be integers, float-point or mixed mode. The result of arithmetic operators is always a numeric value


If operand contains integer values then Operation is integer arithmetic E.g. If a =14 and b=4 a-b=10 a+b =18 a/b=3 a%b =2 a*b =56 Integer Arithmetic

Real Arithmetic:

Real Arithmetic When operand/variable contains real/float values, it is known as real arithmetic . Example: If x,y are floats x=6.0/7.0=0.857143 y=-2.0/3.0=-0.666667

Mixed –mode Arithmetic:

Mixed –mode Arithmetic One of the operands is real and the other is integer For example 19/10.0=1.9

Relational Operators:

Relational Operators Relational operators are used for comparing values of two operands used in a relational expression. These operands can be of any type. The result of relational operators is only TRUE or FALSE . TRUE is numerically stored in C++ as value 1 and FALSE is stored as a value 0.The various relational operators present in C++ are:

Relational Operators:

28 Relational Operators Operator Meaning < is less than <= is less than or equal to > is greater than >= is greater than or equal to == is equal to != is not equal to


Examples if a=10, b=6 a>b (a greater than b) a<b ( a less than b) a==b ( a equal to b) a<=b (a less than equal to b) a>=b ( a greater than equal to b)

Logical operators :

Logical operators Logical operators are used to group two or more relational operations together in various ways. The logical operators are:

Logical operators :

Logical operators Operator Meaning && Logical AND || Logical OR ! Logical NOT


Examples Logical AND (&&) a > b && x = = 10 , a>b && a>c Logical OR (||) a < m || a < n Logical NOT (!) ! (x >= y)

Assignment Operators :

Assignment Operators These operators are used to assign values to variables in C++. The left hand side of the operator contains a variable which is assigned a value given on right hand side of the operator. The value can be a constant, another variable or an expression. The syntax of assignment operator is: A=b, a=10, c= a+b C++ contains five additional assignment operators: +=, -= , /= , *= , %=

Conditional operators:

Conditional operators The conditional operator consists of 2 symbols the question mark (?) and the colon (:) Syntax: exp1 ? exp2: exp3


Example a=16 b=25; x=(a>b) ? a : b; if (a>b) x=a; else x=b;

Increment and Decrement:

Increment and Decrement syntax: 1. ++variable name 2. variable name++ 3. – –variable name 4. variable name– – x= 5; y = ++x; (prefix) In this case the value of y and x would be 6 x= 5; y = x++; (post fix) Then the value of y will be 5 and that of x will be 6.

Increment and Decrement:

Increment and Decrement syntax: 1. ++variable name 2. variable name++ 3. – –variable name 4. variable name– – x = 5; y = --x; (prefix) In this case the value of y and x would be 4 x = 5; y = x--; (post fix) Then the value of y will be 5 and that of x will be 4.

Unary Operators:

Unary Operators Which operators that require only one operand to act upon. Such operators are known as unary operators. Unary operators usually precede their single operand though some unary operators are written after their operand. These unary operators include: (-)Unary minus- for negating a particular value (+)Unary add- for adding a particular value (++) Increment operator- for incrementing the value by 1 ( -- ) decrement operator- for decrementing the value by 1 Example: ++ 4, -20, +10, etc

Binary Operators:

Binary Operators Which operators that require two operand to act upon. Such operators are known as binary operators. Examples:- A+b A-b A*c A<b A==b

Ternary Operators:

Ternary Operators Which operators that require three operand to act upon. Such operators are known as ternary operators. Examples:- Conditional operators Expression 1? Expression 2: expression 3 If a=10, b=20 X= (a>b) ? a:b


Punctuators The following characters are used as punctuators. [ ] ( ) { } , ; : * … = # Brackets [ ] opening and closing brackets indicate single and multidimensional array subscripts. Parenthesis ( ) these indicate function calls and function parameters.


Braces { } these indicates the start and end of a compound statement. Comma , it is used as separator in a function argument list. Semicolon ; it is used as statement terminator. Collon : it indicates a labeled statement. Asterisk * it is used for pointer declaration. Punctuators


Ellipsis … Ellipsis (...) are used in the formal argument lists of the function prototype to indicate a variable number of argument. Equal to sign = It is used for variable initialisation and an assignment operator in expressions. Pound sign # this sign is used for preprocessor directive. Punctuators

Comma Operator:

Comma Operator Link related expressions together Expressions are evaluated left to right for e.g . value = (x = 10, y = 5, x + y); for (n=1, m=10, n <=m; n++, m++)

The size of Operator:

The size of Operator Gives of bytes occupied in the memory. To determine the lengths of arrays and structures when their sizes are not known to the programmer for e.g. x = sizeof (sum); y = sizeof (long int ); z= sizeof (235L);

Precedence and Associativity :

Precedence and Associativity Precedence rules decides the order in which different operator are applied. Associativity rule decides the order in which multiple operators are applied.

A First look at C++ Program:

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

Predefined streams in I/O Library:

Predefined streams in I/O Library A stream is simply a sequence of bytes. The predefined stream objects for input, output, error as follows: Cin cin stands for console input. Cout cout stands for console output. Cerr cerr stands for console error.

Comments in a C++ Program:

Comments in a C++ Program Comments are pieces of codes that the compiler discards or ignores or simply does not execute. Types of comments: Single line comments Multiline or block comments

Single line comment:

Single line comment These comments begins with // are single line comments. The compiler simply ignores everything following // in that same line Example:- #include< iostream.h > Void main() // the program about addition.

Multi line comments:

Multi line comments The block comments, mark the beginning of comment with /* and end with */. That means, everything that falls between/* and*/ is considered as comment. Example:- #include< iostream.h > Void main() /*the program is about addition*/

Using I/O operators:

Using I/O operators Output operator “ << “ The output operator (“<<“), also called stream insertion operator is used to direct a value top standard output. Input operator “ >> ” The input operator(“>>“), also known as stream extraction operator is used to read a value from standard input.


Variable A variable refers to a storage area whose contents can vary during processing . Cascading of I/O operators The multiple use of input or output operators(“>>”or”<<“) in one statement is called cascading of I/O operators.

Role of compiler:

Role of compiler A part of the compiler’s job is to analyze the program code for ‘correctness’. If the meaning of the program is correct, then a compiler can not detect errors. Types of errors: Syntax Errors Semantic Errors Type Errors Run-time Errors Logical Errors


Syntax Errors are occurred when rules of the program is misused i.e., when grammatical rule of C++ is violated. Ex:- int a, b (semicolon missing) Semantic Errors are occur when statements not meaningful. Ex:- x*y=z; Type Errors are occurred when the data types are misused. Ex:- int a; a=123.56;


Run-time Errors are occurred at the time of execution. Logical Errors are occurred when the logic of program is not proper. Ex:- ctr =1; While ( ctr >10) { cout <<n* ctr ; ctr =ctr+1; }


Made BY- Akshit Kumar Chandora XI Science Roll no. – 4 Thank You

authorStream Live Help