Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hierarchy/Inheritance/Polymorphism 2. Write a snippet of code to declare (.h) an

ID: 3833566 • Letter: H

Question

Hierarchy/Inheritance/Polymorphism

2. Write a snippet of code to declare (.h) and the implement (.cpp) the family of classes in quality control domain, including

-base class “Test

-derived classes RegressionTest, loadTest

The base class should have a virtual boolean method called run which is overridden in the sub-class, where specific logic is placed(//some code). As an exercise, let the method return true/false randomly.

Then in main, declare a pointer to the base class Test and use it to instantiate and run loadTest. Then use the same pointer to instantiate and run a RegressionTest.

Explanation / Answer

CODE:

#include<iostream>
using namespace std;

class Test
{
public:
   virtual bool run() {
   cout<<" In Base class ";
   return true;
   }
};

class RegressionTest: public Test
{
public:
   bool run() {
   cout<<"In RegressionTest ";
   return false;
   }
};

class LoadTest: public Test
{
public:
   bool run() {
   cout<<"In LoadTest ";
   return true;
   }
};

int main(void)
{
   Test *bp = new LoadTest;
   cout << bp->run() << endl; // RUN-TIME POLYMORPHISM
   bp = new RegressionTest;
   cout << bp->run() << endl; // RUN-TIME POLYMORPHISM
   return 0;
}

OUTPUT:
In LoadTest
1
In RegressionTest
0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote