Skip to main content

Object oriented programming using Cpp (BCA-301) . Question paper with Solution. (Year 2020)

OOPs Using C++. Question paper with solution.

Very Short Answer Questions (Attempt All)  (3*5=15).

1- Define Encapsulation?

Ans- Encapsulation is a programming principle that refers to the bundling of data and functions that operate on that data within a single unit or object. It is used to hide the implementation details of a class from the outside world and to make the object self-contained, so that it can be treated as a black box that can be used and interacted with without the need for other parts of the program to know how the object's internal data and functions are implemented. Encapsulation is one of the fundamental principles of object-oriented programming and is used to reduce the complexity of a program and make it easier to maintain and modify over time.

2- List the features of OOPs.

Ans- Object-oriented programming (OOP) is a programming paradigm that is based on the concept of "objects", which can contain data and code that operates on that data. Some of the key features of OOP are:


  1. Encapsulation: The bundling of data and functions that operate on that data within a single unit, or object.
  2. Abstraction: The act of representing essential features without including the background details or explanations.
  3. Inheritance: The ability to create a new class that is a modified version of an existing class. The new class is called the subclass, and the existing class is the superclass.
  4. Polymorphism: The ability to take on multiple forms. In OOP, polymorphism refers to the ability of a function or operator to work with multiple types of data.
  5. Overloading: The ability to create multiple functions with the same name but different parameters.
  6. Method overloading: The ability to create multiple methods with the same name but different parameters in the same class.
  7. Method overriding: The ability to create a method in a subclass with the same name, return type, and parameters as a method in the superclass, and then use the subclass method to provide a specific implementation of the method.

3- How do define member function outside the class? Give example.
Ans- In C++, you can define a member function outside of the class by using the scope resolution operator (::) and the name of the class. Here is an example:

class Point { #include <iostream> class Point { public: Point(double x, double y) : x_(x), y_(y) {} // Declare member function (only a prototype) void print() const; private: double x_; double y_; }; // Define member function outside of class definition void Point::print() const { std::cout << "(" << x_ << ", " << y_ << ")" << std::endl; } int main() { Point p(1.0, 2.0); p.print(); return 0; }

4- What is Abstract class?
Ans- An abstract class is a class that cannot be instantiated and is used as a base class for one or more derived classes. It defines a common interface for the derived classes and allows those classes to be treated polymorphically. Abstract classes are declared using the virtual keyword and pure virtual functions, which are functions that have no implementation and are indicated by appending = 0 to the function declaration. Abstract classes are useful for defining a common interface for a set of related classes and for implementing polymorphism in C++.
Here is a example of an abstract class in C++:

class Shape { public: // Pure virtual function (has no implementation) virtual double area() = 0; // Non-virtual function (has implementation) double perimeter() { return 0.0; } }; class Rectangle : public Shape { public: Rectangle(double width, double height) : width_(width), height_(height) {} double area() override { return width_ * height_; } private: double width_; double height_; }; int main() { // Cannot create an instance of the abstract class Shape // Shape s; Rectangle r(10, 20); std::cout << "Area of rectangle: " << r.area() << std::endl; std::cout << "Perimeter of rectangle: " << r.perimeter() << std::endl; return 0; }
In this example, the Shape class is an abstract class because it has a pure virtual function (area) that has no implementation. The Rectangle class is a derived class of Shape and it overrides the area function to provide an implementation. Because Rectangle has implemented all of the pure virtual functions of its base class, it can be instantiated.
5- Explain the basic data type in C++.
Ans- 
  1. Integral types:
char letter = 'A'// char stores a single character short number = 32767// short stores a small integer int age = 20// int stores a medium-sized integer long distance = 3456789// long stores a larger integer long long num = 1234567890// long long stores an even larger integer
  1. Floating point types:
float pi = 3.14159// float stores a single-precision decimal value double e = 2.71828// double stores a double-precision decimal value
  1. Boolean type:
bool is_true = true// bool stores a true or false value
  1. Void type:
void print_message() { // void is used as the return type for functions that don't return a value std::cout << "Hello, world!" << std::endl; }
  1. Enumerated types:
enum Color { RED, ORANGE, YELLOW, GREEN, BLUE, INDIGO, VIOLET }; Color favorite = BLUE; // an enumerated type can be used like a variable of an integral type
  1. Pointer types:
int value = 5int *ptr = &value; // ptr is a pointer to an integer and stores the memory address of value
  1. Array types:
int numbers[5] = {12345}; // an array of integers
  1. Structure types:
struct Point { int x; int y; }; Point p = {12}; // a structure type can be used like a variable that stores multiple values
  1. Class types:
class Rectangle { int width; int height; public: int area() { return width * height; } // a class can have member functions }; Rectangle rect; rect.width = 5; rect.height = 10std::cout << rect.area() << std::endl// prints 50


Short Answer Questions(Attempt any 2) (7.5*2= 15).

6- What do you mean by nesting of classes? also explain how friend function is important in cpp.
Ans- In C++, you can define a class inside another class, which is known as nesting of classes. Nesting of classes can be useful in a variety of situations, such as when you want to create a class that is closely related to another class, or when you want to define a class that has a member of another class type.

Here is an example of a class (Outer) that nests another class (Inner):

class Outer { public: Outer(int x) : x_(x) {} class Inner { public: Inner(int y) : y_(y) {} int getValue() const { return y_; } private: int y_; }; private: int x_; }; int main() { Outer o(10); Outer::Inner i(20); std::cout << i.getValue() << std::endl; return 0; }

In this example, the Outer class defines the Inner class as a nested class. The Inner class has its own member variables and member functions, and it can be used like any other class. However, it can only be accessed from within the context of the Outer class.

A friend function is a function that is declared as a friend of a class. A friend function is not a member of the class, but it has access to the private and protected members of the class. Friend functions are often used to implement operator overloading or to provide additional functionality for a class.

Here is an example of a class (Complex) that declares a friend function (operator+) for performing addition of two complex numbers:

#include <iostream> class Complex { public: Complex(double real, double imag) : real_(real), imag_(imag) {} // Declare operator+ as a friend function friend Complex operator+(const Complex& c1, const Complex& c2); private: double real_; double imag_; }; // Define operator+ as a friend function Complex operator+(const Complex& c1, const Complex& c2) { return Complex(c1.real_ + c2.real_, c1.imag_ + c2.imag_); } int main() { Complex c1(1.0, 2.0); Complex c2(3.0, 4.0); Complex c3 = c1 + c2; std::cout << c3.real_ << ", " << c3.imag_ << std::endl; return 0; }

In this example, the operator+ function is declared as a friend of the Complex class, which allows it to access the private members of the Complex class. The operator+ function is then defined outside of the Complex class, and it can be used to add two Complex objects together using the + operator.

7- Explain static data member and static data member functions with example.
Ans- In C++, a static data member is a class member that is shared by all instances of the class. A static data member is defined with the static keyword, and it is typically initialized outside of the class definition.

Here is an example of a class (Counter) with a static data member (count_):

#include <iostream> class Counter { public: Counter() { ++count_; } static int getCount() { return count_; } private: // Static data member (initialized to 0) static int count_; }; // Initialize static data member int Counter::count_ = 0; int main() { Counter c1; std::cout << Counter::getCount() << std::endl; Counter c2; std::cout << Counter::getCount() << std::endl; return 0; }

In this example, the Counter class has a static data member called count_ that is used to count the number of Counter objects that have been created. The Counter class also has a static member function called getCount, which returns the value of the count_ data member.

Static data members can only be accessed through the class name, and they are not associated with any particular instance of the class. In the example above, the getCount function can be called without an instance of the Counter class, and it will always return the current value of the count_ data member.

A static member function is a class member function that is defined with the static keyword. A static member function is not associated with any particular instance of the class, and it can be called without an instance of the class.

Here is an example of a class (Math) with a static member function (abs) that computes the absolute value of a number:

#include <iostream> #include <cstdlib> class Math { public: // Static member function static int abs(int x) { return std::abs(x); } }; int main() { int x = -10; std::cout << Math::abs(x) << std::endl; return 0; }

In this example, the abs function is a static member function of the Math class, and it can be called without an instance of the Math class. The abs function uses the std::abs function from the C standard library to compute the absolute value of its argument.

8- Define polymorphism. What are the different methods of emplementing polymorphism in C++?
Ans- Polymorphism is the ability of an object to take on multiple forms. In object-oriented programming, polymorphism refers to the ability of a class to have multiple implementations of a particular function or behavior.

There are several ways to implement polymorphism in C++, including:

  1. Virtual functions: Virtual functions are member functions that are declared with the virtual keyword in the base class and are overridden by derived classes. When a virtual function is called through a pointer or reference to the base class, the compiler will automatically dispatch the call to the appropriate implementation in the derived class.

  2. Function overloading: Function overloading is the ability of a class to have multiple functions with the same name but different signatures. The compiler will choose the appropriate function to call based on the arguments provided.

  3. Operator overloading: Operator overloading is the ability of a class to redefine the meaning of an operator for its own type. For example, you can define the + operator for a class to perform addition of two objects of that class.

  4. Type casting: Type casting is the process of converting an object of one type to another type. In C++, you can use type casting to convert an object of a base class to a derived class, or vice versa.

  5. Templates: Templates are a feature of C++ that allow you to define a class or function with a placeholder type that can be replaced with a specific type at compile time. Templates can be used to implement generic algorithms that work with any data type.

  6. Multiple inheritance: Multiple inheritance is a feature of C++ that allows a class to inherit from multiple base classes. This can be used to achieve polymorphism by creating a class that has multiple implementations of a particular function or behavior inherited from different base classes.


Detailed Answer Questions(Attempt any 3) (15*3= 45).


 9-(a) Define Inheritance. What are the various types of inheritance? Explain with suitable example.(10 Marks)
Ans- Inheritance is a mechanism in object-oriented programming that allows a class (the derived class) to inherit the properties and behaviors of another class (the base class). Inheritance is used to create a relationship between classes, where the derived class is a specialized version of the base class.

There are several types of inheritance in C++, including:

  1. Single inheritance: Single inheritance is a type of inheritance where a derived class has only one base class.
#include <iostream> class Animal { public: void makeNoise() { std::cout << "Some noise" << std::endl; } }; class Dog : public Animal { public: void makeNoise() { std::cout << "Woof!" << std::endl; } }; int main() { Animal a; a.makeNoise(); Dog d; d.makeNoise();
return 0; }

n this example, the Animal class is the base class and the Dog class is the derived class. The Dog class inherits the makeNoise function from the Animal class and overrides it to provide a different implementation.
  1. Multiple inheritance: Multiple inheritance is a type of inheritance where a derived class has more than one base class.
#include <iostream> class Shape { public: virtual double area() = 0; }; class Color { public: virtual std::string getColor() = 0; }; class Rectangle : public Shape, public Color { public: Rectangle(double width, double height, std::string color) : width_(width), height_(height), color_(color) {} double area() override { return width_ * height_; } std::string getColor() override { return color_; } private: double width_; double height_; std::string color_; }; int main() { Rectangle r(10, 20, "red"); std::cout << "Area of rectangle: " << r.area() << std::endl; std::cout << "Color of rectangle: " << r.getColor() << std::endl; return 0; }

In this example, the Rectangle class is a derived class of both the Shape and Color classes, and it inherits the area and getColor functions from these base classes.

  1. Hierarchical inheritance: Hierarchical inheritance is a type of inheritance where a base class has more than one derived class.
#include <iostream> class Animal { public: virtual void makeNoise() = 0; }; class Dog : public Animal { public: void makeNoise() { std::cout << "Woof!" << std::endl; } }; class Cat : public Animal { public: void makeNoise() { std::cout << "Meow!" << std::endl; } }; int main() { Dog d; d.makeNoise(); Cat c; c.makeNoise(); return

(b) Give the general form of derived class.(5 Marks)
Ans- The general form of a derived class in C++ is:

class DerivedClassName : [access-specifier] BaseClassName { // Class members (data members and member functions) };

Here, DerivedClassName is the name of the derived class, BaseClassName is the name of the base class, and access-specifier is an optional keyword (such as public, protected, or private) that specifies the access level of the derived class members.

For example, here is a simple derived class (Rectangle) that is derived from a base class (Shape):

class Shape { public: virtual double area() = 0; }; class Rectangle : public Shape { public: Rectangle(double width, double height) : width_(width), height_(height) {} double area() override { return width_ * height_; } private: double width_; double height_; };

In this example, the Rectangle class is a derived class of the Shape class, and it inherits the area function from the Shape class. The Rectangle class also provides its own implementation of the area function, which calculates the area of a rectangle based on its width and height.

10-(a) What is File? write a program to update the content using random access.
Ans- A file is a collection of data stored on a computer or other device. Files are used to store various types of data, such as text, images, audio, and video.

In C++, you can use the fstream library to perform file input and output operations. The fstream library provides three classes for working with files: fstream, ifstream, and ofstream. The fstream class can be used for both input and output, the ifstream class is used for input only, and the ofstream class is used for output only.

To update the content of a file using random access, you can use the seekp function to move the output position to the desired location in the file, and then use the write function to write the new data to the file.

Here is an example program that updates the content of a file using random access:

#include <iostream> #include <fstream> int main() { std::ofstream file("test.txt", std::ios::binary | std::ios::in | std::ios::out); // Write some initial data to the file file << "Hello, world!" << std::endl; file << "This is a test." << std::

#include <iostream> #include <fstream> int main() { std::ofstream file("test.txt", std::ios::binary | std::ios::in | std::ios::out); // Write some initial data to the file file << "Hello, world!" << std::endl; file << "This is a test." << std::

(b)- Explain the concept of reusability with example.(7 Marks)
Ans- Reusability is a design principle in software development that refers to the ability to reuse code or other assets in multiple contexts or projects. Reusability can be achieved through various techniques, such as modular design, inheritance, and encapsulation.

One way to achieve reusability in C++ is through the use of class inheritance. Inheritance is a mechanism in object-oriented programming that allows a class (the derived class) to inherit the properties and behaviors of another class (the base class). By creating a hierarchy of classes, you can create reusable components that can be specialized or extended as needed.

Here is an example of a class (Shape) that can be reused through inheritance:

#include <iostream> class Shape { public: virtual double area() = 0; }; class Circle : public Shape { public: Circle(double radius) : radius_(radius) {} double area() override { return 3.14 * radius_ * radius_; } private: double radius_; }; class Rectangle : public Shape { public: Rectangle(double width, double height) : width_(width), height_(height) {} double area() override { return width_ * height_; } private: double width_; double height_; }; int main() { Circle c(10); std::cout << "Area of circle: " << c.area() << std::endl; Rectangle r(10, 20); std::cout <<

11- Define functions. what are the advantages of using function? what are the various methods of parameters passing through the functions? explain.

Ans- In programming, a function is a block of code that performs a specific task. Functions allow you to reuse code, making it easier to write and maintain your programs.

There are several advantages to using functions:

  1. Code reusability: You can use the same function multiple times in your program, saving you the effort of writing the same code over and over again.

  2. Modularity: Functions allow you to break down a large program into smaller, more manageable pieces. This makes it easier to understand and modify the code.

  3. Improved readability: By dividing your code into functions, you can give each function a descriptive name that reflects its purpose. This makes it easier to understand what the code does and how it fits into the overall program.

There are several methods of passing parameters through functions:

  1. Pass by value: In this method, the value of the parameter is passed to the function. Any changes made to the parameter within the function have no effect on the original value outside the function.

  2. Pass by reference: In this method, a reference to the memory location of the parameter is passed to the function. Any changes made to the parameter within the function are reflected in the original value outside the function.

  3. Default parameters: You can specify default values for parameters in a function definition. If a caller does not provide a value for the parameter, the default value is used.

  4. Variable-length arguments: You can use an asterisk (*) to specify that a function can accept a variable number of arguments. These arguments are packed into a tuple and can be accessed within the function.

12- Write short notes on:

  • Operator overloading.
  • function overloading.
  • Namespaces.
Ans-

Operator overloading.

Operator overloading is a feature in some programming languages that allows operators (such as +, -, or *) to have different meanings depending on the context in which they are used. For example, the + operator might be used to add two numbers together, but it could also be used to concatenate two strings.

Function overloading

Function overloading is a feature that allows a single function to have multiple definitions with different sets of parameters. The function can then be called with different sets of arguments and the appropriate definition will be used.

Namespaces

Namespaces are a way of organizing code in a logical manner. They allow you to group related functions, variables, and other code elements under a common name. This can help to avoid naming conflicts and make it easier to manage large codebases.

13-(a) What is constructor? explain various types of constructor with example.

Ans- A constructor is a special type of function that is called when an object is created from a class. It is used to initialize the object and allocate memory for it.

There are several types of constructors in programming:

  1. Default constructor: This is a constructor that takes no arguments and is used to initialize the object to default values. For example:
class Person { string name; int age; public: Person() { name = ""; age = 0; } };
  1. Parameterized constructor: This is a constructor that takes one or more arguments and is used to initialize the object to specific values. For example:
class Person { string name; int age; public: Person(string n, int a) { name = n; age = a; } };

  1. Copy constructor: This is a constructor that takes an object of the same class as an argument and creates a new object that is a copy of the original. For example:
class Person { string name; int age; public: Person(const Person &p) { name = p.name; age = p.age; } };

  1. Conversion constructor: This is a constructor that takes an argument of a different type and converts it to an object of the class. For example:
class Person { string name; int age; public: Person(string n) { name = n; age = 0; } };

(b)- What is the advantages of using new and delete operator as compare to malloc() and calloc()?
Ans- The new and delete operators are used for dynamically allocating memory in C++, while malloc() and calloc() are used for dynamically allocating memory in C.

There are several advantages of using new and delete over malloc() and calloc():

  1. Type safety: The new operator automatically initializes the memory it allocates to the default value for the type being allocated, and it returns a pointer to the newly allocated memory. This ensures that the memory is properly initialized and reduces the risk of errors. On the other hand, malloc() and calloc() return void pointers, which do not provide any type information and can lead to type-related errors.

  2. Automatic memory management: The delete operator automatically frees the memory that was allocated using new when it is no longer needed. This eliminates the need to manually manage memory and reduces the risk of memory leaks. In contrast, malloc() and calloc() do not have any built-in mechanism for freeing memory, so you have to manually call free() to release the memory.

  3. Exception handling: The new operator can throw an exception if it fails to allocate memory, which allows you to handle the error in a more controlled way. On the other hand, malloc() and calloc() return a null pointer if they fail to allocate memory, which can be difficult to handle.

  4. Overloading: The new and delete operators can be overloaded, which allows you to customize their behavior. This can be useful for implementing custom memory allocators or for adding additional functionality to the memory management process. Malloc() and calloc() cannot be overloaded.

Overall, new and delete offer a more robust and flexible way of managing memory in C++ compared to malloc() and calloc() in C.

Comments

Popular posts from this blog

बन्दूक के लाइसेंस के लिए ऑनलाइन कैसे अप्लाई करें? How to Online apply for a gun license?

बन्दूक के लाइसेंस के लिए ऑनलाइन कैसे अप्लाई करें? How to Online apply for a gun license? दोस्तों आप सभी को पता ही होगा की बिना लाइसेंसे के गन रखना एक बड़ा अपराध है यदि ऐसा करता कोई पकड़ा जाता है तोह 7 से 14 साल तक की जेल और जुरमाना दोनों हो सकता है! तोह आज की इस पोस्ट में हम आपको बतायेंगे की आप किसी भी तरह की गन के लिए ऑनलाइन अप्लाई कैसे कर सकते हैं! दोस्तों इंडिया में गन का लाइसेंस लेना मुस्किल तोह है लेकिन नामुमकिन नही है बस आपके पास एक वेलिड कारन होना चाहिए गन लेने का! क्या डाक्यूमेंट्स चाहियें गन के लाइसेंस के लिए अप्लाई करने के लिए? What Documents are needed to apply For a Gun license? ID Proof Address Proof Age Proof Fitness Proof Income Proof Latest Photographs तोह चलिए आपको बताते हैं की कैसे आप गन लाइसेंस के लिए ऑनलाइन अप्लाई कर सकते हैं? एक भारत सरकार की वेबसाइट है जिसमे आप भारत के किसी भी राज्य से गन के लाइसेंस के लिए ऑनलाइन अप्लाई कर सकते हैं! आप यहाँ पर क्लिक करके वेबसाइट पर जा सकते हैं लेकिन वेबसाइट पे जाने से पहले पूरा प्रोसेस जान लें! वेबसाइट में जाने के बाद आप...

How to earn money from Amazon associate.

What is the Amazon Associates Program? In 1998, when the Amazon Associates Program began in 1998, there wasn't a lot of chance for business visionaries to bring in cash on the stage. Making a benefit was conceivable simply by selling books from the Amazon list. Amazon then, at that point changed their approach, extended their product offering, and turned into a major name on the lookout. Most online customers go to Amazon to see item audits and settle on what to buy. They likewise love the comfort that Amazon Prime gives them. Despite the fact that Amazon has a lower payout rate than most offshoot programs, you can in any case get more cash-flow through Amazon due to the quantity of items they sell. Presently, it's a lot simpler to bring in cash through the Amazon Associates Program. Here's the manner by which you can get everything rolling. Necessities to Join the Amazon Associates Program 👉 You should have a site, blog, or YouTube channel to turn into a...

How To Boost Immunity Naturally? Complete Solution.

  Boost Immunity Naturally With a Simple Spray.   Promoting a healthy immune system has never been more important.A new ingestible oral spray from established oral care company TheraBreath gives you a combination of vitamins and minerals designed to boost your immune system and help protect you and your family against colds, flu, and other airborne viruses. The spray is packaged in a small bottle that is easy to take anywhere and use on the go."The immunity spray fills an untapped niche of oral products that provide an overall health benefit," says CEO and founder, Dr. Harold Katz, a dentist with an additional degree in bacteriology."For many years, folks have overlooked the fact that their mouth and throat are the doorways to the rest of their body," he explains."TheraBreath Immunity Support Spray is an easy way for everyone to improve their body’s natural immunity."The spray is based on solid science, and is designed to be a simple and efficient way to i...