Skip to the content.

Object Oriented Programming

Class

Its like a signature which consists all data methods access specifiers etc

class Person{
    // your Code
}; 
class Person{
    int a=10;
public:
    Person(int a){
        cout<<a;//5
        cout<<this->a;//10
    }
};
int main(){
    Person p(5);
}

Object

Instance of class is called a object

Person P;

Some Other terms in class

Static Data in Class(DOUBT HERE)

class Person{
    static int x;
    static printX(){
        cout<<x;
    };
}
int main(){
    Person::x=5;
    Person::printX();//5 is printed
}

Example2:

class Person{
    static int x;
    static printX(){
        cout<<x;
    };
    Person(){
        x+=2;
    }
}
int main(){
    Person::x=5;
    Person::printX();//5 is printed
    Person p;
    Person::printX();//7 is printed
}

Abstraction

Hiding Unnesassary data

Acess Specifiers

Encapsulation

The process of placig all methods data and related content at one place is called Encapsulation

Inheritence

A process of achiving parent child relation

class Base{
public:
    Base(){
        cout<<"Base";
    }
};
class Derived:public Base{
    Derived():Base(){
        cout<<"Derived";
    }
};
int main(){
    Derived D;
}

Output: Base Derived

At first constructor of base is called and then constructor of derived is called

Hierchial Inheritence

Many classes derives from same base class

hierchial

Multi Level Inheritence

A class can be derived from other derived class

multi level

Multiple Inheritence

A class can be inherited from more than one base classes

multiple

class Base1{};
class Base2{};
class Derived:public Base1,public Base2{
    Derived():Base2(),Base1(){

    }
}

execution of constructors : Base1 Base2 Derived

Hybrid inheritence

A mixture of one or more types

Example

hybrid

Access specifiers of base class

class Base(){
    public: int a;
    protected: int b;
    private: int c;
};
class Derived1:public Base{

};
class Derived2:protected Base{

};
class Derived3:private Base{

};
Type A(public) B(protected) C(private)
Derived1(public) public protected private
Derived2(protected) protected protected private
Derived3(private) private private private

its too simple just upgrade the access time to as much as possible public<protected<private

i.e max(accessType in base,accessType when deriving)

Polimorphism

many forms in one name

Compile Time polimorphism

Resolves at compile time

Functional Overloading

func();
func(int a);
func(double a);
func(int a,int b);
func(int a,double b);

Operator overloading

int operator+(int a,int b){

}
class Person(){
    int val;
public:
    int operator+(int a){
        return this->val+a;
    }
}

Function hiding

while using inheritence if a function of base class is rewritten in derived class then all copies of the function in base class are no longer useful and those which are in dervied class are used

Example

class Base{
public:
    func();
    func(int,int);
    func(int);
    func(int,double);
};
class Derived:public Base{
public:
    func(int);//as this is declared all the four func methods in Base are no longer accessible  (THEY ARE HIDDEN);
};

(NO LONGER ACCESSIBLE FROM OUTSIDE BUT CAN BE USED INTERNALLY(DOUBT HERE))