Vous êtes sur la page 1sur 33

Getting Started With C++

Chapter : 6

Introduction
The

C++ language was developed at AT&T Bell Laboratories in the early 1980s by Bjarne Stroustrup. Features are added from his favourite langauge, Simula 67. Bjarne Stroustrup called it C Classes originally. Rick Mascitti coined C++ as the increment operator.

C++ Character Set


Character

set is a set of valid characters that a language can recognise. Letters A Z, a z Digits 09 Special Symbols + _ ) ( * & ^ % $ # @ ! { } White spaces Blank space, tab, Newline Other characters ASCII characters 0 -255

Tokens
The

smallest individual unit in a program is known as a Token or a lexical unit. C ++ has the following 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. asm continue private do typedef register public if case return volatile int static_cast const_cast dynamic_cast

Identifiers
Identifiers

are fundamental building blocks of a program and are used as the general terminology for the names given to different parts of the program like variables, objects, classes, functions, arrays etc. An identifier is an arbitrarily long sequence of letters and digits. The first character must be a letter or the underscore.

Rules for defining an identifier


C++

is case sensitive. Identifier must start with a character or Underscore. No special symbol is used as a identifier. No space is used in defining an identifier. Reserved word cannot be an identifier. Identifier can not start with a number.

Literals
Literals

(often referred as constants) are

data items that never change their value during a program run.
Integer

constant constant

Character Floating String

constant

literal

Integer Constants
Integer

constants are whole numbers without any fractional part. An integer constant must have atleast one digit and must not contain any decimal point. It may either + or sign. A number with no sign is assumed to be positive. Commas cannot appear in an integer constant. C++ allows three types of integer constants. 1. Decimal 2. Octal 3. Hexadecimal

Decimal Integer constants


Decimal

(base 10) An integer constant consisting of a sequence of digits is taken to be decimal integer constant unless it begins with 0 (zero). For Example
12345 - 97 + 45

Octal Integer constants


Octal A

(base 8)

sequence of digits starting with 0 (zero) example:


8 will be written as 010 (810=108) 12 will be written as 014 (1210=148)

is taken to an octal integer.


For
Decimal Decimal

Hexadecimal Integer constants


Hexadecimal A

(Base 16 0 to 9 & A to F)

sequence of digits preceded by 0x or 0X example:


12 will be written as OXC. 14 will be written as OXE.

is taken to be an hexadecimal integer.


For
Decimal Decimal

Character constants
A

character constant is one character enclosed in single quotes. A character constant in C++ must contain one character and must be enclosed in single quotation marks. For example:
a $

&

1 )

The

value of a single character constant is the numerical value of the character in the machines character set known as ASCII

C++

allows you to have certain non-graphic characters in character contants which can not be typed directly from keyboard. For e.g. backspace, tabs, enter. Etc. These nongraphic characters can be represented by using escape sequences. An escape sequence is represented by back slash(\). \b Backspace \n new line \r carriage return \t horizontal tab \\ backslash \ double quotes An escape sequence represents a single character.

Floating Constants
Floating

constants are called real constants. Real constants are numbers having fractional parts. These may be written in two forms called:
Fractional

form Exponent form

for e.g 34.567, -13.0 for e.g 5.8 can be written as

0.58 x 101 = 0.58 E 01 Mantissa Exponent

String literals
A

string literal is a sequence of characters surrounded by double quotes. String literals are array of char. The benefit of array is that each constituent character of the string can be dealt with separately. Each string literal by default is added with a special character \0. For e.g abc size is 4. \nab size is 4 as \n is treated as one character.

Punctuators
Also known as separators in C++. Brackets [ ] Open & close of arrays. Parentheses ( ) Indicate function calls and function parameters. Braces { } Indicate start & end of a compound statement. Comma , Used as a separator in a function argument list. Semicolon ; Used as a statement terminator. Colon : It Indicates a labeled statement. Asterisk * Used for pointer declaraction. Ellipsis Used in formal argument lists of function prototypes to indicates a variable number of argument. Equal to = Used for variable initialisation & as an assignment operator in expressions. Pound # Used for preprocessor directive & (# #) is used as operator to perform token replacement and merging during the preprocessor scanning phase.

Operators
Operators

are tokens that trigger some computation when applied to variables and other objects in an expression. Unary operators are those operators that require one operator to operate upon. & Address operator * Indirection + Unary Plus ++ increment ~ Bitwise complement ! Logical negation

Binary

operators are those operators that require two operands to operate upon.
Arithmetic + (Addition) , (Subtraction) , / (Division), % (Remainder/Modulus), * (Multiplication) << (Shift left) >> (Shift right) & (Bitwise AND) | (Bitwise OR) ^ (Bitwise exclusive OR (XOR) && (Logical AND) | | (Logical OR)

Shift Bitwise Logical

Assignment Relational Component Selection Class member Conditional

= *= /= %= += -= <<= >>= &= ^= |= < > <= >= == !=

(Direct component selector) Alt + 149 (Indirect component selector) Alt + 174

:: (Scope access/resolution)
.* (Deference pointer to class member) ->* (Deference pointer to class member)

Start first program in C++


#include <iostream.h> int main( ) // user define function { cout <<Welcome to C++ programming; return 0; } Compile file Alt + F9 Run file Ctrl + F9 User Screen Alt + F5 Try another program to print your name & address in new line

#include <iostream.h>
#

(hash/pound) sign are directives for preprocessor. These statements are processed before compilation takes place. Include<iostream.h> tells the compiler to include the header file in the program. It can also be written as #include<iostream>

int main()
This

line indicates the beginning of main function. The main() function is the point by where all the C++ programs begin their execution. It is essential that all C++ programs have a main( ) function A program terminates normally following execution of the last statement of main().

Role of Compiler
A

part of the compilers job is to analyze the program code for correctness. If the meaning of a program is then a compiler can not detect errors. Compiler can detect following errors.
Syntax

errors Semantics errors Type errors


Not

detected by compiler.

Run-time

errors Logical errors

Syntax Errors
Syntax

errors occur when rules of a programming language are misused i.e when a grammatical rule of C++ is violated. Syntax refers to formal rules governing the construction of valid statements in a lanaguage.

Semantics Errors
Semantic

errors occur when statements are not meaningful. It refers to the set of rules which give the meaning of a statement. E:g Mahesh plays violin is correct But Violin plays Mahesh is semantically incorrect. E:g X + Y = Z is incorrect.

Type Errors
Data

in C++ has an associated data types. If a function given wrong type of data. Type error is signalled by the compiler. For instance, if the integer argument was expected but actually string was provided in place of it, it is a type error.

Run-time error
Also

known as execution errors, because it occurs at the time of execution. It is caused of some illegal operation taking place or unavailability of desired or required conditions for the execution of the program. For E.g Program trying to open a file which does not exist, or an expression is trying to divide a number by Zero

Logical Errors
A

logical error is that error which causes a program to produce incorrect or undesired output. For E.g counter=1
{ while (counter > 10) cout << no * counter; counter=counter+1;

} Incorrect as condition is not fulfilled, will not produce any output.

Practical Journal Programs


1. 1.

WAP to display given output


Your Name and Address in center of screen

WAP to display given output


********************************************** * WELCOME TO THE WORLD * * OF PROGRAMMING * **********************************************

3.

WAP to display given output: %1 %2%1%2 %3%2%1%2%3 %2%1%2 %1

4. WAP to display the use of all arithmetic operators (+ - * / %) using cin. 5. WAP to create a marksheet 5 sub (1 stu) 6. WAP to evaluate the given expression. x++ - ++x + - - x + x++ - - -x //x=41 7. WAP to evaluate the given expression. ++p + ++q - - -r + p++ - q-- // p=5 q=4 r=2 8. WAP to generate a Playslip of ABCL Corporation using float data type

9. WAP to print the square and cube of entered number. 10. WAP to read values of w,x,y, and z and display the value of p, where p = w+x y-z 11. WAP to accept radius of a sphere and print its area and volume. // a=4pir2 v=4/3 pi r3 12. WAP to print sum of first n. nos. without using loop.

13.

Convert given hours in minutes and minutes into seconds. Convert a given number of days into year, weeks and days. WAP that accepts a character between a to p and prints next 4 characters

14.

15.

Vous aimerez peut-être aussi