Thread: constructor test (how would I do this?)

  1. #1
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589

    Question constructor test (how would I do this?)

    I created an overloaded operator for the first time.
    It works and tests ok.
    My teacher wants me to convert this overloaded operator into a copy constructor.
    I got it to link, but I was wondering how would I go about testing it.

    Below is my overloaded operator.
    Code:
     
    //prototype
    		linklist operator=(linklist*);
    
    //overloaded operator function
    	linklist linklist::operator=(linklist *Original)
    	{
    		Original->current=Original->head;
    		while(Original->current!=NULL)
    		{
    			link* newlink = new link;
    
    			strcpy(newlink->lname,Original->current->lname);
    			strcpy(newlink->fname,Original->current->fname);
    			strcpy(newlink->ssnumber,Original->current->ssnumber);
    			newlink->age=Original->current->age;
    			newlink->weight=Original->current->weight;
    
    			newlink->next = head;
    			if (head == NULL) //this tells me I have an empty list
    			{
    				head = newlink;
    				tail = head;
    			}
    			else
    			{
    				head = newlink;
    			}
    			Original->current=Original->current->next;
    		}
    		return *this;
    	}
    and this is my copy constructor
    Code:
     
    //prototype
    		linklist(linklist*);
    
    // copy constructor function
    	linklist::linklist(linklist *Original)
    	{
    		Original->current=Original->head;
    		while(Original->current!=NULL)
    		{
    			link* newlink = new link;
    
    			strcpy(newlink->lname,Original->current->lname);
    			strcpy(newlink->fname,Original->current->fname);
    			strcpy(newlink->ssnumber,Original->current->ssnumber);
    			newlink->age=Original->current->age;
    			newlink->weight=Original->current->weight;
    
    			newlink->next = head;
    			if (head == NULL) //this tells me I have an empty list
    			{
    				head = newlink;
    				tail = head;
    			}
    			else
    			{
    				head = newlink;
    			}
    			Original->current=Original->current->next;
    		}
    	}
    can you pass arguments to the object initialization?
    Code:
    linklist listD(&listA);
    Last edited by xviddivxoggmp3; 10-18-2003 at 04:39 PM.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  2. #2
    Registered User
    Join Date
    Aug 2002
    Posts
    14
    To test your copy constructor create a function that recieves your linked list as one of its parameters.

    Then in that function output the conents of the linked list you sent.

  3. #3
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    can you elaborate?

    doesn't a constructor only execute at the time of object instansiation?
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>doesn't a constructor only execute at the time of object instansiation?<<
    And when you create a copy of an object, isn't that creating a new instance of said object?

    Code:
    #include <iostream>
    using namespace std;
    
    class myClass
    {
      public:
      myClass() 
      {
        cout <<"Default constructor activated!" <<endl;
      };
      
      myClass(myClass &C)
      {
        cout <<"Copy constructor activated!" <<endl;
      }
      
    };
    
    void foo (myClass C)
    {
      cout <<"Inside foo()" <<endl;
    }
    
    int main(void)
    {
      /* The next line will invoke the default constructor */
      myClass c1;
      /* The next line will invoke the copy constructor */
      myClass c2 = c1;  
      /* The next line will also invoke the copy constructor */
      foo(c2);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    thanks for the details.
    i figured it out.

    I ended up testing it like the segment of my switch below.
    Code:
     
    			case'=':
    					system("cls");
    					listQ=listA;
    					listQ.display();
    					cout<<"\nCopy complete with Overload..."<<endl;
    					getch();
    					break;
    			case' ':
    					linklist listR(listB);
    					listR.display();
    					cout<<"\nCopy complete with Constructor..."<<endl;
    					getch();
    					break;
    Last edited by xviddivxoggmp3; 10-18-2003 at 10:25 PM.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

Popular pages Recent additions subscribe to a feed