Question

A.3 - 10 pts It is legal to have a pure virtual destructor in a C++ class as far as that destructor has a function body. Plea

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANS 1 : Yes , it is legal to have a pure virtual destructor in a C++ class as far as the destrutor has a function body.

Explanation : Here , when we come to the pure virtual functions here the pure virtual functions are the functions that are declared in a base class for which the definition is not relative to the base class.So, in these cases the compiler requires each derived class to either define the function or redeclare it is a pure virtual function.

- And when it comes to a pure virtual destructor here we know that the destructor is different from the other functions because these are not "overridden" but the actual process what will be done is that these are called in the reverse order of the class derivation i.e; here the derived class destructor will be called before the base class destructor and in case if we don't provide any function body then it will not find any object to destruct and then it will raise an error .So, if there is a function body then it is easy to make a pure virtual destructor by a simple definition.

Code :

class Base_class {
public:
  virtual ~Base_class() = 0;
};

Base_class::~Base_class() {}

class Derived_class : public Base_class {};

int main() { 
  
  Derived_class d; 
}

Ans 2: We know that , the pure virtual functions are the virtual functions that are simply declared without any implementation and they are initialized to 0 and here when we come the function body implementation in c++ here the implementation can be used in the situation i.e; here if you have a certain program that will be executed by a derived class but here it was not wanted to be executed directly but it should be executed in such a way that it was forced to be overridden in such cases the implementation of pure virtual functions are used and here it forces the derived ones to override the methods.

Code :

/*

#include <iostream>
using namespace std;

class Parent
{
public:
Parent(){}
virtual void fun() = 0;
};

void Parent::fun()
{
cout<<"Hi";
}

class Children : public Parent
{
public:
Children(){}

void fun(){
cout<<"Hello";
}
};

int main(int argc, char const *argv[])
{
Children a;
a.Parent::fun();
return 0;
}

*/

Output :

Hi ..Program finished with exit code 0 Press ENTER to exit console.

Add a comment
Know the answer?
Add Answer to:
A.3 - 10 pts It is legal to have a pure virtual destructor in a C++...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • please write the code in C++ 2 Base class File 3 Derived class PDF 3.1 Class...

    please write the code in C++ 2 Base class File 3 Derived class PDF 3.1 Class declaration • The class PDF inherits from File and is a non-abstract class 1. Hence objects of the class PDF can be instantiated. 2. To do so, we must override the pure virtual function clone) in the base class • The class declaration is given below. • The complete class declaration is given below, copy and paste it into your file. . It is...

  • C++ help This assignment gives you practice with inheritance and pure virtual functions. You will implement...

    C++ help This assignment gives you practice with inheritance and pure virtual functions. You will implement some code that could be part of a program to play a popular card game called “Crazy 8’s”, which is the basis for the game “Uno”. You will start to implement the rules, as described at: https://www.pagat.com/eights/crazy8s.html . Note that the inheritance relationship below is also based on the organization of card games described on that website. Requirements: your work will be split into...

  • write C++ program Create a class named Bicycle with one public pure virtual function showFeatures(). Create...

    write C++ program Create a class named Bicycle with one public pure virtual function showFeatures(). Create the two classes MountainBike and BeachCruiser that both publicly inherit from Bicycle. Please add necessary member variables and functions to each class. The showFeatures() function shows the details about a bicycle. should have main.cpp Bicycle.h Bicycle.cpp  MountainBike .h  MountainBike.cpp  BeachCruiser.h  BeachCruiser.cpp

  • C++ Could you check my code, it work but professor said that there are some mistakes(check...

    C++ Could you check my code, it work but professor said that there are some mistakes(check virtual functions, prin() and others, according assignment) . Assignment: My code: #include<iostream> #include<string> using namespace std; class BasicShape { //Abstract base class protected: double area; private:    string name; public: BasicShape(double a, string n) { area=a; name=n; } void virtual calcArea()=0;//Pure Virtual Function virtual void print() { cout<<"Area of "<<getName()<<" is: "<<area<<"\n"; } string getName(){    return name; } }; class Circle : public...

  • please write in c++. 4 Derived class JPG 4.1 Class declaration • The class JPG inherits...

    please write in c++. 4 Derived class JPG 4.1 Class declaration • The class JPG inherits from File and is a non-abstract class. 1. Hence objects of the class JPG can be instantiated. 2. To do so, we must override the pure virtual function clone() in the base class. • The class declaration is given below. class JPG : public File { public: JPG(const std::string& n, int n, int w, const double rgb()) : File(...) { // ... } *JPG()...

  • 4.a) 4.b> 4.c) C++ Programming Lab Exercise 09 Inheritance. Friend Functions, and Polymorphism (virtual functions) 4.a)...

    4.a) 4.b> 4.c) C++ Programming Lab Exercise 09 Inheritance. Friend Functions, and Polymorphism (virtual functions) 4.a) Run the following code and observe the output. #include <iostream> #include <string> using namespace std; class Vehicle public: void print() { cout << "Print: I am a vehicle. \n"; } void display() { cout << "Display: I am a vehicle. \n"; } }; class Car: public Vehicle { public: void print() { cout << "Print: I am a car.\n"; } void display() { cout...

  • Question 1 2 pts The preprocessor executes after the compiler. False True Question 2 2 pts...

    Question 1 2 pts The preprocessor executes after the compiler. False True Question 2 2 pts What code is human-readable and follows the standards of a programming language? Secret code Source code Key code None of these Machine code Question 3 2 pts What is the symbol that marks the beginning of a one line comment? Question 1 2 pts The preprocessor executes after the compiler. True False Question 5 2 pts A statement that may be used to stop...

  • Question 1 10 pts Inheritance is a capability of C++ to represent increased specialization that is...

    Question 1 10 pts Inheritance is a capability of C++ to represent increased specialization that is often found in real world relationships. True False Question 2 10 pts Inheritance between a child and parent object exhibits an 'relationship. Question 3 10 pts As you move down an inheritance relationship from parent to child, you typically move from a general to specific description. True False Question 4 10 pts The syntax for declaring DerivedClass to publicly inherit from BaseClass is given...

  • .Your solution must include header, implementation file, and test files .In C++ write a code to...

    .Your solution must include header, implementation file, and test files .In C++ write a code to Consider a graphics system that has classes for various figures rectangles, squares, triangles, circles, and so on. For example, a rectangle might have data members for Height, Width and center point, while a square and circle might have only a center point and an edge length or radius. In a well-designed system, these would be derived from a common class, Figure. You are to...

  • C++ assignment help! The instructions are below, i included the main driver, i just need help...

    C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT