C/C++ Tricky Programs (Set 4)
Post
Cancel

# C/C++ Tricky Programs (Set 4)

• ## Name the Operators that cannot be Overloaded.

1 2 3 4 5 6 sizeof – sizeof operator . – Dot operator .* – dereferencing operator -> – member dereferencing operator :: – scope resolution operator ?: – conditional operator 

• ## Function can be overloaded based on the parameter which is a value or a reference. Explain if the statement is true.

1 2 False. Both, Passing by value and Passing by reference look identical to the caller 

By overloading standard operators on a class, we can extend the meaning of these operators, so that they can also operate on the other user-defined objects.
Function overloading allows us to reduce the complexity of the code and make it more clear and readable as we can have the same function names with different argument lists.

• ## What are the advantages of Inheritance?

Inheritance allows code re-usability, thereby saving time on code development.
By inheriting, we make use of a bug-free high-quality software that reduces future problems.

Yes.

• ## What are Multiple Inheritances (virtual inheritance)? What are its advantages and disadvantages?

In multiple inheritances, we have more than one base classes from which a derived class can inherit. Hence, a derived class takes the features and properties of more than one base class.
For Example, a class driver will have two base classes namely, employee and a person because a driver is an employee as well as a person. This is advantageous because the driver class can inherit the properties of the employee as well as the person class.
But in the case of an employee and a person, the class will have some properties in common. However, an ambiguous situation will arise as the driver class will not know the classes from which the common properties should be inherited. This is the major disadvantage of multiple inheritances.

• ## Does a derived class inherit or doesn't inherit?

When a derived class is constructed from a particular base class, it basically inherits all the features and ordinary members of the base class. But there are some exceptions to this rule. For instance, a derived class does not inherit the base class’s constructors and destructors.
Each class has its own constructors and destructors. The derived class also does not inherit the assignment operator of the base class and friends of the class. The reason is that these entities are specific to a particular class and if another class is derived or if it is the friend of that class, then they cannot be passed onto them.

• ## What are Virtual Functions?

A virtual function allows the derived classes to replace the implementation provided by the base class.
Whenever we have functions with the same name in the base as well as derived class, there arises an ambiguity when we try to access the child class object using a base class pointer. As we are using a base class pointer, the function that is called is the base class function with the same name.
To correct this ambiguity we use the keyword “virtual” before the function prototype in the base class. In other words, we make this polymorphic function Virtual. By using a Virtual function, we can remove the ambiguity and we can access all the child class functions correctly using a base class pointer.

• ## What is the use of private destructor?

Whenever we want to control destruction of objects of a class, we make the destructor private. For dynamically created objects, it may happen that you pass a pointer to the object to a function and the function deletes the object. If the object is referred after the function call, the reference will become dangling.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 // CPP program to illustrate // Private Destructor #include <iostream> // A class with private destuctor class Test { private: ~Test() {} friend void destructTest(Test*); }; // Only this function can destruct objects of Test void destructTest(Test* ptr) { delete ptr; } int main() { // create an object Test* ptr = new Test; // destruct the object destructTest(ptr); return 0; } 

• ## Virtual Function in C++

A virtual function is a member function which is declared within a base class and is re-defined(Overriden) by a derived class. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class’s version of the function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 // CPP program to illustrate // concept of Virtual Functions #include <iostream> using namespace std; class base { public: virtual void print() { cout << "print base class" << endl; } void show() { cout << "show base class" << endl; } }; class derived : public base { public: void print() { cout << "print derived class" << endl; } void show() { cout << "show derived class" << endl; } }; int main() { base* bptr; derived d; bptr = &d; // virtual function, binded at runtime bptr->print(); // Non-virtual function, binded at compile time bptr->show(); } 

#### Output

1 2 print derived class show base class