0

guys here is the question
elow is the main function that creates the object of Mile and Kilometer classes. Write the definition of
Mile and Kilometer classes such that each statement in the main function is executed without any error.
Both classes must have default constructor that initialize the private data member of both classes with
zero.

B
main() {
float k = 10, k1, k2 = 30 ;
Kilometer km = k;
km.display(); //display the value of data member of km and also how many objects of
// Kilometer class has been created
k1 = km;
cout<<”k1 = ”<<km<<endl;
km = k2;
km.display();
float m = 13.5, m1;
Mile mile = 25;
Km = mile;
Km.display();
Kilometer km1;
km1 = 15;
mile = km1;
mile.display(); //display the value of data member of mile and also how many objects of
// Miles class has been created
m1 = mile;
cout<<”m1 = ”<<m1<<endl;

my question is very simple i know how to convert a usertype to float type etc but how do u covert a user defined type to basic type with out the use of a constructor il be glad if u ppl can help me

Edited by a.muqeet khan: n/a

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by a.muqeet khan
1

You can do it through a conversion operator :

class PositiveInteger{
  int i;
public:
  PositiveInteger(int i1 = 0 ) 
   { i = i1 <  0 ? 0 : i1; }
  operator int(){ return i; }
};
int main(){
 PositiveInteger i(23);
 int j = i; //convert to int
}

Edited by firstPerson: n/a

Votes + Comments
I learned something.
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.