0% found this document useful (0 votes)
6 views

OOps Assignment

Uploaded by

niyasan663
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

OOps Assignment

Uploaded by

niyasan663
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Here are the expanded answers for each question:

Q1: How do we solve ambiguity problems in multiple inheritance using virtual functions?

Answer: In multiple inheritance, ambiguity arises when a derived class inherits from two or more base classes that have functions with the
same name. This leads to confusion about which function to call when accessed from the derived class. One way to solve this is by using
virtual functions. A virtual function in C++ is declared using the virtual keyword in the base class, and it allows the derived class to override the
function. When a virtual function is overridden in a derived class, the version of the function that is called is determined by the type of the object
at runtime, ensuring that the correct function is executed, regardless of the ambiguity. Another technique involves using virtual base classes,
which share a single instance of a base class between multiple derived classes, thus eliminating ambiguity.

Q2: Can a derived class inherit the Constructors and Destructors of a base class?

Answer: No, constructors and destructors of a base class are not inherited by the derived class. However, when an object of the derived class is
created, the constructors of the base class are automatically called to initialize the base class part of the derived object. Similarly, when a
derived class object is destroyed, the base class destructor is called after the derived class destructor to release any resources held by the base
class. The derived class can explicitly call the base class constructor using an initializer list and can invoke the destructor by letting the object
go out of scope or explicitly using the delete keyword in the case of dynamic memory allocation.
Q3: How is containership in C++ different from inheritance in the context of programming?

Answer: Containership, also known as composition, refers to a relationship where one class contains objects of another class. This is a "has-a"
relationship, meaning that the containing class has objects of another class as its member variables. In contrast, inheritance represents an "is-
a" relationship, where a derived class inherits properties and behaviors from a base class.

For example, a car object may contain an engine object (composition), while an electric car may inherit from a generic car class (inheritance).
Containership involves creating objects as members within a class, which are typically managed within the containing class, while in
inheritance, the derived class has direct access to the base class’s attributes and methods.

Q4: What are the implications of the following two definitions?

class A: public B, public C { //… };

class A: public C, public B { //… };

Answer: In C++, when a derived class inherits from multiple base classes, the order in which the base class constructors and destructors are
called is determined by the order of inheritance. In the first case (class A: public B, public C), the constructor of class B will be called first,
followed by the constructor of class C. Similarly, when an object of class A is destroyed, the destructor of class C will be called first, followed by
the destructor of class B.

In the second case (class A: public C, public B), the order is reversed—class C's constructor is called first, followed by class B's constructor. This
difference in the order of constructor and destructor calls can affect how resources are allocated and deallocated, especially when base
classes manage resources like memory or file handles.

Q5: In what order are the class constructors called when a derived class object is created?

Answer: When a derived class object is created, the constructors are called in the following order:

1. The constructor of the most base class (the farthest in the inheritance hierarchy) is called first.
2. The constructors of intermediate base classes are called next in the order they appear in the inheritance chain.

3. Finally, the constructor of the derived class is called. If multiple inheritance is involved, the constructors are called from left to right,
according to the order in which the base classes are listed in the derived class definition. This ensures that all base class components of the
derived object are initialized before the derived class’s specific members are initialized.

Q6: Describe how an object of a class that contains objects of other classes is created?

Answer: When an object of a class that contains objects of other classes (composition) is created, the process involves calling the constructors
of the member objects first, followed by the constructor of the containing class. This ensures that all member objects are fully initialized before
the main object starts its own initialization.

For example, if class Car contains an object of class Engine, when a Car object is created, the Engine constructor is called first, initializing the
engine, and then the Car constructor is executed. This behavior ensures that the Car object can use the Engine object in its initialization, as the
engine has already been set up.

Q7: What does this pointer point to?

Answer: The this pointer in C++ is an implicit parameter to all non-static member functions of a class. It points to the object on which the
member function is being called. In other words, it refers to the address of the current object. Using this, a member function can access the
calling object’s data members and other member functions.

For example, if an object obj of class MyClass calls a member function func(), inside the func() function, the this pointer will hold the address of
obj. This pointer is useful in chaining member function calls or returning the current object from a member function.

Q8: Why do we need a Virtual Function?

Answer: Virtual functions are needed to achieve runtime polymorphism in C++. Polymorphism allows one function to behave differently based
on the object that invokes it. A virtual function ensures that when a base class pointer or reference is used to refer to a derived class object, the
derived class's overridden function is called, not the base class's version. This is crucial for designing flexible and extensible systems,
especially when working with inheritance. Without virtual functions, the function call is resolved at compile time based on the type of reference,
preventing the derived class's function from being invoked dynamically.
Q9: When should a virtual function be "pure"? What are the implications of making a function a pure virtual function?

Answer: A virtual function should be made "pure" when the base class does not provide any meaningful implementation for the function, and it
is intended to be implemented by derived classes. A pure virtual function is declared by assigning 0 to the function, like this:

virtual void func() = 0;

Making a function pure virtual has the following implications:

The class that contains the pure virtual function becomes an abstract class, which means it cannot be instantiated directly.

Any derived class that inherits from an abstract class must provide an implementation for the pure virtual function. If it does not, the derived
class will also be abstract.
Q10: Explain with an example how you would create space for an array of objects using pointers.

Answer: In C++, you can dynamically allocate an array of objects using pointers and the new operator. For example:

ClassName* objArray = new ClassName[10];

This code creates an array of 10 objects of ClassName type in dynamic memory. The new operator allocates enough memory for 10 objects
and calls the constructor for each one. You can then access individual objects in the array using array notation, such as objArray[0], or pointer
arithmetic like *(objArray + i). When you're done with the array, you must free the memory using the

delete[] operator to avoid memory leaks:

delete[] objArray;

You might also like