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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.