🌟 MODULE – 1: Overview of C++ and Object-Oriented Programming
1. What is Object-Oriented Programming (OOP)?
Definition:
Object-Oriented Programming (OOP) is a method of programming that organizes software design around
objects rather than functions and logic.
Objects are instances of classes, which bundle data and the operations (functions) that work on that data.
Features of OOP:
Encapsulation – Wrapping data and functions together in a single unit (class).
Abstraction – Hiding unnecessary details and showing only essential
features. Inheritance – Deriving new classes from existing ones to promote
code reuse.
Polymorphism – The ability of one function or operator to behave differently depending on the context.
Advantages:
Code reusability
Data security
Better maintainability
Real-world modeling
2. The General Form of a C++ Program
Explanation:
A basic C++ program contains:
Header files (for I/O and libraries)
Namespace declaration
Class definitions or functions
main() function, which is the program’s entry point
Syntax:
#include <iostream>
using namespace std;
class ClassName {
// data members and member functions
};
int main() {
// program statements
return 0;
}
Example:
#include <iostream>
using namespace std;
class Hello {
public:
void display() {
cout << "Welcome to C++ Programming!" << endl;
}
};
int main()
{ Hello h;
[Link]();
return 0;
}
Output:
Welcome to C++ Programming!
3. What are Classes and Objects?
Definition:
Class: A user-defined data type that acts as a blueprint for creating objects.
Object: An instance of a class which holds real data and performs actions defined by the class.
Syntax:
class ClassName {
public:
// data members
// member functions
};
Example:
#include <iostream>
using namespace std;
class Student {
public:
string name;
int roll;
void display() {
cout << "Name: " << name << "\nRoll: " << roll << endl;
}
};
int main()
{ Student s1;
[Link] =
"Alok"; [Link] =
101; [Link]();
return 0;
}
Output:
Name: Alok
Roll: 101
4. Friend Function and Friend Class
Definition:
A friend function can access private and protected members of a class even though it is not a member of
that class.
A friend class allows another class to access its private members.
Syntax:
class A {
private:
int x;
friend void show(A); // friend function
};
Example:
#include <iostream>
using namespace std;
class Box {
private:
int length;
public:
Box(int l) : length(l) {}
friend void display(Box); // friend function
};
void display(Box b) {
cout << "Length = " << [Link] << endl;
}
int main()
{ Box
b1(25);
display(b1);
return 0;
}
Output:
Length = 25
Explanation:
The function display() is not part of class Box, but because it is declared as a friend, it can access its
private member length.
5. Inline Functions
Definition:
An inline function is expanded in line when it is called. The compiler replaces the function call with the
function code, reducing the overhead of a function call.
Syntax:
inline return_type function_name(arguments) {
// code
}
Example:
#include <iostream>
using namespace std;
inline int square(int n)
{ return n * n;
}
int main() {
cout << "Square of 4 = " << square(4) << endl;
return 0;
}
Output:
Square of 4 = 16
Advantages:
Faster execution for small functions
Reduces function call overhead
6. Parameterized Constructor
Definition:
A parameterized constructor accepts parameters to initialize object members with specific values.
Syntax:
class ClassName
{ ClassName(parameter_list) { ...
}
};
Example:
#include <iostream>
using namespace std;
class Point
{ int x, y;
public:
Point(int a, int b) {
x = a;
y = b;
}
void show() {
cout << "x = " << x << ", y = " << y << endl;
}
};
int main() {
Point p1(10, 20);
[Link]();
return 0;
}
Output:
x = 10, y = 20
7. Static Class Members
Definition:
A static data member is shared among all objects of a class. It is initialized only once.
A static member function can access only static data members.
Example:
#include <iostream>
using namespace std;
class Counter {
public:
static int count;
Counter() { count++; }
static void showCount() {
cout << "Count = " << count << endl;
}
};
int Counter::count = 0; // definition outside class
int main() {
Counter c1, c2, c3;
Counter::showCount();
return 0;
}
Output:
Count = 3
Explanation:
count is common for all objects, so it increments with each object creation.
8. When are Constructors and Destructors Executed?
Explanation:
Constructor: Called automatically when an object is created.
Destructor: Called automatically when an object goes out of scope or is deleted.
Example:
#include <iostream>
using namespace std;
class Demo {
public:
Demo() { cout << "Constructor called\n"; }
~Demo() { cout << "Destructor called\n"; }
};
int main()
{ Demo
d1;
{
Demo d2;
} // d2 destroyed here
cout << "End of program\n";
return 0;
}
Output:
Constructor called
Constructor called
Destructor called
End of program
Destructor called
9. Scope Resolution Operator (::)
Definition:
Used to define a class member function outside the class or to access a global variable when a local
variable has the same name.
Example:
#include <iostream>
using namespace std;
int x = 100; // global variable
class Test {
public:
void display();
};
void Test::display() {
cout << "Function defined using scope resolution operator\n";
}
int main()
{ int x =
10;
cout << "Local x = " << x << endl;
cout << "Global x = " << ::x << endl;
Test t;
[Link]();
return 0;
}
Output:
Local x = 10
Global x = 100
Function defined using scope resolution operator
10. Passing and Returning Objects
Definition:
Objects can be passed to functions as arguments and can also be returned by functions.
Example:
#include <iostream>
using namespace std;
class Number
{ int x;
public:
Number(int n = 0) { x = n; }
void show() { cout << "x = " << x << endl; }
};
void display(Number n) {
[Link]();
}
Number getNumber()
{ Number temp(99);
return temp;
}
int main()
{ Number
n1(10);
display(n1);
Number n2 = getNumber();
[Link]();
return 0;
}
Output:
x = 10
x = 99
11. Object Assignment
Definition:
C++ provides a default assignment operator (=) that performs member-wise copy of one object to
another.
Example:
#include <iostream>
using namespace std;
class Sample {
public:
int a;
Sample(int x = 0) { a = x; }
};
int main()
{ Sample
s1(10); Sample
s2;
s2 = s1; // object assignment
cout << "Value of s2.a = " << s2.a << endl;
return 0;
}
Output:
Value of s2.a = 10
🌟 MODULE – 2: Arrays, Pointers, References, and
Function Overloading
1. What are Arrays of Objects?
Definition:
An array of objects is a collection of multiple objects of the same class stored in consecutive memory
locations.
It allows you to manage a group of related objects easily.
Syntax:
ClassName objectName[array_size];
Example:
#include <iostream>
using namespace std;
class Student {
public:
string name;
int age;
void getData(string n, int a)
{ name = n;
age = a;
}
void display() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
int main() {
Student s[3]; // Array of objects
s[0].getData("Alok", 18);
s[1].getData("Ravi", 19);
s[2].getData("Neha", 20);
for(int i = 0; i < 3; i++)
s[i].display();
return 0;
}
Output:
Name: Alok, Age: 18
Name: Ravi, Age: 19
Name: Neha, Age: 20
Explanation:
Each element s[i] is an object of the Student class.
2. Pointers to Objects
Definition:
A pointer to an object stores the address of an object and is used to access members using the ->
operator.
Example:
#include <iostream>
using namespace std;
class Sample {
public:
int x;
void show() { cout << "x = " << x << endl; }
};
int main()
{ Sample obj;
Sample *ptr = &obj; // pointer to object
ptr->x = 50;
ptr->show();
return 0;
}
Output:
x = 50
Explanation:
The arrow operator (->) is used to access the data and functions of the object through its pointer.
3. The ‘this’ Pointer
Definition:
this is an implicit pointer available inside all non-static member functions, pointing to the object that
invoked the function.
It is used to resolve conflicts between local and class variables having the same name.
Example:
#include <iostream>
using namespace std;
class Test
{ int x;
public:
void setValue(int x) {
this->x = x; // differentiate local x and data member x
}
void show() {
cout << "Value of x = " << x << endl;
}
};
int main()
{ Test t;
[Link](25);
[Link]();
return 0;
}
Output:
Value of x = 25
Explanation:
this->x refers to the data member of the current object.
4. Pointers to Derived Types
Definition:
A pointer of a base class can point to an object of a derived class.
This helps in implementing polymorphism (run-time behavior).
Example:
#include <iostream>
using namespace std;
class Base {
public:
void show() { cout << "Base class function\n"; }
};
class Derived : public Base {
public:
void show() { cout << "Derived class function\n"; }
};
int main()
{ Base
*ptr;
Derived d;
ptr = &d; // base class pointer pointing to derived object
ptr->show(); // calls base function (not
virtual) return 0;
}
Output:
Base class function
Explanation:
By default, a base class pointer can call only base functions unless the function is declared virtual.
5. Pointers to Class Members
Definition:
A pointer to a class member stores the address of a class data member or member function.
Example:
#include <iostream>
using namespace std;
class Sample {
public:
int a;
void show() { cout << "a = " << a << endl; }
};
int main()
{ Sample s;
s.a = 10;
int Sample::*ptr = &Sample::a; // pointer to data member
void (Sample::*fptr)() = &Sample::show; // pointer to
function
cout << "Value of a using pointer: " << s.*ptr <<
endl; (s.*fptr)(); // function pointer call
return 0;
}
Output:
Value of a using pointer: 10
a = 10
6. Function Overloading
Definition:
Function Overloading allows multiple functions to have the same name but with different parameter
types or numbers.
The compiler decides which function to call based on arguments.
Example:
#include <iostream>
using namespace std;
class Math {
public:
int add(int a, int b)
{ return a + b;
}
double add(double a, double b)
{ return a + b;
}
int add(int a, int b, int c)
{ return a + b + c;
}
};
int main()
{ Math m;
cout << "Sum1 = " << [Link](2, 3) << endl;
cout << "Sum2 = " << [Link](2.5, 3.5) << endl;
cout << "Sum3 = " << [Link](1, 2, 3) << endl;
return 0;
}
Output:
Sum1 = 5
Sum2 = 6
Sum3 = 6
Explanation:
The function add() is called based on the number and type of arguments.
7. Constructor Overloading
Definition:
When a class has multiple constructors with different parameter lists, it is called constructor
overloading.
This helps create objects in different ways.
Example:
#include <iostream>
using namespace std;
class Rectangle
{ int length,
width;
public:
Rectangle() { length = width = 0; } // default constructor
Rectangle(int l, int w) { length = l; width = w; } // parameterized
constructor void display() {
cout << "Length = " << length << ", Width = " << width << endl;
}
};
int main() {
Rectangle r1; // calls default constructor
Rectangle r2(10, 5); // calls parameterized
constructor [Link]();
[Link]();
return 0;
}
Output:
Length = 0, Width = 0
Length = 10, Width = 5
8. Copy Constructor
Definition:
A copy constructor initializes an object using another object of the same class.
Syntax:
ClassName(const ClassName &obj);
Example:
#include <iostream>
using namespace std;
class Example {
int value;
public:
Example(int v) { value = v; }
Example(const Example &obj) { value = [Link]; } // copy constructor
void display() { cout << "Value = " << value << endl; }
};
int main()
{ Example
e1(10);
Example e2 = e1; // invokes copy constructor
[Link]();
return 0;
}
Output:
Value = 10
Explanation:
The copy constructor is called when an object is initialized from another existing object.
9. Default Function Arguments
Definition:
Default arguments are values provided in a function definition that are used if no arguments are passed for
those parameters.
Example:
#include <iostream>
using namespace std;
void printDetails(string name, int age = 18) {
cout << "Name: " << name << ", Age: " << age << endl;
}
int main() {
printDetails("Alok");
printDetails("Ravi", 20);
return 0;
}
Output:
Name: Alok, Age: 18
Name: Ravi, Age: 20
Explanation:
When age is not provided, the default value 18 is used.
10. Function Overloading and Ambiguity
Definition:
Ambiguity in function overloading occurs when the compiler cannot determine which version of the
overloaded function to call.
Example:
#include <iostream>
using namespace std;
void test(int a) { cout << "Integer function\n"; }
void test(double a) { cout << "Double function\n"; }
int main() {
test(10); // exact match → int version
test(10.5); // exact match → double
version
// test('A'); // could cause ambiguity if both match equally
return 0;
}
Output:
Integer function
Double function
Note:
Ambiguity occurs when conversions are required for both functions and neither is a better match.
11. Dynamic Memory Allocation Operators (new and delete)
Definition:
C++ provides new and delete operators to allocate and deallocate memory dynamically during runtime.
Example:
#include <iostream>
using namespace std;
int main() {
int *ptr = new int; // allocate memory for one int
*ptr = 50;
cout << "Value = " << *ptr << endl;
delete ptr; // deallocate memory
int *arr = new int[3]; // dynamic array
arr[0] = 10; arr[1] = 20; arr[2] = 30;
for (int i = 0; i < 3; i++)
cout << arr[i] << " ";
delete[] arr; // delete array
return 0;
}
Output:
Value = 50
10 20 30
Explanation:
new allocates memory dynamically and returns a pointer.
delete frees the allocated memory.
✅ Conclusion for Module 2:
Concept Key Idea
Arrays of Objects Group of objects in continuous
memory Pointers to Objects Access object using -
>
this Pointer Refers to current object
Pointers to Derived Base pointer → Derived
object
Function Overloading Same function name, different parameters
Construct
Multiple constructors in one class
or
Overloadi
ng
Copy Constructor Initializes object from
another Default Arguments Predefined
parameter values
Compiler confusion between overloaded
Ambigui
functions
ty
Dynamic Allocation Memory managed using new and delete