Image

Imageworldsoutro wrote in Imagecpp

Code Help ??

I was wondering if i could make a assignment operator or a copy constructor in Class B instead of having the code::
-----------------------
public:

    B(A & x)
      {
        a = &x;

        }
-----------------------

Would it be possible taking this out and adding a assignment operator or copy constructor ???   My professor wanted me to do something with this program so i added the copy constructor in class B and work perfect. So i was wondering if i could also do that?


Here is the code::

#include <iostream>
using namespace std;

class A
{
  int n ;
  public:
   A():n(0)
   {
   }
   A(int x):n(x)
   {
    n = x;
   }

   void print()
   { cout << n;
   }
 



    A(const A& objectCopy){

            n = objectCopy.n;

    }


   };

   class B
   {
    A * a;

     public:

    B(A & x)
      {
        a = &x;               /// Here ???  Making an copy constructor or a assignment operator??

        }
   
      void print ()
       {
         a->print();
         }
       B(const B& copy){
          a = copy.a;
           
         }
         B::~B(){
             delete a;
            
            
             }       
        };


      int main()
      {

        A a(5);
        A a1(7);


        a1.print();
        cout << endl;
        B b(a);

         B c(a1);
         b = c;
           
                   
        b.print();
        cout << endl;
        int trick;
        cin >> trick;
        return 0;
        }

 

Thank you,

 

chris