Resolution
Object-Oriented Programming At compile time
At run time
(OOP) in C++ Flexibility
Less
More
This is just a Sample Question Paper along with Sample Outline
Answers, Real Answers will be larger and more descriptive. Compile-Time Example:
void print(int i) { cout << "Int: " << i; }
Q1: What are the main concepts of Object-Oriented Programming void print(double d) { cout << "Double: " << d; }
(OOP) in C++? Explain with real-life examples and code. Run-Time Example:
Answer: OOP revolves around objects and classes. The main pillars are: class Base {
Encapsulation – Binding data and methods into a single unit (class). public:
Abstraction – Hiding complex details and showing only essential features. virtual void show() { cout << "Base"; }
Inheritance – Acquiring properties and behaviors from another class. };
Polymorphism – Many forms: function/operator overloading (compile-time) class Derived : public Base {
and virtual functions (run-time). void show() override { cout << "Derived"; }
Real-Life Analogy: A class Car defines structure and behavior. An object };
Tesla is a specific instance.
Code Example: Q3: Explain Inheritance in C++ and its types with examples.
class Car { Answer: Inheritance enables code reuse and establishes a parent-child
string brand; relationship between classes.
public: Types:
Car(string b) : brand(b) {} Single – One base, one derived.
void show() { cout << "Brand: " << brand << endl; } Multilevel – Derived from a derived.
}; Multiple – Derived from multiple bases.
Hierarchical – Multiple derived from one base.
int main() { Hybrid – Combination of above.
Car myCar("Tesla"); Example:
myCar.show(); class Animal {
} public:
void sound() { cout << "Animal sound"; }
Q2: Differentiate between Compile-time and Run-time Polymorphism };
with examples. class Dog : public Animal {
Answer: void bark() { cout << "Bark!"; }
Feature };
Compile-Time
Run-Time Q4: What are constructors and destructors in C++? Write a program
Mechanism using all types of constructors.
Function/Operator Overloading Answer:
Virtual Functions Constructor: Initializes objects.
Destructor: Cleans up resources.
Types: class Circle : public Shape {
Default public:
Parameterized void draw() override { cout << "Drawing Circle"; }
Copy Constructor };
Example:
class Student { Q7: How is file handling done in C++? Write a program to read and
string name; write a file.
int age; Answer:
public: Use <fstream> with ifstream, ofstream, or fstream.
Student() : name("Unknown"), age(0) {} // Default Example:
Student(string n, int a) : name(n), age(a) {} // Parameterized #include <fstream>
Student(const Student &s) : name(s.name), age(s.age) {} // Copy using namespace std;
~Student() { cout << "Destroying " << name; } int main() {
}; ofstream out("data.txt");
out << "Hello File!";
Q5: What is operator overloading? Illustrate with an example of out.close();
overloading the '+' operator.
Answer: Operator overloading allows custom behavior for operators when ifstream in("data.txt");
used with user-defined types. string content;
Example: getline(in, content);
class Complex { cout << content;
int real, imag; in.close();
public: return 0;
Complex(int r = 0, int i = 0) : real(r), imag(i) {} }
Complex operator+(const Complex &c) { Q8: Write a program to define a Student class with roll number, name,
return Complex(real + c.real, imag + c.imag); and three subject marks. Calculate and display the percentage.
} Answer:
}; class Student {
string name;
Q6: Explain virtual functions and abstract classes with examples. int roll, m1, m2, m3;
Answer: public:
Virtual Function: Enables dynamic dispatch. Student(int r, string n, int x, int y, int z) : roll(r), name(n), m1(x), m2(y),
Abstract Class: Contains at least one pure virtual function. m3(z) {}
Example: void display() {
class Shape { float percent = (m1 + m2 + m3) / 3.0;
public: cout << "Roll: " << roll << "\nName: " << name << "\n%: " << percent
virtual void draw() = 0; // Pure virtual << "%\n";
}; }
}; cout << "Non-positive";
}
Q9: What is Encapsulation and Data Hiding? How does C++ support
it? switch-case:
Answer: int choice = 2;
Encapsulation: Bundling data and methods into one unit. switch(choice) {
Data Hiding: Restricting access to internal data. case 1: cout << "One"; break;
C++ supports it via access specifiers: private, protected, and public. case 2: cout << "Two"; break;
Example: default: cout << "Other";
class Account { }
private:
double balance; Ternary Operator:
public: int a = 5, b = 10;
void setBalance(double b) { balance = b; } int max = (a > b) ? a : b;
double getBalance() const { return balance; } Iterative Statements: for, while, do-while.
}; for(int i = 0; i < 5; i++) cout << i << " ";
Q10: Differentiate between Function Overloading and Operator Q12. Write a C++ Program to Calculate Factorial Using Recursion.
Overloading. Answer:
Answer: int factorial(int n) {
Feature if(n <= 1) return 1;
Function Overloading return n * factorial(n - 1);
Operator Overloading }
Purpose
Multiple functions with same name Q13. Write a C++ Program to Calculate Total Number of Handshakes
Custom behavior for operators Using Recursion.
Based On Answer:
Parameter list Handshakes formula: n(n-1)/2
Operator behavior int handshake(int n) {
Example if(n <= 1) return 0;
void sum(int), sum(float) return (n - 1) + handshake(n - 1);
Complex operator+(Complex) }
Input: 4 → Output: 6
Q11. Explain Different Types of Conditional Statements in C++.
Answer: C++ supports the following conditional and iterative constructs: Q14. Check if a Number is Palindrome.
Answer:
if-else Statement: int num, rev = 0, temp;
int num = 10; cin >> num;
if(num > 0) { temp = num;
cout << "Positive"; while(temp > 0) {
} else { rev = rev * 10 + temp % 10;
temp /= 10; void deposit(double amt) { balance += amt; }
} void withdraw(double amt) { if(amt <= balance) balance -= amt; }
if(rev == num) cout << "Palindrome"; void display() { cout << owner << " Balance: " << balance; }
else cout << "Not a Palindrome"; };
Q15. Polymorphism: Function Overloading & Function Overriding Q21. Time Class to Add Two Times
Function Overloading: class Time {
int add(int a, int b) { return a + b; } int h, m, s;
double add(double a, double b) { return a + b; } public:
Function Overriding: void input() { cin >> h >> m >> s; }
class Base { public: void show() { cout << "Base"; } }; void display() { cout << h << ":" << m << ":" << s; }
class Derived : public Base { public: void show() { cout << "Derived"; } }; Time add(Time t) {
Time res;
Q16. Explain Function and its Types. res.s = s + t.s; res.m = m + t.m + res.s / 60;
Answer: Types: res.s %= 60; res.h = h + t.h + res.m / 60;
Built-in (e.g., main(), pow(), sqrt()) res.m %= 60;
User-defined return res;
Recursive }
int sum(int a, int b) { return a + b; } // User-defined };
Q17. Program to Get Length of String Q22. Rectangle Constructor Overloading
string s; class Rectangle {
getline(cin, s); int l, w;
cout << "Length: " << s.length(); public:
Rectangle() : l(1), w(1) {}
Q18. string Header Functions: Rectangle(int a, int b) : l(a), w(b) {}
string a = "Hello", b = "World"; Rectangle(const Rectangle &r) : l(r.l), w(r.w) {}
a.append(" ").append(b); int area() { return l * w; }
cout << a.compare(b); };
cout << strlen(a.c_str());
char c[20]; strcpy(c, a.c_str()); Q23. Hierarchical Inheritance
getline(cin, a); class Animal { public: void sound() { cout << "Sound"; } };
class Dog : public Animal { public: void bark() { cout << "Bark"; } };
Q20. BankAccount Class class Cat : public Animal { public: void meow() { cout << "Meow"; } };
class BankAccount {
int accNum; Q24. Operator Overloading:
double balance; + for Complex Numbers
string owner; class Complex {
public: int real, imag;
BankAccount(int num, string name) : accNum(num), owner(name), public:
balance(0) {} Complex(int r = 0, int i = 0): real(r), imag(i) {}
Complex operator + (Complex c) { public:
return Complex(real + c.real, imag + c.imag); Point(int a, int b): x(a), y(b) {}
} Point operator+(Point p) { return Point(x + p.x, y + p.y); }
}; };
Q25. File Handling: Student Records Q30. Virtual vs Pure Virtual Functions
class Student { class Base { public: virtual void show() { cout << "Base"; } };
char name[20]; int roll; float marks; class Abstract { public: virtual void show() = 0; };
public: Use case: Interfaces and extensibility.
void input() { cin >> name >> roll >> marks; }
void show() { cout << name << roll << marks; } Q31. Function Overloading vs Overriding
}; Aspect
ofstream fout("students.dat", ios::binary); Overloading
Student s; s.input(); fout.write((char*)&s, sizeof(s)); fout.close(); Overriding
Parameters
Q26. Define Polymorphism: Compile-time vs Run-time Different
Type Same
Compile-time Scope
Run-time Same class
Mechanism Base-Derived classes
Function/Operator Overload
Virtual Functions Q32. Polymorphism Real-World Example
Binding class Employee { public: virtual void work() = 0; };
Early class Manager : public Employee { void work() { cout << "Manage"; } };
Late (vtable) class Developer : public Employee { void work() { cout << "Code"; } };
Flexibility
Less Q33. Inheritance Types with Example
More Single, Multilevel, Multiple, Hierarchical, Hybrid Diagrams + Code for each
(See Q23, Q38, Q36)
Q27. Run-time Polymorphism with Virtual Function
class Shape { public: virtual void draw() { cout << "Shape"; } }; Q34. Base and Derived Classes with Access Specifiers
class Circle : public Shape { public: void draw() override { cout << "Circle"; } class Base { protected: int a; };
}; class Derived : public Base { public: void show() { cout << a; } };
class Rectangle : public Shape { public: void draw() override { cout <<
"Rectangle"; } }; Q35. Constructor and Destructor in Inheritance Constructor:
Shape* s = new Circle(); s->draw(); Base → Derived Destructor: Derived → Base
class Base { public: Base() { cout << "Base"; } ~Base() { cout << "~Base"; }
Q29. Operator Overloading Demonstrating Compile-time };
Polymorphism class Derived : public Base { public: Derived() { cout << "Derived"; }
class Point { ~Derived() { cout << "~Derived"; } };
int x, y;
Q36. Multiple Inheritance & Ambiguity
class A { public: void show() { cout << "A"; } };
class B { public: void show() { cout << "B"; } }; // Task: Check whether a number is a palindrome
class C : public A, public B { #include <iostream>
void show() { A::show(); } // Resolving ambiguity using namespace std;
};
int main() {
Q37. Diamond Problem & Virtual Inheritance int num, original, reversed = 0, remainder;
class A { public: int x; }; cout << "Enter a number: ";
class B : virtual public A { }; cin >> num;
class C : virtual public A { };
class D : public B, public C { }; if (num < 0) {
D obj; obj.x = 10; // No ambiguity cout << "Not a Palindrome";
return 0;
Q38. Multilevel Inheritance }
class A { public: void showA() { cout << "A"; } };
class B : public A { public: void showB() { cout << "B"; } }; original = num;
class C : public B { public: void showC() { showA(); showB(); cout << "C"; } while (num != 0) {
}; remainder = num % 10;
reversed = reversed * 10 + remainder;
Q39. Composition vs Inheritance Inheritance: num /= 10;
IS-A (Car is a Vehicle) Composition: HAS-A (Car has an Engine) }
class Engine { };
class Car { Engine e; }; // Composition if (original == reversed)
class Vehicle { }; cout << "Palindrome";
class Car2 : public Vehicle { }; // Inheritance else
cout << "Not a Palindrome";
return 0;
}
/*
Test Cases:
Input: 151 -> Output: Palindrome
Input: 153 -> Output: Not a Palindrome
Input: 1881 -> Output: Palindrome
*/
Conditional Statements:
1. if-else:
if (condition) {
// code if true return (n * (n - 1)) / 2;
} else { }
// code if false
} int main() {
int n;
Example: cout << "Enter number of participants: ";
int a = 500; cin >> n;
if (a > 0) if (n < 2) {
cout << "Positive"; cout << "Invalid input";
else return 0;
cout << "Non-positive"; }
cout << "Total handshakes: " << handshakes(n);
2. switch-case: return 0;
switch (value) { }
case 1: cout << "One"; break;
case 2: cout << "Two"; break; /*
default: cout << "Other"; Test Cases:
} Input: 2 -> Output: 1
Input: 3 -> Output: 3
3. Ternary Operator: Input: 4 -> Output: 6
(condition) ? expr1 : expr2; */
Example:
int a = 10, b = 20;
cout << ((a > b) ? a : b); Functions:
- A block of code that performs a task
4. Iterative Statements: Types:
for, while, do-while 1. Built-in functions (e.g., cout, cin)
Example: 2. User-defined functions:
for (int i = 0; i < 5; i++) cout << i;
*/ Example:
int add(int a, int b) {
Task: Recursive Handshake Counter return a + b;
#include <iostream> }
using namespace std;
int main() {
int factorial(int n) { cout << add(3, 4); // Output: 7
if (n <= 1) return 1; return 0;
return n * factorial(n - 1); }
} */
int handshakes(int n) {
// Remove smallest number from array Polymorphism:
#include <iostream> 1. Function Overloading:
using namespace std; Multiple functions with same name but different parameters.
int main() { Example:
int n; int add(int a, int b) { return a + b; }
cout << "Enter number of values: "; double add(double a, double b) { return a + b; }
cin >> n;
2. Function Overriding:
if (n == 0) { Derived class redefines base class method.
cout << "No values";
return 0; class Base {
} public:
virtual void show() { cout << "Base"; }
int arr[n]; };
cout << "Enter values sizes: "; class Derived : public Base {
for (int i = 0; i < n; i++) cin >> arr[i]; public:
void show() override { cout << "Derived"; }
int minIndex = 0; };
for (int i = 1; i < n; i++) */
if (arr[i] < arr[minIndex])
minIndex = i; // Count message length
#include <iostream>
for (int i = minIndex; i < n - 1; i++) #include <string>
arr[i] = arr[i + 1]; using namespace std;
for (int i = 0; i < n - 1; i++) int main() {
cout << arr[i] << " "; string message;
cout << "Enter message: ";
return 0; getline(cin, message);
} cout << "Length: " << message.length();
return 0;
/* }
Test Cases:
Input: 4, 4 2 8 1 -> Output: 4 2 8 /*
Input: 5, 12 5 9 3 7 -> Output: 12 5 9 7 Test Cases:
Input: 0 -> Output: No values "Hello, World!" -> 13
*/ "Java is hot" -> 11
"Secret Santa 457" -> 17
// Q(b) "" -> 0
/* " " -> 1
*/
/*
String Functions:
1. compare():
string a = "abc", b = "abd";
a.compare(b); // returns < 0
2. strcpy():
char dest[20];
strcpy(dest, "hello");
3. strlen():
strlen("hello"); // returns 5
4. append():
string s = "Hi";
s.append(" there");
5. getline():
string input;
getline(cin, input);
*/