#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?