Knowee
Questions
Features
Study Tools

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.

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.

🧐 Not the exact question you are looking for?Go ask a question

Solution 1

Sure, here is a simple C++ program that does what you asked for:

#include<iostream>
using namespace std;

class Distance {
public:
    float speed;
    float time;

    Distance(float s, float t) {
        speed = s;
        time = t;
    }

    friend float calculateDistance(Distance d);
};

float calculateDistance(Distance d) {
    return d.speed * d.time;
}

int main() {
    float speed, time;
    cout << "Enter speed in km/h: ";
    cin >> speed;
    cout << "Enter time in hours: ";
    cin >> time;

    Distance d(speed, time);
    cout << "The distance traveled is: " << calculateDistance(d) << " km" << endl;

    return 0;
}

This program first asks the user to input the speed and time. It then creates an instance of the Distance class with these values. The calculateDistance function is then called with this instance as an argument, and it returns the product of the speed and time. This value is then printed to the console.

This problem has been solved

Solution 2

Here is a simple C++ program that accomplishes this:

#include<iostream>
using namespace std;

class Distance {
public:
    float speed;
    float time;
};

float calculateDistance(Distance d) {
    return d.speed * d.time;
}

int main() {
    Distance d;
    cout << "Enter speed in km/h: ";
    cin >> d.speed;
    cout << "Enter time in hours: ";
    cin >> d.time;
    cout << "The distance traveled is: " << calculateDistance(d) << " km" << endl;
    return 0;
}

This program first defines a class Distance with speed and time as public attributes. Then it defines a function calculateDistance that takes an object of type Distance as an argument and returns the product of speed and time. In the main function, it prompts the user to enter the speed and time, calculates the distance using the calculateDistance function, and then prints the result.

This problem has been solved

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?

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.

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);

What mathematical formula or equation do we need to use in order to solve this problem?"Suppose that you are travelling with your family to Batangas City Pier on a weekend, for a distance of about 150 km and your average speed is 75 km/h."Question 4AnswerA.Speed = Distance ÷ TimeB.Time = Distance ÷ SpeedC.Distance = Speed x TimeD.Distance = Time ÷ Speed

1/3

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.