Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Design two classes, namely, Kilometers and Miles, for storing distance in kilometers and miles as private data using respective constructors. In a sub function CONVERT (not member function) convert

i) The kilometer from the object into miles

ii) Miles from the object into kilo meter

The converted values are displayed in the sub function itself. [Hint: 1 km = 0.62137 miles and 1 mile = 1.60934 km]

What I have tried:

Yes I made classes but I got stuck how to create function
Posted
Updated 29-May-22 1:17am

Creating a function is simple:
C++
void myFunction() {
  ... Code to be executed ...
}


So I assume it the "Code to be executed" bit that is giving you trouble.

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Their are different solutions.
a) create 2 CONVERT function with different input parameters. That is overloading.
b) miles and kilometers have a common base class and you check the object with instanceof operator. Read the code carefully.
c) the CONVERT function has two pointers inputs and check with any is nonnull and does the job

Somehow strange best is to ask your teacher how to solve ist.
 
Share this answer
 
Given the constraints, an idiomatic way to do this in C++ would be Solution 2(a). Here's the interface; you have to provide the implementation:
C++
class Kilometers
{
public:
   Kilometers(int kilometers);
   int value() const;
private:
   int kilometers_;
};

class Miles
{
public:
   Miles(int miles);
   int value() const;
private:
   int miles_;
};

void Convert(const Kilometers& kilometers);
void Convert(const Miles& miles);
Does that count as one Convert function or two? If two, I'd probably go with Solution 2(c), which could even provide two outputs if both pointers were valid.

Another question is whether the two constructors should be tagged explicit, which is described here[^]. Ignore what it says about C++17 and C++20 and just focus on what the presence or absence of explicit does when a constructor takes one argument. I probably wouldn't use it in these cases, but it's always something to decide when a constructor takes one argument.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900