![]() |
|
#1
|
||||
|
||||
A Comprehensive Digest of C++Name/Brief Description:
A Comprehensive Digest of C++ Date of Original Submission: June 21st 2004 Submitted by: Mithun Jacob a.k.a Phantom XXIII I'm sure all the other tutorials give an in-depth idea of what programming, OOP, data encapsulation etc. is. Well, I'm not going to do that..I'll give you a basic outline of the theory of programming in C++. Contents: Introduction I.Programming II.A Basic Outline on how C++ Works a)Basic View of Memory b)Basics of how a C++ Compiler works III.Basic Definitons a)Header Files b)Operators c)Variables etc. i)Assigning data to Variables d)Functions IV.Using COUT and CIN VI.Logical Statements a)Types of Statements b)Logical Operators i)Relational Operators ii)Equality Operators iii)Logical Operators VI.Loops a)while Loop b)do while Loop c)for Loop VII.Things you Ought to Know I a)Shorthand Operators b)Comments VIII.The if else Statements Example Programme 1 (EP1) IX.Arrays Example Programme 2 (EP2) Example Programme 3 (EP3) X.Multi-dimensional Arrays XI.Structures a)What is an object? b)Defining an object i)Accessing the elements XII.Functions a)Definition b)Calling a function c)Some Things You Ought to Know About Functions i)Inline Functions ii)Function Prototypes iii)Global & Local Variables Example Programme 4 (EP4) XIII.Things you Ought to Know II a)Switch b)Useful Functions i)getch() ii)clrscr() iii)sizeof(data type) iv)kbhit() v)exit(int arg) vi)delay(int), sleep(int c)TIME_T d)Infinite Loops e)Use of int main() f)Some Important ASCII values g)The Conditional Operator XIV.Classes a)Some Concepts b)Defining a class c)Hail the Constructor!Down with the Destructor! d)Visibility e)The Truth Behind cout & cin XV.Overload a)Overloading Functions b)Overloading Operators XVI.File Handling a)Declaring the object, Opening & Closing a file b)Writing & Reading Example Programme 6 (EP6) XVII.Pointers a)Variables b)Arrays c)Applications in Functions i)Reference Arguments ii)Returning by Reference d)Dynamic Allocation XVIII.DOS (BGI) Graphics a)Syntaxes i)Initialization ii)Line iii)Circle iv)Ellipse b)Co-ordinate System Example Programme 7 (EP7) XIX.Thing you Ought to Know III a)Manipulators i)setw ii)setiosflags iii)endl b)Type Casting c)goto d)enum e)Default Variables f)Storage Classes g)Constants i)const ii)#define h)Bitwise Operators Example Programme 8 (EP XX.An Introduction to Perl a)Scalar b)File Handling c)Default Variables Example Programme 9 (EP9) Example Programme 10 (EP10) Conclusion Appendix A: ASCII Chart Appendix B: Operators Appendix C: Data Types Appendix D: Truth Tables Appendix E: IOS Formatting Flags Appendix F: Colour Chart __________________
[b]There are times when the Phantom walks the streets as an ordinary man...this is one of those times. Last edited by mithunjacob : 20-Jun-2004 at 18:18.
Reason: Formatting & Typos Correction
|
||||
|
#2
|
||||
|
||||
I.ProgrammingI.Programming
A PC's memory bank can be percieved as a collection of files or as a group of memory blocks. While programming I tend to think of it as a universe where things can only be created and moulded my observing certain commands and protocols. Most people think programming consists of keying lines of code;that's incorrect. Whilst keying those lines you must keep in mind, you are building something, something as concrete and real as a bed, or a cupboard but infinitely more complex. Quoting a reply a Multivac programmer gives his son, when asked about how smart Multivac was (in an Asimov story), "If you knew the way we have to give it instructions, son, you wouldn't ask". When programming you must keep in mind the limitations of your tools and must learn that the only way to make a programme easily is to keep every fundamental feature in mind and code on that. Every feature. |
|
#3
|
||||
|
||||
II.A Basic Outline on how C++ WorksII.a)Basic View of Memory
As I've stated before, the PC is a memory bank of blocks, let's call them boxes. Each box can hold a byte of information. Now this is what all programmers have at their disposal, a vast array of boxes into which we can fill up 8 bits of information. Bits, bytes, nibbles...someone must have been pretty famished to coin these terms.... Remember all those 1s and 0s in The Matrix, well, a 1 or 0 is a bit or Binary digIT. The binary system is simply another mode of communication, which is more efficient than the decimal system because it's easily implemented. I won't go into details, but just remember that I can store 8 bits of information in one byte. eg.11100011 or 10101010 or ... So, how can I use this? I'll give you a simple example, characters. A character is a symbol. It can be a letter, a number, a punctuation mark, any symbol you see on the keyboard. How does it exist in a PC? Simple, each character is defined by an 8 bit code. There are totally 256 of those characters, and they constitute the ASCII characters. eg.The letter 'A' is a character with ASCII value 65, which means that a memory block or a byte with the following bits: 1000001, is read as 'A'. [Appendix A contains a list of ASCII characters and their equivalent values in hex, decimal, and octal] Alright, so a programmer has a bunch of boxes to make programmes with, how is that possible? Assembly is a fairly old language and isn't very popular as it's pretty hard to understand. But an assembly programmer has an excellent idea of what's really going on. A programme is given a memory block(a bunch of bytes) to work with. Assembly uses a "scratchpad" of memory to temporarily store the bytes. This "pad" is called a stack and can be perceived as a stack of boxes piled on top of the other. 2 basic commands are PUSH and POP. PUSH pushes a memory block on to the stack, and POP pops it off the stack. So suppose I want to do some simple math (let's assume that the arithmetic functions are already defined): Output of 1+2-3 Operation Stack PUSH 1 1 2 PUSH 2 1 + 2 PUSH + 1 PUSH 1+2 3 - PUSH - 3 3 - PUSH 3 3 PUSH 3-3 0 Alright, hope you've understood that...read an assembly tutorial if you want more information. The above will be useful when it comes to dynamic memory allocation, by the way, the PUSH/POP thing was just an example. II.b)Basics of how a C++ Compiler works Suppose I wanted to print a character on the screen. Well, the correct way to do so is to send the character to a certain memory block called a register (the AX register) and then execute it. Instead of going through all that, the C++ compiler uses HEADER files which allows us to do the same job without any direct contact with memory, registers, etc. So, basically a compiler takes the code and converts it into assembly and then creates the programme. Alright, I hope you've understood what's actually going on behind a compiler as we can start off with the obligatory "Hello world" programme. __________________
[b]There are times when the Phantom walks the streets as an ordinary man...this is one of those times. |
|
#4
|
||||
|
||||
III.Basic DefinitionsIII.a)Header Files
The header (or include) file used for the input/output (I/O) operations is IOSTREAM.H (IO STREAM {a stream is a collection of bytes} ). A header file contains the definitions of the tools we are going to use. We include a header file in the programme by using #include <filename> Example: #include <iostream.h> Using < > tells the compiler that the location of IOSTREAM.H is in the pre-defined directory for header files which is specified somewhere else in the Compiler's OPTIONS. Suppose you wanted to enter the exact path of the header file. #include "c:\tc\include\iostream.h" You use " ". III.b)Operators An operator is used with operands. They define the type of function which must be applied to the operands. For example consider the values 4 and 5. They are operands, but the operator decides whether they are to be added, divided, multiplied or subtracted (resp.+,/,*,-) The above are mathematical operators and operands. The same concept can be extended to anything. For example, suppose a room contained a table. Then we could define the '.' as an operator which is used to access the contents of the operand. Eg.Room.table, would give us access to the table. Table.drawer, to the drawer Another aspect of operators is precedence. Suppose I had to evaluate the following mathematical expression: 1+2+4*5-6/3 If we didn't have the BODMAS precedence rule, we'd certainly be stuck as we wouldn't know how to evaluate the expression. Eg. (1+2+4)*5-(6/3) = 33 Or. (1+2)+(4*5)-(6/3) = 21 Or. 1+(2+4)*(5-6)/3 = ?? So all possible combinations are dispelled using BODMAS (Brackets,Division,Multiplication,Addition,Subtrac tion) Therefore, the 2nd combination is correct. [Appendix B contains a list of operators arranged in order of their precedence] Last edited by mithunjacob : 20-Jun-2004 at 18:24.
Reason: Typo
|
|
#5
|
||||
|
||||
III.Basic DefinitionsIII.c)Variables etc.
Variable are memory blocks assigned to the programme at the beginning of the programme. They are declared by the programmer and remain fixed or static throughout runtime. Declaring a variable tells the programme to assign a specific amount of memory to it from the beginning of the programme. Some data types which are already created in C++ are: int: Integer, can only store integral values, i.e. no decimals float: Decimal values, as well as integral values char: Character, can store one character III.c.i)Assigning data to Variables The general format to declare a variable (DATA TYPE) (NAME OF VARIABLE); Ah yes, before I forget..the infamous semicolon. The semicolon or ';' is used at the end of every line which actually does some work. For instance, deciding whether 5 is greater than 3 is not WORK, but printing a character on to the screen IS WORK. Similary, declaring a variable is also WORK as we're allocating a certain amount of memory space for that variable. The semicolon is one of the most amusing operators in C++. When I first learnt the language I remember adding a semicolon to the end of my sentences instead of '.'.... Let the names of all the variables in the following examples be a,b,c...They can be ANYTHING but can only start with a character which is not an operator or a number. eg. 1,*,/,...are not allowed. eg. a is allowed eg. a34b is allowed eg. a*yr is not allowed The name of a variable can only be used ONCE, and it cannot be that of a structure, function, class, etc. Let's use an integer first. int a; There, you have declared an integer which henceforth will be identified as 'a'. Suppose I wanted to give it a value, 5..to do so, I would use the ASSIGNMENT operator or '='. eg. a=5; Or you could declare it and assign a value simultaneously... int a=5; Please note that once a variable name or IDENTIFIER is used, it CANNOT be used for another variable. Eg. int a=5; int a=6;--->INCORRECT With float: float a=0.5; Using the COMMA operator many variables can be simultaneously declared. eg.int a,b,c; eg.int a=5,b=6,c=7; Now, suppose I wanted to equate 2 int variables: int a=5,b; b=a; The ASSIGNMENT (=) operator works by assigning the value of the variable to the RIGHT of '=' to the variable on the LEFT. So, a=b, is incorrect as b does not have a value. Now, for char. Suppose I tried to declare a character with the character 'a' in the following manner: char c=a; That is incorrect. Why? As explained above, it will assign the value of a variable a to the character c. So, to overcome this, all characters are denoted by ' '. Therefore, char c='a'; Is the correct syntax. While we're dealing with characters, I might as well talk about strings. Strings are merely a "string" or group of characters. Eg."Phantom" is a string of characters 'P','h','a','n','t','o' and 'm' in a specific order. I'll show you how to declare strings later as they come under arrays, but remember a string is defined between " " and a character between ' '. III.d)Functions Functions are what programmes are made of. Mathematically, a function takes a variable and does some work on it. For example, consider a funtion f which adds a value 5 to the variable. So, f(3)=8 Similarly, in C++ a function is used to execute the programme. We create functions by ourselves and the most important function is the MAIN function. This is the function which is run by the compiler. More on functions later. So just remember we define the function main, and the compiler runs it. How do we define it? Simple: CPP / C++ / C Code:
The above programme merely declares a variable 'a' and assigns a value 5 to it. The code between the braces { } is the program. () shows that main is a function void will be explained later. {More on functions in Functions} __________________
[b]There are times when the Phantom walks the streets as an ordinary man...this is one of those times. |
|
#6
|
||||
|
||||
IV.Using COUT and CINThe cout and cin tools (actually objects, and that's how I'm going to refer to them from now onwards) are used to print text and input text from the standard output and input devices respectively. The most common ones are the monitor and keyboard.
Object: cout Header: IOSTREAM.H Operator: << Operand: Variable CPP / C++ / C Code:
There, your first "Hello World" programme. A useful feature of << is that you can cascade it. Suppose you wanted to display the contents of 3 variables. CPP / C++ / C Code:
Now, that's a waste of space....you could simply cascade the entire thing. CPP / C++ / C Code:
You can cascade ANY type of variables. From now on I'm not going to type in the entire code, if you want to execute it, just add the required lines. int a=5; cout<<a<<" mugs of beer and "<<a+10<<" cases of rum"; Now for cin: Object: cin Header: IOSTREAM.H Operator: >> Operand: Variable It works exactly like cout with the cascading feature etc, except that the operator is >>. Many novices find it hard to distinguish between these operators. Just imagine that CIN represents the input device or the keyboard. So, cin>>b means we're taking the data from the input device >> the variable b. --------- |Monitor| << VARIABLE; (cout<<b) --------- ---------- |Keyboard| >> VARIABLE; (cin>>b) ---------- Alright, that's all for cout and cin for now. |
|
#7
|
||||
|
||||
V.Logical StatementsV.a)Types of Statements
A logical statement usually consists of a condition such as "4 equals 5" which is obviously false and "3 less than or equal to 5" which is true. If you've noticed, the latter statement can be considered as 2 statements coupled together. Such as, "3 less than 5" OR "3 equal to 5" where if any one of the 2 statements are proved true results in the main statement "3 less than or equal to 5" being true. V.b)Logical Operators V.b.i)Relational Operators Using these operators on 2 values, will give it's truth value in Boolean. In Boolean, a true statement is represented by 1 and a false one by 0. I'm quoting Appendix B if you're too lazy to refer. 7. Relational ¦ < ¦ Less than ¦ <= ¦ Less than or equal to ¦ > ¦ Greater than ¦ >= ¦ Greater than or equal to Example: CPP / C++ / C Code:
V.b.ii)Equality Operators 8. Equality ¦ == ¦ Equal to ¦ != ¦ Not equal to Which is fairly obvious. Using a,b from above, CPP / C++ / C Code:
V.b.iii)Logical Operators 2. Unary ¦ ! ¦ Logical negation (NOT) 12. ¦ && ¦ Logical AND 13. ¦ || ¦ Logical OR Suppose I wanted to check if a value is between 0 and 9. It obviously includes two conditions, "greater or equal to 0" AND "lesser or equal to 9". So, to obtain this condition BOTH conditions must be true. CPP / C++ / C Code:
The OR operator works differently. It is used when only one of the conditions has to be right. Suppose I wanted to check whether a number was positive OR negative. CPP / C++ / C Code:
Even though a>0 was false, a<0 is true, so the net result is 1. [A useful way to study logical operations is to use a truth table. It simply displays all possible combinations of operands for the logical operations. Check Appendix D for truth tables] Another simple, but useful operator is !. It simply reverses the boolean value of the expression. CPP / C++ / C Code:
Obviously, 5<3 is 0 but 0 reversed or !0 is 1, so the output is 1. |
|
#8
|
||||
|
||||
VI.Loops :: While & Do-While LoopsA loop is simply used to repeatedly execute a certain piece code a set number of times with specific constraints. It is used for the simplification of programs, exhaustive selection among other things. Once I've the 3 types of loops, I'll give examples of these uses.
VI.a)while Loop The while loop simply keeps executing the code within its braces until a certain condition is satisfied. CPP / C++ / C Code:
VI.b)do while Loop Now suppose, I didn't initialize q with 'n'....that would result in bad code as q has a garbage value and we're checking whether it's 'y'. So, to avoid that, we have this loop. CPP / C++ / C Code:
|
|
#9
|
||||
|
||||
VI.Loop :: FOR LoopVI.c)for Loop
This loop is my favourite as it's so useful. CPP / C++ / C Code:
You might the syntax is a bit vague, but therein lies the beauty of the FOR loop, it can be used for so many DAMN THINGS!! Suppose you want to display all the numbers from 1-10000 for(int i=0;i<10000;++i) cout<<i; Yeah, if there's only one statement, there's no need for the braces. And the ++ .. later. As you can see there are so many applications to a for loop. Another incredible useful method is to use it exhaust all possible combinations. Here's a counter. for(int i=0;i<10;++i) for(int j=0;j<10;++i) cout<<i<<j<<"\t"; Now, what you see here is a NESTED loop. A nested loop basically contains 1 or more loops within itself. What occurs is that each loop executes its entire run before movig out into the next loop. So , for each i=0, we have j=0, then 1,2,3...9. Here, it stops and i=1. Now, we have i=1, and j=0, then 1,2,....9 and so on until i=9,j=9. Note: Using break will break the loop Example: for(int i=0;i<100;++i) if(i==50) break; Therefore when i=50, the loop will stop. As there should be balance, in the universe, we have the continue statement too; the break statement takes you out of the loop whereas the continue statement forces the programme flow to the beginning of the code inside the loop. |
|
#10
|
||||
|
||||
VII.Things you Ought to Know IVII.a)Shorthand Operators
These shorthand operators clean up your code makng it more compact, and readable CPP / C++ / C Code:
Another useful operator is %. This is the remainder operator. Now, the remainder of 9/2 is 1. Therefore 9%2=1. This operator can be used to determine whether a number is a factor of another number. As 3 is a factor of 9, there is no remainder, therefore, 9%3=0. The ++ and -- operators have 2 aspects to them: ->If the operator is before the variable (preincrement), then the incrementation takes place instantaneously. CPP / C++ / C Code:
->But if the operator (postincrement) is after the variable, the incrementation takes place after the operation is complete. CPP / C++ / C Code:
VII.b)Comments Making comments is an excellent way to understand a programme. You may not have a grasp on the fundamentals of your programme the next day, and there's nothing like a host of comments to jerk your memory. There are 2 ways of making comments: // : Using this the entire LINE will be considered as a comment and will be ignored by the compiler /* COMMENT */ : Here ALL the text between /* */ wil be considered as a comment even if it's on the next line |
Recent GIDBlog
Not selected for officer school by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The