JMI - DCS 2nd Year - OOPs Assignment Paper - 2021


HOW TO PREPARE THESE QUESTION.

That's a comprehensive set of questions covering key concepts in Object-Oriented Programming (OOP), particularly in the context of C++.


💡 Preparation Strategy

  1. Understand the Core Concepts: Focus on the fundamental pillars of OOP: Encapsulation, Abstraction, Inheritance, and Polymorphism.

  2. Compare and Contrast: Be prepared to clearly articulate the differences between related concepts (e.g., OOP vs. POP, inline vs. macro, hierarchical vs. hybrid inheritance).

  3. Code Examples: For questions asking for examples (Virtual Functions, Operator Overloading, Templates, Constructors), make sure you can write short, working code snippets.

  4. Syntax and Rules: Know the specific syntax for inheritance, function templates, and constructor definitions.

  5. Pros and Cons: Understand the advantages and disadvantages (merits/demerits) of features like friend functions and inline functions.


📝 Key Topics and Concepts

1. OOP vs. POP

FeatureObject-Oriented Programming (OOP)Procedure-Oriented Programming (POP)
FocusData (Data and its security are central)Procedure (Functions or logic are central)
ApproachTop-down/Bottom-up (depends on design)Top-down (breaking down a problem into functions)
Data HidingYes (using access specifiers like private)No (Global data is easily accessible)
InheritanceSupportedNot supported
PolymorphismSupported (Function/Operator Overloading, Virtual Functions)Not supported
ExampleC++, Java, PythonC, Pascal, FORTRAN

2. Virtual Functions and Runtime Polymorphism

A virtual function is a member function declared within a base class and re-defined (overridden) by a derived class.

  • Implementation: It is declared using the virtual keyword in the base class. When a virtual function is called through a pointer or reference to the base class, the specific function version executed is determined at runtime (late binding) based on the actual type of the object being pointed to.

  • Runtime Polymorphism: This mechanism, often achieved using virtual functions, allows a single interface (the base class pointer/reference) to represent different underlying forms (the derived class objects). This is also known as dynamic binding or late binding.

3. Multiple Inheritance Ambiguity (Diamond Problem)

  • Ambiguity: In multiple inheritance, if a derived class inherits from two base classes, and both base classes inherit from a common ancestor, the derived class will receive two copies of the common ancestor's members. If the derived class tries to access a member of the common ancestor, the compiler won't know which copy to use. This is often called the Deadly Diamond of Death or Diamond Problem.

  • Overcoming Ambiguity (Solution): Use Virtual Base Classes. By declaring the common base class as virtual in the inheritance list of the intermediate classes, the compiler ensures that only a single copy of the common base class's members is inherited by the final derived class.

4. Function Templates (Minimum Value in Array)

A function template is a blueprint for creating functions that can handle different data types without rewriting the entire function body.

C++
template <typename T>
T find_minimum(T arr[], int size) {
    T min_val = arr[0];
    for (int i = 1; i < size; ++i) {
        if (arr[i] < min_val) {
            min_val = arr[i];
        }
    }
    return min_val;
}

// Example usage:
// int intArray[] = {5, 2, 9, 1};
// int minInt = find_minimum(intArray, 4); // minInt will be 1

5. Polymorphism in C++

Polymorphism literally means "many forms." In C++, it allows a single function name or operator symbol to perform different operations depending on the type or number of its arguments or the object it is called upon.

  • Compile-time Polymorphism (Static Binding):

    • Function Overloading: Functions with the same name but different argument lists.

    • Operator Overloading: Defining how an operator (like + or *) works for user-defined types (classes).

  • Run-time Polymorphism (Dynamic Binding):

    • Achieved primarily through Virtual Functions and Inheritance.

6. Inheritance Types

FeatureHierarchical InheritanceHybrid Inheritance
StructureOne base class, multiple derived classes. (Tree-like structure)A combination of two or more types of inheritance (e.g., combining multiple and hierarchical).
DiagramBase -> Derived1, Base -> Derived2, etc.Base -> Derived1, Derived2 -> Derived3 (where Derived3 uses multiple and Derived1/2 use hierarchical)
ComplexityGenerally simpler.Can be complex, often leads to the Diamond Problem.
ExampleShape (Base) $\rightarrow$ Circle, Rectangle, Triangle (Derived)Mixing Multiple and Hierarchical inheritance.

7. Inline Function vs. Preprocessor Macro

FeatureInline FunctionPreprocessor Macro
ProcessingHandled by the C++ compiler.Handled by the Preprocessor before compilation.
Type SafetyType-safe (Arguments are checked).Not type-safe (Simple text substitution).
DebuggingCan be debugged.Cannot be debugged easily.
Scope/AccessIs a true function, can access private/protected members.Simple text substitution, has no access to class members.
  • Significant Advantage of Inline Function: It avoids the overhead of a function call (stack management, register saving) by substituting the function code directly at the call site while still being a true function with type-checking and proper scope.

8. Friend Function

A friend function is a non-member function that is granted special permission to access the private and protected members of a class. It is declared inside the class using the friend keyword.

  • Merits (Advantages):

    • Allows access to private members, useful for functions that need to operate on two different classes (e.g., operator overloading for non-member functions like stream insertion <<).

    • Improves encapsulation by keeping the function outside the class interface while granting necessary access.

  • Demerits (Disadvantages):

    • Violates the principle of data hiding and encapsulation.

    • Can make the code less readable and harder to maintain if overused.

9. Visibility and Derived Class Syntax

Visibility of Base Class Members

The access level of base class members (private, protected, public) in the derived class depends on the access specifier used during inheritance.

Base Class Member AccessInherited as privateInherited as protectedInherited as public
PrivateNot accessibleNot accessibleNot accessible
ProtectedPrivateProtectedProtected
PublicPrivateProtectedPublic
  • Rule of Thumb: The derived class's access specifier acts as a ceiling. You cannot make a base class member more accessible than the specifier allows.

Syntax for Creating a Derived Class

C++
class DerivedClassName : access_specifier BaseClassName {
    // New members and methods of the derived class
};
  • Example: class Dog : public Animal { ... };

10. Characteristics of a Constructor and Parameterized Constructor

Characteristics of a Constructor

  • It has the same name as the class.

  • It does not have a return type (not even void).

  • It is called automatically when an object is created.

  • It is typically used to initialize the object's data members.

  • It can be overloaded (have multiple versions with different parameter lists).

Parameterized Constructor with Default Values

C++
class Distance {
private:
    int feet;
    int inches;

public:
    // Parameterized Constructor with Default Arguments
    Distance(int f = 0, int i = 0) {
        feet = f;
        inches = i;
    }
    // ... other methods (like display)
};

11. Operator Overloading

Operator Overloading is a form of compile-time polymorphism where you can redefine the way an operator works for user-defined data types (classes). It allows operators like +, -, *, <<, >>, etc., to be used with objects.

  • Example (Overloading the + operator for a Complex class):

C++
class Complex {
private:
    double real;
    double imag;

public:
    Complex(double r = 0, double i = 0) : real(r), imag(i) {}

    // Operator Overloading for binary '+' operator
    Complex operator+(const Complex& other) const {
        // Creates and returns a new Complex object
        return Complex(real + other.real, imag + other.imag);
    }
};

// Example usage:
// Complex c1(10, 5), c2(2, 3);
// Complex c3 = c1 + c2; // Calls c1.operator+(c2)

12. Graphics Program

This question requires the use of the graphics.h header file, which is specific to older C++ compilers (like Turbo C++ on Windows) and typically requires a special graphics environment (like the BGI graphics driver). It is not standard C++ and won't work in modern compilers (like GCC/Clang) without external libraries (like WinBGIm).

  • If you are required to answer it, you must know the functions like initgraph(), circle(), rectangle(), line(), setcolor(), and closegraph().

  • A "Car using graphics header file" would involve drawing a rectangle for the body, circles for the wheels, and possibly lines for the windows using these functions.


Comments

Popular posts from this blog

What is GPT-4? - How Does GPT-4 Work? - Key Features of GPT-4 - Applications of GPT-4 - The Future of GPT-4 and Beyond.

The de-Broglie wavelength associated with a particle of mass m and energy E is h/2mE. The dimensional formula for Planck's constant is :

What is kernel, types of kernal, functions of kernal.