Skip to main content

Basic OOP concepts

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
Parent / Super / Base class
                 ↓
Child / Sub / Derived class
Types:
The child class can access private members of parent class indirectly, through the inherited member function of the parent class (when the inherited member function is accessing the private members of their original class)

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)-

  1. Number of parameters
  2. Type of parameters
  3. Sequence of parameters
It does NOT depend upon the return type of function.
Example:
class Poly
{
public :
             float area (float a){
                             return a*a ; }              //square
             float area (float a, float b){
                             return a*b ; }             //rectangle
             double area (float a){
                             return 3.14*a*a ; }    //circle
}

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 (?:)
Syntax : returnType className :: operator op (argument_list){
              }
- existing operators can be overloaded, not new
- overloaded operators contain at least one operand of user-defined datatype
- member function can be used to overload operators that can't be overloaded by the friend function
- unary operator, member function---no explicit argument
                            friend function---takes 1 argument
- binary operator, member function---1 explicit argument
                             friend function---2 explicit argument

OUTPUT: -3
                  -4


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.

class Parent {
        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. 

[Parent class pointer can point to the object of any of its descendant classes but its converse is not true 
example: let car(parent) and sports car(child) be two classes 
if you make pointer p of car, then datatype of pointer p will be car so traditionally p should contain address of car's object but p can actually point to/store address of car's child class object as well]

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.

To do that we can add virtual keyword before f1 in A (child class f1 is also automatically virtual) i.e, 
class A{
        public: virtual void f1() {  }
};

Comments

Popular posts from this blog

Data Representation

 Digital Number System In digital representation, various number systems are used. The most common ones are : Binary : base 2 (0, 1) Octal : base 8 (0, 1, 2, 3, 4, 5, 6, 7) Decimal : base 10 (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) Hexadecimal : base 16 (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F) Number Conversions : From Decimal to any system (base) (Decimal) 10 = (Any) base

Data Type Modifiers

 As the name suggests, data type modifiers are used with fundamental data types to modify the length of data that a particular data type can hold. Following 4 datatype modifiers are available in C++: Signed Unsigned Short Long

Selection statement

  If statement  If tests a particular condition, if the condition evaluates to true then set of statements under the body of 'if' statement will be executed otherwise it will be ignored by the compiler. Syntax : if(<condition>) {      statements; } <> : programmer defined If else statement If we use 'else' statement with 'if' statement then if the condition of 'if' statement evaluates to false, in that case body of else statement will be executed, otherwise it will be ignored. Syntax :  if(<condition>) {      statements; } else {     statements; } If-else-if ladder If we use 'else if' statement with 'if' statement then if any condition evaluates to true, in that case its associated statement will be executed and all other 'else if' and 'else' statement will be ignored by the compiler. Syntax :  if(<condition>) {      statements; } else if(<condition>) {    statements; } els...