Question

In: Computer Science

IN C++, IN C++, IN C++, IN C++, IN C++ Write the A and B classes,...

IN C++, IN C++, 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

Solutions

Expert Solution

Solution:

#include<iostream>

using namespace std;

class A

{

public:

virtual void hi()

{

cout<<"Hi A"<<endl;

}

void selam()

{

cout<<"Selam A"<<endl;

}

};

class B : public A

{

public:

void hi()

{

cout<<"Hi B"<<endl;

}

void selam()

{

cout<<"Selam B"<<endl;

}

};

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;

}

Output:

Please give thumbsup, or do comment in case of any query. Thanks.


Related Solutions

IN C++, IN C++, IN C++ Write the A and B classes, the properties of which...
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...
Objectives:  Write classes in C++  Use dynamic arrays  Write and read from files...
Objectives:  Write classes in C++  Use dynamic arrays  Write and read from files 1. WriteaclassGradeBookcontainingthefollowing: Private attributes: - courseName: a string representing the name of the course. - nbOfStudents: an integer representing the number of students enrolled in the course. The number of students is greater than or equal to 5. - grades: a double dimensional array of integers representing the grades of Test1, Test2 and Final of every student. It should be a dynamic array. Public...
Write a C# code that creates objects and classes with their member functions for KrisFlyer, a...
Write a C# code that creates objects and classes with their member functions for KrisFlyer, a Singapore Airlines Loyalty program. You are asked to write an inheritance hierarchy discount system that benefits KrisFlyer members program to calculate their profit. A brief about KrisFlyer is that it is useful for those who fly on Singapore Airlines (its partners like Virgin Australia and Air New Zealand) frequently. KrisFlyer miles can be earned through credit cards, flying and bonus miles promotions. The miles...
Provide the following parameter values for each of the IPv4 network classes, A, B, and C,...
Provide the following parameter values for each of the IPv4 network classes, A, B, and C, respectively. Ignore any reserved addresses. a. Class id b. Number of bits in network portion of address, including the class id c. Number of bits in host portion of address d. Number of distinct networks allowed e. Number of distinct hosts allowed per network
C++ - When working on this assignment, focus on memorizing the syntax for writing classes. Write...
C++ - When working on this assignment, focus on memorizing the syntax for writing classes. Write a simple class named Circle, with three private variables: doubles named x, y and radius. The center of the circle is denoted by coordinates (x,y), and the radius of the circle is denoted by radius. It should have public member functions with the following signatures: void setRadius(double r) void setX(double value) void setY(double value) double getRadius() double getX() double getY() double getArea() bool containsPoint(double...
Write a C++ program that will be an information system for Oregon State University using classes...
Write a C++ program that will be an information system for Oregon State University using classes as well as demonstrating a basic understanding of inheritance and polymorphism. You will create a representation of an Oregon State University information system that will contain information about the university. The university will contain a name of the university, n number of buildings, and m number of people. People can be either a student or an instructor. Every person will have a name and...
C++ HW Aim of the assignment is to write classes. Create a class called Student. This...
C++ HW Aim of the assignment is to write classes. Create a class called Student. This class should contain information of a single student. last name, first name, credits, gpa, date of birth, matriculation date, ** you need accessor and mutator functions. You need a constructor that initializes a student by accepting all parameters. You need a default constructor that initializes everything to default values. write the entire program.
Write a Simple C++ Code to show that the Constructors of Composed Classes Execute before Container...
Write a Simple C++ Code to show that the Constructors of Composed Classes Execute before Container Classes.
Write a C++ program to allow the user to: 1. Create two classes. Employee and Departments....
Write a C++ program to allow the user to: 1. Create two classes. Employee and Departments. The Department class will have: DepartmentID, Departmentname, DepartmentHeadName. The Employee class will have employeeID, emploeename, employeesalary, employeeage, employeeDepartmentID. Both of the above classes should have appropriate constructors, accessor methods. 2. Create two arrays . One for Employee with the size 5 and another one for Department with the size 3. Your program should display a menu for the user to do the following: 1....
Write two classes Class A and Class B in class A you should read from the...
Write two classes Class A and Class B in class A you should read from the keyboard a number and you should call a public method of the class B that will print on the screen whether the number that was read from the keyboard in class A is multiple of 7 and has the last digit 5.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT