Write a program that takes the speed (in km/h) and the time (in hours) as input and calculates the distance traveled using the formula: distance = speed * time.Create a class called Distance with speed and time as public attributes. Using a friend function, the program should calculate the total distance.Input format :The input consists of two integers: speed and time separated by a space.
Question
Write a program that takes the speed (in km/h) and the time (in hours) as input and calculates the distance traveled using the formula: distance = speed * time.Create a class called Distance with speed and time as public attributes. Using a friend function, the program should calculate the total distance.Input format :The input consists of two integers: speed and time separated by a space.
Solution
Sure, here is a simple C++ program that does what you asked for:
#include<iostream>
using namespace std;
class Distance {
public:
int speed;
int time;
friend int calculateDistance(Distance d);
};
int calculateDistance(Distance d) {
return d.speed * d.time;
}
int main() {
Distance d;
cout << "Enter speed and time separated by space: ";
cin >> d.speed >> d.time;
cout << "The total distance is: " << calculateDistance(d) << " km";
return 0;
}
This program first includes the necessary libraries. Then, it defines a class Distance with speed and time as public attributes. A friend function calculateDistance is declared which takes an object of Distance as argument and returns the product of speed and time.
In the main function, an object d of class Distance is created. The user is asked to enter speed and time separated by space. These values are stored in the attributes of d. Then, the calculateDistance function is called with d as argument and the returned value (which is the total distance) is printed.
Similar Questions
Given that distance = speed x time, write a program that asks for the user for a speed (in mph) and time (in hours) and will calculate and report how far that person can travel going that speedfor the provided time.The program should accept and correctly utilize fractional (i.e. decimal) inputs.
Shwetha is fascinated by physics and wants to calculate the distance covered by an object in motion. She has the initial speed, time, and acceleration values. The program takes these values as input, calculates the distance using the formula (distance = initial speed * time + 0.5 * acceleration * time2), and displays the result in meters. Can you help her by writing a program?
What line of code would print the speed in km/hr if the distance is given in km and the time is given in minutes? (Speed = distance/time)println(distance/(time/60));println(distance/time);println(distance*(time*60));println(distance*time);
A robot moves at a constant speed. The distance it moves and the time it spends moving are shown in this table:Time (in minutes) Distance (in kilometers) Write an equation to describe the relationship between , the time, and , the distance.
Jack crosses a street that is ‘X’ meter long in ‘Y’ minutes. Build a pseudocode, to compute the speed in km/hr, given X and Y. (Formula: Speed=Distance/time)Explanation : Suppose the input for X is 500 and Y is 10, then the speed will be calculated asSpeed = 500 / 10 meter/minute = 50 m/mim. But the output should be in km/hr. 1 m/min=0.06 km/hr.Hence 50 m/min = 50 * 0.06 = 3 km/hrHence output should be X/Y * 0.06.
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.