0% found this document useful (0 votes)
46 views16 pages

OOP Assignment SyedZaeemAzharF21BINFT1E02054

Assignment

Uploaded by

jawadbahi756
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views16 pages

OOP Assignment SyedZaeemAzharF21BINFT1E02054

Assignment

Uploaded by

jawadbahi756
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Assignment

Name: Syed Zaeem Azhar

Roll No: F21BINFT1E02054

Semester: 2nd

Subject: Object Oriented Programming

Submitted To: Sir Faisal Shahzad

1. What is OOP?

Object-Oriented Programming (OOP) is a programming approach that structures


code around objects, which integrate both data and functions. By modeling real-
world entities and their interactions, OOP simplifies the design, management, and
scalability of software systems.

2. What are the different components of OOP?

Objects:Classes are instantiated with both data and methods.

Classes: Templates or blueprints are used to create objects.


Encapsulation: Encapsulation is the process of combining data and methods into a
single class and restricting access to certain components.

Abstraction: Hide intricate implementation details and display only relevant


functionality.

Inheritance: A process for creating a new class from an existing one.

Polymorphism:A feature that enables classes to be processed using a common


interface.

3. What are the characteristics of the Object-Oriented Programming


languages?
- Encapsulation: This is a combination of data and actions into a unit, often
known as an object; it also includes control over access to that object.
- Abstraction: It simplifies interaction by making only certain details visible to
the user and hiding others.
- Inheritance: Classes can inherit properties and methods from other classes.
- Polymorphism: Objects from any class can be viewed as instances of
another class and can implement a shared interface.

4. What are the basic concepts used in Object-Oriented Programming


language?

Basic concepts include:

- Objects: Real-world entities are represented by instances of classes.


- Classes: Definitions of object structure and behavior, including
encapsulation:
- Encapsulation: Enclosing data and methods in a class.
- Abstract: Hide complex information and display only relevant characteristics.
- Inheritance: Generating new classes from existing ones.
- Polymorphism: Using one interface to express multiple underlying forms.

5. What is an Object?

Object is an instance of a class that represents some real-world entity. It has:

- Behaviour: Methods that indicate the operations performed on the data.


- State: The properties or data it contains

It is called encapsulation, or protection, because bundling the data (attributes) and


methods (functions) within a class usually limits direct access to some of the object's
components. It thus protects the internal state of the object from improper
interference and misuse.

7. What is a Class?

A class functions as an object's prototype or blueprint. It specifies the data members,


sometimes called as attributes, and methods that the objects derived from the class
will include.

8. What is Meta-Class?
A meta-class is a class that contains instances of other classes. It describes class
structure and behavior in the same way as classes describe object structure and
behavior.

9. What is Inheritance?

Inheritance is an OOP concept in which one class, commonly referred to as the


derived or child class, inherits the characteristics and methods of another class,
known as the base or parent class. It encourages code reuse and hierarchical
organization, among other things.

10. What Is the Difference Between Object-Based and Object-Oriented?

Object-Based Programming: Supports the concept of objects and encapsulation


but lacks the full-fledged features of OOP like inheritance and polymorphism. For
example, early JavaScript.

Object-Oriented Programming: All the features of OOP, such as inheritance,


polymorphism, and abstraction, are provided by it; therefore, it forms a
comprehensive framework for complex software designs.

11. What is Abstraction?

Abstraction is a process for simplifying complicated systems by focusing only on the


aspects that are critical to them and hiding the extraneous elements. It aids in the
management of complexity by presenting the user with only the relevant
information.

12. What is a Class Diagram?

A class diagram is a sort of diagram that graphically represents a system's structure


using the Unified Modeling Language. These include classes, properties, methods,
and connections such as inheritance and association.

13. What is Method Overriding?

Method Overriding: It is the operation of overwriting a method declared in the


parent class. The overridden method in the subclass has the same name, return
type, and parameters as the parent class.
14. What is Operator Overloading?

Operator overloading enables you to define or override the behavior of operators like
as +, -, and * for user-defined types. This allows operators to operate with custom
classes in a more semantic and natural way, akin to built-in ones.

15. What is Method Overloading?

This is the provision that allows various methods in a class to share the same name
but differ in their parameters. This can, in this situation, enable various functions
depending on the arguments supplied into the method.

16. What is Polymorphism?

Polymorphism is the ability for distinct objects to behave differently to the same
method call. It so permits methods to behave differently depending on the object
calling them.

17. What is Inheritance?

Inheritance is the process by which a new class, known as the derived or child class,
obtains the properties and methods of an existing class, known as the base or parent
class. It simplifies code reuse and establishes a hierarchical relationship.

18. What is Base Class?

A base class, also known as a parent or superclass, is the source of properties and
methods for other classes known as derived or subclasses.

19. What is Concrete Class?

A concrete class is a class that implements all of its methods and can be created
using an object. This contrasts with abstract classes, which cannot be instantiated.

20. What are Data Members?

Data members are class variables specified within a class to store its status or
properties. They represent the class's properties, which can be accessed through its
methods.
What is a Constructor?

A constructor is a specific method that is automatically called when an object is


created. It initializes the object's data members and returns the object to a valid
state.

What is a Destructor?

Dtor - A destructor is a particular method that is executed when an object is


destroyed. It is capable of releasing resources allocated throughout the object's
lifetime, such as memory or file handles.

23. What is a Global Variable?

A global variable is one that is declared outside of any function or class and can be
accessed from anywhere in the program. It has a worldwide scope.

24. What is a Local Variable?

A local variable is a declared variable that can only be accessed within the same
function or block. Its scope is local.

25. What is a Null Pointer?

A null pointer is the pointer that does not point to any valid memory address or
object. It generally means that a pointer does not currently reference something. In
modern C++, it is represented by `nullptr`.

26. What is a Pointer?

A pointer is a variable that contains memory address values. It immediately accesses


and modifies the value stored in that memory.

27. What Does Protected Mean?


In OOP, `protected` is an access specifier. In this, members are data or methods
can be accessed within that class, as well as it allows them in a derived (sub)classes,
but from outside the class it is not accessible, neither for the unrelated classes.

28. Describe What Kind of Copy the Default Assignment Operator "=".
Makes on Objects. (Shallow Copy or Deep Copy).

The default assignment operator (`=`) makes a **shallow copy**. It copies the
values of an object's data members but, for pointers, only copies the addresses. This
means that more than one object may point to the same location in memory.

29. Difference Between Simple Association and Aggregation.

- Simple Association: A general relationship between two classes where one


class uses or is associated with another-for example, a `Teacher` class
associated with a `Student` class.
- Aggregation: An association that is specific because one class contains
another class, while the latter can be an independent class (for example, a
`Department` class that aggregates `Professor` classes).

30. Friend Function Minimizes Encapsulation. Explain in Your Own Words.

In C++, a friend function can access a class's private and protected members,
reducing its encapsulation. While this allows for greater flexibility in class
interactions, it also exposes information that was intended to be kept private.

31. Program About Constant Member Function.

A constant member function promises not to change any member variables of the
class:

```cpp

class Example {

public:;

```

void display() const { /* method body */ };

};

```

32. Can Binary Operators Be Overloaded? How Can They Be Overloaded?


Give an Example for Any One Operator.
Yes, binary operators can be overloaded. Here's an example of overloading the `+`
operator:

```cpp

class Vector {

public:

Vector(int x, int y) : x(x), y(y) {}

Vector operator+(const Vector& other) const {

```

return Vector(x + other.x, y + other.y);

private:

int x, y;

};

```

34. Define "Object Orientation" in One Line.

Object Oriented Programming (OOP) is a programming paradigm for developing


software around data, or objects that incorporate both state and behavior.

35. What is an Abstraction? Why do we use it?

Abstraction is the process of extracting relevant information and suppressing the


rest. It is an abstraction that hides intricate implementation details while presenting
only an item's most significant features. We use abstraction to reduce the amount of
interactions with system-specific details, hence managing complexity by focusing on
high-level functions.

36. What is IS-A Relationship? Show How It Is Implemented Using C++ Code
(You Do Not Have to Give the Detailed Code Simply Show in a Single Line or
Two How It Will Be Implemented).
The IS-A relationship defines that one class is a type of another class. In C++, it is
implemented using the mechanism of inheritance:

```

class Animal {}; // Base class

class Dog : public Animal {}; // Derived class

```

37. Write the Syntax of Declaring a Pure Virtual Function in a Class.

The syntax for declaration of pure virtual function in a class is as follows:

```

class Base {

```

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

};

```

38. What is Direct Base Class?

A straight base class derives from another class without passing through any
intermediate classes.

39. How to Declare Template Class as Friend Class of Any Other Class.

To declare a template class as a friend of another class, you may use the following
syntax:

``.END

template <typename T>

class FriendClass; // Forward declaration

class OtherClass {

template <typename T>

friend class FriendClass; // Declare template class as friend


};

```

40. What is the Purpose of a Template Parameter?

A template parameter allows classes or functions to interact with a variety of data


types or values, increasing flexibility and reusability while eliminating the need for
duplicate code.

41. Can We Use the Compiler-Generated Default Assignment Operator In


Case Our Class is Using Dynamic Memory? Explain Why.

No, the compiler-generated default assignment operator should not be used in


dynamic memory classes because it only performs a shallow copy. This could lead to
double deletion issues or multiple objects referencing to the same memory location.
Thus, it is fair to create an assignment operator for deep copying.

42.Give the names of three ways to handle errors in a program.

- Exception Handling: Use try-catch-throw statements to handle runtime


errors.
- Error Codes: Function return error codes and check them to take care of
errors.
- Assertions: Using assertions to check for conditions that, by all means,
should not occur during running time of a program.

Given the code:

```cpp

class Base {

private:

void base1();

protected:

void base2();

public:

void base3();

};
class Derived : public Base {

private:

void derived1();

protected:

void derived2();

public:

void derived3();

};

int main() {

Derived *derived = new Derived();

return 0;

```

43. What is Generic Programming

Definition:

Generic Programming is a paradigm to write code independent of any actual data


type. This is generally done by using templates in C++ to specify functions and
classes which can handle any data type.

44. Criteria a Container Shall Meet to Implement Generic Algorithms

- Iterators: It must provide iterators to traverse all of its elements, which


allows generic algorithms to traverse the container.
- Access to Elements: The container should offer access to the elements
either directly or indirectly through iterators so that algorithms can read as
well as modify elements.
- Modifiability: The container must permit insertion, deletion, and even
modification of the elements so that it can work efficiently with generic
algorithms.

45. Is Deque a Bidirectional Container?


Solution:

Yes, `deque` is a bidirectional container. It allows the fast insertion and deletion at
both ends.

46. Sort the Following Data in the Order in Which Compiler Searches a
Function

Order:

- Standard Function
- Partial Specialization
- Complete Specialization
- Generic Template

47. Conflicting Issue Due to Multiple Inheritance

Conflicting Example:

Another common problem in multiple inheritance is the "Diamond Problem." The


"diamond problem" occurs when a derived class is inheriting from two classes, both
of which are inheriting from a base class that they both share. If another member is
called, ambiguity may occur because both paths contain different implementations
of the same member.

```cpp

class A {

public:

void foo() { cout<< "A's foo

";

}; class B : public A {

public: } ;

void foo() { cout<< "B's foo

"; }

};
class C : public A {

public:

void foo() { cout<< "C's foo

"; }

};

class D : public B, public C {

// Which foo() does D call?

};

```

48. Features of Virtual Functions

1. Dynamic Binding: Supports runtime polymorphism as the function to be called


can be resolved at runtime based on the actual object type.

2. Inheritance: Derived classes can override functions inherited from base classes
to provide specific implementations.

3. Access Through Base Class Pointer: Virtual functions ensure that the correct
function implementation is actually called even when a base class pointer is used to
access the derived-class instance.

49. Method Overriding of Class B Using Polymorphism

Code Snippet:

#include <iostream>

using namespace std;

acoास Α('.');

class A {

public:

virtual void method() { cout<< "A's method

"; }

};
class B : public A {

public:

void method() override { cout<< "B's method

"; }

};

// Pretends public access to method() declared

int main()

A* a = new B(); // Pointer to base class A, but object of derived class B

a->method(); // Calls B's method due to dynamic binding

delete a;

return 0;

**

END

- Add `virtual` keyword to the declaration of `method` function inside class `A` to
ensure runtime polymorphism.

50. Exceptions in Destructors

A destructor's exception may also cause complications. If an exception occurs during


the destruction of an object, the program quits instantly unless another exception is
being handled. In most circumstances, destructors should not throw exceptions.

Example:

```cpp

#include <iostream>

using namespace std;

class Example {
public:

~Example() {

```

try {

throw 1; // Throwing an exception in destructor

catch (.) {

// Handle the exception or log it

cout<< "Exception in destructor caught.";

};

};

int main()

Example* ex = new Example();

delete ex; // Destructor will be called

return 0;

End

If an exception is thrown in the destructor and is not caught, the program may end.
For example, in the code above, exceptions in the destructor are captured within the
destructor, preventing program termination.

51. What is Ternary Association?

A ternary association is a relationship between three different entities or classes.

52. What is Abstraction?

Definition:
Abstraction is the concept of not displaying complex implementation details but
instead presenting simply the features required for an object. Abstraction minimizes
complexity by focusing on key features rather than precise implementation details.

Useage:

- Reduce complexity by hiding unnecessary details.


- Improve code readability and maintainability.
- Encourage modular and reusable software design.

53. Derived Class Inheriting Base Class Features

Definition:

It is defined as the capacity of a derived class to inherit features from its base class.
A derived class can inherit aspects from the base class, such as data members and
member functions. This allows the derived class to use or override those features
and build upon them.

Example:

```cpp

#include <iostream>

using namespace std;

//

class Base {

public :

void baseMethod() { cout<< "Base method\

"; }

};

//

class Derived : public Base {

public :

void derivedMethod() { cout<< "Derived method\

"; }

};
//------------------------------------------------

int main() {

Derived obj;

``

obj.baseMethod(); // Inherited from Base

obj.derivedMethod(); // Specific to Derived

return 0;

```

Explanation:

- `Derived` inherits `baseMethod` from `Base` and can call it, but that doesn't
necessarily mean it doesn't have its own method `derivedMethod`.

You might also like