Transcription of 12 INTRODUCTION TO C++
1 MODULE 3 247 INTRODUCTION to C++Computer ScienceProgramminig in C++Notes12 INTRODUCTION TO C++In the previous lesson you have learnt about open source softwares. In thislesson you will learn about C++ programming. You may know that C++ is anextension to C Programming language. It was developed at AT&T BellLaboratories in the early 1980s by Bjarne Stroustrup. It is a deviation fromtraditional procedural languages in the sense that it follows Object OrientedProgramming (OOP) approach which is quite suitable for managing large andcomplex reading this lesson , you will be able to:zlearn about C++ character set, tokens and basic data types;zexplain the utility of different types of operators used in C++;zidentify the difference between implicit and explicit conversions;zexplain about Input/Output streams supported by C++;zexplain the structure of a C++ program;zwrite a simple program in C++.
2 C++ CHARACTER SETC haracter set is a set of valid characters that a language can recognize. Acharacter represents any letter, digit or any other special character. The C++programming language also has some character set. Now let us learn C++language character ScienceMODULE 3 INTRODUCTION to C++ 248 Programminig in C++NotesTable : C++ character setLettersA Z, a zDigits0 9 Special CharactersSpace + - * / ^ \ ( ) [ ] { } = ! = < > . $ , ; : %! & ? _ # <= >= @White spacesHorizontal tab ( ), Blank space, Carriage return(< ) Newline, form BASIC DATA TYPESNow you will learn about basic data types used in C++ language. Every programspecifies a set of operations to be done on some data in a particular , the data can be of many types such as a number, a character, booleanvalue etc.
3 C++ supports a large number of data types. The built in or basic datatypes supported by C++ are integer, floating point and character type. A briefdiscussion on these types is given below:Table : Data Types with RangeData TypeRangechar 128 to 127int 32768 to 32767short int 32768 to 32767long int 2147483648 to 2147483647float 10 38 to 10 10 308 to 10 308long 10 4932 to 10+4932 Integer type (int)An integer is an integral whole number without a decimal point. These numbersare used for counting. For example 26, 373, 1729 are valid an integer can hold numbers from 32768 to 32767. However, if theneed be, a long integer (long int) can also be used to hold integers from 2, 147, 483, 648 to 2, 147, 483, point type (float)A floating point number has a decimal point.
4 Even if it has an integral value,it must include a decimal point at the end. These numbers are used for 3 249 INTRODUCTION to C++Computer ScienceProgramminig in C++NotesExamples of valid floating point numbers are: , -927., float type data can be used to hold numbers from *10 38 to *10+38 withsix or seven digits of precision. However, for more precision a double precisiontype (double) can be used to hold numbers from *10 308 to *10+308 withabout 15 digits of Type (Char)It is a non numeric data type consisting of single alphanumeric of valid character types are : A , 9 , P , 8 , & .It may be noted here that 9 and 9 are of different data types. The former isof type int and later of type TOKENSA token is a group of characters that logically belong together.
5 The programmercan write a program by using tokens. C++ uses the following types of KeywordsThere are some reserved words in C++ which have predefined meaning tocomplier called keywords. Some commonly used keywords are given below:List of KeywordsTable : List of keywordsasmdoublenewswitchautoelseoperat ortemplatebreakenumprivatethiscaseextemp rotectedtrycatchfloatpublictypedefcharfo rregisterunionclassfriendreturnunsignedc onstgotoshortvirtualcontinueifsignedvoid defaultinlinesizeofvolatiledeleteintstat icwhiledolongstructComputer ScienceMODULE 3 INTRODUCTION to C++ 250 Programminig in C++ IdentifiersSymbolic names can be used in C++ for various data items used by a programmerin his/her program. For example, if you want to store a value 50 in a memorylocation, you can choose any symbolic name (say MARKS) and use it as givenbelow:MARKS = 50 The symbol = is an assignment operator.
6 The significance of the abovestatement is that MARKS is a symbolic name for a memory location wherethe value 50 is being symbolic name is generally known as an identifier. The identifier is a sequenceof characters taken from C++ character set. The rules for the formation of anidentifier are:(i)An identifier can consist of alphabets, digits and/or underscores.(ii)It must not start with a digit.(iii)C++ is case sensitive, , upper case and lower case letters are considereddifferent from each other. It may be noted that TOTAL and total are twodifferent identifier names.(iv) It should not be a reserved word (keywords). LiteralsLiterals (often referred to as constants) are data items that never change theirvalue during the execution of the program. The following types of literals areavailable in C++.
7 (i)integer-constants(ii)character-consta nts(iii)floating-constants(iv) string-literalsInteger constantsInteger constants are whole numbers without any fractional part. It may containeither + or sign, but decimal point or commas does not appear in any integerconstant. C++ allows three types of integer decimal (Base 10)2. octal (Base 8)3. hexadecimal (Base 16)MODULE 3 251 INTRODUCTION to C++Computer ScienceProgramminig in C++NotesDecimal integer constantsIt consists of sequence of digits and should not begin with 0 (zero). For example124, - 179, + integer constantsIt consists of sequence of digits starting with 0 (zero). For example, 014, integer constantIt consists of sequence of digits preceded by ox or OX. For example OXD, suffix l or L and u or U attached to any constant forces it to be representedas a long and unsigned constantsA character constant in C++ must contain one or more characters and must beenclosed in single quotation marks.
8 For example A , 9 , etc. C++ allows non-graphic characters which cannot be typed directly from keyboard, , backspace,tab, carriage return etc. These characters can be represented by using an escapesequence. An escape sequence represents a single character. The following tablegives a listing of common escape : List of escape sequenceEscape SequenceNongraphic Character\aBell (beep)\bBackspace\fFormfeed\nNewline or line feed\rCarriage return\tHorizontal tab\vVertical tab\?Question mark\\Backslash\ Single quote\ Double quote\xhhHexadecimal number (hhrepresents the number inhexadecimal\000 Octal number (00 represents thenumber in octal\0 NullComputer ScienceMODULE 3 INTRODUCTION to C++ 252 Programminig in C++NotesFloating constantsFloating constants are also called real constants.))
9 These numbers have fractionalparts. They may be written in fractional form or exponent form. A real constantin fractional form consists of signed or unsigned digits including a decimal pointbetween digits. For example , , real constant in exponent form has two parts: a mantissa and an mantissa is either an integer or a real constant followed by letter E or eand the exponent which must be an integer. For example 2E03, LiteralsA sequence of character enclosed within double quotes is called a string literal is by default (automatically) added with a special character \O which denotes the end of the string. Therefore the size of the string is increasedby one character. For example COMPUTER will be represented as COMPUTER\O in the memory and its size is 9 PunctuatorsThe following characters are used as punctuators in C++.
10 Brackets [ ]opening and closing brackets indicate single andmultidimensional array ( )opening and closing brackets indicate functions calls, functionparameters for grouping expressions { }opening and closing braces indicate the start and end of acompound ,it is used as a separator in a function argument ;it is used as a statement :it indicates a labelled statement or conditional operatorAsterisk *it is used in pointer declaration or as multiplication sign =it is used as an assignment sign #it is used as pre-processor OperatorsOperators are special symbols used for specific purposes. C++ includes operatorszRelational operatorsMODULE 3 253 INTRODUCTION to C++Computer ScienceProgramminig in C++NoteszLogical operatorszUnary operatorszAssignment operatorszConditional operatorszComma operatorArithmetical operatorsAn operator that performs an arithmetic (numeric) operation +, -, *, / , or %.