Data Abstraction :
representing essential features without including background details.
Example : driving a car (wiring, motor working is hidden from us), switch board (hidden wiring)
Encapsulation :
(information hiding) wrapping of data (and function operating on data) into a single unit (like class).
- data can't be accessed directly (hidden) ∴safe from alteration
- you have to call member function (of class) which will read item and return value
- ensures data is accessed accurately and not corrupted by inept or outside users.
Example : if an employee from production department wants to see the sales department files, (s)he will have to issue a memo (direct access is not allowed)
Modularity :
partitioning program into modules to reduce complexity and create boundaries.
Example : Music system (modules: speaker, recorder, tuner)
Inheritance :
capability of one class to derive abilities/properties of another class. Properties:
- closeness with real world (humans inherit from mammals)
- re-usability
- transitive nature : if A inherits from B, it implies that all sub-classes of A will inherit from B
Polymorphism :
ability for a message/data to be processed in more than 1 form. It allows objects having different internal structure to share same external interface.
Example : cats being mammals can see in light as well as dark (2 forms)
FUNCTION OVERLOADING : (same name, different meaning) when 2 or more functions have the same name but different parameter/signature (argument list)-
- Number of parameters
- Type of parameters
- Sequence of parameters
OPERATOR OVERLOADING : The operator is overloaded to provide a special meaning to user-defined data type. We can modify the default meaning to the operators like '+ ' can be overloaded for string concatenation.
Operator overloading helps to redefine most operators
Operator that CANNOT be overloaded :
- Scope operator (::)
- Sizeof
- Member selector (.)
- Member pointer selector (*)
- ternary operator (?:)
FUNCTION OVERRIDING : runtime polymorphism
If a base class and derived class, each include a member function with same name and argument then the member function of derived class will be called if the scope operator is not used.
public: void print() { cout<<"Parent"; }
};
class Child : public Parent {
public : void print() { cout<<"Child"; }
};
void main() {
child C;
C.print();
}
OUTPUT : Child
VIRTUAL FUNCTION : Member function in parent class that you redefine (override) in child class. It is declared using virtual keyword.
Virtual keyword tells that the function will have late binding. Virtual Function provides a solution to the problem encountered during overriding.
Solution : Stop early/compile time binding. Why? because of early binding the datatype of pointer is the basis but the content of pointer should have been the deciding factor.
We want late/execution/runtime/dynamic binding. Because at runtime we can determine the content (address) stored in pointer. During execution f1() should be binded.
class A{
public: virtual void f1() { }
};
Comments
Post a Comment