Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. In type conversion of 2 different classes can i convert both ways ?

In type conversion of 2 different classes can i convert both ways ?

Scheduled Pinned Locked Moved C / C++ / MFC
question
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tarun Jha
    wrote on last edited by
    #1

    i want to make a program that can convert from class a type to class b & vice versa.. i already made a program that does one type of conversion..

    //define 2 classes polar & rectangle to represent points int the polar & rectanglular system.
    #include
    #include
    using namespace std;

    class Polar;

    //====================================================
    class Rectangle{
    float length, bredth;
    public:
    Rectangle(){}
    Rectangle(float ,float);

    /\*
      operator Polar(){
        Polar temp;
        float a = atan(length/bredth), r = sqrt(x\*x + y\*y);
        temp.getAngle(a);
        temp.getRadius(r);
    
        return(temp);
    }
    \*/
    
    int getLength(){return(length);}
    int getBredth(){return(bredth);}
    void putLength(float x){length = x;}
    void putBredth(float y){bredth = y;}
    
    void putData(){cout<<"Length : "<
    

    as in the above code i have converted class Polar to class Rectangle, but i also want to have a conversion of class Rectangle to class Polar.

    Thank you

    V CPalliniC 2 Replies Last reply
    0
    • T Tarun Jha

      i want to make a program that can convert from class a type to class b & vice versa.. i already made a program that does one type of conversion..

      //define 2 classes polar & rectangle to represent points int the polar & rectanglular system.
      #include
      #include
      using namespace std;

      class Polar;

      //====================================================
      class Rectangle{
      float length, bredth;
      public:
      Rectangle(){}
      Rectangle(float ,float);

      /\*
        operator Polar(){
          Polar temp;
          float a = atan(length/bredth), r = sqrt(x\*x + y\*y);
          temp.getAngle(a);
          temp.getRadius(r);
      
          return(temp);
      }
      \*/
      
      int getLength(){return(length);}
      int getBredth(){return(bredth);}
      void putLength(float x){length = x;}
      void putBredth(float y){bredth = y;}
      
      void putData(){cout<<"Length : "<
      

      as in the above code i have converted class Polar to class Rectangle, but i also want to have a conversion of class Rectangle to class Polar.

      Thank you

      V Offline
      V Offline
      Victor Nijegorodov
      wrote on last edited by
      #2

      Tarun Jha wrote:

      as in the above code i have converted class Polar to class Rectangle,

      Did you test it? Does it work? I ask because I see neither the assignment operator

      Rectangle& Rectangle::operator =(const Polar& polar)

      nor the ctor to create Rectangle instance from the Polar one... :confused:

      T 1 Reply Last reply
      0
      • T Tarun Jha

        i want to make a program that can convert from class a type to class b & vice versa.. i already made a program that does one type of conversion..

        //define 2 classes polar & rectangle to represent points int the polar & rectanglular system.
        #include
        #include
        using namespace std;

        class Polar;

        //====================================================
        class Rectangle{
        float length, bredth;
        public:
        Rectangle(){}
        Rectangle(float ,float);

        /\*
          operator Polar(){
            Polar temp;
            float a = atan(length/bredth), r = sqrt(x\*x + y\*y);
            temp.getAngle(a);
            temp.getRadius(r);
        
            return(temp);
        }
        \*/
        
        int getLength(){return(length);}
        int getBredth(){return(bredth);}
        void putLength(float x){length = x;}
        void putBredth(float y){bredth = y;}
        
        void putData(){cout<<"Length : "<
        

        as in the above code i have converted class Polar to class Rectangle, but i also want to have a conversion of class Rectangle to class Polar.

        Thank you

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        #include #include using namespace std;

        class Polar;

        //====================================================
        class Rectangle
        {
        float length, bredth;
        public:
        Rectangle(){}
        Rectangle(float ,float);

        operator Polar();

        int getLength(){return(length);}
        int getBredth(){return(bredth);}
        void putLength(float x){length = x;}
        void putBredth(float y){bredth = y;}

        void putData(){cout<<"Length : "<rectangle->polar
        Polar p1(radians, 25);
        p1.putData();

        Rectangle r1 = p1;
        r1.putData();

        Polar p2 = r1;
        p2.putData();

        return 0;
        }

        In testa che avete, signor di Ceprano?

        T 1 Reply Last reply
        0
        • CPalliniC CPallini

          #include #include using namespace std;

          class Polar;

          //====================================================
          class Rectangle
          {
          float length, bredth;
          public:
          Rectangle(){}
          Rectangle(float ,float);

          operator Polar();

          int getLength(){return(length);}
          int getBredth(){return(bredth);}
          void putLength(float x){length = x;}
          void putBredth(float y){bredth = y;}

          void putData(){cout<<"Length : "<rectangle->polar
          Polar p1(radians, 25);
          p1.putData();

          Rectangle r1 = p1;
          r1.putData();

          Polar p2 = r1;
          p2.putData();

          return 0;
          }

          T Offline
          T Offline
          Tarun Jha
          wrote on last edited by
          #4

          thank you :)

          CPalliniC 1 Reply Last reply
          0
          • V Victor Nijegorodov

            Tarun Jha wrote:

            as in the above code i have converted class Polar to class Rectangle,

            Did you test it? Does it work? I ask because I see neither the assignment operator

            Rectangle& Rectangle::operator =(const Polar& polar)

            nor the ctor to create Rectangle instance from the Polar one... :confused:

            T Offline
            T Offline
            Tarun Jha
            wrote on last edited by
            #5

            but i am not using any variables with reference to heap, so i thought it shouldn't be needed for ex:- new, *variable etc.

            1 Reply Last reply
            0
            • T Tarun Jha

              thank you :)

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #6

              You are welcome.

              In testa che avete, signor di Ceprano?

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups