In: Computer Science
IN C++, IN C++, IN C++
Write the A and B classes, the properties of which will produce
the expected output with the test code provided, and the
characteristics of which are specified below. Note the types of
pointers used in the test code.
Methods of class A:
- void hi () - Prints “Hi A” on the screen.
- void selam() - Prints "Selam A" on the screen.
Class B must inherit class A. Class methods should be written so
that access levels can produce the desired output in the test code
provided.
Test code:
int main() {
A* ptrA1 = new A{};
A* ptrB1 = new B{};
B* ptrB2 = new B{};
ptrA1->selam();
ptrA1->hi();
ptrB1->selam();
ptrB1->hi();
ptrB2->selam();
ptrB2->hi();
return 0;
}
Expected output:
Selam A
Hi A
Selam A
Hi B
Selam B
Hi B
//C++ CODE TO COPY//
//precompiled used headers
#include<iostream>
using namespace std;
//parent class A
class A
{
//class functions
//public virtual function hi()
public:
virtual void hi()
{
cout << "\nHi A";
}
//public function selam()
void selam()
{
cout << "\nSelam A";
}
};
//Derived class B inherited fronm A
class B : public A
{
//public functions
public:
void hi()
{
cout << "\nHi B";
}
void selam()
{
cout << "\nSelam B";
}
};
//main() test function
int main()
{
A* ptrA1 = new A{};
A* ptrB1 = new B{};
B* ptrB2 = new B{};
ptrA1->selam();
ptrA1->hi();
ptrB1->selam();
ptrB1->hi();
ptrB2->selam();
ptrB2->hi();
return 0;
}
//PROGRAM OUTPUT//
//COMMENT DOWN FOR ANY QUERIES...
//HIT A THUMBS UP IF YOU DO LIKE IT!!!