BSc CSIT Second Semester OOP: Important Chapter-Wise Questions

GYAN WALLA
26 minute read
0

 



BSc CSIT Second Semester OOP: Important Chapter-Wise Questions

Welcome, Second Semester BSc CSIT students! Preparing for your OOP exam requires understanding key concepts and practicing relevant problems. To help you focus your studies, we've compiled a list of important questions based on past papers and the official syllabus, categorized chapter-wise. You can use this guide to test your knowledge and prepare effectively for your examinations.



📘 BSc CSIT – Object-Oriented Programming (OOP) Chapter-wise Past Questions

This post contains the past 5 years' questions of BSc CSIT OOP subject, organized chapter-wise according to the official syllabus. 


📘 Unit 1: Introduction to Object-Oriented Programming

1. Define object. What are the benefits of object-oriented programming Language?
Solution:
Object:
An object can be defined as a data field that has unique attributes and behaviors.
The benefits of object-oriented programming Language are 
Modularity: Code is divided into objects, making it easier to manage and understand.
Reusability: Classes and objects can be reused across programs using inheritance.
Scalability: OOP makes it easy to update or scale applications by adding new classes and features.
Maintainability: Since the code is modular, it is easier to fix bugs or make changes without affecting the entire system.
Data Hiding (Encapsulation): Internal details of objects are hidden from the outside world, exposing only the necessary parts.
Real-world Modeling: OOP is based on real-world objects, making programs more understandable and relatable.
Improved Productivity: Reusability and modularity speed up the development process.
Inheritance: It is a feature or a process in which new classes are created from an existing class.
Polymorphism: Polymorphism means "many forms." In OOP, it allows one function, method, or operator to behave differently based on the object it is acting upon.
Encapsulation: It is a way of hiding data in a single entity or unit and a way of protecting information from the outside world.


2. Describe the characteristics of an object-oriented programming language.
Solution:
The characteristics of an object-oriented programming language are 
Object: An object can be defined as a data field that has unique attributes and behaviors.
Class: A class represents a set of related objects.
Abstraction: It is a way of hiding unnecessary information.
Inheritance: It is a feature or a process in which new classes are created from an existing class.
Polymorphism: Polymorphism means "many forms." In OOP, it allows one function, method, or operator to behave differently based on the object it is acting upon.
Encapsulation: It is a way of hiding data in a single entity or unit and a way of protecting information from the outside world.

3. How does object-oriented programming differ from object-based programming? Discuss the benefits of OOP.
Solution:

The benefits of object-oriented programming Language are 
Modularity: Code is divided into objects, making it easier to manage and understand.
Reusability: Classes and objects can be reused across programs using inheritance.
Scalability: OOP makes it easy to update or scale applications by adding new classes and features.
Maintainability: Since the code is modular, it is easier to fix bugs or make changes without affecting the entire system.
Data Hiding (Encapsulation): Internal details of objects are hidden from the outside world, exposing only the necessary parts.
Real-world Modeling: OOP is based on real-world objects, making programs more understandable and relatable.
Improved Productivity: Reusability and modularity speed up the development process.
Inheritance: It is a feature or a process in which new classes are created from an existing class.
Polymorphism: Polymorphism means "many forms." In OOP, it allows one function, method, or operator to behave differently based on the object it is acting upon.
Encapsulation: It is a way of hiding data in a single entity or unit and a way of protecting information from the outside world.
4. What is structured programming? Discuss characteristics and problems associated with structured programming.
Solution:
Structured programming is a programming pattern that gives special importance to breaking down a program into smaller, manageable, and logical units or modules.

Characteristics of Structured Programming:
-Top-down design: The program is broken into small, manageable modules or functions.
-Use of control structures only: Focuses on if-else, loops, and function calls instead of goto.
-Modular approach: Code is divided into functions or procedures for better organization.
-Improved readability and maintainability
-Single entry and single exit in each block of code

Problems with Structured Programming:
-No Object Reusability: Lacks inheritance and object reuse.
-Not suitable for large applications: Managing thousands of functions without classes/objects can be messy.
-Less real-world modeling: Hard to represent real-world entities compared to OOP.
-Poor scalability: Difficult to extend and modify as complexity grows.

Some important topics:











📘 Unit 2: Basics of C++ Programming 

1. Explain the significance of type conversion. How do we achieve dynamic memory allocation in C++? Explain with an example.

Solution:

Type conversion is the process of changing one data type to another, such as converting an int to a float or vice versa.

Importance / Significance:

- Ensures compatibility between different data types during operations.

- Prevents data loss or errors during calculations.

- Helps in type-safe operations in mixed expressions.

- Improves code readability and maintainability by making type intentions clear.

Types of Type Conversion in C++:

a) Implicit Conversion: Done automatically by the compiler. (e.g., int to float)

b) Explicit Conversion (Casting): Done manually by the programmer using cast syntax. (e.g., (float)num)

Dynamic Memory Allocation:  The process of allocating memory during runtime using pointers.

Keyword use in Dynamic Memory Allocation. 

new:  Allocates memory on the heap

delete: Deallocates memory from the heap

 Example 

 #include<iostream>

using namespace std;

int main()

{

    int size;

    cout << "Enter how many numbers you want to enter \n ";

    cin>>size;

    int *ptr;

    ptr=new int[size];

    for(int i=0;i<size;i++)

    {

    cout<<"enter"<<i+1<<"number\n";

    cin>>ptr[i];

    }

  for(int i=0;i<size;i++)

    {

    cout<<"entered"<<i+1<<"number\n";

    cout<<ptr[i]<<endl;

    }

delete (ptr);

}


Why Use Dynamic Memory?

-When the size of the data is unknown at compile time

-To create flexible data structures like linked lists, trees, etc.

-More efficient memory use in large program

2. What is meant by type conversion? Define two ways of converting one user-defined data type (object) to another.


Type conversion is the process of changing one data type to another, such as when operations involve different data types or when custom objects (user-defined types) need to interact.

Two Ways of Converting One User-Defined Data Type (Object) to Another in C++:

1. Conversion through Constructor (Single-Argument Constructor):

This allows a class to accept an object of another class and convert it.

🔹 Use Case: Target class has a constructor that accepts the source class object.

Example:

#include <iostream>

using namespace std;

class A {

public:

    int x;

    A(int val) { x = val; }

};

class B {

public:

    int y;

    // Constructor that accepts an object of A

    B(A obj) {

        y = obj.x;

    }

void show() {

        cout << "Value in B: " << y << endl;

    }

};

int main() {

    A a1(5);

    B b1 = a1;  // A --> B conversion using constructor

    b1.show();

    return 0;

}

2. Conversion through Operator Overloading (Type Conversion Operator):

We define a type-cast operator inside the class to convert one object type to another.

🔹 Use Case: Source class defines a conversion function to return the target type.

Example:

#include <iostream>

using namespace std;

class B; // Forward declaration

class A {

public:

    int x;

    A(int val) { x = val; }

// Type conversion function from A to B

    operator B();

};

class B {

public:

    int y;

void show() {

        cout << "Value in B: " << y << endl; 

    }

};

// Define the conversion

A::operator B() {

    B temp;

    temp.y = x;

    return temp;

}

int main() {

    A a1(10);

    B b1 = a1;  // A --> B conversion using operator overloading

    b1.show();

    return 0;

}

3. How dynamic memory allocation is done using new and delete? Write a program illustrating the use of new and delete.

Solution:

Dynamic Memory Allocation:  The process of allocating memory during runtime using pointers.

Keyword use in Dynamic Memory Allocation. 

new:  Allocates memory on the heap

delete: Deallocates memory from the heap

 Example 

#include<iostream>

using namespace std;

int main()

{   int size;

 cout << "Enter how many numbers you want to enter \n ";

cin>>size;

int *ptr;

ptr=new int[size];

 for(int i=0;i<size;i++){

    cout<<"enter"<<i+1<<"number\n";

cin>>ptr[i];

    }for(int i=0;i<size;i++)

    {cout<<"entered"<<i+1<<"number\n";

 cout<<ptr[i]<<endl;

    }delete (ptr);}

Why Use Dynamic Memory?

-When the size of the data is unknown at compile time

-To create flexible data structures like linked lists, trees, etc.

-More efficient memory use in a large program. 

4. What is meant by pass-by-reference? How can we pass arguments by reference using reference variables? Illustrate with an example.

Solution:

Pass-by-reference means passing the actual address (reference) of a variable to a function, so that any changes made inside the function affect the original variable.

We can pass arguments using reference variables by using the '&'  symbol in the function parameters.

Example 

#include <iostream>

using namespace std;

void swap(int &a, int &b) { // Pass-by-reference using &

    int temp = a;

    a = b;

    b = temp;

}

int main() {

    int x = 5, y = 10;

    cout << "Before swap: x = " << x << ", y = " << y << endl;

    swap(x, y);  // Actual variables are passed

    cout << "After swap: x = " << x << ", y = " << y << endl;

    return 0;

}

5. Define class and object with suitable examples. How can members of the class be accessed?

Solution:

Class:

- A class is a user-defined data type in C++ that acts as a blueprint for creating objects.

- A class represents a set of related objects.

Object:

- An object can be defined as a data field that has unique attributes and behaviors.

- An object is a real-world instance of a class.

#include<iostream>

using namespace std;

class Student {         // Class definition

public:

    int roll;           // Data member

    string name;

    void display() {    // Member function

        cout << "Roll: " << roll << ", Name: " << name << endl;

    }

};

int main() {

    Student s1;         // Object creation

    // Accessing members using dot operator

    s1.roll = 101;

    s1.name = "Madhav";

    s1.display();       // Calling member function

    return 0;

}

Members of the Class Can Be Accessed by using 

Using the dot (.) operator with the object.

Syntax: objectName.memberName

6. What is an inline function? Why is it used? Write a program to illustrate the inline function. 

Solution:

-An inline function is a function in C++ where the code is expanded at the point of function call, instead of jumping to the function definition. 

-It is declared using the keyword inline.

It is used for the following reason:

-To reduce function call overhead (especially for small functions).

-To increase execution speed.

-Useful when a function is called many times.

Example:

#include<iostream>

using namespace std;

inline product(int a,int b)

{

    return a*b;

}

int main()

{

    int a;

    int b;

    cout<<"enter first number\n";

    cin>>a;

    cout<<"enter second number\n";

    cin>>b;

    cout<<"the product is "<<product(a,b)<<endl;

}

7. What is the principal reason for using default arguments? Explain how missing and default arguments are handled.

Solution:

Default arguments in C++ allow us to assign default values to function parameters so that a function can be called with fewer arguments.

Example:

void greet(string name = "User") {

    cout << "Hello, " << name << "!" << endl;

}

The principal reason for using default arguments is 

-To make functions more flexible and easier to use by allowing optional arguments.

-To reduce function overloading.

Missing and Default Arguments Are Handled:

-If an argument is provided, it is used.

-If an argument is missing, the default value is used.

-Default arguments are always filled from right to left.

Example:

#include<iostream>

using namespace std;

void display(string name = "Guest", int age = 18) {

    cout << "Name: " << name << ", Age: " << age << endl;

}

   int main() {

    display();                   // Uses both default arguments

    display("Madhav");           // Uses default age

    display("Madhav", 20);       // Uses provided name and age

    return 0;

}

8. Define and write syntax for default arguments. Write a program to display N number of characters using default arguments.

Solution:

Default arguments in C++ allow us to assign default values to function parameters so that a function can be called with fewer arguments. 

Syntax:

return_type function_name(parameter1 = default_value1, parameter2 = default_value2, ...);

Program to display N number of characters using default arguments:

#include<iostream>

using namespace std;

// Function with default arguments

void displayChars(char ch = '*', int n = 5) {

    for(int i = 0; i < n; i++) {

        cout << ch << " ";

    }

    cout << endl;

}

int main() {

    displayChars();              // Uses default: * 5 times

    displayChars('#');           // Uses default count: # 5 times

    displayChars('$', 3);        // Uses both provided values: $ 3 times

    return 0;

}

9. Create a function called swaps() that interchanges values using a function template. Write a main to test with multiple types.

Solution:

#include<iostream>

using namespace std;

// Function template

template <typename T>

void swaps(T &a, T &b) {

    T temp = a;

    a = b;

    b = temp;

}

int main() {

    int x = 10, y = 20;

    float f1 = 1.5, f2 = 2.5;

    char c1 = 'A', c2 = 'B';

     cout << "Before swap - Integers: x = " << x << ", y = " << y << endl;

    swaps(x, y);

    cout << "After swap  - Integers: x = " << x << ", y = " << y << endl;

    cout << "\nBefore swap - Floats: f1 = " << f1 << ", f2 = " << f2 << endl;

    swaps(f1, f2);

    cout << "After swap  - Floats: f1 = " << f1 << ", f2 = " << f2 << endl;

    cout << "\nBefore swap - Characters: c1 = " << c1 << ", c2 = " << c2 << endl;

    swaps(c1, c2);

    cout << "After swap  - Characters: c1 = " << c1 << ", c2 = " << c2 << endl;

    return 0;

}

10. Write a member function reverseit() That reverses a string.

Solution:

#include<iostream>

using namespace std;

class MyString {

public:

    string str;

    void reverseit() {

        int n = str.length();

        for(int i = 0; i < n / 2; i++) {

            char temp = str[i];

            str[i] = str[n - i - 1];

            str[n - i - 1] = temp;

        }

    }

};

int main() {

    MyString s;

    cout << "Enter a string: ";

    cin >> s.str;

    s.reverseit();

   cout << "Reversed string: " << s.str << endl;

    return 0;

}

Some other topics:

Manipulator: It is a special function that can be used to control the format of the input and output stream.

Reference variable: A reference variable provides an alternative name for a variable that is previously defined.




📘 Unit 3: Classes and Objects

1. What is a constructor? Why is it needed in a class? Illustrate types of constructors with examples.
Solution:
Constructor: A constructor is a special member function of a class that is automatically called when an object is created.
A constructor is needed in a class for the following reasons:
-To automatically initialize data members when an object is created.
-Saves time by avoiding manual initialization.
-Supports overloading and flexible object creation.
Constructor types:
a. Default Constructor    
-No parameters                 
 b. Parameterized Constructor 
-Takes arguments to initialize different objects with different values.
c. Copy Constructor
-Creates a new object by copying values from another existing object.

Example:
#include <iostream>
using namespace std;

class Add {
private:
    int a, b;

public:
    // 1. Default Constructor
    Add() {
        a = 0;
        b = 0;
        cout << "Default Constructor called." << endl;
    }

    // 2. Parameterized Constructor
    Add(int x, int y) {
        a = x;
        b = y;
        cout << "Parameterized Constructor called." << endl;
    }

    // 3. Copy Constructor
    Add(const Add &obj) {
        a = obj.a;
        b = obj.b;
        cout << "Copy Constructor called." << endl;
    }

    // Method to display the sum
    void displaySum() {
        cout << "Sum = " << a + b << endl;
    }
};

int main() {
    // Using default constructor
    Add obj1;
    obj1.displaySum();

    // Using parameterized constructor
    Add obj2(10, 20);
    obj2.displaySum();

    // Using copy constructor
    Add obj3 = obj2;
    obj3.displaySum();

    return 0;
}
2. What is the use of a constructor and a destructor? Write a program illustrating default, parameterized, and copy constructors.
Solution:
Constructor: A constructor is a special member function of a class that is automatically called when an object is created.
Purpose:
-To initialize objects of a class.
-To set default values or custom values to data members.
Destructor:
A destructor is a special member function of a class that is automatically called when an object is destroyed.
Purpose:
-To perform cleanup operations, like releasing memory, closing files, and freeing resources.

Example:
#include <iostream>
using namespace std;

class Add {
private:
    int a, b;

public:
    // 1. Default Constructor
    Add() {
        a = 0;
        b = 0;
        cout << "Default Constructor called." << endl;
    }

    // 2. Parameterized Constructor
    Add(int x, int y) {
        a = x;
        b = y;
        cout << "Parameterized Constructor called." << endl;
    }

    // 3. Copy Constructor
    Add(const Add &obj) {
        a = obj.a;
        b = obj.b;
        cout << "Copy Constructor called." << endl;
    }

    // Method to display sum
    void displaySum() {
        cout << "Sum = " << a + b << endl;
    }
};

int main() {
    // Using default constructor
    Add obj1;
    obj1.displaySum();

    // Using parameterized constructor
    Add obj2(10, 20);
    obj2.displaySum();

    // Using copy constructor
    Add obj3 = obj2;
    obj3.displaySum();

    return 0;
}
3. What is a destructor? List its characteristics. Explain the use of the default copy constructor with an example.
Solution:
Destructor:
A destructor is a special member function of a class that is automatically called when an object is destroyed.
Characteristics of Destructor:
-Same name as the class, but preceded with a tilde ~.
-No return type, not even void.
-No parameters (i.e., cannot be overloaded).
-Called automatically when an object is destroyed.
-Defined only once per class.
-Used to perform cleanup operations before object deletion.
-If not defined by the programmer, the compiler provides a default destructor.

A copy constructor is used to create a new object as a copy of an existing object.

Example:
#include <iostream>
using namespace std;

class Add {
private:
    int a, b;

public:
    // 1. Default Constructor
    Add() {
        a = 0;
        b = 0;
        cout << "Default Constructor called." << endl;
    }

    // 2. Parameterized Constructor
    Add(int x, int y) {
        a = x;
        b = y;
        cout << "Parameterized Constructor called." << endl;
    }

    // 3. Copy Constructor
    Add(const Add &obj) {
        a = obj.a;
        b = obj.b;
        cout << "Copy Constructor called." << endl;
    }

    // Method to display sum
    void displaySum() {
        cout << "Sum = " << a + b << endl;
    }
};

int main() {
    // Using default constructor
    Add obj1;
    obj1.displaySum();

    // Using parameterized constructor
    Add obj2(10, 20);
    obj2.displaySum();

    // Using copy constructor
    Add obj3 = obj2;
    obj3.displaySum();

    return 0;
}
4. What is a destructor? Write a program to show a destructor call (e.g., "memory is released").
Solution:
Destructor:
A destructor is a special member function of a class that is automatically called when an object is destroyed.

#include <iostream>
using namespace std;

class Memory {
public:
    Memory() {
        cout << "Constructor called: Memory allocated!" << endl;
    }
    ~Memory() {
        cout << "Destructor called: Memory released!" << endl;
    }
};
int main() {
    {
        Memory obj;  // Constructor is called here  
       // Destructor is automatically called here when the object goes out of scope
    } 

    return 0;
}
5. Write a C++ program to display the number of objects created using static members.
Solution:
#include <iostream>
using namespace std;
class Counter {
private:
    static int count;  
public:
    Counter() {
        count++;  
        cout << "Object " << count << " created." << endl;
    }
       static void showCount() {
        cout << "Total objects created: " << count << endl;
    }
};
int Counter::count = 0;
int main() {
    Counter obj1;
    Counter obj2;
    Counter obj3;

    // Display total count
    Counter::showCount();

    return 0;
}
6. What is this pointer? How can we use it for name conflict resolution? Illustrate with an example.
Solution:
-The 'this' pointer is a C++ keyword that represents the current instance of a class.
-It is a hidden pointer passed as an implicit argument to all non-static member functions.
-It allows member functions to access and manipulate the data members of the objects on which the functions are being called.
#include<stdio.h>
#include<iostream>
#include<sting>
using namespace std;
class student
{
    private:
    string name;
    int id;
    public:
    student(string name,int id)
    {
        this->name=name;
        this->id=id;
        
    }
    void print()
    {
        cout<<"The given name is "<<name<<endl;
        cout<<"The given id is "<<id<<endl;
    }
};
int main()
{
    student obj1("Madhav",12);
    obj1.print();
    return 0;
    
}


7. Write short notes on: this pointer, Static data members.
this pointer:
-The 'this' pointer is a C++ keyword that represents the current instance of a class.
-It is a hidden pointer passed as an implicit argument to all non-static member functions.
-It points to the current object that invoked the member function.
-It allows member functions to access and manipulate the data members of the objects on which the functions are being called.
Syntax: this->member or (*this).member
Useful for:
Resolving naming conflicts.
Returning the current object from member functions for chaining.

 Static Data Members
-A static data member belongs to the class itself rather than to any particular object.
-There is only one copy of the static data member shared by all objects of the class.
-Used to keep class-wide information, e.g., counting how many objects are created.
-Declared inside the class with the keyword static.
-Must be defined outside the class (usually in the .cpp file).
-Can be accessed using the class name: ClassName::staticMember or through objects.


📘 Unit 4: Operator Overloading

 1. What is operator overloading? Why is it necessary? Write a program for the overloading comparison operator.
Solution:
Operator Overloading: The mechanism of giving an additional task to any operator without affecting its existing semantics.
Operator Overloading is necessary for the following reasons:
- Makes code more intuitive and readable.
- It improves runtime performance.
- Helps mimic natural expressions
Example:
#include<iostream>
using namespace std;

class Student {
public:
    int roll;

    Student(int r) {
        roll = r;
    }

    // Overloading == operator
    bool operator==(const Student &s) {
        return roll == s.roll;
    }
};

int main() {
    Student s1(101);
    Student s2(101);
    Student s3(202);

    if (s1 == s2)
        cout << "s1 and s2 are equal." << endl;
    else
        cout << "s1 and s2 are not equal." << endl;

    if (s1 == s3)
        cout << "s1 and s3 are equal." << endl;
    else
        cout << "s1 and s3 are not equal." << endl;

    return 0;
}
2. Write a program to overload the binary + operator using the friend function to add two heights.
Solution:
#include<iostream>
using namespace std;

class Height {
    int feet;
    int inches;

public:
    Height(int f = 0, int i = 0) {
        feet = f;
        inches = i;
    }

    // Friend function to overload +
    friend Height operator+(Height h1, Height h2);

    void display() {
        cout << feet << " feet " << inches << " inches" << endl;
    }
};

// Friend function definition
Height operator+(Height h1, Height h2) {
    Height temp;
    temp.feet = h1.feet + h2.feet;
    temp.inches = h1.inches + h2.inches;

    // Convert inches to feet if >= 12
    if (temp.inches >= 12) {
        temp.feet += temp.inches / 12;
        temp.inches = temp.inches % 12;
    }

    return temp;
}

int main() {
    Height h1(5, 8);   // 5 feet 8 inches
    Height h2(6, 7);   // 6 feet 7 inches

    Height h3 = h1 + h2;

    cout << "Height 1: ";
    h1.display();
    cout << "Height 2: ";
    h2.display();
    cout << "Total Height: ";
    h3.display();

    return 0;
}

3. Write a program that overloads extraction and insertion operators.
Solution:
#include<iostream>
using namespace std;

class Student {
    string name;
    int roll;

public:
    // Friend function to overload >>
    friend istream& operator>>(istream &in, Student &s);

    // Friend function to overload <<
    friend ostream& operator<<(ostream &out, const Student &s);
};

// Extraction operator (input)
istream& operator>>(istream &in, Student &s) {
    cout << "Enter name: ";
    in >> s.name;
    cout << "Enter roll number: ";
    in >> s.roll;
    return in;
}
// Insertion operator (output)
ostream& operator<<(ostream &out, const Student &s) {
    out << "Name: " << s.name << ", Roll: " << s.roll;
    return out;
}

int main() {
    Student s;
    cin >> s;      // Calls overloaded >>
    cout << s;     // Calls overloaded <<

    return 0;
}

4. Write a program for the overloading comparison operator. 
Solution:
#include<iostream>
using namespace std;

class Student {
public:
    int roll;

    Student(int r) {
        roll = r;
    }

    // Overloading == operator
    bool operator==(const Student &s) {
        return roll == s.roll;
    }
};

int main() {
    Student s1(101);
    Student s2(101);
    Student s3(202);

    if (s1 == s2)
        cout << "s1 and s2 are equal." << endl;
    else
        cout << "s1 and s2 are not equal." << endl;

    if (s1 == s3)
        cout << "s1 and s3 are equal." << endl;
    else
        cout << "s1 and s3 are not equal." << endl;

    return 0;
}
5. Explain the concept of operator overloading. List operators that cannot be overloaded.
Solution:
Operator Overloading: The mechanism of giving an additional task to any operator without affecting its existing semantics.
Operator Overloading is necessary for the following reasons:
- Makes code more intuitive and readable.
- It improves runtime performance.
- Helps mimic natural expressions



Example:
#include<iostream>
using namespace std;

class Student {
public:
    int roll;

    Student(int r) {
        roll = r;
    }

    // Overloading == operator
    bool operator==(const Student &s) {
        return roll == s.roll;
    }
};

int main() {
    Student s1(101);
    Student s2(101);
    Student s3(202);

    if (s1 == s2)
        cout << "s1 and s2 are equal." << endl;
    else
        cout << "s1 and s2 are not equal." << endl;

    if (s1 == s3)
        cout << "s1 and s3 are equal." << endl;
    else
        cout << "s1 and s3 are not equal." << endl;

    return 0;
}
The operator is not allowed to overload:
- Class members access operator(->)
- Scope resolution operator(::)
- Size operator (size of)
- Conditional operator(?)
- Dereference operator (*) in case of pointer 

6. Write a program to add two distance objects using member and friend functions.
Solution:
#include<iostream>
using namespace std;

class Distance {
    int feet, inches;

public:
    Distance(int f = 0, int i = 0) {
        feet = f;
        inches = i;
    }

    // Member function
    Distance add(Distance d) {
        int f = feet + d.feet;
        int i = inches + d.inches;
        if (i >= 12) {
            f += i / 12;
            i %= 12;
        }
        return Distance(f, i);
    }

    // Friend function
    friend Distance addFriend(Distance d1, Distance d2);

    void show() {
        cout << feet << " feet " << inches << " inches" << endl;
    }
};

// Friend function definition
Distance addFriend(Distance d1, Distance d2) {
    int f = d1.feet + d2.feet;
    int i = d1.inches + d2.inches;
    if (i >= 12) {
        f += i / 12;
        i %= 12;
    }
    return Distance(f, i);
}

int main() {
    Distance d1(4, 9), d2(5, 7);

    Distance result1 = d1.add(d2);            // Member function
    Distance result2 = addFriend(d1, d2);     // Friend function

    cout << "Using member function: ";
    result1.show();

    cout << "Using friend function: ";
    result2.show();

    return 0;
}

📘 Unit 5: Inheritance

1. Explain the practical implications of protected specifiers in inheritance. List the advantages and disadvantages.
Solution
The practical implications of protected specifiers in inheritance are 
  • What are the various class access specifiers? How does public inheritance differ from private inheritance?
  • Depict the difference between private and public derivation. Explain the derived class constructor with the program.
  • Briefly explain the types of inheritance used in OOP.
  • Describe the chain of constructors and destructors in inheritance.
  • How does ambiguity arise in multipath inheritance? How can you remove it?

📘 Unit 6: Virtual Functions, Polymorphism & Miscellaneous

  • Difference between compile time and run time polymorphism.
  • Explain function template overloading with examples.
  • Explain how exceptions are used with a design that includes multiple exceptions.
  • What is the use of reinterpret cast operator? Why do we need a virtual function?
  • Differentiate between concrete class and abstract class. Define class and function templates with syntax.
  • Explain the reason for member function overriding when using virtual function.
  • Explain types of polymorphism briefly. Write the roles of polymorphism and how to achieve dynamic polymorphism.
  • What is an exception? Why use exception handling? Explain try…catch with an example.
  • Explain the default action of the copy constructor. 🔁
  • How can you define a catch statement that can catch any type of exception? Use multiple catches with examples.

📘 Unit 7: Function Templates and Exception Handling

  • Write a program to implement a function template with multiple arguments.
  • What is an exception? Why use exception handling? Explain with try…catch using suitable examples. 🔁
  • When are class templates useful? How to implement a stack with int and string? Illustrate with example.
  • Create a function called swaps() a template for multiple types. 🔁
  • Explain how exceptions are used in C++ with multiple exception design. 🔁

📘 Unit 8: File Handling

  • Write a program that stores employee info in a file and displays it in ascending salary order.
  • Write a program to illustrate the use of seekg() and tellg().
  • Write a program that reads the content of the file data.txt and displays it.
  • What do you mean by the stream? Explain different stream classes for file I/O.
  • Write a program that reads and displays data for 2 student objects.
  • Write a program:
    • Class Account with acc no, balance, min_balance (static)
    • Use an array to store and display 5 accounts
  • Write a program to overload << and >> operators. 🔁
  • What is the use of get and getline? Explain with an example.

Hope this helps you in your exam preparation! 🔁 Questions indicate a high chance of repetition in future exams. Best of luck!

Tags

Post a Comment

0Comments

Post a Comment (0)