Software Development Exam  >  Software Development Notes  >  DSA in C++  >  Assignment: OOPS in C++

Assignment: OOPS in C++ | DSA in C++ - Software Development PDF Download

Multiple Choice Questions (MCQs)


Q.1. Which of the following is not a characteristic of Object-Oriented Programming (OOP)?
(a) Encapsulation
(b) Polymorphism
(c) Inheritance
(d) Procedural programming

Ans. (d)

Q.2. In C++, objects are created from __________.
(a) Functions
(b) Classes
(c) Structures
(d) Variables

Ans. (b)

Q.3. Which keyword is used to inherit a class in C++?
(a) inherit
(b) extends
(c) implements
(d) : (colon)

Ans. (d)

Q.4. Polymorphism in C++ allows __________.
(a) Objects of different types to be stored in the same array
(b) Objects of different types to be assigned to each other
(c) Objects of different types to be passed as arguments to functions
(d) All of the above

Ans. (d)

Q.5. What is the purpose of encapsulation in C++?
(a) To hide implementation details and provide a clean interface
(b) To prevent inheritance
(c) To allow multiple inheritance
(d) To restrict access to class members

Ans. (a)

High Order Thinking Questions (HOTS)


Q.1. Explain the concept of inheritance in C++ and provide an example.

Inheritance is a mechanism in C++ that allows a class to inherit properties and behaviors from another class. It promotes code reuse and establishes a hierarchical relationship between classes. Here's an example:

class Shape {

protected:

    int width;

    int height;

public:

    void setWidth(int w) {

        width = w;

    }

    void setHeight(int h) {

        height = h;

    }

};

class Rectangle : public Shape {

public:

    int getArea() {

        return width * height;

    }

};

Q.2. Describe the difference between function overloading and operator overloading in C++.

Function overloading allows multiple functions with the same name but different parameters to exist in the same scope. Operator overloading, on the other hand, allows the redefinition of operators such as +, -, *, etc., for user-defined types. Function overloading is based on the number and/or types of parameters, while operator overloading is based on the operator being used.

Q.3. How does encapsulation contribute to the concept of data hiding in C++? Provide an example.

Encapsulation in C++ combines data and functions into a single unit, called a class. It allows the hiding of implementation details and provides a clean interface for interacting with the class. Data hiding is achieved by making the member variables private and accessing them through public member functions. Example:

class BankAccount {

private:

    int accountNumber;

    double balance;

public:

    void setAccountNumber(int num) {

        accountNumber = num;

    }

    int getAccountNumber() {

        return accountNumber;

    }

    void deposit(double amount) {

        balance += amount;

    }

    double getBalance() {

        return balance;

    }

};

Q.4. What is the significance of the virtual keyword when using polymorphism in C++? Explain with an example.

The virtual keyword is used in C++ to achieve runtime polymorphism. When a base class pointer points to a derived class object, virtual functions allow the correct member function to be called based on the actual object type at runtime. Example:

class Animal {

public:

    virtual void sound() {

        cout << "Animal sound\n";

    }

};

class Cat : public Animal {

public:

    void sound() {

        cout << "Meow\n";

    }

};

class Dog : public Animal {

public:

    void sound() {

        cout << "Woof\n";

    }

};

int main() {

    Animal* animalPtr;

    Cat cat;

    Dog dog;

    animalPtr = &cat;

    animalPtr->sound();  // Output: Meow

    animalPtr = &dog;

    animalPtr->sound();  // Output: Woof

    return 0;

}

Q.5. Discuss the concept of abstraction in C++ and provide an example.

Abstraction in C++ refers to the process of providing only essential information to the outside world and hiding unnecessary details. It focuses on what an object does rather than how it does it. Abstraction is achieved using abstract classes and pure virtual functions. Example:

class Shape {

public:

    virtual void draw() = 0;  // Pure virtual function

};

class Circle : public Shape {

public:

    void draw() {

        cout << "Drawing a circle\n";

    }

};

int main() {

    Shape* shapePtr;

    Circle circle;

    shapePtr = &circle;

    shapePtr->draw();  // Output: Drawing a circle

    return 0;

}

Fill in the Blanks


1. A class is a ________ that defines a set of ________.

A class is a blueprint that defines a set of attributes.

2. ________ is the process of wrapping data and functions into a single unit.

Encapsulation is the process of wrapping data and functions into a single unit.

3. The derived class inherits the ________ of the base class.

The derived class inherits the members of the base class.

4. In C++, function overloading is achieved by using the same ________ with different ________.

In C++, function overloading is achieved by using the same name with different parameters.

5. Operator overloading allows us to redefine the ________ of an operator.

Operator overloading allows us to redefine the behavior of an operator.

True or False


1. Inheritance allows a class to inherit properties and behaviors from multiple classes. (True/False)

False

2. The private members of a class can be accessed by other classes. (True/False)

False

3. Abstraction is the process of exposing the implementation details of a class. (True/False)

False

4. Function overloading is a type of polymorphism. (True/False)

True

5. Encapsulation provides data hiding and restricts access to class members. (True/False)

True

Hands-On Questions


Q.1. Create a class named "Rectangle" with the following attributes: length and width. Include member functions to calculate the area and perimeter of the rectangle.

class Rectangle {

private:

    int length;

    int width;

public:

    void setLength(int l) {

        length = l;

    }

    void setWidth(int w) {

        width = w;

    }

    int getArea() {

        return length * width;

    }

    int getPerimeter() {

        return 2 * (length + width);

    }

};

Q.2. Implement a derived class "Circle" from the base class "Shape." The "Circle" class should have a radius attribute and a member function to calculate the area of the circle.

class Shape {

public:

    virtual float getArea() = 0;  // Pure virtual function

};

class Circle : public Shape {

private:

    float radius;

public:

    Circle(float r) : radius(r) {}

    float getArea() {

        return 3.14 * radius * radius;

    }

};

Q.3. Create a base class "Animal" with a virtual function "sound()". Implement derived classes "Cat" and "Dog" that override the "sound()" function and display different sounds.

class Animal {

public:

    virtual void sound() = 0;  // Pure virtual function

};

class Cat : public Animal {

public:

    void sound() {

        cout << "Meow\n";

    }

};

class Dog : public Animal {

public:

    void sound() {

        cout << "Woof\n";

    }

};

Q.4. Write a C++ program to demonstrate function overloading by creating a class "Math" with two functions named "add" that perform addition with different numbers of parameters.

class Math {

public:

    int add(int a, int b) {

        return a + b;

    }

    int add(int a, int b, int c) {

        return a + b + c;

    }

};

Q.5. Create a class "Complex" to represent complex numbers. Implement the operator overloading for addition and subtraction of complex numbers.

class Complex {

private:

    int real;

    int imaginary;

public:

    Complex(int r = 0, int i = 0) : real(r), imaginary(i) {}

    Complex operator+(const Complex& c) {

        Complex result;

        result.real = real + c.real;

        result.imaginary = imaginary + c.imaginary;

        return result;

    }

    Complex operator-(const Complex& c) {

        Complex result;

        result.real = real - c.real;

        result.imaginary = imaginary - c.imaginary;

        return result;

    }

};

The document Assignment: OOPS in C++ | DSA in C++ - Software Development is a part of the Software Development Course DSA in C++.
All you need of Software Development at this link: Software Development
153 videos|115 docs|24 tests

Top Courses for Software Development

153 videos|115 docs|24 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Summary

,

video lectures

,

pdf

,

Previous Year Questions with Solutions

,

ppt

,

Viva Questions

,

study material

,

mock tests for examination

,

Assignment: OOPS in C++ | DSA in C++ - Software Development

,

past year papers

,

shortcuts and tricks

,

Important questions

,

Semester Notes

,

Exam

,

Sample Paper

,

Extra Questions

,

Assignment: OOPS in C++ | DSA in C++ - Software Development

,

practice quizzes

,

Free

,

Objective type Questions

,

Assignment: OOPS in C++ | DSA in C++ - Software Development

,

MCQs

;