Help...how do i write test program for this copy constructor and assignment operator?

ok so how do i write main test program to test the copy constructor and assignment operator in this program...how do i know if its working as its suppose to?i just want to know about copy and assignment operator..i have figured out the test program for other things..Here my program :

ListType.h

#ifndef LISTTYPE_H
#define LISTTYPE_H

#include<iostream>

class ListType{
public:
ListType(size_t=10);
ListType(const ListType&);
virtual ~ListType();
const ListType& operator=(const ListType&);
virtual bool insert(int)=0;//pure virtual method
virtual bool eraseAll();
virtual bool erase(int)=0;//pure virtual method
virtual bool find(int) const=0;//pure virtual method
size_t size() const;
bool empty() const;
bool full() const;
friend std::ostream& operator<<(std::ostream&, const ListType&);

protected:
int *items;
size_t capacity;
size_t count;
};

#endif // LISTTYPE_H

ListType.cpp

#include "ListType.h"

ListType::ListType(size_t cap)
{
capacity=cap;
count=0;
items=new int[capacity];
}
/*//////////////////////////////////////////////////////
copy constructor-copies the copy i
*///////////////////////////////////////////////////////
ListType::ListType(const ListType& list)
{
capacity=list.capacity;
count=list.count;
items=new int[capacity];
for(int i=0;i<count;++i)
{
items[i]=list.items[i];
}
}

/*/////////////////////////////////////////////////
Destructor-Deallocates the memory created by items.
*/////////////////////////////////////////////////
ListType::~ListType()
{
delete[] items;
}
/*//////////////////////////////////////////////////////////////////////////////////////
Assignment Operator
*///////////////////////////////////////////////////////////////////////////////////////

const ListType& ListType::operator=(const ListType& rhs){
if(this!=&rhs){
delete[]items;
capacity=rhs.capacity;
count=rhs.count;
items=new int[capacity];
for(int i=0;i<count;++i)
{
items[i]=rhs.items[i];
}
}
return *this;
}

/*
erase method
*/
bool ListType::eraseAll(){
count=0;
return true;
}
/*size method
*/

size_t ListType::size()const{
return count;
}

bool ListType::empty() const{
return count==0;
}
/*full method
*/

bool ListType::full() const{
return count==capacity;
}
/*
*/
std::ostream& operator<<(std::ostream& out,const ListType& list){
if(!list.empty()){
out<<list.items[0];
for(size_t i=1;i<list.count;++i){
out<<","<<list.items[i];
}
}
return out;
}


how do i write main test program to test the copy constructor and assignment operator in this program

As your class has pure virtual functions, you can't instantiate it directly. You have to create a derived class that fills in the missing functions appropriately.

You then put a certain number of items in the list and copy it to a new one and compare them. That'll test copy construction.

Do the same again, but assign to an existing empty list. That'll test assignment.

If it's automated, test at different number of values.
Topic archived. No new replies allowed.