OOP PAST PAPER FALL-2023

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

OOP PAST PAPER FALL-2023 BY HAMZA AFZAL

1. What are the differences between a class and an object?


Class: A class is a template or blueprint that defines the properties (member
variables/data members/attributes) and functionalities (member functions/ methods) that
objects of that class will share. It acts like a cookie cutter that creates objects with the
same characteristics.
Object: An object is a real-world entity that has properties (data) and behaviours
(functions) associated with it. In C++, we create objects based on a blueprint called a
class.

2. Can access identifiers be changed during runtime? If not, why?

No, access identifiers (like public, private & protected) in most programming languages cannot
be changed during runtime. There are a few reasons for this:

 Security: Access modifiers define the intended access level of a class member (variable,
method). Changing them at runtime could lead to security vulnerabilities by exposing
data or functionality that wasn't meant to be public.
 Code Stability: The code relies on the defined access levels to function correctly. If
access levels could change dynamically, the code's behaviour would be unpredictable and
difficult to maintain.
 Design Principles: Access modifiers are a core concept in object-oriented programming
(OOP) that promotes encapsulation and data hiding. Changing them would violate these
principles.

3. Can destructor be overloaded? if so, how?

No, destructors cannot be overloaded in C++. A class can only have one destructor.

Here's why overloading destructors isn't allowed:

 Unambiguous Cleanup: The destructor's purpose is to clean up resources associated


with an object when it goes out of scope. Having multiple destructors would create
confusion about which one to call for proper cleanup.
 Automatic Invocation: Destructors are invoked automatically by the compiler when the
object goes out of scope or is explicitly deleted using delete. Overloading would make it
unclear which destructor to call in these scenarios.

4. What is static Member function and how is it used?

A static member function in C++ is a special type of member function


that belongs to the class itself, rather than to individual objects
(instances) of the class.
 Declared using the static keyword within the class definition.
 Associated with the class itself, not with individual objects (instances) of the class.
 Can be called directly using the class name and scope resolution operator (::), without
needing an object.

Example:

class Math {

static const double PI = 3.14159;

static int square(int x) {

return x * x;
}
};

// Accessing static member variable


cout << "Value of PI: " << Math::PI <<::endl;

// Calling static method using class name


int result = Math::square(5);
cout << "Square of 5: " << result <<::endl;

In this example:

 PI is a static member variable accessible directly using Math::PI.


 square is a static member function that calculates the square and can be called using
Math::square(x).

5. What is difference between static and dynamic polymorphism


with example?

Static Polymorphism (Compile-Time Polymorphism):

 Resolution: Determined by the compiler at compile time based on the object's declared
type.
 Mechanism: Achieved through method overloading.
 Method Overloading: Multiple methods exist within the same class with the same name
but different parameter lists (number, type, or order of parameters). The compiler chooses
the appropriate method based on the arguments used in the function call.

Example:
#include <iostream>
using namespace std;

void showInfo(int age){

cout<<age<<endl;

void showInfo(string name){

cout<<name<<endl;

void showInfo(double salary){

cout<<salary<<endl;

main(){

showInfo(24);

showInfo(“HAMZA AFZAL”);

showInfo(25000.52);

Dynamic Polymorphism (Runtime Polymorphism)

 Resolution: Determined at runtime based on the object's actual type.


 Mechanism: Achieved through method overriding.
 Method Overriding: Subclasses inherit methods from their parent class and can redefine
them to provide specific behavior. When a method is overridden, the decision of which
method to call is made at runtime based on the actual object's type (not the declared type
of the reference variable).

Example:

#include <iostream>
using namespace std;
class A{
public:
Virtual void disp(){

Cout<<”Its class A”<<endl;

};

class B : public A{
public:

Virtual void disp(){

Cout<<”Its class B”<<endl;

};

main(){

A *ptr;
B obj;
ptr = &obj;
ptr->disp();

LONG QUESTION:

#include <iostream>
using namespace std;

class Binary{

private:

int x;
int y;

public:

Binary(){

x = 1;
y = 0;
};

};

int main (){

return 0;

a) Write Parameterized & copy constructor.

Binary(int a, int b){

x = a;
y = b;

};

Binary(const Binary &a){

x = a.x;

y = b.y;

};

You might also like