Pageviews

Tuesday, November 25, 2014

UML - Class Diagram - C++

1. Class representation
    • Visibility: public "+" / private "-"
    • Parameter direction: input "in" / in&out "inout" / output param "out"
    • Type of members: static method is underlined / pure virtual function italics
2. Class relationship
    • Association         ------------>
      • maybe aggregation
      • maybe composition
      • maybe dependency
    • Dependency        - - - - - - ->
      • class X { void foo(Y y){}; }
      • A dependency is very much implied by an association.
    • Aggregation        <>--------->
      • X weakly contains Y
      • class X { Y* y; }
    • Composition       <+>------->
      • X strongly contains Y
      • class X { Y y; }
    • Generalization    ------------|>
      • inheritance
    • Class template    
      • two class representation overlay
      • generic programming
    • Multiplicity in a relationship
      • "0..1" none or one
      • "1"     exactly one
      • "*"     zero or more
      • "1..*" at least one

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. }

Monday, November 17, 2014

Object Oriented Programming Concepts in C++

1, Coding structures
  • templates < inheritance < composition in terms of better usage frequency.
2, Helper class
  • as a static - does not keep state of the class instance.
  • as a singleton - keep a universal state of a single class instance.
  • as a base class - distinct states and fine-grain control of accessibility to its helper functions.  
3, Namespace
  • Preventing redefinition of user-defined keyboards.
  • A C++ framework should have at least one namespace to group all files. 
  • Compiler just add this constant string as prefix of variables.
4, Forward declarations
  • Only used as pointer types.
  • Behaving like void* but no need to cast.
5, Virtual inheritance 
  • To solve ambiguous hierarchy composition (known as the "diamond problem")
  • A particular base class in an inheritance hierarchy is declared to share its member data instances with any other inclusions of that same base in further derived classes.
6, Class ID Macro
  • AMF_DECLARE_IID("AMFCFilter")
  • Macro methods for getting current class name for debugging.
7, x86 calling conventions
  • Caller clean-up: cdecl syscall optlink
  • Callee clean-up: stdcall fastcall register // AMF_STD_CALL informs compiler how to call.
8, Pure virtual functions
  • It overwrites this method pointer in class instance memory to point to a warning methods. The program terminates if the pointer is not overwritten and output warning.
  • Basically pure virtual functions are required to be implemented in sub classes.
9, Virtual functions

  • Able to calling the corresponding virtual methods based on class instance memory instead of static pointer type.
  • If B is a subclass of A: A* pA = new B(); pA->foo(); => invokes B's foo().
10, Call base class' overwritten methods from subclass.
  • base_class::foo(...)
  • Unlike Java/C#: C++ does not have super or base keywords due to support of multiple inheritance.
11, String
  • wchar_t:  2 bytes per char unicode - support minimum Chinese, Korean, etc.
  • ANSI:     1 byte per char
12, Single/ Double/ Triple, etc. pointers
  • A *pA - dereference 1 time to get value.
  • A **pA - dereference 2 times to get value.
  • A ****pA - dereference 4 times to get value.
13, Synonyms
  • int i = 0; int &j = i; compiler will replace j with i during compilation.
  • Compare to pointer, reference could control size of an array. foo(int[4] &pA);
14, static_cast vs reinterpret_cast vs const_cast vs dynamic_cast
  • static_cast is a strict explicit casting. (Make sure casting fine at compile time.)
  • reinterpret_cast is a loose explicit C style casting. (You know what you doing or it fails.)
  • const_cast converts const variable to be non-const variable. (Should not use unless its a hack.)
  • dynamic_cast is the same as static_cast at compile time, but it also checks at runtime. (Never used)
15, Include guards
  • #ifndef #define #endif - C style
  • #pragma once - C++ style
16, Operator overloading
  • TYPE& operator=() 
  • Only the above function signature matches then the overloaded methods is called.
17, Constructors vs Copy Constructors vs ...