#include "ShepherdDog.h"
#include <iostream>
using namespace std;

Node* ShepherdDog::pFreeList = NULL;    // initialize static data member
const int ShepherdDog::regionSize = 3; // initialize static const member

ShepherdDog::ShepherdDog() : Dog() 	// constructor
{
	weight = 0.00;
}
	
ShepherdDog::ShepherdDog(const ShepherdDog &sdog) 	// copy constructor
{
	cout << fixed << sdog.weight << endl;
	weight = sdog.weight;
}

ShepherdDog& ShepherdDog::operator=(const ShepherdDog &rhs)	// assignment operator
{
	if(this != &rhs)
	{
		weight = rhs.weight;
	}
	return *this;
}

void* ShepherdDog::operator new(size_t size)	// new operator
{
	Node *pList;
	if(pFreeList == NULL) // free list empty?
	{
		pFreeList = (Node*) new char[size * regionSize];
		for(pList = pFreeList; pList != &pFreeList[regionSize-1]; pList++)
			pList->next = pList + 1;
		pList->next = NULL;
	}
	pList = pFreeList;
	pFreeList = pFreeList->next;
	return (ShepherdDog*)pList;		
}

void ShepherdDog::operator delete(void* pNode, size_t size)	// delete operator
{
	// prepend new node to existing free list for later use
	((Node*)pNode)->next = pFreeList;
	pFreeList = (Node*) pNode;
}

ShepherdDog* ShepherdDog::copy() const
{
	return new ShepherdDog(*this);
}

ShepherdDog::~ShepherdDog()
{
}

#include "Dog.h"
#include "LinkedList.h"
#include "Node.h"
#include "ShepherdDog.h"
#include <iostream>
using namespace std;

int main()
{
	// assignment operator test for Dog.h
	/*Dog *dog1 = new Dog();
	Dog *dog2 = new Dog();
	Dog *dog3 = new Dog();

	cout << "Enter dog2's name: ";
	cin >> dog2->name;
	cout << "Enter dog3's name: ";
	cin >> dog3->name;

	dog1->partner->insert(dog2);
	dog1->partner->insert(dog3);

	Dog *dog4 = new Dog();
	*dog4 = *dog1;

	dog1->partner->print();
	dog4->partner->print();

	cout << "Enter new name is: ";
	cin >> dog1->partner->headNode->dog->name;

	dog1->partner->print();
	dog4->partner->print();*/

	// copy constructor test
	ShepherdDog *dog1 = new ShepherdDog();

	cout << "Enter dog1's name: ";
	cin >> dog1->weight;

	ShepherdDog *dog2 = dog1->copy();

	cout << fixed << dog1->weight << endl;
	cout << fixed << dog2->weight << endl;

	cout << "Enter dog1's new name: ";
	cin >> dog1->weight;

	cout << fixed << dog1->weight << endl;
	cout << fixed << dog2->weight << endl;

	
}

Result of main:

Enter dog1's weight: 12
dog1's weight: 0.000000 <---problem here
dog2's weight: 0.000000 <---problem here
Enter dog1's new weight: 12
dog1's new weight: 12.000000
dog2's weight: 0.000000

I don't know why but for some reason, the initial weight of my dogs are 0 when they should be 12. Can anyone help me with this?

Edited 6 Years Ago by newtoc_23: n/a

The output does not really match with the code you've posted, too bad. But I suspect that something like following occurs ..

// You prompt for the dog's name ..
cout << "Enter dog1's name: ";
// Note: weight
cin >> dog1->weight;

// You actually typed in a string above
// and knocked out cin while doing that i.e. cin is 
// non-functional at this point and the following 
// attempt to read fails (weight retains its original value)
cin >> dog1->weight;

That kind of situation you can remedy using cin.clear() perhaps along with cin.ignore() .

Edited 6 Years Ago by mitrmkar: re-wording (remedying)

Well, weight is fine until the copy method calls the copy constructor. After weight in pointer ShepherdDog is passed through to the copy constructor, the weight resets to 0. So it can read in my input fine. Something weird is just happening between the copy method and the copy constructor.

Edited 6 Years Ago by newtoc_23: n/a

The copy method is declared as a pure virtual function in the base class, Dog, if that helps at all...

Edited 6 Years Ago by newtoc_23: n/a

It works fine when i don't call the base class constructor, Dog, in SheperdDog.

[B]ShepherdDog.h[/B]
#include <stddef.h>
#include "Dog.h"
#include "Node.h"
#ifndef SHEPHERDDOG_H
#define SHEPHERDOG_H

class ShepherdDog : public Dog
{
    public:
	float weight;
	static Node *pFreeList;
	static const int regionSize;

	ShepherdDog();	// constructor
	ShepherdDog(const ShepherdDog &sdog);	// copy constructor
	ShepherdDog& operator=(const ShepherdDog &rhs);	// assignment operator
	void* operator new(size_t size);	// new operator
	void operator delete(void* pNode, size_t size);	// delete operator
	ShepherdDog* copy() const;
	~ShepherdDog();	
};

#endif
[B]ShepardDog.cpp[/B]
#include "ShepherdDog.h"
#include <iostream>
using namespace std;

Node* ShepherdDog::pFreeList = NULL;    // initialize static data member
const int ShepherdDog::regionSize = 3; // initialize static const member

ShepherdDog::ShepherdDog() : Dog() 	// constructor
{
	weight = 0.00;
}
	
ShepherdDog::ShepherdDog(const ShepherdDog &sdog) 	// copy constructor
{
	weight = sdog.weight;
}

ShepherdDog& ShepherdDog::operator=(const ShepherdDog &rhs)	// assignment operator
{
	if(this != &rhs)
	{
		weight = rhs.weight;
	}
	return *this;
}

void* ShepherdDog::operator new(size_t size)	// new operator
{
	Node *pList;
	if(pFreeList == NULL) // free list empty?
	{
		pFreeList = (Node*) new char[size * regionSize];
		for(pList = pFreeList; pList != &pFreeList[regionSize-1]; pList++)
			pList->next = pList + 1;
		pList->next = NULL;
	}
	pList = pFreeList;
	pFreeList = pFreeList->next;
	return (ShepherdDog*)pList;		
}

void ShepherdDog::operator delete(void* pNode, size_t size)	// delete operator
{
	// prepend new node to existing free list for later use
	((Node*)pNode)->next = pFreeList;
	pFreeList = (Node*) pNode;
}

ShepherdDog* ShepherdDog::copy() const
{
	return new ShepherdDog(*this);
}

ShepherdDog::~ShepherdDog()
{
}	

[B]Dog.h[/B]
#ifndef DOG_H_
#define DOG_H_



class LinkedList;

class Dog
{
    public:
	char *name;
	char *gender;
	char *breed;
	LinkedList *partner;
	LinkedList *children;

	Dog();	// constructor
	Dog(const Dog &dog); // copy constructor
	Dog& operator=(const Dog &rhs);	// assigment operator
	void copyCharArray(char *newArray, char *oldArray, int sizeOfOldArray);
	void copyLinkedList(LinkedList* oldLL, LinkedList* newLL);
	virtual Dog* copy() const = 0;	// pure abstract copy function
	virtual ~Dog();	// destructor
};

#endif

[B]Dog.cpp[/B]
#include "Dog.h"
#include "LinkedList.h"
#include "Node.h"
#include <stdio.h>
#include <iostream>
using namespace std;

Dog::Dog()
{
	name = new char[20];
	gender = new char[10];
	breed = new char[20];
	partner = new LinkedList();
	children = new LinkedList();
}

Dog::Dog(const Dog &dog)
{
	cout << "what what" << endl;
	//create new instance of name, gender, partner, and children
	name = new char[sizeof(dog.name)];
	gender = new char[sizeof(dog.gender)];
	breed = new char[sizeof(dog.breed)];
	partner = new LinkedList();
	children = new LinkedList();

	
	// copy name, gender, and breed
	copyCharArray(name, dog.name, sizeof(dog.name));
	copyCharArray(gender, dog.gender, sizeof(dog.name));
	copyCharArray(breed, dog.breed, sizeof(dog.breed));
	
	// create new instance of partner and children then copy partner and children
	copyLinkedList(dog.partner, partner);
	copyLinkedList(dog.children, children);
	
}

Dog& Dog::operator=(const Dog &rhs)
{
	if(this != &rhs)
	{
		// delete original instance of name, breed, gender, partner, and children
		delete [] name;
		delete [] breed;
		delete [] gender;
		delete partner;
		delete children;
	
		// create new instance of name, breed, gender, partner, children
		name = new char[sizeof(rhs.name)+1];
		breed = new char[sizeof(rhs.breed)+1];
		gender = new char[sizeof(rhs.gender)+1];
		partner = new LinkedList();
		children = new LinkedList();

		// copy name, breed, gender, partner, and children
		copyCharArray(name, rhs.name, sizeof(rhs.name));
		copyCharArray(breed, rhs.breed, sizeof(rhs.breed));
		copyCharArray(gender, rhs.gender, sizeof(rhs.gender));
		copyLinkedList(rhs.partner, partner);
		copyLinkedList(rhs.children, children);			
	}
	return *this;
}

void Dog::copyCharArray(char *newArray, char *oldArray, int sizeOfOldArray)
{
	for(int i = 0; i < sizeOfOldArray; i++)
		newArray[i] = oldArray[i];
}

void Dog::copyLinkedList(LinkedList* oldLL, LinkedList* newLL)
{
	Node *newNode = NULL;
	Node *tailNode = NULL;
	Node *tempNode = oldLL->headNode;

	while(tempNode != NULL)
	{
		newNode = new Node();
		*(newNode->dog) = *(tempNode->dog);
		
		if(newLL->headNode == NULL)
		{
			newLL->headNode = newNode;
			tailNode = newNode;
		}
		else
		{
			tailNode->next = newNode;
			tailNode = newNode;
		}
		tempNode = tempNode->next;
	}	
}

Dog::~Dog()
{
	delete [] name;
	delete [] breed;
	delete [] gender;
}

Edited 6 Years Ago by newtoc_23: n/a

This article has been dead for over six months. Start a new discussion instead.