OOPs Object Oriented
Programming C++
Module 5: Basics of Object-Oriented Programming
(OOP)
What is OOP?
OOP means writing code using objects, making it easier to manage and reuse.
// Example: Simple class
class Student {
private: int marks;
public: void setMarks(int m) { marks = m; }
};
Classes and Objects
A class is like a template. Objects are made using that template.
class Car {
public: void start() { cout << "Car started\n"; }
};
Car c1; c1.start();
this Pointer
Used inside a class to refer to the current object.
class Demo {
int x;
public: void set(int x) { this->x = x; }
};
Constructors and Destructors
OOPs Object Oriented Programming C++ 1
Constructor runs when object is created. Destructor runs when object is
deleted.
class A {
public: A() { cout << "Created\n"; }
~A() { cout << "Deleted\n"; }
};
Static Members
Same value shared by all objects of the class.
class Counter {
static int count;
public: static void show() { cout << count; }
};
int Counter::count = 0;
Inline Functions
Tells the compiler to directly put function code instead of calling it.
inline int square(int x) { return x * x; }
Call by Reference
Changes made inside the function affect the original value.
void update(int &x) { x += 10; }
Default Arguments
If you don’t give a value, a default one will be used.
void greet(string name = "User") { cout << "Hi " << name; }
Passing Objects to Functions
You can send objects to functions like normal variables.
OOPs Object Oriented Programming C++ 2
class Test {
public: void show() { cout << "Hello"; }
};
void display(Test t) { t.show(); }
Friend Function and Friend Class
Can access private stuff of a class.
class Box {
int size;
public: friend void show(Box);
};
void show(Box b) { cout << b.size; }
Module 6: Inheritance
What is Inheritance?
One class takes stuff from another class.
class A { public: void msg() { cout << "Base"; } };
class B : public A {};
Single Inheritance
One class takes from one base class.
class A {}; class B : public A {};
Multiple Inheritance
One class takes from two base classes.
class A {}; class B {}; class C : public A, public B {};
Multilevel Inheritance
OOPs Object Oriented Programming C++ 3
Class takes from another class which also took from another.
class A {}; class B : public A {}; class C : public B {};
Hierarchical Inheritance
Many classes take from one class.
class A {}; class B : public A {}; class C : public A {};
Multipath Inheritance
Same class taken through two paths. Can cause confusion (diamond problem).
class A {};
class B : public A {};
class C : public A {};
class D : public B, public C {};
Constructors in Inheritance
Base class constructor runs before derived class constructor.
class A { public: A() { cout << "Base"; } };
class B : public A { public: B() { cout << "Derived"; } };
Module 7: Polymorphism
Function Overloading
Same function name, but different inputs.
void show(int x); void show(double x);
Operator Overloading
You can change how operators work for your class.
OOPs Object Oriented Programming C++ 4
class A {
int x;
public:
A(int val) : x(val) {}
A operator+(A obj) { return A(x + obj.x); }
};
Dynamic Polymorphism
Which function to run is decided at runtime.
class A { public: virtual void show() { cout << "A"; } };
class B : public A { public: void show() override { cout << "B"; } };
Virtual Functions
Let derived class replace base class function.
class Base {
public: virtual void print() { cout << "Base"; }
};
Pure Virtual Functions
No body. Must be written in derived class.
class Shape {
public: virtual void draw() = 0;
};
Abstract Classes
Class with at least one pure virtual function.
class Interface {
public: virtual void work() = 0; // Must be implemented in child
};
OOPs Object Oriented Programming C++ 5