Object Oriented Programming Using C++
UNIT-I: Introduction
Introducing Object-Oriented Approach, Relating to other paradigms {Functional, Data decomposition}. Basic terms and ideas: Abstraction, Encapsulation, Inheritance, Polymorphism, Review of C, Difference between C and C++ - cin, cout, new, delete, operators.
UNIT-II: Classes and Objects
Encapsulation, information hiding, abstract data types, Objects & classes, attributes, methods, C++ class declaration, State identity and behaviour of an object, Constructors and destructors, instantiation of objects, Default parameter value, object types, C++ garbage collection, dynamic memory allocation, Meta class / abstract classes.
UNIT-III: Inheritance and Polymorphism
Inheritance, Class hierarchy, derivation – public, private & protected, Aggregation, composition vs classification hierarchies, Polymorphism, Categorization of polymorphism techniques, Method polymorphism, Polymorphism by parameter, Operator overloading, Parametric Polymorphism.
UNIT-IV: Generic Functions
Template function, function name overloading, overriding inheritance methods, Run time polymorphism, Multiple Inheritance.
UNIT-V: Files and Exception Handling
Streams and files, Namespaces, Exception handling, Generic Classes.

UNIT-I: Introduction to Object Oriented Programming (OOP)

1. Object-Oriented Approach

Object-Oriented Programming (OOP) is a paradigm based on the concept of "objects", which contain data and methods. OOP helps in designing reusable, maintainable, and scalable software.

2. Comparison with Other Paradigms

3. Basic OOP Concepts

4. Review of C (Key Differences)

C is a procedural language, whereas C++ is object-oriented. C++ includes features like classes, objects, and advanced memory management using new and delete.

5. Difference Between C and C++

6. Example: Basic C++ Program using OOP Features

#include <iostream> using namespace std;
class Student {
  private:     string name;     int age;
  public:     void setData(string n, int a) {       name = n;       age = a;     }
    void display() {       cout << "Name: " << name << ", Age: " << age << endl;     } };
int main() {   Student s1;   s1.setData("Amit", 20);   s1.display();   return 0; }

7. Example: Use of new and delete

int* ptr = new int; // dynamically allocate memory *ptr = 100; cout << "Value: " << *ptr << endl; delete ptr; // free memory

UNIT-II: Classes and Objects

1. Encapsulation & Information Hiding

Encapsulation is the concept of bundling data and methods that operate on that data within one unit (class).
Information hiding is achieved by using access specifiers like private, public, and protected.

2. Abstract Data Types (ADT)

ADT refers to a data structure defined by the user that hides implementation details and exposes only operations.

3. Objects & Classes

class Car {   public:     string brand;     int year;     void display() {       cout << brand << " - " << year << endl;     } };
int main() {   Car c1;   c1.brand = "Toyota";   c1.year = 2022;   c1.display(); }

4. State, Identity, and Behaviour of an Object

5. Constructors and Destructors

Constructor: Special method called when an object is created. It has the same name as the class.
Destructor: Called when the object goes out of scope to clean up resources.

class Student {   public:     Student() {       cout << "Constructor called" << endl;     }     ~Student() {       cout << "Destructor called" << endl;     } };
int main() {   Student s; }

6. Instantiation of Objects

Creating objects of a class using class name.

Car c1, c2; // Two objects created

7. Default Parameter Value

Allows passing fewer arguments by giving default values in function declaration.

void greet(string name = "Guest") {
  cout << "Hello, " << name << endl;
}
greet(); // Output: Hello, Guest
greet("Nivesh"); // Output: Hello, Nivesh

8. Object Types

9. C++ Garbage Collection

C++ does not have automatic garbage collection like Java. The delete operator is used to manually deallocate dynamic memory.

int* ptr = new int(10);
cout << *ptr;
delete ptr;

10. Dynamic Memory Allocation

Using new and delete to allocate and free memory during runtime.

11. Meta Class / Abstract Class

An abstract class has at least one pure virtual function and cannot be instantiated directly.

class Shape {   public:     virtual void draw() = 0; // Pure virtual function };
class Circle : public Shape {   public:     void draw() override {       cout << "Drawing Circle" << endl;     } };
int main() {   Circle c;   c.draw(); }

UNIT-III: Inheritance and Polymorphism

1. Inheritance

Inheritance allows one class to inherit the properties and behaviors (members) of another class. It promotes code reusability and forms a hierarchy of classes.

Example:

class Animal {   public:     void sound() {       cout << "Animal sound" << endl;     } };
class Dog : public Animal {   public:     void bark() {       cout << "Bark" << endl;     } };
int main() {   Dog d;   d.sound();   d.bark(); }

2. Class Hierarchy

A class hierarchy shows the relationship between a base class and its derived classes.
It can be single, multilevel, hierarchical, or hybrid inheritance.

3. Derivation – Public, Private & Protected

These determine how the members of the base class are inherited:

4. Aggregation

Aggregation is a "has-a" relationship where one class contains another as a member.

Example:

class Engine {   public:     void start() { cout << "Engine starts" << endl; } };
class Car {   Engine e;   public:     void startCar() {       e.start();     } };

5. Composition vs Classification Hierarchies

Composition: One object is made of other objects.
Classification: An object is a specific type of a more general class.

6. Polymorphism

Polymorphism means "many forms". It allows functions or operators to act differently based on the input.
Types: Compile-time (method/ operator overloading), Run-time (virtual functions).

7. Categorization of Polymorphism Techniques

8. Method Polymorphism (Function Overloading)

Function with same name but different parameters.

class Print {   public:     void show(int a) { cout << a << endl; }     void show(string s) { cout << s << endl; } };
Print p; p.show(5); p.show("Hello");

9. Polymorphism by Parameter

Achieved when the same function name handles different types/number of parameters.

10. Operator Overloading

Operator overloading allows redefining the meaning of operators for user-defined types.

class Complex {   int real, imag;   public:     Complex(int r, int i) { real = r; imag = i; }     Complex operator + (Complex c) {       return Complex(real + c.real, imag + c.imag);     }     void display() { cout << real << "+" << imag << "i" <&endl; }
};
int main() {   Complex c1(2, 3), c2(4, 5);   Complex c3 = c1 + c2;   c3.display(); }

11. Parametric Polymorphism

Using templates to write generic and reusable code.

template <class T> T add(T a, T b) {   return a + b; }
int main() {   cout << add<int>(5, 10) << endl;   cout << add<float>(2.5, 3.5); }

UNIT-IV: Generic Function

1. Template Function

Template functions allow writing generic and reusable code. It enables the creation of functions that can operate on different data types without rewriting code.

template <typename T> T add(T a, T b) {   return a + b; }
int main() {   cout << add(10, 20) << endl; // For int   cout << add(2.5, 3.6) << endl; // For float   return 0; }

2. Function Overloading

Overloading allows functions with the same name but different parameters.

void print(int i) {   cout << "Integer: " << i << endl; } void print(double d) {   cout << "Double: " << d << endl; } void print(string s) {   cout << "String: " << s << endl; }

3. Method Overriding

Overriding is redefining a base class method in the derived class using the same signature.

class Base {   public:     virtual void show() {       cout << "Base class" << endl;     } }; class Derived : public Base {   public:     void show() override {       cout << "Derived class" << endl;     } };

4. Run-Time Polymorphism

Achieved using virtual functions and base class pointers.

Base* ptr; Derived d; ptr = &d; ptr->show(); // Calls Derived's show() method

5. Multiple Inheritance

A class can inherit from more than one base class.

class A {   public:     void showA() { cout << "Class A" << endl; } }; class B {   public:     void showB() { cout << "Class B" << endl; } }; class C : public A, public B { };
int main() {   C obj;   obj.showA();   obj.showB();   return 0; }

UNIT-V: Files and Exception Handling

1. Streams and Files

Streams in C++ are used to perform input and output operations. Files allow storing and retrieving data from storage.

#include <iostream> #include <fstream> using namespace std;
int main() {   ofstream outfile("example.txt");   if (outfile.is_open()) {     outfile << "Hello, file!" << endl;     outfile.close();   }   ifstream infile("example.txt");   string line;   while (getline(infile, line)) {     cout << line << endl;   }   infile.close();   return 0; }

2. Namespaces

Namespaces in C++ are used to organize code into logical groups and prevent name collisions.

#include <iostream> using namespace std;
namespace first_namespace {   void display() {     cout << "This is the first namespace!" << endl;   } }
int main() {   first_namespace::display();   return 0; }

3. Exception Handling

Exception handling in C++ is done using try, catch, and throw keywords. It is used to handle runtime errors.

#include <iostream> using namespace std;
int divide(int a, int b) {   if (b == 0) {     throw "Division by zero!";   }   return a / b; }
int main() {   try {     cout << "Result: " << divide(10, 0) << endl;   }   catch (const char* msg) {     cout << "Error: " << msg << endl;   }   return 0; }

4. Generic Classes

Generic classes in C++ are used to create classes that can work with any data type.

template <typename T> class Box {   T value;   public:     Box(T val) : value(val) {}     T getValue() { return value; } };
int main() {   Box<int> intBox(5);   cout << "Integer Value: " << intBox.getValue() << endl;   Box<double> doubleBox(3.14);   cout << "Double Value: " << doubleBox.getValue() << endl;   return 0; }