Janu is developing a time conversion program that consists of a base class called Time and two derived classes named TimeToSeconds and TimeToMinutes.Time class: - Stores hours and minutes and has a virtual function display that prints the input time.TimeToSeconds class: Overrides the display method to convert the time to seconds and displays it.TimeToMinutes class: Overrides the display method to convert the time to minutes and displays it.To assist Janu, we need to create a program that accepts input for hours and minutes, creates instances of the derived classes, and displays the original time, the time in seconds, and the time in minutes.
Question
Janu is developing a time conversion program that consists of a base class called Time and two derived classes named TimeToSeconds and TimeToMinutes.Time class: - Stores hours and minutes and has a virtual function display that prints the input time.TimeToSeconds class: Overrides the display method to convert the time to seconds and displays it.TimeToMinutes class: Overrides the display method to convert the time to minutes and displays it.To assist Janu, we need to create a program that accepts input for hours and minutes, creates instances of the derived classes, and displays the original time, the time in seconds, and the time in minutes.
Solution
Sure, here is a simple implementation in C++:
#include<iostream>
using namespace std;
class Time {
protected:
int hours;
int minutes;
public:
Time(int h, int m) : hours(h), minutes(m) {}
virtual void display() {
cout << "Time: " << hours << " hours and " << minutes << " minutes" << endl;
}
};
class TimeToSeconds : public Time {
public:
TimeToSeconds(int h, int m) : Time(h, m) {}
void display() override {
cout << "Time in seconds: " << (hours * 60 * 60) + (minutes * 60) << endl;
}
};
class TimeToMinutes : public Time {
public:
TimeToMinutes(int h, int m) : Time(h, m) {}
void display() override {
cout << "Time in minutes: " << (hours * 60) + minutes << endl;
}
};
int main() {
int hours, minutes;
cout << "Enter hours: ";
cin >> hours;
cout << "Enter minutes: ";
cin >> minutes;
Time *t1 = new Time(hours, minutes);
Time *t2 = new TimeToSeconds(hours, minutes);
Time *t3 = new TimeToMinutes(hours, minutes);
t1->display();
t2->display();
t3->display();
delete t1;
delete t2;
delete t3;
return 0;
}
This program first asks the user to input hours and minutes. Then it creates instances of the Time, TimeToSeconds, and TimeToMinutes classes with the inputted hours and minutes. The display method is then called on each instance, which prints the time in the appropriate format.
Similar Questions
Develop a C++ program that uses a class called time A time is represented in hours, minutes and seconds. For example (3:35:50). Define three objects for time class and get the user input values for first object. Initialize the second object using constructors. Set the third object equal to the sum of other two and display the value of the new timings using operator overloading, destructors and friend functions. Sample Input 3:35:504:30:20Sample Output: 8:06:10.
Nandhini is tasked with creating a program to calculate and display the speed of a car based on the distance covered and the time taken.Write a program with two classes: Vehicle as the base class and Car as the derived class, which inherits the properties from the Vehicle class for calculating the speed. The base class fetches the input as a float value, whereas the derived class calculates and prints the output as a float value.Note: Use public inheritanceNote: This kind of question will help in clearing Wipro recruitment.Input format :The input consists of two floating-point numbers separated by a space. The first number represents the distance traveled by the car, and the second number represents the time taken to cover that distance, separated by a space.Output format :The output displays a single line of output, showing the speed of the car in kilometers per hour.Refer to the sample output for the formatting specifications.Code constraints :The input distance and time should be non-negative floating-point numbers.The input time should not be zero (to avoid division by zero).Sample test cases :Input 1 :15.0 5.0Output 1 :Speed of car: 3 km/hrInput 2 :100.23 96.0Output 2 :Speed of car: 1.04406 km/hrNote :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.
Dinesh is working in a supermarket and he is developing a program to calculate the cost of different types of items. Help him write the program that does the following:a) Create a base class, ItemType, with one virtual function double calculateAmount()b) Create a class called wooden that extends ItemType class with a number of items and cost as its private attributes. Obtain the data members and override the virtual function and calculate the total amount.c) Create a class called electronics that extends ItemType class with cost as its private attribute. Obtain the data member and override the virtual function and calculate the amount with 20% discount.Note: This question helps in clearing Infosys tests.Input format :The first line consists of an integer choice (1 or 2) representing the choice of item type.If the choice is 1 (wooden items), the next line consists of two space-separated integers: noOfItems and cost, representing the number of wooden items and their individual cost, respectively.If the choice is 2 (electronics), the next line consists of a single floating-point number cost, representing the cost of the electronic item.Output format :The output prints a floating-point number representing the calculated total cost of the chosen item type rounded off to two decimal places.Code constraints :10 < cost < 1060 < noOfItems < 20Sample test cases :Input 1 :15 840.5Output 1 :4202.50Input 2 :21800.56Output 2 :1440.45Note :
Define pure virtual functions. Write a C++ program to illustrate pure virtual functions.With an example, explain the syntax for passing arguments to base class constructors in multiple inheritance.
You're tasked with designing an employee management system for a company. You've created a base class Employee with virtual functions calculateSalary() and displayDetails(). Now, you need to implement derived classes FullTimeEmployee and PartTimeEmployee. How would you utilize virtual functions to calculate the salary and display details for each type of employee?
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.