Documente Academic
Documente Profesional
Documente Cultură
Module I
(10 hours)
Introduction to object oriented programming, user defined types, polymorphism, and encapsulation. Getting started with C++ -syntax, data-type, variables, strings, functions, exceptions and statements, namespaces and exceptions, operators. Flow control, functions, recursion. Arrays and pointers, structures. Module II (10 hours) Abstraction mechanisms: Classes, private, public, constructors, member functions, static members, references etc. Class hierarchy, derived classes. Inheritance: simple inheritance, polymorphism, object slicing, base initialization, virtual functions. Prototypes, linkages, operator overloading, ambiguity, friends, member operators, operator function, I/O operators etc. Memory management: new, delete, object copying, copy constructors, assignment operator, this Input/output. Exception handing: Exceptions and derived classes, function exception declarations, Unexpected exceptions, Exceptions when handling exceptions, resource capture and release etc.
Module III (10 hours) Templates and Standard Template library: template classes, declaration, template functions, namespaces, string, iterators, hashes, iostreams and other type. Design using C++ design and development, design and programming, role of classes.
CHAPTER 1
Description
Size*
Range*
Char
short int(short)
Short Integer.
65535 signed: 2147483648 to 4bytes 2147483647 unsigned: 0 to 4294967295 signed: 2147483648 to 4bytes 2147483647 unsigned: 0 to 4294967295
Int
Integer.
Long integer.
Bool
Boolean value. It can take one of 1byte true or false two values: true or false. Floating point number. 4bytes +/- 3.4e +/- 38 (~7 digits)
float
double
Double precision +/- 1.7e +/floating point 8bytes 308 (~15 number. digits) Long double +/- 1.7e +/precision floating 8bytes 308 (~15 point number. digits)
long double
Operators
Arithmetic operators ( +, -, *, /, % )
Increase and decrease (++, --) c++; c+=1; c=c+1; Relational and equality operators ( ==, !=, >, <, >=, <= ) In order to evaluate a comparison between two expressions we can use the relational and equality operators. The result of a relational operation is a Boolean value that can only be true or false, according to its Boolean result. We may want to compare two expressions, for example, to know if they are equal or if one is greater than the other is. Here is a list of the relational and equality operators that can be used in C++:
== Equal to != Not equal to > Greater than < Less than >= Greater than or equal to <= Less than or equal to
Logical operators ( !, &&, || ) && OPERATOR a b a && b true true true true false false false true false false false false The operator || corresponds with Boolean logical operation OR. This operation results true if either one of its two operands is true, thus being false only when both operands are false themselves. Here are the possible results ofa || b: || OPERATOR a b a || b true true true true false true false true true false false false
Comma operator ( , ) The comma operator (,) is used to separate two or more expressions that are included where only one expression is expected. When the set of expressions has to be evaluated for a value, only the rightmost expression is considered.
a = (b=3, b+2);Bitwise Operators ( &, |, ^, ~, <<, >> ) Bitwise operators modify variables considering the bit patterns that represent the values they store.
operator asm equivalent & | ^ ~ << >> AND OR XOR NOT SHL SHR
description Bitwise AND Bitwise Inclusive OR Bitwise Exclusive OR Unary complement (bit inversion) Shift Left Shift Right
Level 1 2 ::
Description
() [] . -> ++ -- dynamic_cast static_cast reinterpret_cast const_cast typeid ++ -- ~ ! sizeof new delete *& +-
Grouping Left-toright Left-toright Right-toleft Right-toleft Left-toright Left-toright Left-toright Left-toright Left-toright Left-toright Left-to-
unary (prefix) indirection and reference (pointers) unary sign operator type casting pointer-to-member multiplicative additive shift relational equality bitwise AND
4 5 6 7 8 9 10 11
(type) .* ->* */% +<< >> < > <= >= == != &
12 13 14 15 16 17 18
} return 0; }
1.4.1 Functions
Using functions we can structure our programs in a more modular way, accessing all the potential that structured programming can offer to us in C++. A function is a group of statements that is executed when it is called from some point of the program. The following is its format: type name ( parameter1, parameter2, ...) { statements } where:
type is the data type specifier of the data returned by the function. name is the identifier by which it will be possible to call the function. parameters (as many as needed): Each parameter consists of a data type specifier followed by an identifier, like any regular variable declaration (for example: int x) and which acts within the function as a regular local variable. They allow to pass arguments to the function when it is called. The different parameters are separated by commas. statements is the function's body. It is a block of statements surrounded by braces { }.
void duplicate (int& a, int& b, int& c) a*=2; b*=2; c*=2; } int main () { int x=1, y=3, z=7; duplicate (x, y, z); cout << "x=" << x << ", y=" << y << ", z=" << z; return 0; }
Default values in parameters. When declaring a function we can specify a default value for each of the last parameters. This value will be used if the corresponding argument is left blank when calling to the function. To do that, we simply have to use the assignment operator and a value for the arguments in the function declaration. // default values in functions #include <iostream> using namespace std; int divide (int a, int b=2) { int r; r=a/b; return (r); } int main () { cout << divide (12); cout << endl; cout << divide (20,4); return 0; } Overloaded functions. In C++ two different functions can have the same name if their parameter types or number are different. That means that you can give the same name to more than one function if they have either a different number of parameters or different types in their parameters. For example:
// overloaded function #include <iostream> using namespace std; int operate (int a, int b) { return (a*b); } float operate (float a, float b) { return (a/b); }
int main () { int x=5,y=2; float n=5.0,m=2.0; cout << operate (x,y); cout << "\n"; cout << operate (n,m); cout << "\n"; return 0; } inline functions. The inline specifier indicates the compiler that inline substitution is preferred to the usual function call mechanism for a specific function. This does not change the behavior of a function itself, but is used to suggest to the compiler that the code generated by the function body .
jimmy represents a bidimensional array of 3 per 5 elements of type int. The way to declare this array in C++ would be:
int jimmy [3][5]; Character Sequences As you may already know, the C++ Standard Library implements a powerful string class, which is very useful to handle and manipulate strings of characters. However, because strings are in fact sequences of characters, we can represent them also as plain arrays of char elements. For example, the following array: char jenny [20]; Pointers We have already seen how variables are seen as memory cells that can be accessed using their identifiers. This way we did not have to care about the physical location of our data within memory, we simply used its identifier whenever we wanted to refer to our variable. // my first pointer #include <iostream> using namespace std; int main () { int firstvalue, secondvalue; int * mypointer; mypointer = &firstvalue; *mypointer = 10; mypointer = &secondvalue; *mypointer = 20; cout << "firstvalue is " << firstvalue << endl; cout << "secondvalue is " << secondvalue << endl; return 0; } Data structures A data structure is a group of data elements grouped together under one name. These data elements, known asmembers, can have different types and different lengths. Data structures are declared in C++ using the following syntax: struct structure_name { member_type1 member_name1; member_type2 member_name2; member_type3 member_name3; .
. } object_names; struct product { int weight; float price; }; product apple; product banana, melon; And that would access the value pointed by a hypothetical pointer member called title of the structure objectpmovie (which in this case would not be a pointer). The following panel summarizes possible combinations of pointers and structure members: Expression What is evaluated Equivalent a.b Member b of object a a->b Member b of object pointed by a (*a).b *a.b Value pointed by member b of object a *(a.b)