In: Computer Science
What is the advantage of defining the member functions outside a class rather than defining inside a class?
How the following are achieved in C++?
a) static polymorphism.
b) dynamic polymorphism
What will happen if you forget to implement the pure virtual function in the derived class?
How a function templates differs from function overloading?
Now we will see the solution of each question one by one
Let take the first one
1. Functions can be defined or declared inside the
class definition. But Many functions have very large defintion that
makes it complex to readable.In this case we can define the class
outside the class using scope resolution operator.
So the flexiblity of code is the biggest advantages
2. We know that Polymorphism means many form. Now
here we have two types of polymorphism
a. Static polymorphism or we can say it as compile time
polymorphism. Now this type of polymorphism can be achieved by
function overloading and operator overloading.
In function overloading we have multiple function with same name
but different parameters.
now in operator overloading we can make single operator to perform
multiple operations.
b. Dynamic polymorphism is also called as run time polymorphism.
And it can be achieved by function overriding.
In function overriding we have multiple function with same name and
same arguments. but they are defined in different classes.
3. If you forget to implement the pure virtual
function in the derived class, then derived class becomes the
abstract class. while compilation we get a error like this:
Compiler Error: cannot declare variable 'variable_name' to be of abstract type 'Derived' because the following virtual functions are pure within 'Derived': virtual void Base::function_name() 4. We know that both funtion overloading and template are the types of Polymorphism.Now template is better if we provide same action on different types.That means template can operate on any data.In function overloading we have multiple functions with same name and different arguments. That means function overloading is used when multiple functions do similar operations. but in function template multiple functions do identical operation. In case you face any difficulty for the soution, let me know in the comment section.