Object-Oriented Programming (OOP) in C++ Sample Paper
Object-Oriented Programming (OOP) in C++ Sample Paper
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 };
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.
/*
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);
*/