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

Objective class hierarchy and polymorphism. Write a snippet of code to declare(

ID: 3835792 • Letter: O

Question

Objective class hierarchy and polymorphism. Write a snippet of code to declare( what would go into the .h file) and then implement ( what would go into the .cpp file) the family of classes in Quality Control domain, including:

*the base class called "Test"

*two derived classes corresponding to the specialized test: RegressionTest and LoadTest.

The base class should have a virtual Boolean method called run which is overridden in the sub-classes, 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

Please create 2 files, abc.h and main.cpp

abc.h

#ifndef abc_h

#define abc_h

#include <stdlib.h>

#include <time.h>

#include <iostream>

using namespace std;

class test

{

public:

  

virtual bool run()

{

cout<<" In base class ";

int temp;

temp=rand()%2;

if(temp==1)

return 1;

return 0;

}

};

class RegressionTest: public test

{

bool run()

{

cout<<" In derived class ";

int temp;

temp=rand()%2;

if(temp==1)

return 1;

return 0;

}

};

class LoadTest: public test

{

bool run()

{

cout<<" In derived class ";

int temp;

temp=rand()%2;

if(temp==1)

return 1;

return 0;

}

};

#endif /* abc_h */

main.cpp

#include "abc.h"

int main(int argc, const char * argv[])

{

srand(time(NULL)); //this is for random number generation

test *t; //base class pointer

RegressionTest r; //derived class object

LoadTest l; //derived class object

  

t=&l; //load test

cout<<"After late binding, base class's run method: "<<t->run()<<endl;

  

t=&r; //regression test

cout<<"After late binding, base class's run method: "<<t->run()<<endl;

return 0;

}

output:

After late binding, base class's run method:

In derived class

1

After late binding, base class's run method:

In derived class

1