OOP Terms
- Class = Blueprints
- Object = Instances of Class
- Properties = Data or Characteristic Attributes
- Method = Function or Prodecure / behaviours
Programming Paradigms
- Imperative Programming Paradigm: (What to do + How to Do)
- Procedural Programming: Step by Step instructions. (C program for factorial calculation)
- Parallel Programming: Executing multiple tasks simultaneously (Python script that uses the
multiprocessing
module for parallel computing)
- Declarative Programming Paradigm: (Just what to do)
- Logical Programming: Define logical facts and rules (A Prolog program that determines family relationships based on given facts)
- Functional Programming: Focuses on pure functions that transform inputs into outputs without modifying the state. (JS function that filters and maps an array of numbers to a new array)
- Database Programming: Focuses on managing and querying structured data. (SQL)
Structured Programming
Method of programming which consists of a completely structured control flow.
i.e A definitive control flow, such as (if/then/else), (while and for), block structures, and subroutines.
Types of Inheritance
Static & Dynamic Polymorphism
Polymorphism: Objects behaves differently under different circumstances or contexts, i.e they "morph" on different requirements.
Static / Compile Time: Function / Operator is defined at compile
Method Overloading - same method for different return / params types
static int plusMethod(int x, int y) {
return x + y;
}
static double plusMethod(double x, double y) {
return x + y;
}
Operator Overloading - Operator logic for OOP
class A {
statements;
};
int main()
{
A a1, a2, a3;
a3 = a1 + a2;
return 0;
}```
**Dynamic / Run Time**:
Method overriding - child method / property overrides parent
```java
class Vehicle{
void run(){System.out.println("Vehicle is running");}
}
class Bike2 extends Vehicle{
void run(){ //child overrides parent's method
System.out.println("Bike is running safely");
}
}
Overloading & Overriding
Overloading: Entity has multiple implementations with the same name (Method & Operator Overloading) Overriding: Entity has the same name, but its implementation changes during execution
Abstract Classes & Interface
Abstract Class:
- Not implemented and only declared.
- When a subclass inherits the abstract class, they need to define and implement them.
How does C++ support Polymorphism?
- Compile Time: Templates, Function overloading, and Default arguments.
- Runtime: Virtual functions
TipClasses don't consume memory, only objects does when they are created
Struct is saved in the stack memory, while the class is saved in the heap memory
Types of Constructor
Default: No args
class ABC
{
int x;
ABC()
{
x = 0;
}
}
Parameterized: >= 1 args
class ABC
{
int x;
ABC(int y)
{
x = y;
}
}
Copy constructor: Clone an object and its values, into another object, is provided that both the objects are of the same class
class ABC
{
int x;
ABC(int y)
{
x = y;
}
// Copy constructor
ABC(ABC abc)
{
x = abc.x;
}
}
Tips on OOPs (For questions)
- Constructor is called from parent to child (destructor is reversed). Example:
#include<iostream>
using namespace std;
class BaseClass1 {
public: BaseClass1() {
cout << " BaseClass1 constructor called" << endl;
}
};
class BaseClass2 {
public: BaseClass2() {
cout << "BaseClass2 constructor called" << endl;
}
};
class DerivedClass: public BaseClass1, public BaseClass2 {
public: DerivedClass() {
cout << "DerivedClass constructor called" << endl;
}
};
int main() {
DerivedClass derived_class;
return 0;
}
Here, the order of constructor is BaseClass1 -> BaseClass2 -> DerivedClass Order of Destructor = DerivedClass -> BaseClass2 -> BaseClass1