Loading...
Loading...
Object-Oriented Programming (OOP) is a programming paradigm that organizes code around objects (data + behavior) rather than functions and logic.
4 Pillars of OOP:
| Pillar | Definition | Keyword/Concept | |---|---|---| | Encapsulation | Bundling data + methods in a class; restricting direct access | private, public, protected | | Abstraction | Hiding implementation, showing only what is necessary | Abstract class, interface | | Inheritance | Deriving new class from existing class | : public Base | | Polymorphism | Same name, different behavior | virtual, override |
class Student {
private:
int roll; // Data members (attributes)
string name;
float cgpa;
public:
// Constructor
Student(int r, string n, float c) {
roll = r; name = n; cgpa = c;
}
// Member functions (methods)
void display() {
cout << roll << " " << name << " " << cgpa << endl;
}
// Getter and Setter (Encapsulation)
float getCGPA() { return cgpa; }
void setCGPA(float c) { cgpa = c; }
// Destructor
~Student() {
cout << "Student " << name << " destroyed" << endl;
}
};
// Creating objects
Student s1(101, "Rahul", 8.5);
Student *s2 = new Student(102, "Priya", 9.0); // Dynamic
s1.display();
s2->display();
delete s2; // Free memory
| Type | Description | |---|---| | Default Constructor | No parameters; auto-created if not defined | | Parameterized | Takes parameters; initializes with values | | Copy Constructor | Creates copy of existing object | | Destructor | Called when object destroyed; ~ClassName() |
class Circle {
float radius;
public:
Circle() { radius = 0; } // Default
Circle(float r) { radius = r; } // Parameterized
Circle(Circle &c) { radius = c.radius; } // Copy
float area() { return 3.14 * radius * radius; }
};
class Animal { // Base/Parent class
protected:
string name;
public:
Animal(string n) { name = n; }
void eat() { cout << name << " is eating" << endl; }
};
class Dog : public Animal { // Derived/Child class
public:
Dog(string n) : Animal(n) {}
void bark() { cout << name << " is barking" << endl; }
};
Dog d("Tommy");
d.eat(); // Inherited from Animal
d.bark(); // Dog's own method
Types of Inheritance:
| Type | Syntax | Description | |---|---|---| | Single | class B : public A | One parent | | Multiple | class C : public A, public B | Two parents | | Multilevel | A → B → C | Chain of inheritance | | Hierarchical | A → B, A → C | One parent, many children | | Hybrid | Combination | Mix of above |
Access in Inheritance:
| Base Member | public inheritance | protected | private | |---|---|---|---| | public | public | protected | private | | protected | protected | protected | private | | private | inaccessible | inaccessible | inaccessible |
Function Overloading:
int add(int a, int b) { return a + b; }
float add(float a, float b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; }
Operator Overloading:
class Complex {
float real, imag;
public:
Complex operator+(Complex c) {
return Complex(real + c.real, imag + c.imag);
}
};
Complex c1, c2, c3;
c3 = c1 + c2; // Uses overloaded +
Virtual Functions:
class Shape {
public:
virtual float area() { // virtual keyword
return 0;
}
virtual void display() = 0; // Pure virtual = abstract
};
class Circle : public Shape {
float r;
public:
Circle(float r) : r(r) {}
float area() override { return 3.14 * r * r; }
void display() override { cout << "Circle area: " << area(); }
};
Shape *s = new Circle(5.0); // Base pointer, derived object
s->area(); // Calls Circle::area() — dynamic dispatch
Virtual Table (vtable): Compiler creates a table of virtual function pointers for dynamic dispatch.
class AbstractShape {
public:
virtual float area() = 0; // Pure virtual
virtual float perimeter() = 0;
void printInfo() { // Concrete method
cout << "Area: " << area();
}
};
// Cannot create object of abstract class:
// AbstractShape s; // ERROR!
class Rectangle : public AbstractShape {
float l, w;
public:
Rectangle(float l, float w) : l(l), w(w) {}
float area() override { return l * w; }
float perimeter() override { return 2*(l+w); }
};
// Function template
template <typename T>
T maximum(T a, T b) {
return (a > b) ? a : b;
}
maximum(5, 3); // int version
maximum(5.5, 3.2); // double version
maximum('z', 'a'); // char version
// Class template
template <class T>
class Stack {
T arr[100];
int top;
public:
Stack() { top = -1; }
void push(T val) { arr[++top] = val; }
T pop() { return arr[top--]; }
};
Stack<int> iStack;
Stack<string> sStack;
try {
int n;
cin >> n;
if (n == 0) throw runtime_error("Division by zero!");
cout << 100 / n;
}
catch (runtime_error &e) {
cout << "Error: " << e.what();
}
catch (...) { // Catch all exceptions
cout << "Unknown error";
}
finally { // Not in C++ natively; use RAII
cout << "Cleanup";
}
Q1 (2023): Explain virtual functions with example. Virtual functions enable runtime polymorphism. When a virtual function is called through a base class pointer, the actual function called depends on the type of the object pointed to, not the pointer type. See vtable mechanism above.
Q2 (2022): What is the difference between overloading and overriding?
Q3 (2024): Write a template function to swap two values.
template <typename T>
void swap(T &a, T &b) {
T temp = a; a = b; b = temp;
}
Complete OOP in C++ notes for B.Tech CS Semester 2 — classes, objects, inheritance, polymorphism, encapsulation, abstraction, templates, and STL with examples.
50 pages · 2.6 MB · Updated 2026-03-11
Encapsulation (bundling data and methods), Abstraction (hiding implementation details), Inheritance (reusing parent class), Polymorphism (same name, different behavior).
In C++, struct members are public by default while class members are private by default. Otherwise they are functionally identical.
Engineering Mathematics 2 — Probability, Statistics, Numerical Methods
Engineering Mathematics 2
Discrete Mathematics Solved PYQs & Complete Notes
Discrete Mathematics
DBMS Complete Notes — B.Tech CS Sem 4
Database Management Systems
Compiler Design — Complete Notes CS Sem 6
Compiler Design
Machine Learning Complete Notes — B.Tech CS Sem 6
Machine Learning
Your feedback helps us improve notes and tutorials.