Data Conversion from one class type to another class type

Aman Kaushal

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.

user4581301

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;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Type conversion to another class with namespace?

From Dev

Implement type casting from one class to another

From Dev

Passing generic type from one class to another?

From Dev

IEnumerable Class Collection type conversion from List

From Dev

How to create a class that one of its object is in the type of another class in VB?

From Dev

How to create a class that one of its object is in the type of another class in VB?

From Dev

"Importing" generic type from another class in Java

From Dev

How to use data from one class into another?

From Dev

Retrieving data from another class in one query

From Dev

Getting the type of another nested class from the same "parent" class

From Dev

Can I specify the type of a class that extends from another class?

From Dev

access a class type variable from another class in swift

From Dev

Dynamic type conversion using Type class

From Dev

implicit conversion of vector from one type to another c++

From Dev

Access Data Variable from one class in another class

From Dev

How to send ArrayList of class object type from one activity to another activity?

From Dev

How to send ArrayList of class object type from one activity to another activity?

From Dev

implicit conversion from class to enumeration type in switch conditional

From Dev

How to convert Collection of One Class type to Another Class Type collection in Entity Framework

From Dev

How to convert Collection of One Class type to Another Class Type collection in Entity Framework

From Dev

Class which converts one type to another using registered lambda

From Dev

Declaring property in a class that has the type of another one in C++

From Dev

Copy data from one object from one class to another boject in another class in Android

From Dev

Using an interface class as member type in another class

From Dev

How to declare a class based on type of another class

From Dev

Can I construct more than one type of object from same class data with the help of constructor in Java?

From Dev

Conversion of Data Class from "Factor" to "TS" class

From Dev

Data type mismatch while fetching data from one table into another

From Dev

Adding objects of one class to the data of another class

Related Related

  1. 1

    Type conversion to another class with namespace?

  2. 2

    Implement type casting from one class to another

  3. 3

    Passing generic type from one class to another?

  4. 4

    IEnumerable Class Collection type conversion from List

  5. 5

    How to create a class that one of its object is in the type of another class in VB?

  6. 6

    How to create a class that one of its object is in the type of another class in VB?

  7. 7

    "Importing" generic type from another class in Java

  8. 8

    How to use data from one class into another?

  9. 9

    Retrieving data from another class in one query

  10. 10

    Getting the type of another nested class from the same "parent" class

  11. 11

    Can I specify the type of a class that extends from another class?

  12. 12

    access a class type variable from another class in swift

  13. 13

    Dynamic type conversion using Type class

  14. 14

    implicit conversion of vector from one type to another c++

  15. 15

    Access Data Variable from one class in another class

  16. 16

    How to send ArrayList of class object type from one activity to another activity?

  17. 17

    How to send ArrayList of class object type from one activity to another activity?

  18. 18

    implicit conversion from class to enumeration type in switch conditional

  19. 19

    How to convert Collection of One Class type to Another Class Type collection in Entity Framework

  20. 20

    How to convert Collection of One Class type to Another Class Type collection in Entity Framework

  21. 21

    Class which converts one type to another using registered lambda

  22. 22

    Declaring property in a class that has the type of another one in C++

  23. 23

    Copy data from one object from one class to another boject in another class in Android

  24. 24

    Using an interface class as member type in another class

  25. 25

    How to declare a class based on type of another class

  26. 26

    Can I construct more than one type of object from same class data with the help of constructor in Java?

  27. 27

    Conversion of Data Class from "Factor" to "TS" class

  28. 28

    Data type mismatch while fetching data from one table into another

  29. 29

    Adding objects of one class to the data of another class

HotTag

Archive