Object Orient Programming (OOP)
Abstract Data Type (ADT): Data + OperationsImplementation - data structure + functions
C is Procedure-Oriented Programming
C++ is OOP
Class: data type, definition of abstract charicteristics
Object: variable, exemplar
Instance: actual object during runtime
Instantiation: making object (e.g. Student stud1)
header file: declaration
cpp file: details of declared things
Encapsulation: concealing details in class
1. Information hiding
2. Operation through interface
3. Separation of interface from implementation
Dynamic Memory Allocation
We can modify the size of data structure during runtimes
-->can save memory spaces
new and delete is used on C++
Memory Leaks: no deallocation --> inaccessible memories remains
Dangling Pointer: pointing site have no data
nullptr: no pointing site
String
#include <string>
C: array of chars with null terminator --> string.size() + 1
string.c_str(): return const char*
strcpy(char*, char or const char*) return char*
using xx::ftn;
using namespace xx;
string.c_str(): return const char*
strcpy(char*, char or const char*) return char*
Scope Resolution Operator (::)
::x <- global variableNamespace
namespace xx{ int a, int ftn( ), b, ... }using xx::ftn;
using namespace xx;
Casting
typename(var);
static_cast<typename>(var): gives compile error instead of runtime error.
example: static_cast<int*>(int)
dynamic_cast<typename>(var): returns null for wrong casting
For(initilization; condition; increment)
Function
Polymorphism: Allow uniform interface to handle different data types.
Overloading: To achieve polymorphism. Same function name for different arguments.
if only type name is different, it will be considered as ambiguous.
Resolving arg. type:
1. exact match --> 2. through promotion --> 3. type conversion
1. exact match --> 2. through promotion --> 3. type conversion
(implicit type conversion: char -> short -> int -> long -> float -> double)
Default arg.: Must be provided for trailing arg.
ftn() and ftn(default args.) are ambiguous
Class
Constructor: No return type, public, same name with class, can be overloaded. Used mainly to initialize instance(private)/class(public) variables.
cls obj2(obj1) <-- copy constructor (shallow copy -> same reference)
Destructor: No return type, public, ~same name with class. Invoked when object goes out of scope and delete applied.
Static: per-class variable. global to all objects. initialized outside
this pointer: to access my object itself
priority: arg. --> member var. --> global var.
Array of class init.: cls clsArr[] = {cls(a,b), cls(c,d), ...}
Array of class_ptr: cls *clsArr[3] = new cls; clsArr[0] = new cls(a, b); ...
Call Overriden ftn. : child.Parent::ftn()
Static Binding: compile-time binding
Parent *father = child; father ->ftn <- Parent function called
Dynamic Binding: run-time binding by virtual function
define virtual ftn() and override
Virtual Destructor: if we use Parent = new Child, we cannot delete children object by just call destructor of Parent.
virtual ~Parent() --> dynamically bound destructor --> ~Child also called
Pure virtual function: virtual ftn = 0 --> cannot be called.
(in case) catch (ParentClass &c) { c. ~~ }
Exception Specification: void ftn() throw(A, B) --> only A, B and its children exception
void ftn() throw(): no exception
void ftn(): any exception
Static: per-class variable. global to all objects. initialized outside
this pointer: to access my object itself
priority: arg. --> member var. --> global var.
Array of class init.: cls clsArr[] = {cls(a,b), cls(c,d), ...}
Array of class_ptr: cls *clsArr[3] = new cls; clsArr[0] = new cls(a, b); ...
Access Specifier
Private: Only for this. (inheritance: public, protected --> private)
Protected: Only for this and child. (inheritance: public, protected --> protected)
Public: Any one. (Inheritance: public--> all, protected --> protected)
Inheritance
Base(Parent) ---- Derived(Child)
Constructor: base --> derived
Can initialize parent's var. with Parent's constructor.
example: Parent(int a): var1(a); Child(int a, int b): Parent(a), var2(b);
example: Parent(int a): var1(a); Child(int a, int b): Parent(a), var2(b);
Destructor: derived --> base
Assignment: Parent to child is impossible
Overriding: definition the same function with Parent
No ambiguous between ftn() and ftn(def args.) since object is differentCall Overriden ftn. : child.Parent::ftn()
Static Binding: compile-time binding
Parent *father = child; father ->ftn <- Parent function called
Dynamic Binding: run-time binding by virtual function
define virtual ftn() and override
Virtual Destructor: if we use Parent = new Child, we cannot delete children object by just call destructor of Parent.
virtual ~Parent() --> dynamically bound destructor --> ~Child also called
Pure virtual function: virtual ftn = 0 --> cannot be called.
Template
template<typename T>
Exception
try{ throw Object }
catch (Class &c) { c. ~~ }(in case) catch (ParentClass &c) { c. ~~ }
Exception Specification: void ftn() throw(A, B) --> only A, B and its children exception
void ftn() throw(): no exception
void ftn(): any exception
Freind
class A{friend class B}; class B can access whole A
class A{friend type ftn}; ftn can access whole A
Const
int x --> int* y = &x --> int** z = &y
const int ** a --> a = const int** --> **a = const int
int* const a = &b --> a = int* const* --> *a = int* const, **a = int
int** const a --> a = int** const
Quiz1: What are the benefits of encapsulation?
Quiz2: Why do programmers use dynamic memory allocation?
Quiz3: How can we dynamically allocate array of pointers?
type** t = new t*[size]
type** t = new t*[size]
Quiz4; How can we dynamically allocate multi-dimensional matrix?
int** a = new int*[rowCount];
for(int i = 0; i < rowCount; ++i)
a[i] = new int[colCount];
No comments:
Post a Comment