Levent Ozturk

C++ Summary by Levent Ozturk (Copyright)

C versus C++: C++ object oriented, message based, event driven. C++ is a superset of C. Most C programs are C++ (additional libs and new '//' comment type,operator overloading '>>'). C++ is Object-Oriented. In C++:

Memory Regions: A C/C++ program uses four logical regions of memory.

Access Modifiers: used to control the ways in which variables maybe accessed or modified.

Storage Class Modifiers:

Object: is a logical entity containing data and code. Encapsulation. Variable of user defined type. Links both code and data.

Polymorphism: Allows on name to be used for several related but different purposes. Sometimes characterized by "one interface, multiple methods". The compiler selects the the correct routine depending on the data type which is called. C++ supports polymorphism at both run-time and compile time. Function Overloading and Operator overloading are (early binding) compile-time Polymorphism. Derived classes and virtual functions are (late binding) run-time polymorphism.

Early binding (compile time polymorphism) advantage is faster and efficient code but less flexibility. Advantage of late binding (run-time polymorphism) is flexibility but slower and less efficient code)

Polymorphism helps building hierarchies gong from general to specialized. figure -> triangle, square.

Inheritance: is the process by which one object can acquire the properties of another object. Apple-Reddelicious apple. Allowing one class to incorporate another class into its declaration.

Function Overloading: Polymorphism is achieved by. More than one function can have the same name as long as they have different parameter declaration.

Operator Overloading: Operators are overloaded in .h files. Eg: >> or a class that defines a linked list might use the + operator to add an object to the list. Operator functions must be either members or friends of the class for which they are being used.

Class: General form an object. Similar to Structure (A new data type). Containes private(default) and public parts. Functions are called member functions.

C++ struct and class are actually the same except that by default members of a class are private while by default the members of a struct are public. C++ struct can have data, code ,construct and destruct funcs.

Union: C++ unions are the same as classes too. A union is essentially a structure in which all elements are stored in the same location. C++ unions have one additional feature called anonymous union.

This keyword: each time a member function is invoked, it is automatically passed a pointer to the object that invoked it. You can access this pointer using this. this->i= val.


Child (derived) Inherit Parent (Base)      
public public public   class c1 {
    int v1;int v2, v3;
     c1 (int i1);
    ~c1 (void);
    void f1(int i);
    friend f2 (int i);

void c1::f1(int i) {

class c2: public c1 {
    friend f2 (int i);
protected   protected      
----   private      
private private public      
private   protected      
----   private      

Scope Resolution: object.classtype::var

Constructor: is a special function that has the same name as the class and called when the object is created. Destructor when destroyed. Constructors are executed starting from base to derived, and destructor in the opposite direction. Both can be overloaded. Multiple base class constructors are called left to right, destructors right to left.




Virtual: run-time polymorphism.is a function that is declared as virtual in a base class and redefined in one or more derived classes. Selection of which derived virtual function to call at run-time is based on the type of the object pointed to. Although you could call a virtual func directly using the object name same as any other member function, run-time polymorphism is achieved only if a virtual function is accesed through a pointer to base class.

Redefinition of virtual func is a special form of func overloading but with some restrictions. prototypes of virt funcs must match. Also a virtual func must be a member not a friend, of the class for which it is friend. However a virtual fu nc can be a friend of another class. Also destructors can be virtue , but constructors cant. So the term overriding is used for virtue funcs rather than overloading. Once a func is declared virtual, it st ayes virtual no matter how many layers of derived classes it can pass through. When a derived class does not override a virtual func, the version of the func in the base class is used.

Pure Virtual function is a function declared in a base class that has no definition relative to the base. So all derived classes must define its own version. By declaring a virtual fu nc as pure, you force any derived class to define its own implementation type f(parlist) =0;

Virtual Base Classes: When two or more objects are derived from a common base class, you can prevent multiple copies of the base class from being presented in an object derived rom those objects by declaring the base class as virtual when it is inherited/ Virtual and base classes differ only when an object inherits the base more than once. When virtual base classes are used, only one base class present in the object. Otherwise multiple copies are found.

class base {...};; class d1: virtual public base {...}; class d2: virtual public base {...}; class d3: public d1, public d2 {...}; Only one copy of base in d3

Abstract Class: if a class has at least one pure virtual fu nc that class is said to be abstract. There can be no object of an abstract class. They can only be used as base class to be inherited can still use an abstract class to declare pointers.

Friend: Allows a nonmember function of a class to have access to the private parts of the class. The nonmember function is defined elsewhere and declared as friend in the pubic part so that this function can access the private part of this class. the classes that has the friend function (except the last class as the referenced classes are deifned already) must be forward referenced (class class_name;) as the friend function references those classes.

Remeber that friend functions are allowed in C++ mostly to handle some special-case operations.

Inline: is a function that is expanded at the point at which it is called instead of actually being called. Like parameterized function-like macro in C but more flexible. Use inline functions for efficiency. no function call procedures, No pushing and poping arguments into stack and returning from function. Faster speed, larger size. Prefer smaller functions for inline. Two ways to create inline. with the inline keyword and inside a class.

Static Class Members: when a member of a class is declared as static, no matter how many objects are created, there is only one copy of the static member. A static member is shared by all objects of the class. All static data is initialized to 0 and no other initialization is allowed. When a Static member function is created only one copy of it exists and used by all objects. Static member functions have access to all static data and other static functions declare in a class, but o access to non static data and functions. Static functions don't have a this pointer so there is no way of knowing which objects non static data or function to access. They don't really belong to an object but the class they are declared.They can be used for automatic access protection mechanism to critical resources.

class c{ static int count; public: static void f () {} };

New and Delete: equalent of malloc() and free() in C. Advantages: no need to calculate the size of memory to alloc(no need for size of). Also automatically returns the correct pointer type. Possible to initialize the object being allocated using new. and new and delete can be overloaded(for example you may want allocation routines that automatically begins using disk file as a virtual memory when the heap has been exhausted)..


Local Variables: In C++ you can declare local variables anywhere within block of code as oppesed in C you must declare all the local variables at the beginnig of a block. Helps encapsulation and localization of code

Dynamic Initialization: In C++ both local and global variables can be initialized at run-time. In C, a variables initial value must be known at compile time requiring functions can not be used to initialize variables. Like variables, objects can be initialized dynamically.

Optional function parameters: f (int a, int b=0) {...}; b does not have to be passed while calling f. Optional. Can be passed though.

Call by reference:in C and C++ int x; f(&x); void f(int *a) {...}; equals in C++ int x; f(x); void f(int &a);Cleaner. Compiler automatically generates the address of the argument used to call function and automatically de references the calling variable. Can't reference a reference vary. cant create arrays of references, cant create pointer to a reference, references are not allowed on bit fields.

Non parameter reference variables (independent or standalone references) ot a good idea but exists. int j,k; int &i = j; j=10; cout << j << "" < i; outputs 10 10 Something like duplication of the same memory area of a varible by another name. Must be initialised at declaration. Adress of a peviously declared varibale. i++ does not caus to point to antoher address instead increses k by 1. References are not pointers.

Reference to overload a Unary operator The way to use a friend whenoverloading a unary ++ or -- is to use a refernece parameter. In this way compiler knows in advance that it must generate an address when it calls the function. This avoids the ambiguity introduced by trying to create a friend operator++() function. P. 806.

Linkage Specification:by using it you can cause a function written in a different languageto be linked into your program. extern "C" void myfunc(void);Must be global cant be used inside a function.

void myfunc(void) {...}

Pointer-to-member operators .* and ->* : They allow you to access a member of a class given a pointer to that member. These are neded beacuse a pointer to a member does not fully determine an adress. Instead, it provides a n offsetinti an object of the member's class...

Conversion functions: You can use a type conversion functionto convert your class into a type compatible with that of the rest of the experession that the object is used. class three_d { ... public: operator int() {return ...;} three_d b(..); cout << b+100; object is used as an integer here and the value is an integer. double or long ...

Granting Access:If you want some of the public elements of the privately inhereted base class to be public, use access declaration. class c1: private c2 { public: c2::i; ...}; Can be used for the protected eleemnts of thebase class too. however status level can not be changed. private of base class can not be public in derived class, nor public base element cannot be private in derived class.Shortly public can stay pubic no matter how the base is inhereted.

Backfire questions:

what is the diffrence between void f(void) and void f()

write a function to calculate c**a

int* a, b; This decleration creates one, not two, integer pointers. In C++ neither & nor * is transitive over a list of variables. & and * arelinked to the individual variable that they precede, not to the type that they follow.

All the material listed and linked at this World Wide Web domain are strictly private property and copyrighted. © Copyright -∞-∞ Levent Ozturk. All rights reserved. Reproduction or use of any material, documents and related graphics and any other material from this World Wide Web server is strictly prohibited. Site Map