Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,095 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,637 people online right now. Registration is fast and FREE... Join Now!




Type conversion in classes

 
Reply to this topicStart new topic

Type conversion in classes

naash
1 Oct, 2007 - 07:15 AM
Post #1

D.I.C Head
**

Joined: 25 Aug, 2007
Posts: 77


My Contributions
Q) Write a type conversion function in C++ for classes

MKS distance ,i.e, (METERS KILOGRAMS SECONDS )
FPS distance ,i.e, (FEET POUNDS SECONDS )


Here's my code. Not able to compile !

Getting following 3 compilation errors :
1) 'get_f' is not a member of 'FPS'
2) couldn't find a match for 'FPS :: FPS()'
3) couldn't find a match for 'MKS :: MKS()'

HELP !!!

CODE


#include<iostream.h>
#include<conio.h>

class FPS;

class MKS
{
  float m,k;
  int s;
  public :
    MKS(float M,float K,int S)
    {
      m=M;
      k=K;
      s=S;
    }
    float get_m()
    {
      return m;
    }
    MKS(FPS p)
    {
      float F=p.get_f();
      int dec=F;
      float frac = F-(float)dec;
      dec=(float)dec*12*2.5/100;
      frac=frac*2.5/100;
      m=dec+frac;
    }
    friend ostream & operator << (ostream & dout,MKS p)
       {
     dout<<"\nDistance : "<<p.m<<" m";
     dout<<"\nWeight : "<<p.k<<" kg";
     dout<<"\nTime : "<<p.s<<" s";
     return dout;
       }
};

class FPS
{
  float f,p;
  int s;
  public :
    FPS(float F,float P,int S)
    {
      f=F;
      p=P;
      s=S;
    }
    float get_f()
    {
      return f;
    }
    FPS (MKS q)
    {
      float M=q.get_m();
      int dec=M;
      float frac = M-(float)dec;
      dec=(float)dec*100/(12*2.5);
      frac=frac*100/2.5;
      f=dec+frac;
    }
    friend ostream & operator << (ostream & dout,FPS q)
       {
     dout<<"\nDistance : "<<q.f<<" ft";
     dout<<"\nWeight : "<<q.p<<" pounds";
     dout<<"\nTime : "<<q.s<<" s";
     return dout;
       }
};

void main()
{
  FPS ob1,ob2(10,20,30);
  MKS obj1,obj2(10,20,30);
  clrscr();
  ob1=obj2;
  cout<<ob1;
  obj1=ob2;
  cout<<obj1;
  getch();
}



User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Type Conversion In Classes
1 Oct, 2007 - 12:00 PM
Post #2

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
you cannot call for the instantiation of two objects of type FPS like this: FPS ob1,ob2(10,20,30); break up the calls into two lines. you will need to call both of them using the FPS(float,float,int) constructor that you have defined. right now, the you are trying to call for the instantiation of one of the objects with an argument-free (default) constructor, but the compiler does not provide this if you have declared any other constructors - you need to write your own, or only use the constructors you've already written.

in your MKS class, you're attempting to access member functions from the FPS class that haven't been defined yet.

-jjh
User is offlineProfile CardPM
+Quote Post

naash
RE: Type Conversion In Classes
1 Oct, 2007 - 06:53 PM
Post #3

D.I.C Head
**

Joined: 25 Aug, 2007
Posts: 77


My Contributions
QUOTE(jjhaag @ 1 Oct, 2007 - 01:00 PM) *

you cannot call for the instantiation of two objects of type FPS like this: FPS ob1,ob2(10,20,30); break up the calls into two lines. you will need to call both of them using the FPS(float,float,int) constructor that you have defined. right now, the you are trying to call for the instantiation of one of the objects with an argument-free (default) constructor, but the compiler does not provide this if you have declared any other constructors - you need to write your own, or only use the constructors you've already written.

in your MKS class, you're attempting to access member functions from the FPS class that haven't been defined yet.

-jjh



I made default constructor in my program. So from 3 errors to 1 error.
What I don't understand is that how come compiler gives get_f() not defined in FPS when I've already defined one ????

This post has been edited by naash: 1 Oct, 2007 - 06:54 PM
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Type Conversion In Classes
1 Oct, 2007 - 07:41 PM
Post #4

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
the problem is that you haven't ALREADY defined it, at least as far as the compiler is concerned. the definition of that function comes later in the file.

there are a number of solutions, but the easiest one in my mind would be to put the prototypes for all of your functions and constructors in the classes, then define them outside of the class. you will need to specify which class the function belongs to, but it should work:

CODE
class MKS {
   //data members
   MKS(FPS p);
   float get_m();
}

class FPS{
   //data members
   MKS(MKS q);
   float get_f();
}

float MKS::get_m() {return m;}
float FPS::get_f() {return f;}
MKS::MKS(FPS p) {
    float F=p.get_f();
    int dec=F;
    float frac = F-(float)dec;
    dec=(float)dec*12*2.5/100;
    frac=frac*2.5/100;
    m=dec+frac;
}

//and so on for the remaining functions and constructors


mutually-dependent classes can get to be kind of tricky. if anyone else has a more elegant solution out there, please post.

-jjh



User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 08:35PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month