Data Conversion from one class type to another class type

14,357

Solution 1

One of the classes must be defined first and the second cannot fully be used until after it is defined. This means you have to break up the class definitions a bit.

Here is an example of how to get at the class that's not yet defined, based off of the OP's post. Explanation comes in the form of comments embedded in the code to keep the code all in one cut-n-pasteable chunk.

#include <iostream>
class example2; // forward declaration to satisfy compiler until example2
                // is defined
class example1
{
    int id;
    int numbers;
    int cost;
public:
    example1(int a, int b, int c)
    {
        id = a;
        numbers = b;
        cost = c;
    }

    example1(const example2 & e)  // the const and reference are just because
                                  // no point copying e, and const ensures no
                                  // side effects to e
    {
        *this = e;  // why duplicate code? Just calling operator=
    }

    example1& operator=(const example2 & e);
        // note the lack of an implementation. This is because at this point
        // the compiler only knows example2 exists, but not what it looks
        // like. Can't copy what what you haven't seen.

    int getid() const //const to allow me to use const references.
    {
        return id;
    }
    int getnumber() const
    {
        return numbers;
    }
    int getcost() const
    {
        return cost;
    }
    void display()
    {
        std::cout << "Example 1" << std::endl;
        std::cout << "id " << id << std::endl;
        std::cout << "numbers " << numbers << std::endl;
        std::cout << "cost " << cost << std::endl;
    }

};

class example2
{
    int id;
    int value;
public:
    example2(int x, int y)
    {
        id = x;
        value = y;
    }
    example2(const example1 &e)
    {
        *this = e;  // once again just calls the equals operator
    }

    example2 & operator=(const example1 & e) // OK. This time we know what
                                             // example1 looks like and can
                                             // actually implement the method
    {
        id = e.getid();
        value = e.getnumber() * e.getcost();
        return *this;
    }

    int getid() const
    {
        return id;
    }
    int getvalue()const
    {
        return value;
    }
    void display()
    {
        std::cout << "Example 2" << std::endl;
        std::cout << "id " << id << std::endl;
        std::cout << "value " << value << std::endl;
    }
};

// and now for the implementation of example1's equals operator
example1& example1::operator=(const example2 & e)
{
    id = e.getid();
    numbers = -1; //do real work to get cost and numbers from e.getvalue()
    cost = -1;
    return *this;
}

int main()
{
    example1 s1(100, 5, 140.0);
    example2 s2(1, 2);
    s2 = s1;
    s2.display();
    example2 s3(314, 278);
    s1 = s3;
    s1.display();
    return 0;
}

Now for overloading the cast operator. This is something you almost never want to do because there are almost always much more obvious ways to accomplish the same goal.

For example:

#include<iostream>

class Integer
{
public:
    Integer(int val):mVal(val)
    {
    }
    operator int()
    {
        return mVal;
    }
    int getVal()
    {
        return mVal;
    }
private:
    int mVal;
};

int main()
{
    Integer t(42);
    int x = (int)t; // We're turning the Integer into an int? Kinda makes sense.
    std::cout << x << std::endl;
    x = t.getVal(); // hey! we're getting the integer's value! Do I need this comment?
    std::cout << x << std::endl;
}

Solution 2

class example2;
class example1(){
   operator example2()
   {
       example2 temp;
       //more stuff
   }
};

At this point in time in the code the full definition of example2 must be known to the compiler because you are creating an instance of this class called temp. However you only have a forwards declaration of example2 at this point in time. So while the compiler knows that example2 is a type because of the forwards declaration it doesn't have the full information about the class at that point in time so it is unable to make temp. This is giving you the error you see.

If what you want to do here is be able to assign to example2s from types of example1s you should create an assignment operator for example2 that takes example1s.

class example2{
    example2& operator= (example1 const& e1){
        this->id = e1.id;
        this->value = e1.numbers * e1.cost;
        return *this;
    }
};

However this strikes me as a bad design, there's probably better ways of achieving what you want that involves a different design. For example a method that's purpose is to take an example1 as a parameter and modify the state would be a lot clearer than this. As would just constructing a new example2 from the information in the example1

Share:
14,357

Related videos on Youtube

Aman Kaushal
Author by

Aman Kaushal

Updated on September 23, 2022

Comments

  • Aman Kaushal
    Aman Kaushal over 1 year

    I was trying to convert one class type data to another class type therefore I was using casting operator method in my oops program in c++.

    class example2;
    class example1
    {
       int id;
       int numbers;
       int cost;
    public:
       example1(int a, int b, int c)
       {
         id=a;
         numbers=b;
         cost=c;
       }
    
    // getid() added here
    // getnumber() added here
    // getcost() added here
    
       operator example2()
       {
        example2 temp;
        temp.id=id;
        temp.value=numbers*cost;
        return temp;
       }
    };
    class example2
    {
       int id;
       int value;
    public:
       example2(int x,int y){
         id=x;
         value=y;
       }
      void display(){
        cout<<"id "<<id<<endl;
        cout<<"value "<<value<<endl;
      }
    };
    

    when I was using casting from example1 to example2. It is showing error.

    int main()
    {
      example1 s1(100,5,140.0);
      example2 s2;
      s2=s1;
      s2.display();
      return 0;
    }
    

    it is giving error but why?. I made an operator overloading member function in example1 class because object of example1 has to be changed into object of example2.So this function invoked from class example1's method only which I think.It should be correct

    Error was like:

    error: return type 'class example2' is incomplete and example2 temp; has incomplete type

    Somehow I resolved this in another way I added a constructor in example2 class side:

    example2(example1 e)
    {
      id=e.getid(); //these functions already added in my code i didnt mentioned them in here.
      value=e.getnumber()*e.getcost();
    }
    

    and make comment of 'operator example2()' part in example1. now it is working. But previous way was not accepting. Please help me to correct me in my previous way of doing this thing.