Mastering Core OOP Concepts: Type Casting to Inheritance

What is type casting? Explain with suitable example.? 5 marks

Type casting refers to the process of converting a variable from one data type to another. It is commonly used in programming when the operations or functions involved require data of a specific type. Type casting can be done explicitly (manual casting) or implicitly (automatic conversion).

Types of Type Casting:


1.)Implicit Type Casting (Type Promotion): The compiler automatically converts a smaller data type to a larger data type to prevent data loss. For example, converting int to float.

2.)Explicit Type Casting: The programmer manually converts a variable’s data type using a cast operator

Example 

Implicit Type Casting:


#include <stdio.H>

int main() {

    int a = 10;

    float b;

    b = a; // ‘int’ is automatically converted to ‘float’

    printf(“Value of b: %.2f\n”, b);

    return

0;

}


Write a program to compute subtraction of two complex numbers using operator overloading.5 marks

#include<iostream>

using namespace std;

Class


ComplexNum {

   private:

   int real, imag;

   public:

   ComplexNum(int r = 0, int i =0) {

      real = r;

      imag = i;

   }

   ComplexNum operator – (ComplexNum const &obj1) {

      ComplexNum obj2;

      obj2.Real = real – obj1.Real;

      obj2.Imag = imag – obj1.Imag;

      return obj2;

   }

   void print() {

      if(imag>=0)

      cout << real << ” + i” << imag <<endl;

      else

      cout << real << ” + i(” << imag <<“)”<<endl;

   }

};

int main() {

   ComplexNum comp1(15, -2), comp2(5, 10);

   cout<<“The two comple numbers are:”<<endl;

   comp1.Print();

   comp2.Print();

   cout<<“The result of the subtraction is: “;

   ComplexNum comp3 = comp1 – comp2;

   comp3.Print();

}


Why is exception handling required? Explain with suitable example.5 marks

Exception handling is necessary to manage errors that occur during the execution of a program and ensure the program can respond gracefully rather than crashing abruptly. It helps:

1.)Maintain Program Flow: Prevent the program from terminating unexpectedly

2.)Provide Useful Feedback: Inform users or developers about issues

3.)Enable Error Recovery: Take corrective actions when an error occurs

4.)Enhance Readability: Makes code more structured and readable

Example

 

# Division without exception handling

num1 = 10

num2 = 0

result = num1 / num2

print(“Result is:”, res

ult)


Differentiate between super class and sub class with suitable examples.5 marks

SuperClass

1.When implementing inheritance, the existing class from which the new classes are derived is the Superclass

2.Superclass is known as base class, parent class

3.A superclass cannot use the properties and methods of the Subclass

4.There is one Superclass

5.There is one Superclass

6.There are many Superclasses

Subclass

1.When implementing inheritance, the class that inherits the properties and methods from the Superclass is the Subclass.

2.Subclass is known as derived class, child class

3.A subclass can use the properties and methods of the Superclass

4.There is one Subclass

5.There are many Subclasses

6.There is one Subclass


Differentiate between function overriding and function overloading. Explain with suitable example.

Function overloading 

1.Function overloading and function overriding are discussion topics in C language. Function overloading lets you declare functions with the same name but different parameters. 

2.Function overloading is when a function has multiple implementations with different numbers, parameters, or both

3.Function overloading is a process of defining the same function with different names and different arguments

4.Function overloading means WHO can define multiple functions in the same scope with the same name

5.Function overloading allows you to use multiple function signatures with the same name (i.E., The functions will have different signatures but share the same name).

Function overriding

1.Function overriding lets you define one or more tasks that will override the function defined in the base class or parent class. WHO can do this by calling virtual prototypes or inheriting the type declared in your base class.

2.Process overriding is when the same function overrides apart from a parent class from a child class

3.The process of redefining the same position in the derived class is known as function overriding

4.Function overriding means a part may override another function if it has the same signature as the original function.

5.Function When we want to override a position within a class, we use overriding. In  same function name must exist in both the base and derived classes, but the code must differ.


Explain the role of polymorphism in Object Oriented Programming? 5 marks

Polymorphism in Object-Oriented Programming (OOP) is a key concept that allows objects to be treated as instances of their parent class rather than their actual class. It enhances flexibility and maintainability in code design. Here’s its role:

1.)Code Reusability: Polymorphism allows a single interface to represent different underlying data types or operations. For instance, a single method draw() can work for various shapes like circles, squares, or triangles.

2.)Method Overriding: In inheritance, child classes can override a parent class method to provide specific behavior, allowing dynamic method calls based on the object‘s runtime type.

3.)Dynamic Binding: Polymorphism enables the program to decide at runtime which method to invoke, fostering extensibility as new classes can be added with minimal changes to existing code.

4.)Simplified Code Maintenance: With polymorphism, developers can write generic code that works for different objects, making the codebase easier to manage and extend.

5.)Enhances Abstraction: By focusing on the interface rather than implementation, polymorphism ensures that different objects can execute the same operation in their own specific way, adhering to the “program to an interface” principle.


Explain the different types of class access specifiers? 5 marks

Class access specifiers are keywords in object-oriented programming that define the accessibility of class members (attributes and methods) within and outside the class. The three main types of access specifiers are:

1.Public:

°Members declared as public are accessible from anywhere, including outside the class.

°They provide unrestricted access and are used when the data or functionality is meant to be shared openly.

class Example {

public:

    int value;

    void display() { cout << value; }

};

2.Private:

°Members declared as private can only be accessed within the class itself.

°They are used to ensure encapsulation, hiding the implementation details and protecting the data from unauthorized access or modification.

3.Protected:

°Members declared as protected are accessible within the class and by derived (inherited) classes.

°They are useful in inheritance when you want child classes to have access to certain members without exposing them to the world.


.Write a a program to find the cube of given integer using inline function.5 marks?

#include <iostream.H>

#include <conio.H>

inline int cube(int r) {

return r*r*r;

}

void main() {

clrscr();

int r;

cout<<“PROGRAM TO COMPUTE CUBE\n”;

cout<<“Enter value to compute cube: “;

cin>>r;

cout<<“cube of the number: “<<cube(r);

getch();

}


Write a program to convert centigrade into Fahrenheit temperature. 5 marks

#include <stdio.H>

 int main()

{

    double Fahrenheit;

    double Celsius;

 printf (“Enter the Temperature in Celsius: “);

    scanf (“%lf”, &Celsius);

  Fahrenheit = (9 * Celsius) / 5 + 32;

    printf (“The Temperature in Fahrenheit is: %.2lf\n”, Fahrenheit);

    return 0;

}


Why are constructor and destructor required on Object Oriented Programming? Explain with suitable example.?

In Object-Oriented Programming (OOP), constructors and destructors are essential for managing the lifecycle of objects. They ensure that objects are properly initialized and cleaned up, reducing errors and improving program stability.

1.Constructor

A constructor is a special member function that is automatically called when an object is created. Its primary purpose is to initialize an object’s data members and allocate resources if necessary.

Features:

°It has the same name as the class.

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

°It can be overloaded to provide different initialization options.

Programming 

#include <iostream>

using namespace std;

class Car {

    string brand;

    int speed;

public:

    // Constructor

    Car(string b, int s) {

        brand = b;

        speed = s;

        cout << “Car object created: ” << brand << ” with speed ” << speed << endl;

    }

    void display() {

        cout << “Brand: ” << brand << “, Speed: ” << speed << endl;

    }

};

int main() {

    Car car1(“Toyota”, 120); // Constructor is called

    car1.Disp

lay();

    return 0;

}


Why do we need object-oriented programming? How can we use inheritance to reuse already written and tested code in programs? Discuss with suitable example.? 10 marks

Object-Oriented Programming (OOP) provides a structured and modular way of programming by organizing code into objects. Objects are instances of classes, which encapsulate data (attributes) and behavior (methods). This approach offers several benefits:

1.Modularity: Code is organized into reusable classes, making it easier to maintain and debug

2.Reusability: Inheritance allows code to be reused across different parts of a program or even in new projects

3.Encapsulation: Data and methods are bundled together, improving security and data hiding

4.Abstraction: Simplifies complex systems by modeling them as interacting objects

5.Polymorphism: Enhances flexibility by allowing one interface to represent different types

Using Inheritance for Code Reusability

Inheritance enables a class (child class) to derive properties and methods from another class (parent class). By reusing the parent class’s tested and reliable code, developers can save time, reduce errors, and focus on implementing specific functionalities for the child class.