Oops Cheats
Oops Cheats
T add(T a, T b) {
return a + b;
}int main() {
// Adding integers
cout << "Sum of integers: " << add(int1, int2) << endl;
// Adding floats
cout << "Sum of floats: " << add(float1, float2) << endl;
// Adding doubles
cout << "Sum of doubles: " << add(double1, double2) << endl;
// Adding strings
cout << "Sum of strings: " << add(str1, str2) << endl;
return 0;
C++, templates allow you to write generic and reusable code. They are of two main types:
Function Template
Function templates are used to create a single function that works with different data types without
rewriting code multiple times.
Uses:
• To perform the same operation on different data types (e.g., add, swap, compare).
Class Template
Class templates allow you to define a class with generic data types, which can later be specified when
the object is created.
Uses:
Unit - 1 Let’s break your question into parts and explain clearly in C++ (not “Coo”, which seems like a
typo):
What is Recursion?
Recursion is a process where a function calls itself to solve a smaller part of the original problem.
It is often used for problems that can be broken down into similar sub-problems, such as:
• Factorial
• Fibonacci
• Tree Traversals
• Tower of Hanoi
1. Base Condition
• Example: If n == 0, return 1.
2. Recursive Call
• The function should call itself with modified arguments that bring it closer to the base case.
1. Call by Value
2. Call by Reference
• The original variable is passed using reference (&).
3. Call by Pointer
Inheritance is an Object-Oriented Programming (OOP) concept where a class (child/derived) can acquire
properties and behavior (data and functions) of another class (parent/base).
• Volume = side³
The "" syntax is used in user-defined literals, introduced in C++11, to extend the meaning of literal
constants (like 10_m for 10 meters).
#include <iostream>
return val;
int main() {
cout << "Total height in centimeters: " << height << " cm" << endl;
return 0;
In C++, an abstract class is a class that cannot be instantiated directly. It is used to provide a base for
other classes.
A class becomes abstract when it has at least one pure virtual function.
Key Concepts
We will:
In C++, constructors and destructors are special member functions of a class used to manage object
initialization and cleanup, respectively.
Constructor in C++-A constructor is a special function that is automatically called when an object of a
class is created.
Destructor in C++
A destructor is a special function that is automatically called when an object goes out of scope or is
deleted.
Uses:• To free resources (like memory, files, etc.) that were allocated during the object’s lifetime.
A pure virtual function is a virtual function that has no definition in the base class and is declared by
assigning = 0.
A class containing at least one pure virtual function becomes an abstract class — it cannot be
instantiated.
Key Features:
#include <iostream>
int main() {
ofstream outFile("ABC.txt");
if (!inFile) {
cout << "Error: Unable to open XYZ.txt for reading!" << endl;
return 1;
if (!outFile) {
cout << "Error: Unable to open ABC.txt for writing!" << endl;
return 1;
string line;
inFile.close();
outFile.close();
cout << "Data copied successfully from XYZ.txt to ABC.txt." << endl;
return 0;