Pageviews

Tuesday, November 18, 2014

All Around Topics for C++ Users

This post is written based on a popular programming website.

1, Variables
  • local vs global vs Constants
2, Operators
  • Scope resolution ::, sizeof
  • Member access   ".", "->"
  • Bitwise                &, |, ^, ~, >>, <<
  • Conditional         ?, :
  • Operator Precedence
3, Control Structures
  • if else
  • return
  • goto
  • exit
  • break
  • continue
4, Arrays
  • pass array as parameters - int sum(int[]);
  • array of strings - char weekend[2] [9] = {"Sunday", "Saturday" };
  • array of objects - objects are user defined data type
5, Functions
  • pass by value
  • pass by reference
6, Class
  • Access specifiers - private, public, protected
  • Friend function - have access to private/protected members of a class, a bridge between classes.
  • Constructors
  • Default Constructors - no parameters or all of default values.
  • Overloading Constructors - increase flexibility of a class by having more constructors.
  • Destructors
  • Nested classes - Not given importance because of the inheritance.
  • Local classes - a class defined inside a function.
7, Predefined Functions
  • I/O Functions - scanf, vsprintf, fread, setbuf
  • String & Character Functions - strcpy, strlen, memset 
  • Mathematical Functions - cos, floor, sin, sqrt
  • Time, Data & Localization Functions - asctime, localtime, strftime
  • Dynamic Allocation Functions - calloc, free, malloc, realloc
  • Utility Functions - abort, bsearch, qsort, rand, exit
8, Object Oriented Programming (OOPS)
  • Object oriented analysis (OOA) - specify real world requirements, behaviour & interactions.
  • Object oriented design (OOD) - convert analysis into a hierarchy of objects.
  • Object oriented programming (OOP) - implement objects using C++.
9, OOPS
  • Objects - instance of a class that interact with each other at runtime.
  • Classes - data members + member functions
  • Inheritance - visibility of base and derived class members.
  • Dynamic Binding - aka "late binding", "runtime binding"
  • Polymorphism
  • Message passing - ?
  • Encapsulation
10, Inheritance Con't
  • Single inheritance
  • Multiple inheritance - a class is derived from more than one base class.
  • Hierarchical inheritance - two or more classes are inherited from a single class.
  • Multilevel inheritance - a derived class is derived from another derived class.
  • Hybrid inheritance - one or more types of inheritance are combined.
  • Virtual inheritance - solve "diamond problem".
11, Polymorphism Con't
  • Compile time polymorphism - aka "function overloading" that depends on the arguments. 
  • Run time polymorphism - late binding by using virtual function to override in derived class.
12, Encapsulation Con't
  • Data abstraction - using data without knowing how its stored.
  • Function abstraction - using function without knowing how its implemented.
13, Type Casting
  • static_cast - convert a base class pointers to the derived class, vice versa.
  • reinterpret_cast - convert a pointer of one type to another unrelated type.
  • const_cast - add or remove a "const" modifier from a type. 
  • dynamic_cast - used with pointers and objects to do casting at runtime. [RTTI]
  • typeid - determine the class (ex, typeid(pA).name()) of an object at runtime. [RTTI]
14, Forward declaration
  • can only use the pointer-to-that-class type. 
  • avoid circular references. 
  • not sufficient if you need to use the actual class type. (base class or use member functions)
*********************************************************************************
Special Topics

Pointers - declare as TYPE *NAME
  • reference operator - "&"
  • dereference operator - "*" or "->"
Structures
  • By default, all members of a struct are "public", members in function of a struct are "private".
Unions
  • At at instance it contains only a single object.
Enumerations
  • create new data types that can take on only a restricted range of values.
typedef
  • used to make the code more portable, easy to change data type.
comments
  • // 
  • /**/
Function overloading 
  • Define multiple function with the same name but perform different tasks based on the number, type of arguments of that function. @compile time.
Operator overloading
  • Define additional task to an operator in reference to a class.
  • Cannot overload: member access operator, scope resolution operator, conditional operator. 
Virtual functions
  • executes based on the type of object pointed by the base pointer at runtime and not on the type of pointer.
Exception handling
  • "try", "throw", "catch"
  • try{ if(1/r)else{throw(r)} } catch(int r) { ... }
Namespace
  • a new concept introduced by the ANSI C++ standards committee.
  • namespace namespace_name{ //declaration of var, func, classes, etc. }