Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
67 views

Quick Guide To Programming in C++: Written For Jonathan, So He Passes His Re-Test

This document provides a quick guide to programming in C++. It discusses the differences between C and C++, data types, variables, operators, comments, and other basic concepts of C++. The guide is written for Jonathan so he can pass his re-test on the topic.

Uploaded by

jmz333
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Quick Guide To Programming in C++: Written For Jonathan, So He Passes His Re-Test

This document provides a quick guide to programming in C++. It discusses the differences between C and C++, data types, variables, operators, comments, and other basic concepts of C++. The guide is written for Jonathan so he can pass his re-test on the topic.

Uploaded by

jmz333
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Written for Jonathan, so he passes his Re-test.

Jm 9/11/2011

Quick guide to programming in C++

Introduction:
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). C has the advantages of both high level and low level languages. But it doesnt support Object Oriented Programming (OOP). C++ is actually C with classes (i.e. it supports OOP).

Difference between C & 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.

Header Files
Header files commonly contain forward declarations of classes, subroutines, variables, and other identifiers. This is to keep the interface in the header separate from the implementation. The C standard library and C++ standard library traditionally declare their standard functions in header files Header files are included by the following syntax: #include<headerfile.h> However the new C++ ISO 2003 Standard does not need the .h extension when including header(library) files. Hence can be written as: #include<headerfile> Header files can also be included in double quotes. Example: #includeiostream

Identifiers
Identifiers=variables=names. (same meaning) The most fundamental of all concepts in C++ is the variable a variable is like a small box. You can store things in the box for later use, particularly numbers. Valid Characters An identifier must start with a letter and is comprised of a sequence of letters and digits. Somewhat surprisingly, in this context the underscore _ is considered to be a letter (although there are conditions associated with its use). There's no restriction on the length of an identifier. Keywords (very important) You can't use an identifier that is a C++ keyword in your programs. Keywords are: and, and_eq, asm, auto, bitand, bitor, bool, break, case, catch, char, class, compl, const, const_cast, continue, default, delete, do, double, dynamic_cast, else, enum, explicit, export, extern, false, float, for, friend, goto, if, inline, int, long, mutable, namespace, new, not, not_eq, operator, or, or_eq, private, protected, public, register, reinterpret_cast, return, short, signed, sizeof, static, static_cast, struct, switch, template, this, throw, true, try, typedef, typeid, typename, union, unsigned, using, virtual, void, volatile, wchar_t, while, xor, xor_eq. C++ is case sensitive, so upper case and lower case characters are distinct. This means that the names userInput and userinput are recognized as two different identifiers.

In short : 1. Variable names must begin with a letter 2. Variable names are "case-sensitive" (i.e., the variable "myNumber" is different from the variable "MYNUMBER" which is different from the variable "mYnUmBeR") 3. variable names can't have spaces 4. variable names can't have special characters (typographic symbols)

Data types
Each data type occupies a different memory size and if a variable is declared as belonging to one particular data type it cannot be assigned a different data type value. The fundamental (primitive) data types are: 1. 2. 3. 4. 5. Integer Floating Point Character Double Bool

Integer Type (int): An integer can contain only digits (numbers) from 0 to 9. Examples of integers are: 0, 10, 345, 6789, -23, -600. It includes positive and negative numbers but the numbers have to be whole numbers. It does accept the decimal point. Hence the following numbers are not integer data types: 3.5, 4.8, 0.23 However; if int a=3.5 then a=3 and not 3.5 (the decimal is truncated, [discarded]). Values can be assigned in the following 2 syntaxes : 1> [data type] [name]=[value]; 2> [data type] [name][(value)]; eg. 1> int a=3; 2> int a(3);

Floating Type (float): Floating type data include integers as well as numbers with a decimal point. It can also have an exponent. Exponent means 10 to the power of some integer value (whole number). 20000 = 2 x 10^4 = 2e4 = 2E4. If you specify decimal numbers, floating point data type will store up to a precision of 6 digits after the decimal point. Suppose 0.1234567 is assigned to a floating-point variable, the actual value stored would be 0.123457 (it will round up to the sixth digit after the decimal place). Valid floating-point numbers are: 0.1276, 1.23, 1.0, 10.2, 2e5 (this will be typed in your code as 2e5) Syntax for declaring a float type variable: Float a;

Double Type (double): This is similar to the floating-point data type but it has an even greater range extending up to 10308. The syntax to declare a variable of type double is: double variable-name;

Character Type (char);

A character uses just one byte of memory. It can store any character present on the keyboard (includes alphabets and numbers). (imp) It stores only ONE character/number/symbol, and can be assigned to an ascii code. Eg. char b=65; here a is assigned to ASCII symbol A If char a='11'; then value of a is ONLY 1. (imp) : Note the single quotes holding the number 11. The syntax to declare a variable of type char is:
char variable-name;

Comments
Comments are not meant for the compiler to read. They are meant to make the program easier to understand for humans who might read the coding later. A comment is made to describe the aim of the written code. It can even give details of the program (when and by whom was the program created, etc.)

There are two ways to write commends in c++ : Single line commenting (//) //this is a single line comment

Multiple line commenting (/*..*/)

/* Multiple line commenting this is also part of the comment comment continued */

Escape Sequences/ Backslash Character Constants

Single character \a \b \t \n \v \f \r \" \' \? \\ \0

Meaning alert backspace horizontal tab new line vertical tab form feed carriage return double quotes single quote Question mark Backslash Null char

ASCII code 7 8 9 10 11 12 13 34 39 63 92 0

Whatever you type within double quotes following cout<< will be printed as it is on the screen \0 is not equivalent to zero. \0 represents a null character (used in strings).
Using escape sequences: Cout << \n; Note (imp) : Escape sequences can only be used in cout statements.

Operators
1. Arithmetic Operators
+ * / % Addition Subtraction Multiplication Division Modulo operator (Remainder after division)

The modulo operator is not used to calculate the percentage. It is used to find the remainder after integer division. While using arithmetic operators in lengthy formulae, care must be taken to avoid BLOCK brackets [] and use ROUND brackets () enclosing the variables within. Using brackets simplifies the equation/formula and makes it more understandable to the reader. In the following example the use of brackets changes the answer: -(-without brackets-)Answer = 51/2*3; here the value of answer is 51/6=8.5 -(-with brackets-)Answer = (51/2)*3; here the value of answer is 25.5*3=76.5 Hence; the use of brackets is very essential.

[ to do ]

Assignment Operators
Initialization (declaration) of value to name (variable) Value of variable is assigned according to the value on the RHS. eg. a=5 eg. a,b,c; a=5, b=3; c=a; then c=a=5

logical unary conditional

You might also like