assign one class object to another class object in c++

Lavnesh

I want to assign one class object to another class object in c++.

Ex: There is one class Dog and another class Cat. Create one one instance of each (d1 & c1). Don't want to use any STL. I want to use this statement in my code

d1 = c1;

Program

class dog
{
    char  dc;
    float df;
    int   di;   
public:
    void setdata2(char c, float f, int i)
    {   dc = c; df = f; di = i; }
    void showdata2()
    {   cout  <<"char =" << dc <<", float =" << df <<", int =" << di  <<endl;   }
};


class cat
{
    float cf;
    int ci;
    char cc;    
public:
    void setdata(float f, int i, char c)
    {   cf = f; ci = i; cc = c; }
    void showdata()
    {   cout <<"float =" << cf <<", int =" << ci <<", char =" << cc  <<endl;    }
};


int main()
{
    dog d1, d2;
    cat c1, c2;

    d1.setdata2('A', 56.78, 30);
    c1.setdata(12.34, 2, 3);
    d1.showdata2();
    c1.showdata();

    d2 = c1;        // Question 1

    dog d3(c1);     // Question 2

    dog d4 = c1;    // Question 3

    return 0;
}

Please answer Question 1/2/3 each separately.

eerorika

I want to assign one class object to another class object in c++.

It is possible to assign an object of type A to an object of type B if A is implicitly convertible to B, and B is assignable - or, if B has an overloaded assignment operator that accepts objects of type A.

There are two ways to define a conversion from a custom type A to an unrelated type B. You can either define a conversion function for A, or you can define a converting constructor for type B.

1) This is copy assignment. The above explanation applies. Either define cat::operator dog() or dog::dog(cat) or dog::operator=(cat).

2) This is direct initialization. It is not an assignment which is what you asked about. Defining an implicit conversion works for this as well, but an overloaded assignment operator will not. However, an overloaded constructor would be a substitute third alternative.

3) Even though this looks a bit like assignment syntactically, this is actually copy initialization. Same rules apply as 2).


PS. Note that you're defining two variables by the name d3. That is an error.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Setting a Class Object as a property of another Class in Objective-C

From Dev

Access object from another class c++

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 pass itcl object of one class to object of another class and use the functions of the passed objects?

From Dev

Assigning one object to another of the same class

From Dev

Assign the pointer of one class object to another class object c++

From Dev

Iterate through an array of one class object and put those elements into an array belonging to another class object with Ruby.

From Dev

How to get a class object to store another class object in c#?

From Dev

Adding an object of one class to an object of another class and checking for duplicates in Python

From Dev

C# Abstract class has another abstract class object

From Dev

F# class assign an object value to another object

From Dev

Method to accept one class object or another class object

From Dev

What happens if i assign object to another object of different class?

From Dev

convert one class object into another using stream

From Dev

How to assign a pointer to object to another pointer to object of same class?

From Dev

Converting object of a class to of another one

From Dev

How to inject one object to another class object in spring?

From Dev

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

From Dev

Assigning one object to another of the same class

From Dev

C++: Can a class be used as an object in another class?

From Dev

How to assign values base class properties inside a derived class object without listing them one by one?

From Dev

Cast one class object to other class object

From Dev

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

From Dev

Using one class object in another class PYTHON

From Dev

How to transfer object from one class to another class - JAVA

From Dev

Pointer to object in another class?

From Dev

Set value to a class object which is property of another class - Objective c

From Dev

Member object of another class

From Dev

Could I overload operator= to assign object of a class to a variable of another class but both class are from the same class template

Related Related

  1. 1

    Setting a Class Object as a property of another Class in Objective-C

  2. 2

    Access object from another class c++

  3. 3

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

  4. 4

    How to pass itcl object of one class to object of another class and use the functions of the passed objects?

  5. 5

    Assigning one object to another of the same class

  6. 6

    Assign the pointer of one class object to another class object c++

  7. 7

    Iterate through an array of one class object and put those elements into an array belonging to another class object with Ruby.

  8. 8

    How to get a class object to store another class object in c#?

  9. 9

    Adding an object of one class to an object of another class and checking for duplicates in Python

  10. 10

    C# Abstract class has another abstract class object

  11. 11

    F# class assign an object value to another object

  12. 12

    Method to accept one class object or another class object

  13. 13

    What happens if i assign object to another object of different class?

  14. 14

    convert one class object into another using stream

  15. 15

    How to assign a pointer to object to another pointer to object of same class?

  16. 16

    Converting object of a class to of another one

  17. 17

    How to inject one object to another class object in spring?

  18. 18

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

  19. 19

    Assigning one object to another of the same class

  20. 20

    C++: Can a class be used as an object in another class?

  21. 21

    How to assign values base class properties inside a derived class object without listing them one by one?

  22. 22

    Cast one class object to other class object

  23. 23

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

  24. 24

    Using one class object in another class PYTHON

  25. 25

    How to transfer object from one class to another class - JAVA

  26. 26

    Pointer to object in another class?

  27. 27

    Set value to a class object which is property of another class - Objective c

  28. 28

    Member object of another class

  29. 29

    Could I overload operator= to assign object of a class to a variable of another class but both class are from the same class template

HotTag

Archive