Object-Oriented Programming
This is a paradigm/technique of programming which revolves around the object, it tries to bridge
the gap between computer programming and real world objects.
Object is an entity(instance of class) ——> States/Properties
——> Behaviours
Classes are custom datatypes created by us.
class Car{
int tire1, tire2, tire3, tire4; //properties
};
int main{
Car nano;
cout << sizeof(nano) << endl; //Always gives the sum of size of all var in class(+padding)
}
Size of empty class is 1 for identi cation.
Access modi ers are: Public, Private(Default) and Protected
1. Public - Can be used inside and outside of class.
2. Private - Can only be inside the class. (Getter, Setter)
3. Protected - Can be used inside a class and by a derived class.
Getter and Setter Functions can be used in private access modi ers.
//Static Allocation
Car nano;
cout << nano.tire1 << endl;
//Dynamic Allocation
Car* comet = new Car;
cout << (*comet).tire1 << endl;
cout << comet—>tire1 << endl;
Constructors
class Car{
int tire1, tire2, tire3, tire4; //properties
Car() {
cout << “Constructor Called”;
}
// Copy Constructor
Car(Car& temp){
this—>tire1 = temp.tire1;
}
// Parametrised constructor
Car(int tire1) {
this—>tire1 = tire1; // this keyword points to address of current object
}
~Car() {
cout << “Destructor Called”;
}
};
fi
fi
fi
Object-Oriented Programming
If we make even one parametrised constructor then the default constructor is removed and we
have to explicitly write it again.
Deep copy vs Shallow copy
Destructor is automatically called for statically created objects.
But objects created by dynamic allocation will be deleted manually. //delete comet;
Static keyword makes a property which belongs to class and we don’t have to create object for it.
static int validity;
int Car::validity = 15; // “::” is scope resolution operator
Static functions: No need to create object and no this keyword(because this points to the address
of object). They can only access static members.
Encapsulation
Wrapping up of Data members and functions.
Main purpose of encapsulation is hiding information.
Advantages
Data hiding and security
Read-Only with getters
Code Reusability
Unit Testing
Inheritance
Human - Height, Weight, Age // Parent class or super class
Male and Female class inherits from Human. // Child class or sub class or derived class
class Human {
public:
int height;
int weight;
int age;
public:
int getAge(){
return this->age;
}
void setWeight(){
this—>weight = w;
}
};
class Man: public Human {
public:
string color;
public:
void attack(){
cout << “Growl” << endl;
}
};
Object-Oriented Programming
Super Class Modi er Access Modi er Mode Sub Class Modi er
Public Public Public
Public Protected Protected
Public Private Private
Protected Public Protected
Protected Protected Protected
Protected Private Private
Private Public Not accessible
Private Protected Not accessible
Private Private Not accessible
Types of Inheritance
1. Single: A —> B, example above.
2. Multi-level: A —> B —> C, Human —> Male —> Employee
3. Multiple: A —|__ C
B —|
class Hybrid: public Human, public Animal {
};
4. Hierarchical: A —> B and A —> C
5. Hybrid: Combination of more than one inheritance.
Inheritance ambiguity: It happens when you inherit 2 functions/parameters from 2 classes with
same name.
Avoid the above using scope resolution operator.
• C Obj;
• Obj.A :: func()
• Obj.B :: func()
Polymorphism
Many-forms.
1. Compile-time polymorphism
1. Function Overloading
So we can achieve this by changing the number of arguments, changing the types of
arguments and by using default arguments.
2. Operator Overloading, so that we can have our operator be applied on objects.
void operator+ (B &obj) {
int value1 = this -> a;
int value2 = obj.a;
cout << "output " << value2 - value1 << endl;
cout << "Hello Babbar" << endl;
}
fi
fi
fi
Object-Oriented Programming
2. Run-time polymorphism - method overriding and inheritance dependence.
Rules - Same function name, same function arguments and inheritance.
class Animal {
public:
void speak() {
cout << "Speaking "<< endl;
}
};
class Dog: public Animal {
public:
void speak() {
cout << "Barking " << endl;
}
};
Abstraction
Implementation Hiding