OOPs concept in C++

     The main aim of oop is to bind together the data and the functions that operate on them so that no other part of the code can access this data except this function.

Class:-

    It is a user defined data types. which holds its own data members and member functions, which can be accessed and used by creating an instance of that class.

Object :- 

    When a class is defined no memory is allocated but when it is instantiated (i.e. object is created) memory is allocated.

Encapsulation :- 

    In oops, Encapsulation is defined as binding together the data and the functions that manipulates them.

Abstraction :-

    Abstraction is a process to show only useful data and remaining hide from user. it means displaying only essential information and hiding the details.

  • Abstraction using classses.
  • Abstraction using header files ( math.h => pow())
Polymorphism :-

    It is process to represent any things in multiple forms. In simple words, we can define polymorphism as the ability of the message to be displayed in more that one form.
  • Operator overloading
  • function overloading ( int sum (10, 20, 30) )
Inheritance :-
    The capability of a class to derive properties and characteristics from another class is called inheritance.
  • Subclass
  • SuperClass
  • Reusability
Dynamic Binding :-
    In dynamic binding, the code to be executed in response to function can is decided at run  time.
Constructors :- 
    A constructor is a member function of a class which initializes objects of a class. In C++ constructor is automatically called when the object creates.
    It has some name as class itself.
    constructor don't have a return type.
  1. Default constructor (no parameter passed )
  2. Parametrized constructor
  3. copy constructor.
Destructor in C++ :-
    Derived class destructor will be invoked first, then the base class destructor will be invoked.

Access Modifier :-     
  • Public - can be accessed by any class.
  • Private - can be accessed only by a function in a class ( inaccessible outside the class )
  • Protected -  can be accessed by subclass or that class.
Note :- If we do not specify any access modifier inside the class then by default the access modifier for the member will be private.

Friend Class :-
    A friend class can access private and protected members of other class in which it is declared as friend 
Ex. friend class B;

Inheritance :-
     


Polymorphism :-
1. Compile time poly
  • Operator overloading
  • Function overloading
2. Run time poly
    Function overloading occurs when a derived class has a definition of one or more members of base class.

Advantages of Data Abstraction.
  • Avoid code duplication and inc reusability.
  • Can change internal implementation of class independently.
Structure VS class :-
        Most important different is security. A structure is not secure an can not hide its member function and variable while class is secure and can hide it's programming and designing details

Local Class in C++ :
    A class declared inside a function becomes local to that function and is called local class.
All the methods of local class must be defined inside the class only.

Virtual function and Runtime Polymorphism :-
        A virtual function is a member function which is declared within a base class and redefined (overriden ) by derives class.
functions are declared with virtual keyword in base class.

Exception Handling in C++ :-
  • try : represent a block of code that can throw an exception.
  • catch : represent a block of cade that get executed when error is throw.
  • throw : used to throw an exception.
There is a special catch block ---> catch(...)
it Catches all types of error.

Inline functions
    Inline is a request not command.
    It is function that is expanded in line when it is called when the inline function is called, whole code get inserted of substituted at the point of inline function call.

Difference between C and C++

C C++
C supports procedural prog. c++ is known as hybrid language, becouse it support both procedural and object oriented programming
As c does not support the oops concept so it has no support for poymorphism, encapsulation and inheritance C++ has upper for polymorphism, encapsulation and inheritance as it is an oops language.
C is a subset of c++ C++ is superset of C
C contains 32 keywords C++ contain 52 keywords ( public, private, protected, try , catch, throw...
C is function driven language C++ is object driven language
Function and operator overloading is not support in c C++ supports function operator overloading
C does not support exception handling C++ supports exception handling using try and catch
Structure is a collection of dissimilar elements

Static Members of C++
  • Static Variable in a function : 
        When a variable is declared as static, space for it gets allocated for the lifetime of the program ( default intialized to 0 ) Even if the function is called multiple times, the space for it is allocated once.
  • Static variable in a class: 
  1. Declared inside the class body.
  2. Also known as class member variable.
  3. Static variable closen't belong to any object, but to te whole class.
  4. There will be any are copy of static member variable for the whole class.
class Accout
{
    private :
        int balance;
        static float roi;
    public:
        void setBalance ( int b)
        {balance = b;}
};
Initialised OutSide Class
class Accout
{
    private :
        int balance;
        static float roi;
    public:
        void setBalance ( int b)
        {balance = b;}
};
Object can also be declared as static
        Static Account a1;

Static function in a class
    static member functions are allows to access only the static data members or other static member functions.

Constructors:
  • Constructors is an special member function of the class. It is automatically invoked when an object is created.
  • it has no return type
  • Constructor as same name as class itself
  • It we do not specify then c++ compiler generates a default constructor for us.

Compiler generates two constructor by itself
  1. Default constructor
  2. copy Constructor
  • But it any of the constructor is created by user then default constructor. will not be created by compiler.
  • construction overloading can be done just like function overloading.

Default (compiler's ) copy constructor can done only shallow copy

Deep Copy  is a possible only with user defined constructor. In user defined copy constructor, we make some that pointers of copied object points to new memory location.

Can we make copy constructer private? YES

Why argument to copy constructor most be passed as a referece?
     Because it we pass value, then it would made to call copy constructor which becomes non-terminatry.

Destructor :-
  • Destructor is a member function which destructs or deletes an object.
  • Destructor don't take any argument and don't have any return type.
  • only one destructor is possible.
  • Destructor can not be static.
  • Actually destructor doesn't destroy object it is the last function that invoked before 0bject destroy.

  • Destructor is used so that before deletion of object we can free space allocated for this resource. B/C it object gets deleted then space allocated for object will be free but resource doesn't.
Operator Overloading
        C++ have the ability to provide special meaning to the operator



class Complex
{
	complex operator + ( Complex &c1 )
    {
    	complex res;
        res.a = c1.a;
        res.b = c2.b;
    }
}
int main c1
{
	c = c1 + c2
}
As '+' can't add complex no's directly. so we can define a function with name + ,but we need write operator keyword before it so, we can use all operator like this.

Friend class :-
        A friend class can access the private and protected members of other class in which it is declared as friend.
There can be friend class and friend function.