Question

In: Computer Science

1 What is a pure virtual function? Why would you define a pure virtual function? Give...

1 What is a pure virtual function? Why would you define a pure virtual function? Give an example of a pure virtual function.

2 When a function has an object parameter that it doesn't modify,
what is the best way to declare the parameter (in the function
signature), and why?

3 Show an example using a function with a string object parameter.

a) When a class has objects as data members, why should its
constructors use initialization lists (member and initializer
syntax) vs. assignment to the members in the body of the ctor?
  
b) Name two cases where you *must* use initialization lists in
constructors (as opposed to assignment in the ctor body).

4 When a function has an object parameter that it doesn't modify,
what is the best way to declare the parameter (in the function
signature), and why?
Show an example using a function with a string object parameter.

Solutions

Expert Solution

Pure virtual function:

It is a type of virtual function whose initialization is done with zero, a pure virtual function is not defined in the base class as it is redefined in the derived class. A base class is said to be an abstract class.

A virtual keyword is used to define a pure virtual function.  

Syntax:

class Parent

{

public:

virtual void see() = 0;

};

Pure virtual function is basically used to create abstract class. A class with at least one pure virtual function is an abstract class.

Also, an object of an abstract class cannot be created, and the definition of a pure virtual function is given in the derived class. Hence, an object of the derived class is used to call a pure virtual function.

Example:

//header file declared

#include <iostream>

//namespace used

using namespace std;

//base class

class Parent

{

//access modifier   

public:

//declaration of pure virtual function

virtual void see() = 0;

};

//inheritance

class Child : public Parent

{

//access modifier   

public:

//definition of pure virtual function

void see()

{

//displaying message

cout<<"pure virtual function is defined in derived class";

}

};

//main function

int main()

{

//creating object of derived class

Child ob;

//calling of pure virtual function

ob.see();

return 0;

}

Output:


Related Solutions

What will happen if you forget to implement the pure virtual function in the derived class?...
What will happen if you forget to implement the pure virtual function in the derived class? How the following are achieved in C++? static polymorphism and dynamic polymorphism. What is the advantage of defining the member functions outside a class rather than defining inside a class?
In C++ write a program with a base class thet has a pure virtual function SALARY,...
In C++ write a program with a base class thet has a pure virtual function SALARY, and two derived classes. In the first derived class salary is increased by 20%, in the second derived class salary is increased by 30%
How would you define health? Why would you define it that way?
How would you define health? Why would you define it that way?
Define and explain what a pure culture is?
Define and explain what a pure culture is?
1. What is reproductive isolation? Why is it important to speciation? 2. Define and give an...
1. What is reproductive isolation? Why is it important to speciation? 2. Define and give an example of the following reproductive isolating mechanisms. Pre-zygotic Mechanisms - isolate two populations without zygotes forming Timing (Temporal isolation) – Behavior (Behavioral isolation) – Habitat (Ecological isolation) – Physically Incompatible (Mechanical isolation) – Post-zygotic Mechanisms – isolate two populations when zygotes form             Hybrids fail to develop (Hybrid Inviability) –             Hybrid Infertility – 3. How does geographic isolation lead to speciation and adaptive...
Why are public relations so difficult to define? How would you define it based on what...
Why are public relations so difficult to define? How would you define it based on what you know? Answer must be 20 lines.
Why would you use "using namespace" inside of your function instead of the above main?- Give...
Why would you use "using namespace" inside of your function instead of the above main?- Give examples
What is a "virtual image"? Give a real life example.
What is a "virtual image"? Give a real life example.
1. What specific numerical evidence would you give to explain why the Great Depression was so...
1. What specific numerical evidence would you give to explain why the Great Depression was so much worse than the Great Recession? 2. What is the key side (supply or demand) of the economy for Keynesian economists? What assumption about prices leads them to the emphasis? What is the key side of the economy (supply or demand) for classical economists? What assumption about prices lead them to this emphasis?
1. How do you define a project? Would you like to be a project manager? Why?
1. How do you define a project? Would you like to be a project manager? Why?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT