C++ because advanced features like namespaces and Compiler : exception handling are not available in older versions of Turbo C++. All compilers will support the basic features of C++. Before providing a technical explanation a compiler in laymans language is a program which will read through the C++ program code (code refers to the If you are using Linux/Unix then you will be having the program written by the programmer) line by line. The GNU C++ compiler in your Linux distribution CD (this compiler will read each and every character that has been compiler is freeware but it doesnt have a graphical user typed in the program code. After reading the entire code, interface- it can only be used from the command line; the compiler will convert the code into machine level refer to the Appendix section). format (i.e. 0s and 1s). The compiler is very dedicated and hard working since it will meticulously read each and every character. Keywords : Technically speaking, the microprocessor in a computer can understand only binary numbers (only 0s and 1s). It is Keyword is a word that the compiler already knows, i.e. not possible for us to write programs in binary numbers. when the compiler sees a keyword somewhere in the Compilers will convert the coding for a program into program it knows what to do automatically. machine understandable form (i.e. it will convert the instructions we type into machine understandable form). For example, when the compiler encounters the keyword The instructions we type (in something similar to English), int, it knows that int stands for an integer. Or if the is known as the source code. The compiler converts the compiler reads a break, then it knows that it should source code into the object code (which the break out of the current loop. microprocessor understands and we cannot understand). There is a detailed discussion on this topic in the chapter Advanced Topics.
Variable and Constant :
If you are new to programming you might wonder as to where you should type your C++ program? First of all you should have a C++ compiler. There are many As the name suggests, a variable is something whose value compilers available for free on the Net and they can be can be changed throughout the program. It is not fixed. On downloaded from the Net. After downloading the the other hand, a constant is one whose value remains the compiler, create a new C++ source file and type the C++ same (constant) throughout the program. code in the compiler. Later on in this book, we shall see how to run a C++ program in the compiler.
A few compilers available on the Net are : Operators and Operands:
1. Borland C++ compiler Operator is something that performs operation on a
: www.borland.com/bcppbuilder/freecompiler/ variable. For example + is an operator that performs the 2. Bloodshed Dev C++ compiler addition operation. The terms on which the operator : www.bloodshed.net/devcpp.html operates are known as the operands. In the expression 3. DJGPP C++ compiler 4. For Linux/Unix the GNU C++ compiler is available x+y in standard installations of the operating system. x and y are known as the operands and + is the operator. Most compilers nowadays provide an Integrated Development Environment (IDE), i.e. they have an editor, a compiler, a linker and some extra tools (like a debugger). In schools and colleges the most commonly used compiler Expression: is Turbo C++. Most of the programs written in this book Expression performs an operation and consists of For example: operators and operands (or variables). a>b a = b; This is an expression in which a and b are the operands. > is the operator. This means that value of b is assigned to a.
Parentheses, braces, angle and square brackets
: Binary and Unary: There are different type of brackets: Binary means two and unary means one. A binary operator operates on two operands (or two values) Parentheses are just the whereas a unary operator operates on just one operand. normal brackets ( ). Braces are { }. Angular brackets are < >. Square brackets are [ ]. Definition and Declaration : All of these brackets are used in C++ but each one is used In C++ these two terms are used frequently. Declaration of for different purposes. a variable means telling the compiler that this variable is an integer, or this variable is a character. A declaration will Character: inform the compiler about the type of a variable. Defining a variable means allocation of memory space by the Characters include alphabets, numbers and other special compiler for that particular variable. symbols as well (like *, /, - etc). Usually one tends to think of characters as only alphabets; but remember a In the case of variables, a single statement performs both character could be anything (an alphabet, a number, a declaration and definition. For example: the statement special symbol on your keyboard or even a blank white space is a character). int x; Syntax : declares and defines x as an integer. Every language has its own syntax. By the Remember that declaration is just declaring the type of term syntax we refer to the structure of a language and something (telling the compiler what data type it belongs use of correct language. English has its own set of rules to) but definition means allotting space to it. It may seem while French has slightly different rules. The vocabularies as if both are one and the same; in many cases they are of both the languages are also different. Programming is but in a few places they are not (these cases will be dealt also another language (thats it is called programming with later). language). Just like any other language it has its own set of rules that must be complied with. This is known as the syntax. Only if the programmer adheres to the syntax will the compiler understand the instructions. Initialization : Errors: Initialization refers to the first (initial) assignment of a value to a variable. This is a very common term in programming x = 50; languages. In fact it is very common to hear someone say, This initializes the value of x to 50. "Syntax Error". Error means a mistake and syntax error Always remember that the value to the right of the equal means that there is a mistake in the syntax. The compiler sign will be assigned (or stored) to the value on the left of reads the entire program before execution. While reading the equal sign. the program, if the compiler discovers that it cant understand some instruction it will display an error message. Till the error is cleared, the program cannot be An interesting point (about Kilo, Mega etc.): executed. The compiler will give a syntax error if the syntax has not been adhered to properly. Errors generated You might have read that 1Kilobyte = 1024 bytes and by the compiler are known as compile-time errors. perhaps you thought about it as well, Shouldnt 1Kilobyte = 1000 bytes? There are other types of errors as well. The compiler is not intelligent to detect all types of errors. For instance if the Lets start with our familiar decimal system. In the decimal programmer make a logical error, the compiler will never system, 1 Kilo unit = 1000 units. know it. Suppose a program is written for adding two numbers and in the coding, instead of the + operator, the For example: operator has been used. The compiler will not recognize any error during compilation and the program will execute 1 Kilometer = 1000 meters = (but the programmer will not get the desired output). This 103 meters is a logical error and as a programmer you have to be very 1 Kilogram = 1000 grams = careful while coding. The compiler is not brilliant enough 103 meters to know what is in the programmers mind! 1 Megawatt = 1000 Kilowatts = 1000000 watts = 106 watts Executable file and linkers: Computers only understand binary language (1s and 0s). Executing refers to the process of running a program. 8 bits form a byte. Weve talked about object file, but can we execute object The different types of storage are: internal files directly? The compiler produces an object code but CPU registers, RAM, ROM and secondary this object code generally depends on some other external storage. files. For example: if you are using complex mathematical RAM is used as main memory and data is functions, you might make use of functions which have present only as long as the computer is switched been defined by some other programmer. That on. programmer would have created a library which you can Main memory is directly accessible by the include in your source code (instead of creating those computer whereas secondary memory is not. functions in your program). A library is a collection of During execution programs should be object files. Thus your program will actually depend on the present in the main memory. other object files for some of the complex mathematical Word-size of a machine depends on the size functions which youve used. Someone now has to link up of the processors internal registers. all these object files to produce an executable file. This Machine level language is written in 1s and work is done by the linker. Most modern compilers have 0s. a linker included in them. Linkers are discussed in detail later. Assembly level languages make use of mnemonics (mnemonics are abbreviated English words used to represent instructions). Remember: Each C++ source code needs to be compiled High level languages like BASIC are in separately to produce an object code (ten source codes simple English but they are far away from the mean that well have ten object codes). Finally the linker real hardware of the system. has to combine these ten object codes to produce one C has the advantages of both high level and single executable module. low level languages. But it doesnt support Object Oriented Programming (OOP). Debugging: C++ is actually C with classes (i.e. it supports OOP). is the process of troubleshooting a program (of course its A compiler is required to convert the source done when the program doesnt work up to your code (i.e. the instructions we type in English) expectation!). Debugging is the process of identifying bugs into the object code (i.e. the machine language in the code. A bug is a programming term for an error. which the computer understands). 1 kilobyte = 1024 bytes (and not 1000 Some extra details: bytes). Cint Converts a variant to an integer CLng Converts a variant to a long Visual basic CSng Converts a variant to a single Keyword/Feature Description CStr Converts a variant to a string Array handling DateSerial Converts a variant to a date IsArray Returns True if a variable is an array DateValue Converts a variant to a date Erase Reinitilizes a fixed-size array Hex Converts a variant to a hex string LBound Returns the lower bound of an array Oct Converts a variant to an octal string UBound Returns the upper bound of an array Fix Converts a variant to a fixed string Assignments Int Converts a variant to an integer string = Assigns a value to a variable Sgn Converts a variant to a single string Let Assigns a value to a variable TimeSerial Converts a variant to a time Set Assigns an object to a variable TimeValue Converts a variant to a time Comments Dates/Times ` Includes inline comments in your script Date Returns the current date Rem Includes comments in your script Time Returns the current time Constants/Literals DateSerial Returns a date from its parts Empty Indicates an uninitialized variable DateValue Returns a date from its value Nothing Disassociates a variable with an object Day Returns day from a date Null Indicates a variable with no data Month Returns month from a date True Boolean True Weekday Returns weekday from a date False Boolean False Year Returns year from a date Control flow Hour Returns hour from a time Do...Loop Repeats a block of statements Minute Returns minute from a time For...Next Repeats a block of statements Second Returns seconds from a time For Each...Next Repeats a block of statements Now Returns current date and time If...Then...Else Conditionally executes statements TimeSerial Returns a time from its parts Select Case Conditionally executes statements TimeValue Returns a time from its value While...Wend Repeats a block of statements Declarations Conversions Dim Declares a variable Abs Returns absolute value of a number Private Declares script-level private variable Asc Returns the ASCII code of a character Public Declares public-level public variable AscB Returns the ASCII code of a character ReDim Reallocates an array AscW Returns the ASCII code of a character Function Declares a function Chr Returns a character from an ASCII code Sub Declares a subprocedure ChrB Returns a character from an ASCII code Error Handling ChrW Returns a character from an ASCII code On Error Enables error handling CBool Converts a variant to a boolean Err Contains information about last error CByte Converts a variant to a byte Input/Output CDate Converts a variant to a date InputBox Prompts the user for input CDbl Converts a variant to a double MsgBox Displays a message to the user Math Strings Atn Returns the Arctangent of a number Instr Returns index of a string in another Cos Returns the cosine of a number InStrB Returns index of a string in another Sin Returns the sine of a number Len Returns the length of a string Tan Returns the tangent of a number LenB Returns the length of a string Exp Returns the exponent of a number Lcase Converts a string to lowercase Log Returns the logarithm of a number Ucase Converts a string to uppercase Sqr Returns the square root of a number Left Returns the left portion of a string Randomize Reseeds the randomizer LeftB Returns the left portion of a string Rnd Returns a random number Mid Returns the mid portion of a string Operators MidB Returns the mid portion of a string + Addition Right Returns the right portion of a string - Subtraction RightB Returns the right portion of a string ^ Exponentiation Space Pads a string with spaces Mod Modulus arithmetic StrComp Compares two strings * Multiplication String Pads a string with a character / Division Ltrim Removes leading spaces from a string \ Integer Division Rtrim Removes trailing spaces from a string - Negation Trim Removes leading and trailing spaces & String concatenation Variants = Equality IsArray Returns True if variable is an array <> Inequality IsDate Returns True if variable is a date < Less Than IsEmpty Returns True if variable is empty <= Less Than or Equal To IsNull Returns True if variable is null. > Greater Than IsNumeric Returns True if variable is a number >= Greater Than or Equal To IsObject Returns True if variable is an object Is Compares expressions VarType Indicates a variable's type And Compares expressions Or Compares expressions Xor Compares expressions Eqv Compares expressions Imp Compares expressions Objects CreateObject Creates reference to an OLE object IsObject Returns True if object is valid Options Option Explicit Forces explicit variable declaration Procedures Call Invokes a subprocedure Function Declares a function Sub Declares a subprocedure
Part - A Experiment No.07 A.1 Aim: To Understand The Below Concept of Operator Overloading. 1. To Overload Unary Operator. 2. To Overload Binary Operator. and Type Conversions
Here are the number of significant figures in each measurement:1. 3.005 g - 4SF2. 820 m - 3SF 3. 0.000 670 km - 4SF4. 0.405 021 kg - 7SF5. 22.4 L - 3SF