C++ Basics to Advanced

December 22, 2018 | Author: Anuj Phadke | Category: Computer Data Storage, Pointer (Computer Programming), Random Access Memory, Bit, Subroutine
Share Embed Donate


Short Description

Download C++ Basics to Advanced...

Description

Conquering C++ This is the first part of C++ (basics of programming and OOP)

Contents - PART I Basic C++ 1.) Introduction • • •

a.) Evolution of Programming Languages



b.) Binary System & Memories c.) Terms used in programming



2.) Simple C++ Programming • • •

• •

6.) More Data Types

Your very first C++ program Compilers and how to run a program Data Types of variables o Integer o Floating Types o Double o Character o Boolean type o Data type range o Determining the range o More on Binary numbers Simple C++ programs Identifiers,Keywords and File Nomenclature,Constants,Comments

• •

• •

Workshop (for chapters 4, 5 and 6) • •

• • • • • •

Arithmetic operator, Escape Sequences Assignment operator, Type Conversion Arithmetic Assignment operators, Relational operators Logical Operators, Unary operator Sizeof operator, Ternary (conditional) operator Operator Precedence and Associativity, Comma Operator, Bitwise Operator

Workshop (for chapters 1, 2 and 3) • •

Test Yourself More questions (extra programs)

Test Yourself-II More Questions (extra programs)

7.) Pointers • •

3.) Operators

Scope of variables, Storage Classes (auto, static, extern, register) Type Qualifiers (const and volatile), Variable Initialization, Arrays Character Arrays Multi Dimensional Arrays, Initializing Multi Dimensional Arrays, Passing Arrays to functions Structures, Nesting of Structures Enumerated Data Types, Unions, Passing structures to functions

• • • • • • • • •

Address of operator, Declaration and use of pointer Arithmetic operation on pointers, Pointers and Arrays Pass by value, Pass by reference (C and C++ style) and reference variables More on references Returning pointers from functions Memory Allocation (new and delete) and Memory Heap Pointers to functions, Pointer Declarations Pointers to Structures, Pointer to Pointer (multiple indirection) Pointer to constants and constant pointers, Void pointers Pointers to characters, Pointers Demystified (2-D arrays and pointers) Passing multidimensional arrays to functions, Functions with varying arguments

4.) Controlling Program Flow •





Loops (Iterations) o For loop and its variations o While, Do-while loop Decision statement o If...elseif..., Nested If, Conditional Operator o Switch Case Loop Control Statements o Break, Continue, Goto, Return



• •



• • •



• •

5.) Functions •

8.) Classes and Objects

Declaration, call and definition, Parameters and arguments Default Arguments, Returning values from a function, Returning void, The int main( ) function An illustration of int main( ) Types of functions, Using Library functions (rand, srand and time), Function overloading, An example using functions Recursion (calling oneself!), Inline Functions

• •





Object Oriented Programming Concept and OOP Languages Class with example Constructors, Constructor with Parameters, Overloaded Constructors, Default Constructors and Arrays of Objects Scope Resolution Operator, Destructor, Objects and Functions (passing and returning objects), Initialializing an object using an object A Practical Use of Classes Data Encapsulation...Who are we hiding data from? More objects and classes, Private member functions (helper functions) Friend Function and Friend Classes, Static Class Members, Constant Objects (and the 'mutable' qualifier) Copy Constructor, this pointer, pointer to objects, objects in memory Initializer list, explicit keyword, declaration and definition, return value optimization

Workshop (for chapters 7 and 8) • •

Test Yourself-III More Questions (extra programs)

If you are done with the basics go on to

Advanced C++ (PART - II)

3

Programming Languages

The earlier first section was Binary numbers. Some people said that I ought to reduce the math in this tutorial and I felt that perhaps I should start off differently. And so I've altered the sequence of sections in this first unit.

Recap of important computer concepts: When we talk of computers, we refer to ‘hardware’ and ‘software’. All the physical components like monitor, keyboard, mouse, modem, printer etc. fall under the hardware category. We need computers to do something useful for us but computers aren’t smart enough to know what we want them to do (at least not yet!). We need to explicitly tell the computer our requirements in the form of instructions. A set of instructions for doing something useful forms a ‘program’. And we refer to these programs as software (i.e. unlike hardware, software is something that we can’t touch and feel). Major hardware parts in a computer: 1.

Input and Output devices: Computer can receive input from input devices (like a keyboard, mouse or a scanner). Output devices are used by the computer to send out information to the user; example: monitor, printer, plotter etc.

2.

Arithmetic and Logical Unit (ALU): As the name implies this unit is responsible for all arithmetic calculations. It is part of the central processing unit.

3.

Memory: When our computer wants to work on some information, it will require some place where it can store data; where it can store the instructions; where it can store intermediate results of operations etc. Memory serves this purpose and there are different types of memory (for different requirements): Primary memory: This memory can be quickly accessed by the computer (in technical jargon we say

that this memory has low access time; i.e. time taken to access data in primary memory is less). Generally, instructions and data needed by the computer immediately for processing are placed in primary memory. Data in primary memory is not permanent. Each time we restart the computer, the memory would get refreshed. Secondary memory/ storage: This is the place where we store all our data. It’s a long-term storage device (like a hard-disk). Access times are higher but secondary memory is cheaper than primary memory.

Introduction

4 4.

CPU (Central Processing Unit): We’ve seen computer parts to get input, store information, display output and also to perform

calculations. But there needs to be someone at a higher level to control the individual units; someone to decide when to capture the input, when to send output information to the monitor etc. The CPU takes up this responsibility and is termed the ‘brain’ of the computer. The CPU is also called the processor (a microprocessor chip).

The language computers understand: We have a lot of languages in this world but all computers can understand only binary language. The vocabulary is very simple and small; it consists of only 2 things: 0 and 1. This is all that a computer understands. (Humans are comfortable with the decimal system - consisting of the numbers 0 to 9). We’ll look at the binary system later in this chapter. Remember: A computer finally needs everything in binary language (instructions and data).

Programming Languages: Programs are written to tell the computer to do something useful for us. It might be as simple a task as adding two numbers or as complex as transferring data between 2 computers in a network. There are several reasons why we need programs. Imagine searching through a stack of papers to search for some telephone bill. Imagine a company maintaining its accounts (expenses and income on a day-to-day basis) - if this was done using the traditional file and paper method, someone would have to keep entering all the details in sheets of paper and then stack them into files. If, in future, someone wants to find out when a particular product was sold they would have to manually read through each

Introduction

5 and every paper in each and every file. Computers can be programmed to perform such tasks and they will do it faster. Basically, the computer is very good in performing repetitive tasks. One point to note: man created computers and man programs computers to perform certain tasks. A program is a set of instructions that can be executed by the computer. The computer will obediently follow whatever the programmer instructs it to do (as long as it understands the instructions). The downside to this is that the computer does not have any special intelligence; it is only as intelligent as it is programmed to be. For instance, a robot can be programmed to walk. If it is not programmed to detect obstacles, the robot will simply bang into obstacles because it does not have the intelligence to recognize and avoid obstacles. If the programmer provides such provisions, then the robot will be able to handle such scenarios. With many software programs already existing, you may wonder why do we need more software; why not just buy and use what already exists? Software is usually not custom designed for the requirements of a particular company and you may not even have software for your own special requirement. For example, dentists never used to have any software specifically for their use but now you can see a range of dental software (the dentist can maintain details of each of his patients’ tooth in the computer). Programs needn’t be as complicated as dental software; you may want to have a little software to calculate your CGPA or to calculate your rank in class. To perform such tasks a programmer has to write a program (or a set of instructions). These instructions are written in a specific programming language and the programmer can chose to write the instruction in any one of the available languages.

Evolution of Programming Languages: Any programming language can be categorized into one of the following categories: •

High Level



Middle Level



Low Level (machine and assembly languages)

Middle level languages are sometimes clubbed together under the category of high-level languages. Machine Level Languages: A computer has a processor and if we want the computer to do something then we need to direct instructions at the processor. The problem is that computers can understand only binary language (i.e. the language which comprises of only 1s and 0s). All instructions to the processor should be in binary and so a programmer can write programs using a series of 1s and 0s. Every

Introduction

6 processor will understand only some instructions (this depends on how the processor was designed). An instruction to increment a variable might be: 110110100011101 Imagine having 100 such instructions in a program! The advantage is that no conversion (or translation) is needed because the computer can understand the 1s and 0s. This is called machine level language and this was used at the time computers came into existence. The drawbacks are quite clear. By using machine language writing programs is very tedious and also prone to error (even if one bit is wrong, the entire program functionality could get altered). Trying to identify the error will also be a very tedious job (imagine reading a series of 1’s and 0’s trying to locate the mistake). Assembly Languages: Since it is very difficult for us to write instructions in binary language, assembly languages were developed. Instead of using a set of 1s and 0s for a particular instruction, the programmer could use some short abbreviations (called ‘mnemonics’) to write the program (for example: ADD is a mnemonic to add two numbers). Every processor has an instruction set that describes the various commands that the processor can understand. A normal instruction set will have instructions like JMP (jump), ADD (for addition) and so on. A programmer will write the program using these instructions and an assembler will convert the mnemonics into binary form so that the processor can understand it. The problem is that assembly level languages are specific to each processor. For example the 8085 (the simplest microprocessor) has an instruction set different from the 8086 microprocessor. Thus you would have to rewrite the program for each microprocessor. Another problem with assembly level programming is that the programmer needs to know details about the processor’s architecture such as the internal registers where values can be stored, how many internal registers are available for use etc. Low level languages are very close to the hardware but difficult for writing programs. High Level Languages: Writing larger programs in assembly language is quite difficult and hence, high-level languages were developed (like BASIC). These languages were not close to the actual computer hardware but were very close to the English language. They tried to simplify the process of programming. In these languages the programmer needn’t worry about internal registers and processor architecture; instructions could be typed in almost normal English. The English-like instructions had to be converted to machine language using a compiler. One simple example of an English like command in COBOL is:

Introduction

7 ADD incentive TO basic GIVING salary. The instruction is quite self-explanatory (Add ‘incentive’ and ‘basic’ and store the result in ‘salary’). COBOL is also a high-level language. As programs grew larger (for example: maintaining a record of all employees, accounts maintenance etc.), even high level languages had certain drawbacks. These languages are also referred to as unstructured form of programming. The reason they are unstructured is because when a large program is written, it becomes very difficult to analyze the program at a later date (by someone else or even by the original programmer himself). There are many reasons for this; one is the use of statements like GOTO in high-level languages. In any program some tasks will either be performed repetitively or the programmer might want the program to execute a different set of instructions if some condition is satisfied. For example in a division program, in case the denominator is 0 then the program should not divide the two numbers; instead it should display an error message. Only if the denominator is not 0 should the program divide the two numbers. This kind of programming is referred to as program flow control. In a larger program there could be various other possibilities and all this was dealt with by using the GOTO statement. If you’ve used BASIC you will be aware that for every instruction that you type, you have to specify a line number (example 10,20,30 and so on). GOTO will be followed by some line number (indicating the instruction that has to be executed next. Ex: 1020 GOTO 50 means that now you want the program flow to go to line number 50). The problem with this is that in a large program it will become very difficult for anyone to understand the program logic. Imagine reading through 100 lines and then finding one GOTO statement to the 200th line. Then on the 205th line there could be another GOTO statement directing you to the 150th line. This should make it clear as to what is meant by the term ‘unstructured’. This is a major problem with high level languages. Another fact is that high level languages are very far away from the actual hardware. Examples of high-level languages are BASIC, Fortran (Formula Translation), COBOL (Common Business Oriented Language) and LIST (List processing). Thus to take advantage of high level and low level languages, middle level languages like C and C++ were developed. They were easy to use and tried to retain the advantages of low level languages (i.e. being closer to the architecture).

Introduction

8

C and C++ Dennis Ritchie developed C and it was quite popular. An interesting feature in C is the use of functions. The programmer could write a function for checking whether a number is odd or even and store it in a separate file. In the program, whenever it is needed to check for even numbers, the programmer could simply call that function instead of rewriting the whole code. Usually a set of commonly used functions would be stored in a separate file and that file can be included in the current project by simply using the #include syntax. Thus the current program will be able to access all functions available in ‘filename’. Programs written in C were more structured compared to high level languages and another feature was the ability to create your own data types like structures. For instance if you want to create an address book program, you will need to store information like name and telephone number. The name is a string of characters while the telephone number is an integer number. Using structures you can combine both into one unit. Similarly there are many more advantages of using C. Though C seemed to be ideal, it was not effective when the programs became even more complex (or larger). One of the problems was the use of many functions (developed by various users) which led to a clash of variable names. Though C is much more efficient than BASIC, a new concept called Object Oriented Programming seemed better than C. OOP was the basis of C++ (which was initially called ‘C with classes’). C++ was developed by Bjarne Strastroup. In object oriented programming, the programmer can solve problems by breaking them down into real-life objects (it presented the programmer with an opportunity to mirror real life). What is an object? This topic is dealt with extensively in the chapter on ‘Objects and Classes’ but a brief introduction is provided here. Consider the category of cars. All cars have some common features (for example all cars have four wheels, an engine, some body colour, seats etc.). Are all cars the same? Of course not. A Fiat and a Ford aren’t the same but they are called as cars in general. In this example cars will form a class and Ford (or Fiat) will be an object.

Introduction

9 For those people who know C programming, it would be useful to know the differences between C and C++. Basically C++ includes everything present in C but the use of some C features is deprecated in C++. •

C does not have classes and objects (C does not support OOP)



Structures in C cannot have functions.



C does not have namespaces (namespaces are used to avoid name collisions).



The I/O functions are entirely different in C and C++ (ex: printf( ), scanf( ) etc. are part of the C language).



You cannot overload a function in C (i.e. you cannot have 2 functions with the same name in C).



Better dynamic memory management operators are available in C++.



C does not have reference variables (in C++ reference variables are used in functions).



In C constants are defined as macros (in C++ we can make use of ‘const’ to declare a constant).



Inline functions are not available in C.

Let’s recap the evolution of programming languages: initially programs were written in terms of 1s and 0s (machine language). The drawback was that the process was very tedious and highly error-prone. Assembly language was developed to write programs easily (short abbreviations were used instead of 1s and 0s). To make it even simpler for programmers, high level languages were developed (instructions were more similar to regular English). As the complexity of programs increased, these languages were found to be inadequate (because they were unstructured). C was developed but even that was not capable of dealing with complex or larger programs. This led to the development of C++. Note: Sometimes languages are divided into low level and high level only. In such a classification, C/C++ will come under high level languages.

Introduction

10 Why do you need to learn C++? There are many people who ask the question, "why should I learn C++? What use is it in my field?" It is a well-known fact that computers are used in all areas today. Programming is useful wherever computers are used because it provides you the flexibility of creating a program that suits your requirements. Even research work can be simulated on your computer if you have programming knowledge. Construction and programming may appear to be miles apart but even a civil engineer could use C++ programming. Consider the case of constructing a physical structure (like a pillar) in which the civil engineer has to decide on the diameter of the rods and the number of rods to be used. There are 2 variables in this case: 1. The number of rods needed (let’s denote it as ‘n’) and 2. The diameter of each rod (let’s call it as ‘d’) The civil engineer might have to make a decision like: "Is it cost-effective for me to have 10 rods of 5cm diameter or is it better to have 8 rods of 6cm diameter?" This is just one of the simple questions he may have in his mind. There are a few related questions: "What is the best combination of number of rods, their diameters and will that combination be able to handle the maximum stress?" Usually equations are developed for each of the factors involved. It would be much easier if the civil engineer could simply run a program and find out what is the best combination instead of manually trying out random values and arriving at a solution. This is where programming knowledge would benefit the engineer. Any person can write a good program if he has adequate knowledge about the domain (domain refers to the area for which the software is developed. In this case it is construction). Since the civil engineer has the best domain knowledge he would be able to write a program to suit his requirements if he knew programming. Programming is applicable to almost every field- banking (for maintaining all account details as well as the transactions), educational institutions (for maintaining a database of the students), supermarkets (used for billing), libraries (to locate and search for books), medicine, electrical engineering (programs have been developed for simulating circuits) etc.

Introduction

11

Binary Numbering System

The following 2 sections on binary numbering system and memory are optional but recommended. If you’ve taken a course in electronics you probably already know about this and can use the material provided here as a refresher. Having knowledge of the binary system and computer memory will help in understanding some features in programming.

The heart of the computer is the microprocessor, which is also referred to as the processor. The microprocessor is the main part of the computer’s CPU (central processing unit). A processor consists of millions of electronic switches; a switch can be either in ON or OFF state. Thus there are only two distinct states possible in these devices. In our realworld calculations we make use of the decimal system (which has 10 distinct states/numbers: 0 to 9). Counting up to ten is easy for humans but would be quite difficult for computers. It is easier to create a device capable of sensing 2 states than one capable of sensing 10 states (this also helps reduce on errors). Computers make use of the binary system (i.e. they store data in binary format and also perform calculations in binary format). The binary system (binary meaning two) has just two numbers: 1 and 0 (which correspond to the ON and OFF state respectively – this is analogous to a switch which can either be in ON state or in OFF state). When we have different systems (binary, decimal etc.), there ought to be a way of converting data from one system to the other. In the decimal system the number 247 stands for 7*100 + 4*101 + 2*102 (add it up and the result will be 247; i.e. in each place we can have one of the 10 digits and to find the actual place value we have to multiply the digit by the corresponding power of 10). For example: 247 = (2 x 102) + (4 x 101) + (7 x 100) = 200 + 40 + 7 1258 = (1 x 103) + (2 x 102) + (5 x 101) + (8 x 100) = 1000 + 200 + 50 + 8 Note: In C++ and most other computer languages, * is used as the multiplication operator. The same concept holds good for a binary number but since only two states are possible, they should be multiplied by powers of 2. Remember: A binary digit is called a bit.

Introduction

12 So, what is the value of 1101? Multiply each position by its corresponding power of 2 (but remember, you have to start from 20 and not from 21). The value for 1101 is 13 as illustrated in the figure below:

An alternate method to obtain the value is illustrated below (but the underlying concept is the same as above):

It is easy to obtain the values which are written above each bit (27=128, 26=64 and so on). Write these values on top and then write the binary number within the squares. To find the equivalent decimal value, add up the values above the square (if the number in the square is 1). If a number is denoted as 1101, then this stands for the lower (or last) four bits of the binary number (the upper bits are set to 0). Hence 1101 will come under the values 8, 4, 2 and 1. Now, wherever there is a 1, just add the value above it (8+4+1=13). Thus 13 is the decimal equivalent of 1101 (in binary format). To distinguish between decimal and binary we usually represent the system used (decimal or binary) by subscripting the base of the system (10 is the base for the decimal system while 2 is the base for the binary system). Hence (13)10 = (1101)2

Introduction

13 Hence (13)10 = (1101)2

Computers store information in the form of bits and 8 bits make a byte. But memory capacity is expressed as multiples of 210 bytes (which is equal to 1024 bytes). 1024 bytes is called a Kilobyte. You may wonder why it is 1024 and not 1000 bytes. The answer lies in the binary system. Keeping uniformity with the binary system, 210=1024 and not 1000 (the idea is to maintain conformity with the binary system). Beware: The bit in position 7 in fig 1.1 is actually the 8th bit of the number (the numbering of bit starts from 0 and not 1). The bit in the highest position is called as the most significant bit. In an 8 bit number, the 7th bit position is called the most significant bit (MSB) and the 0th bit is known as the least significant bit (or LSB). This is because the MSB in this case has a value of 128 (28) while the LSB has a value of just 1.

Introduction

14

Computer Memory We know that computers can operate only on bits (0s and 1s). Thus any data that has to be processed by the computer should be converted into 0s and 1s. Let us suppose that we want to create a text file containing a single word “Hello”. This file has to be stored physically in the computer’s memory so that we can read the file anytime in the future. For the time being forget about the file-storage part. Let’s just concentrate on how the word “hello” is stored in the computer’s memory. Computers can only store binary information; so how will the computer know which number corresponds to which alphabet? Obviously we cannot map a single bit to a character. So instead of bits we’ll consider a byte (8 bits). Now we can represent 256 characters. To perform map a character to a byte we’ll need to use some coding mechanism. For this purpose the ASCII (American Standard Code for Information Interchange) is used. In this coding system, every alphabet has an equivalent decimal value. When the computer uses ASCII, it cannot directly use the decimal value and it will convert this into an 8-bit binary number (in other words, into a byte) and store it in memory. The following table shows part of the ASCII code.

Character A B a b

Equivalent decimal value 65 66 97 98

Binary value 0100 0001 0100 0010 0110 0001 0110 0010

In this way each character is mapped to a numeric value. If we type the word hello, then it is converted into bytes (5 bytes- one for each character) based on the ASCII chart and is stored in memory. So ‘hello’ occupies 5 bytes or 40 bits in memory. Note: It is very important to know the binary system if you want to use the bitwise operators available in C++. The concept of memory is useful while learning pointers. A question arises, “where are the individual bits stored in memory?” Each individual bit is stored in an electronic device (the electronic device is technically called a flip-flop; which is something like a switch). A single flip-flop can store one bit. Consider the fig. below:

Introduction

15

As mentioned earlier we deal in terms of bytes rather than bits. The figure shows a 4-byte memory (which means it can hold 32 bits – each cell can store one bit). All information is stored in memory and the computer needs some method to access this data (i.e. there should be some way of distinguishing between the different memory locations). Memory addresses serve this purpose. Each bit can be individually accessed and has a unique memory address. To access the first byte, the computer will attempt to read the byte stored at memory address 1. If memories didn’t have addresses then the computer would not know from where it has to read data (or where it has to store data). An analogy to memory address is the postal address system used in real-life. A city will have a number of houses and each house has a unique address. Just imagine the situation if we didn’t have any postal address (we wouldn’t be able to locate any house in the city!). One difference is that a memory address can house only bits and nothing else. Memory address representations are not as simple as shown above. In the above case we’ve considered a memory that has capacity to store just 4 bytes. Memories usually contain kilobytes or gigabytes of space. As the amount of memory available increases, so does the size of the address (the address number might be in the range of millions). Thus instead of using the decimal system for addresses, the hexadecimal numbering system is used. Hexa means 16 and the distinct numbers in this system are 0 to 9 followed by A, B, C, D, E and F where A= 10 in decimal, B= 11 in decimal and F= 15 in decimal. Counting in hexadecimal system will be 0…9, A, B…F, 10,11,12,13…19,1A, 1B, 1C…and so on. It is quite clear that 10 in hexadecimal does not equal 10 in decimal. Instead, (0F)16 = (15)10, (10)16 = (16)10 and (11)16 = (17)10 Four bits form a ‘nibble’. Eight bits form a ‘byte’ and four bytes of memory are known as a ‘word’. There is some significance attached to a ‘word’ which we shall deal with later.

Introduction

16 Another term related to memory is ‘register’. A register consists of a set of flip-flops (flip-flops are electronic devices that can store one bit) for storing information. An 8-bit register can store 8 bits. Every processor has a set of internal registers (i.e. these registers are present within the processor and not in an external device like a hard disk). These registers (which are limited in number depending on the processor) are used by the processor for performing its calculations and computations. They usually contain the data which the processor is currently using. The size of the register (i.e. whether the registers will be 16-bit, 32-bit or 64-bit registers also depends on the processor). The common computers today use 32-bit registers (or 4-byte registers). The size of a register determines the ‘word-size’ of a computer. A computer is more comfortable (and prefers) working with data that is of the word-size (if the word-size is 4 bytes then the computer will be efficient in computations involving blocks of 4 byte data). You’ll understand this when we get into data types in the subsequent chapters. The different types of memory are: 1. Secondary storage (for example the hard disk, floppy disks, magnetic disks, CD-ROM) where one can store information for long periods of time (i.e. data is retained in memory irrespective of whether the system is running or not). 2. The RAM (random access memory) is used by the computer to store data needed by programs that are currently running. RAM is used for the main (primary) memory of the computer (all programs which are executed need to be present in the main memory). The RAM will lose whatever is stored in memory once the computer is switched off. 3.

ROM (read only memory): This contains instructions for booting up the system and performing other start-up operations. We cannot write to this memory.

4.

The internal registers within the processor- these are used by the computer for performing its internal operations. The compiler will decide what has to be stored in which register when it converts our high-level code into low-level language. As such we won’t be able to use these registers in our C++ code (unless we write assembly code).

Remember: Secondary storage (also called auxiliary memory) is not directly accessible by the CPU. But RAM is directly accessible (thus secondary memory is much slower than the primary memory). For a program to execute it needs to be present in the main memory. Which memory has lowest access time (or which memory can the CPU access quickly)? The internal registers can be accessed quickly and the secondary storage devices take much longer to access. Computers also make use of a cache-memory. Cache memory is a highspeed memory which stores recently accessed data (cache access is faster than main memory access).

Introduction

17

Related concept: In general cache means storing frequently accessed data temporarily in a place where it can be accessed quickly. Web browsers tend to cache web pages you visit frequently on the hard disk. Generally when we type in a website address, the browser needs to query the website and request for the page; which is a time consuming process. When the browser displays this webpage, it internally caches (stores a copy) this webpage on our hard disk also. The next time we time the same website address, the browser will directly read out from the hard disk rather than query the website (reading from hard disk is faster than accessing a web server). Remember: Computers store information using the binary system, addresses are represented in the hexadecimal system and in real-life we use the decimal system. A 3-bit number can be used to form 8 different combinations (including the 000 combination).

Introduction

Binary

Decimal Equivalent

000

0

001

1

010

2

011

3

100

4

101

5

110

6

111

7

18 If 3 bits are used then the maximum possible decimal number that can be represented is 7 (not 8 because the first number is 0). Similarly if an 8-bit number can be used to represent up to (2^8) different values (0 to 255 in the decimal system). A binary number can be either signed or unsigned. When a number is unsigned (we don’t bother about the sign), it means that the number is always positive. If a number is signed, then it could be positive or negative. +33 is a signed number (with a positive sign). In real life, we can use + or – to indicate whether a number is positive or negative but in computers only 1s and 0s can be used. Every decimal number has a binary equivalent. The binary equivalent for the decimal number 8 is 00001000. If this is a signed number then +8 would be written as 00001000 and –8 would be denoted by 10001000. Notice the difference between the two. The 7th bit (or the most significant bit) is set to 1 to indicate that the number is negative. Assume the number 127. For +127 you will write: 01111111 For –127 you will write: 11111111 For 255 you will write: ? Well, the value for 255 cannot be written using a signed 8-bit number (because the 7th bit is reserved for the sign). If the unsigned representation was used then 255 can be represented as 11111111 (in this case the MSB signifies a value and is not concerned about the sign of the number). What is the point to note here? By using a signed representation the maximum value that can be represented is reduced. An 8 bit unsigned binary number can be used to represent values from 0 to 255 (11111111 will mean 255). On the other hand, if the 8 bit binary number is a signed number then it can represent from –127 to +127 only (again a total of 255 values but the maximum value that can be represented is only 127). Beware: Signed representation in binary format will be explained in detail later. Computers store negative numbers in 2s complement rather than storing them directly as shown above. Remember: In signed numbers the MSB (in the binary representation) is used to indicate the sign of the number.

Introduction

19

Terms used in C++ •

Compiler : Before providing a technical explanation a compiler in layman’s language is a

program which will read through the C++ program code (code refers to the program written by the programmer) line by line. The compiler will read each and every character that has been typed in the program code. After reading the entire code, the compiler will convert the code into machine level format (i.e. 0s and 1s). The compiler is very dedicated and hard working since it will meticulously read each and every character. Technically speaking, the microprocessor in a computer can understand only binary numbers (only 0s and 1s). It is not possible for us to write programs in binary numbers. Compilers will convert the coding for a program into machine understandable form (i.e. it will convert the instructions we type into machine understandable form). The instructions we type (in something similar to English), is known as the source code. The compiler converts the source code into the object code (which the microprocessor understands and we cannot understand). There is a detailed discussion on this topic in the chapter “Advanced Topics”. 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 compilers available for free on the Net and they can be downloaded from the Net. After downloading the compiler, create a new C++ source file and type the C++ 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 : 1. 2. 3. 4.

Borland C++ compiler : www.borland.com/bcppbuilder/freecompiler/ Bloodshed Dev C++ compiler : www.bloodshed.net/devcpp.html DJGPP C++ compiler For Linux/Unix the GNU C++ compiler is available in standard installations of the operating system.

Introduction

20 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 is Turbo C++. Most of the programs written in this book are compatible with Turbo C++. It is better to use Visual C++ because advanced features like namespaces and exception handling are not available in older versions of Turbo C++. All compilers will support the basic features of C++. If you are using Linux/Unix then you will be having the GNU C++ compiler in your Linux distribution CD (this compiler is freeware but it doesn’t have a graphical user interface- it can only be used from the command line; refer to the Appendix section).



Keywords :

Keyword is a word that the compiler already knows, i.e. when the compiler sees a keyword somewhere in the program it knows what to do automatically. For example, when the compiler encounters the keyword ‘int’, it knows that ‘int’ stands for an integer. Or if the compiler reads a ‘break’, then it knows that it should break out of the current loop.



Variable and Constant :

As the name suggests, a variable is something whose value can be changed throughout the program. It is not fixed. On the other hand, a constant is one whose value remains the same (constant) throughout the program.



Operators and Operands:

Operator is something that performs operation on a variable. For example + is an operator that performs the addition operation. The terms on which the operator operates are known as the operands. In the expression x+y x and y are known as the operands and + is the operator.

Introduction

21 •

Expression:

Expression performs an operation and consists of operators and operands (or variables). a>b This is an expression in which a and b are the operands. > is the operator.



Binary and Unary:

Binary means ‘two’ and ‘unary’ means one. A binary operator operates on two operands (or two values) whereas a unary operator operates on just one operand.



Definition and Declaration :

In C++ these two terms are used frequently. Declaration of a variable means telling the compiler that this variable is an integer, or this variable is a character. A declaration will inform the compiler about the type of a variable. Defining a variable means allocation of memory space by the compiler for that particular variable. In the case of variables, a single statement performs both declaration and definition. For example: the statement int x; declares and defines ‘x’ as an integer. Remember that declaration is just declaring the type of something (telling the compiler what data type it belongs to) but definition means allotting space to it. It may seem as if both are one and the same; in many cases they are but in a few places they are not (these cases will be dealt with later).



Initialization :

Initialization refers to the first (initial) assignment of a value to a variable. x = 50; This initializes the value of x to 50. Always remember that the value to the right of the equal sign will be assigned (or stored) to the value on the left of the equal sign. For example: a = b; This means that value of b is assigned to a.

Introduction

22 •

Parentheses, braces, angle and square brackets :

There are different type of brackets: • • • •

Parentheses are just the normal brackets ( ). Braces are { }. Angular brackets are < >. Square brackets are [ ].

All of these brackets are used in C++ but each one is used for different purposes.



Character:

Characters include alphabets, numbers and other special symbols as well (like *, /, - etc…). Usually one tends to think of characters as only alphabets; but remember a character could be anything (an alphabet, a number, a special symbol on your keyboard or even a blank white space is a character).



Syntax : Every language has its own ‘syntax’. By the term syntax we refer to the

structure of a language and use of correct language. English has its own set of rules while French has slightly different rules. The vocabularies of both the languages are also different. Programming is also another language (that’s it is called programming 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.



Errors: This is a very common term in programming languages. In fact it is very

common to hear someone say, "Syntax Error". Error means a mistake and syntax error means that there is a mistake in the syntax. The compiler reads the entire program before execution. While reading the program, if the compiler discovers that it can’t understand

Introduction

23 some instruction it will display an error message. Till the error is cleared, the program cannot be executed. The compiler will give a syntax error if the syntax has not been adhered to properly. Errors generated by the compiler are known as compile-time errors. There are other types of errors as well. The compiler is not intelligent to detect all types of errors. For instance if the programmer make a logical error, the compiler will never know it. Suppose a program is written for adding two numbers and in the coding, instead of the + operator, the – operator has been used. The compiler will not recognize any error during compilation and the program will execute (but the programmer will not get the desired output). This is a logical error and as a programmer you have to be very careful while coding. The compiler is not brilliant enough to know what is in the programmer’s mind!



Executable file and linkers:

Executing refers to the process of running a program. We’ve talked about object file, but can we execute object files directly? The compiler produces an object code but this object code generally depends on some other external files. For example: if you are using complex mathematical functions, you might make use of functions which have been defined by some other programmer. That programmer would have created a library which you can include in your source code (instead of creating those functions in your program). A library is a collection of object files. Thus your program will actually depend on the other object files for some of the complex mathematical functions which you’ve used. Someone now has to link up all these object files to produce an executable file. This work is done by the ‘linker’. Most modern compilers have a linker included in them. Linkers are discussed in detail later. Remember: Each C++ source code needs to be compiled separately to produce an object code (ten source codes mean that we’ll have ten object codes). Finally the linker has to combine these ten object codes to produce one single executable module. •

Debugging:

is the process of troubleshooting a program (of course it’s done when the program doesn’t work up to your expectation!). Debugging is the process of identifying bugs in the code. A bug is a programming term for an ‘error’.

Introduction

24

Some extra details: An interesting point (about Kilo, Mega etc.): You might have read that 1Kilobyte = 1024 bytes and perhaps you thought about it as well, “Shouldn’t 1Kilobyte = 1000 bytes?” Let’s start with our familiar decimal system. In the decimal system, 1 Kilo unit = 1000 units. For example: • • •

1 Kilometer = 1000 meters = 103 meters 1 Kilogram = 1000 grams = 103 meters 1 Megawatt = 1000 Kilowatts = 1000000 watts = 106 watts

This is fine as far as the decimal system is concerned. Here the base used is 10 and we express everything in powers of 10. Computers speak in binary language and the base used here is 2. Let’s extend the concept of ‘kilo’ to the binary system. Can we represent the value 1000 as a power of 2? Let’s try: Power 20 21 22 23 24 25 26 27 28 29 210

Value 0 2 4 8 16 32 64 128 256 512 1024

Oops! We’ve just exceeded 1000 in 210. In the binary system, the closest we come to 1000 is 1024. Thus 1 Kilobyte = 1024 bytes (but for rough calculations we assume it as 1000 bytes).

Introduction

25

Recap of the First Unit: ¤

Computers only understand binary language (1s and 0s).

¤

8 bits form a byte.

¤

The different types of storage are: internal CPU registers, RAM, ROM and secondary storage.

¤

RAM is used as main memory and data is present only as long as the computer is switched on.

¤

Main memory is directly accessible by the computer whereas secondary memory is not.

¤

During execution programs should be present in the main memory.

¤

Word-size of a machine depends on the size of the processor’s internal registers.

¤

Machine level language is written in 1s and 0s.

¤

Assembly level languages make use of mnemonics (mnemonics are abbreviated English words used to represent instructions).

¤

High level languages like BASIC are in simple English but they are far away from the real hardware of the system.

¤

C has the advantages of both high level and low level languages. But it doesn’t support Object Oriented Programming (OOP).

¤ ¤

¤

C++ is actually C with classes (i.e. it supports OOP). A compiler is required to convert the source code (i.e. the instructions we type in English) into the object code (i.e. the machine language which the computer understands). 1 kilobyte = 1024 bytes (and not 1000 bytes).

Introduction

Your very first C++ Program

26

All the example programs in this book have been tested in Turbo C++ compiler (the programs were also tested in Visual C++ 6.0). They should run on other C++ compilers as well. Let us start with a basic C++ program. This will give you an idea about the general structure of a C++ program. Let’s write a program in which the user can enter a character and the program will display the character that was typed: // My first C++ program # include int main( ) { char letter; cout >letter; cout >variable; This will cause an error while compiling. ‘cout’ and ‘cin’ are already pre-defined and so you can use them directly in your programs. 'iostream.h’ is a header file that is used to perform basic input and output operation (or general I/O like using ‘cin’ and ‘cout’). Remember: C++ is a case-sensitive language, which means that the compiler will not consider ‘letter’, ‘LETTER’ and ‘Letter’ as the same. Data Types & Variables

How to run your first program in the29 compiler? Saving and compiling the program: There are many C++ compilers available in the market. Some of them are freeware (meaning that they are free to use) while others have to be paid for. Turbo C++ compiler might be the simplest to use (but it is not freeware). Simply choose "New File" and type out the program coding. Suppose you are using some other compiler, click on File and choose "New". Some compilers may have the option of creating a C++ source file while other compilers may require a new project to be created. Whatever the method you will ultimately come to the step of creating a C++ source file. After typing the code in the compiler, save the file by giving it some name. The "Save As" option will appear under the "File" menu. Give a name (for example: first). Now the file is saved as first.cpp. All C++ source files are saved in *.cpp format. Just like *.doc represents a Word document file, *.cpp denotes a C++ (C Plus Plus) source file. In the compiler program there will be an option called ‘Compile’ in the menu bar. Select the compile option and the compiler will do its work. It will compile the program (or in other words, it will read whatever has been typed) and in case there are any errors, the compiler will point out the line where the error was detected. Check whether the program has been typed exactly as given earlier. Even if a semi-colon is missing, it will lead to errors. If the compiler says no errors (or if the message "compiled successfully" appears), then you can go to the next stage: building the *.exe file. *.exe file extension stands for executable files. A *.cpp file cannot be run directly on the computer. This has to be converted into a *.exe file and to do so select the "Make or Build exe" option in the compiler. The file ‘first.exe’ will be created. Now the program can be executed from the DOS prompt by typing ‘first’. But instead of running the program every time from DOS, it will be convenient to run the program from the compiler itself and check the output. In the compiler there will be another option called "Run". Just click on this and the program will run from the compiler itself (you needn’t switch back and forth between DOS and the compiler screen). The figure should give you a rough idea as to how the executable file is created from the C++ source code.

Data Types & Variables

30

The source code is the program that you type. Source code is converted into object code by the compiler. The linker will link your object code with other object codes. This process of creating a C++ program is discussed in the last chapter.

Modification that might be needed in first.cpp (depending on your compiler): A little problem might be encountered while running your program from the compiler (this problem will exist in Turbo C++ compiler). While running the program from the DOS prompt, this problem will not occur. What’s the problem? When first.cpp is executed from the compiler the program will ask the user to enter a character. Once a character has been entered, the program will return to the compiler screen. You won't see your output! It might appear as if there is some problem with the program. What happens is that the program displays the character on the screen, immediately terminates the program and returns to the compiler screen. This happens so fast that you can’t see the output being displayed. Data Types & Variables

Modify your program as shown below (if you are using Turbo C++):

31

// Your first program modified: first.cpp # include # include int main( ) { char letter; cout>letter; cout
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF