0% found this document useful (0 votes)
2 views6 pages

Oops Cheats

The document covers various C++ programming concepts including function and class templates, recursion, inheritance, user-defined literals, abstract classes, constructors, and destructors. It provides examples of adding different data types using templates, implementing recursion for factorial, and demonstrating inheritance types. Additionally, it discusses file handling for reading from and writing to text files.

Uploaded by

system123420
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Oops Cheats

The document covers various C++ programming concepts including function and class templates, recursion, inheritance, user-defined literals, abstract classes, constructors, and destructors. It provides examples of adding different data types using templates, implementing recursion for factorial, and demonstrating inheritance types. Additionally, it discusses file handling for reading from and writing to text files.

Uploaded by

system123420
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Unit4 #include <iostream>

using namespace std;

// Function template to add two variables

template <typename T>

T add(T a, T b) {

return a + b;

}int main() {

// Adding integers

int int1 = 10, int2 = 20;

cout << "Sum of integers: " << add(int1, int2) << endl;

// Adding floats

float float1 = 5.5, float2 = 2.3;

cout << "Sum of floats: " << add(float1, float2) << endl;

// Adding doubles

double double1 = 3.1415, double2 = 2.7182;

cout << "Sum of doubles: " << add(double1, double2) << endl;

// Adding strings

string str1 = "Hello ", str2 = "World!";

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:

Uses of Class Template and Function Template

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).

• Reduces code duplication.


• Useful for generic operations where type is not important at compile time.

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:

• To create generic data structures (like Stack, Queue, LinkedList).

• Helps in creating type-safe container classes.

Encourages code reuse and cleaner design

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

Conditions for Recursion

To avoid infinite recursion, two key conditions must be met:

1. Base Condition

• It stops the recursion.

• 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.

Different Ways to Call a Function in C++

In C++, functions can be called in the following ways:

1. Call by Value

• A copy of the variable is passed.

2. Call by Reference
• The original variable is passed using reference (&).

3. Call by Pointer

• The address of the variable is passed using pointers (*).

Example Program: Using Recursion and Function Call Types

We will:• Write a recursive function for factorial.

Use a simple function to demonstrate call by value and call by reference.

Unit2What is Inheritance in C++?

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).

It promotes code reusability, extensibility, and maintainability.

Types of Inheritance in C++

1. Single Inheritance• One base class → One derived class

2. Multiple Inheritance• Two or more base classes → One derived class

3. Multilevel Inheritance• A class is derived from a derived class

4. Hierarchical Inheritance• Multiple classes inherit from a single base class

5. Hybrid Inheritance• Combination of two or more types of inheritance

Multiple Inheritance Example:

We will create a program using multiple inheritance to calculate the:

• Area of the surface of a cubic box

• Volume of a cubic box

Formula:• Area = 6 × side²

• 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).

This is done by overloading the operator"" function.

Example: User-Defined Literal for Distance Conversion

Let’s write a program that:

• Uses 1.0_m to represent meters

• Uses 10.0_cm to represent centimeters


Converts everything to centimeters using user-defined literal functions

#include <iostream>

using namespace std;

// User-defined literal for meters -> returns value in centimeters

long double operator"" _m(long double val) {

return val * 100; // convert meters to centimeters

// User-defined literal for centimeters

long double operator"" _cm(long double val) {

return val;

int main() {

long double height = 1.75_m + 25.0_cm; // 1.75 meters + 25 centimeters

cout << "Total height in centimeters: " << height << " cm" << endl;

return 0;

What is an Abstract Class and Abstract Method in C++?

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

Abstract Class:• Contains one or more pure virtual functions.

• Used for inheritance and polymorphism.

• Cannot create objects of abstract classes.

Pure Virtual Function:• A function declared with = 0 in the base class.

Forces derived classes to implement this function.

Program: Calculate Area of Cube Using Abstract Class

We will:

• Create an abstract class Shape with a pure virtual function calculateArea().

• Inherit it in class Cube and implement the area calculation.


• Area of cube = 6 × side²

What is the Use of Constructor and Destructor in C++?

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.

Uses:To initialize data members of a class.

• Automatically sets up the object when it is created.

• Can be overloaded to allow different ways to construct an object.

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.

Ensures clean-up before the object is destroyed.

2. Pure Virtual Function

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:

• Declared in the base class without a body.

• Forces derived classes to override the function.

Makes the class abstract.

Xyz and abctxt

#include <iostream>

#include <fstream> // For file handling

#include <string> // For string operations

using namespace std;

int main() {

// Input file stream to read from XYZ.txt


ifstream inFile("XYZ.txt")

// Output file stream to write to ABC.txt

ofstream outFile("ABC.txt");

// Check if input file opened successfully

if (!inFile) {

cout << "Error: Unable to open XYZ.txt for reading!" << endl;

return 1;

// Check if output file opened successfully

if (!outFile) {

cout << "Error: Unable to open ABC.txt for writing!" << endl;

return 1;

string line;

// Read line-by-line from XYZ.txt and write to ABC.txt

while (getline(inFile, line)) {

outFile << line << endl;

// Close the file streams

inFile.close();

outFile.close();

cout << "Data copied successfully from XYZ.txt to ABC.txt." << endl;

return 0;

You might also like