More complex set example using subclasses of a
class as the objects in the set.
In this demonstation program a class called SetObject is defined. Three subclasses
of SetObject are then defined. These subclasses are Triangle, Circle and Rectangle.
These classes are the types of objects that can be contained in a set. There is a class called
Set that can contain any number of these objects stored in a linked list. Notice how
type casting of pointers is used.
This program also demonstrates the Union, Intersection, and Difference set operations.
Header file defining a Set class.
//===================================================================
// File: Set.h
// Purpose: Header file for a demonstration of a set class.
// Author: Dr. Rick Coleman
//===================================================================
#ifndef SET_H
#define SET_H
#include "SetObject.h"
#include "Triangle.h"
#include "Rectangle.h"
#include "Circle.h"
class Set
{
private:
SetObject *head; // Head of linked list of items in the set
public:
Set(); // Constructor
~Set(); // Destructor
void addItem(SetObject *item); // Add an item to the set
bool removeItem(int type, int key); // Remove an item from the set
Set *Union(Set *aSet); // Return new set-Union this and aSet
Set *Intersection(Set *aSet); // Return new set--Intersection this and aSet
Set *Difference(Set *aSet); // Return new set--Differrence this and aSet
bool contains(SetObject *item); // Return true if item in this set
int itemCount(); // Return number of items in this set
SetObject *copy(SetObject *anItem); // Copy an item in the set
SetObject *copy(int index); // Copy item at index (item at head = index 0)
void PrintSet(); // Print all data in this set
};
#endif
Implementation file for the Set class.
//===================================================================
// File: Set.cpp
// Purpose: Implementation file for a demonstration of a set class.
// Author: Dr. Rick Coleman
//===================================================================
#include "SetInfo.h"
#include "SetObject.h"
#include "Triangle.h"
#include "Rectangle.h"
#include "Circle.h"
#include "Set.h"
#include <iostream>
using namespace std;
//------------------------------------------
// Class constructor
//------------------------------------------
Set::Set()
{
head = NULL;
}
//------------------------------------------
// Class destructor
//------------------------------------------
Set::~Set()
{
SetObject *temp;
temp = head;
while(temp != NULL)
{
head = head->next; // Advance head
delete temp; // Delete this object
temp = head;
}
}
//------------------------------------------
// Add an item to the set.
// Since order does not matter all new items
// will be added at the tail of the list.
//------------------------------------------
void Set::addItem(SetObject *item)
{
SetObject *temp;
if(head == NULL)
{
// Add first item to set
head = item;
return;
}
// Add item at end of the list
temp = head;
while(temp->next != NULL) temp = temp->next;
temp->next = item;
}
//------------------------------------------
// Remove an item from the set
//------------------------------------------
bool Set::removeItem(int type, int key)
{
SetObject *back, *temp;
if(head == NULL) return false; // Nothing to remove
back = NULL;
temp = head;
while((temp != NULL) && (temp->getType() != type) && (temp->getKey() != key))
{
back = temp;
temp = temp->next;
}
if(temp != NULL) // found it
{
// Found the one to delete
if(back == NULL) // Delete head of list
head = head->next;
else
back->next = temp->next;
// Delete the object just removed
delete temp;
// Done so return
return true;
}
// If we get here we didn't find the one to remove
return false;
}
//------------------------------------------
// Return a new set which is the union of
// this set and the set passed in as an arg.
//------------------------------------------
Set *Set::Union(Set *aSet)
{
Set *newSet = new Set();
SetObject *temp;
int count;
// Add all the items from this set
temp = head;
while(temp != NULL)
{
newSet->addItem(this->copy(temp));
temp = temp->next;
}
// Now add all the items from the other set
count = aSet->itemCount();
for(int i=0; i<count; i++)
{
SetObject *theObj = aSet->copy(i);
if(theObj != NULL)
newSet->addItem(aSet->copy(i));
else
{
cout << "Copy returned null in Union\n";
cout.flush();
}
}
return newSet;
}
//------------------------------------------
// Return a new set which is the intersection
// of this set and the set passed in as an arg.
//------------------------------------------
Set *Set::Intersection(Set *aSet)
{
Set *newSet = new Set();
SetObject *temp;
// Add all the items in both sets
temp = head;
while(temp != NULL)
{
if(aSet->contains(temp))
newSet->addItem(this->copy(temp));
temp = temp->next;
}
return newSet;
}
//------------------------------------------
// Return a new set which is the difference
// of this set and the set passed in as an
// arg. Difference means all itens in this
// set that are not in the arg set.
//------------------------------------------
Set *Set::Difference(Set *aSet)
{
Set *newSet = new Set();
SetObject *temp;
// Add all the items in this set but not in the other
temp = head;
while(temp != NULL)
{
if(!(aSet->contains(temp)))
newSet->addItem(this->copy(temp));
temp = temp->next;
}
return newSet;
}
//------------------------------------------
// Return true if this set contains the
// indicated item. We consider a match
// if the itemType and values both match
// those in the given item.
//------------------------------------------
bool Set::contains(SetObject *item)
{
SetObject *temp;
temp = head;
while(temp != NULL)
{
if(item->getType() == temp->getType())
{
switch(item->getType())
{
case TRIANGLE :
if((((Triangle *)item)->getPointX(1) == ((Triangle *)temp)->getPointX(1)) &&
(((Triangle *)item)->getPointY(1) == ((Triangle *)temp)->getPointY(1)) &&
(((Triangle *)item)->getPointX(2) == ((Triangle *)temp)->getPointX(2)) &&
(((Triangle *)item)->getPointY(2) == ((Triangle *)temp)->getPointY(2)) &&
(((Triangle *)item)->getPointX(3) == ((Triangle *)temp)->getPointX(3)) &&
(((Triangle *)item)->getPointY(3) == ((Triangle *)temp)->getPointY(3)))
return true;
break;
case RECTANGLE :
if((((Rectangle *)item)->getPointX() == ((Rectangle *)temp)->getPointX()) &&
(((Rectangle *)item)->getPointY() == ((Rectangle *)temp)->getPointY()) &&
(((Rectangle *)item)->getWidth() == ((Rectangle *)temp)->getWidth()) &&
(((Rectangle *)item)->getHeight() == ((Rectangle *)temp)->getHeight()))
return true;
break;
case CIRCLE :
if((((Circle *)item)->getCenterX() == ((Circle *)temp)->getCenterX()) &&
(((Circle *)item)->getRadius() == ((Circle *)temp)->getRadius()))
return true;
break;
}
}
// That's not it so advance to next item
temp = temp->next;
}
// If still here we didn't find it
return false;
}
//------------------------------------------
// Return the number of items in this set.
//------------------------------------------
int Set::itemCount()
{
SetObject *temp;
int count = 0;
temp = head;
while(temp != NULL)
{
count++;
temp = temp->next;
}
return count;
}
//------------------------------------------
// Copy a SetObject structure and its item.
//------------------------------------------
SetObject *Set::copy(SetObject *anItem)
{
SetObject *newObj = NULL;
newObj = anItem->copy(); // This will call the appropriate copy function
switch(anItem->getType())
{
case TRIANGLE : // Duplicate a TRIANGLE object
newObj = (SetObject *)(((Triangle *)anItem)->copy()); // This will call the appropriate copy function
break;
case RECTANGLE : // Duplicate a RECTANGLE object
newObj = (SetObject *)(((Rectangle *)anItem)->copy()); // This will call the appropriate copy function
break;
case CIRCLE : // Duplicate a CIRCLE object
newObj = (SetObject *)(((Circle *)anItem)->copy()); // This will call the appropriate copy function
break;
}
return newObj;
}
//------------------------------------------
// Copy a SetObject structure and its item at
// the given index. Assuming the index of
// of the item at the head of the list is 0.
//------------------------------------------
SetObject *Set::copy(int index)
{
SetObject *temp, *newObj = NULL;
int count = 0;
// Find the item to copy
temp = head;
while(temp != NULL)
{
if(count == index) break; // terminate while loop
count++;
temp = temp->next;
}
if(temp == NULL) return NULL; // No node at this index
switch(temp->getType())
{
case TRIANGLE : // Duplicate a TRIANGLE object
newObj = (SetObject *)(((Triangle *)temp)->copy()); // This will call the appropriate copy function
break;
case RECTANGLE : // Duplicate a RECTANGLE object
newObj = (SetObject *)(((Rectangle *)temp)->copy()); // This will call the appropriate copy function
break;
case CIRCLE : // Duplicate a CIRCLE object
newObj = (SetObject *)(((Circle *)temp)->copy()); // This will call the appropriate copy function
break;
}
return newObj;
}
//------------------------------------------
// Print all data in this set.
//------------------------------------------
void Set::PrintSet()
{
SetObject *temp;
temp = head;
cout << "==================== Objects in Set ====================\n";
while(temp != NULL)
{
switch(temp->getType())
{
case TRIANGLE :
((Triangle*)(temp))->printData();
break;
case RECTANGLE :
((Rectangle*)(temp))->printData();
break;
case CIRCLE :
((Circle*)(temp))->printData();
break;
}
temp = temp->next;
}
cout << "======================================================\n\n";
cout.flush();
}
Header file defining a SetObject parent class.
//===================================================================
// File: SetObject.h
// Purpose: Parent class for objects that can be included in the set.
// Author: Dr. Rick Coleman
//===================================================================
#ifndef SETOBJECT_H
#define SETOBJECT_H
class SetObject
{
protected:
int type;
char *description;
int key;
public:
SetObject *next; // Pointer to next
SetObject(); // Class constructors
~SetObject(); // Class destructor
int getType(); // Return the type code
char *getDesc(); // Return a copy of the description string
int getKey(); // Return the key
SetObject *copy(); // Copy the sub class object
virtual void printData();// Print data in this object
(All subclasses must implement the virtual function.)
protected:
void setType(int type); // Set the type. Can only be called by subclasses
void setDesc(char *desc); // Set description. Can only be called by subclasses
char *dupString(char *str); // Duplicate a character array using dynamic memory
};
#endif
Implementation file for the SetObject parent class.
//===================================================================
// File: SetObject.cpp
// Purpose: Implementation file for the parent class for objects that
// can be included in the set..
// Author: Dr. Rick Coleman
//===================================================================
#include "SetObject.h"
#include <string.h>
//-----------------------------------------------
// Default class constructor
//-----------------------------------------------
SetObject::SetObject()
{
type = -1; // No type
description = NULL; // No description
next = NULL;
}
//-----------------------------------------------
// Class destructor
//-----------------------------------------------
SetObject::~SetObject()
{
// Delete description string
delete description;
}
//-----------------------------------------------
// Get the object type code
//-----------------------------------------------
int SetObject::getType()
{
return type;
}
//-----------------------------------------------
// Set the object type code
//-----------------------------------------------
void SetObject::setType(int type)
{
this->type = type;
}
//-----------------------------------------------
// Set the object description
//-----------------------------------------------
void SetObject::setDesc(char *desc)
{
this->description = dupString(desc);
}
//-----------------------------------------------
// Duplicate a character array using dynamic
// memory allocation.
//-----------------------------------------------
char *SetObject::dupString(char *str)
{
char *dup = new char[strlen(str) + 1];
strcpy(dup, str);
return dup;
}
//-----------------------------------------------
// Get the key
//-----------------------------------------------
int SetObject::getKey()
{
return key;
}
//-----------------------------------------------
// Superclass function does nothing. Subclasses
// override to copy themselves.
//-----------------------------------------------
SetObject *SetObject::copy()
{
return NULL;
}
//-----------------------------------------------
// Superclass function does nothing. Subclasses
// override to print themselves.
//-----------------------------------------------
void SetObject::printData()
{
return;
}
Header file defining a Triangle class as a subclass of SetObject.
//===================================================================
// File: Triangle.h
// Purpose: Definition of a triangle object
// Author: Dr. Rick Coleman
//===================================================================
#ifndef TRIANGLE_H
#define TRIANGLE_H
#include "SetObject.h"
#include "Sets2.h"
class Triangle:SetObject
{
private:
SetPoint2D pt1, pt2, pt3;
public:
Triangle(); // Class constructors
Triangle(double x1, double y1,
double x2, double y2,
double x3, double y3);
~Triangle(); // Class destructor
double getPointX(int pt); // Return point pt X coordinate
void setPointX(int pt, double x); // Set point pt X coordinate
double getPointY(int pt); // Return point pt Y coordinate
void setPointY(int pt, double y); // Set point pt Y coordinate
SetObject *copy(); // Copy this sub class object
void printData(); // Print data in this object
protected:
void setKey(); // Set the key for this object
};
#endif
Implementation file defining a Triangle class as a subclass of SetObject.
//===================================================================
// File: Triangle.cpp
// Purpose: Implementation of a triangle object
// Author: Dr. Rick Coleman
//===================================================================
#include "Triangle.h"
#include <iostream>
using namespace std;
//--------------------------------
// Default class constructor
//--------------------------------
Triangle::Triangle()
{
pt1.X = pt2.X = pt3.X = 0.0;
pt1.Y = pt2.Y = pt3.Y = 0.0;
this->type = TRIANGLE;
this->description = dupString("TRIANGLE");
}
//--------------------------------
// Class constructor
//--------------------------------
Triangle::Triangle(double x1, double y1,
double x2, double y2,
double x3, double y3)
{
pt1.X = x1;
pt2.X = x2;
pt3.X = x3;
pt1.Y = y1;
pt2.Y = y2;
pt3.Y = y3;
this->type = TRIANGLE;
this->description = dupString("TRIANGLE");
}
//--------------------------------
// Class destructor
//--------------------------------
Triangle::~Triangle()
{
// Nothing to do. Auto call to super destructor
// will delete the description string.
}
//--------------------------------
// Get the X coordinate of a point
//--------------------------------
double Triangle::getPointX(int pt)
{
switch(pt)
{
case 1 : return pt1.X; break;
case 2 : return pt2.X; break;
case 3 : return pt3.X; break;
}
return 0.0;
}
//--------------------------------
// Set the X coordinate of a point
//--------------------------------
void Triangle::setPointX(int pt, double x)
{
switch(pt)
{
case 1 : pt1.X = x; break;
case 2 : pt2.X = x; break;
case 3 : pt3.X = x; break;
}
}
//--------------------------------
// Get the Y coordinate of a point
//--------------------------------
double Triangle::getPointY(int pt)
{
switch(pt)
{
case 1 : return pt1.Y; break;
case 2 : return pt2.Y; break;
case 3 : return pt3.Y; break;
}
return 0.0;
}
//--------------------------------
// Set the Y coordinate of a point
//--------------------------------
void Triangle::setPointY(int pt, double y)
{
switch(pt)
{
case 1 : pt1.Y = y; break;
case 2 : pt2.Y = y; break;
case 3 : pt3.Y = y; break;
}
}
//-----------------------------------------------
// Set the key.
//-----------------------------------------------
void Triangle::setKey()
{
static int keybase = 1;
key = keybase;
keybase++;
}
//-----------------------------------------------
// Copy this object
//-----------------------------------------------
SetObject *Triangle::copy()
{
Triangle *tri = new Triangle(this->pt1.X, this->pt1.Y,
this->pt2.X, this->pt2.Y,
this->pt3.X, this->pt3.Y);
return (SetObject *)tri;
}
//-----------------------------------------------
// Print data in this circle
//-----------------------------------------------
void Triangle::printData()
{
cout << "TRIANGLE" << "\tP1=" << pt1.X << ", " << pt1.Y <<
"\t" << "\tP2=" << pt2.X << ", " << pt2.Y <<
"\t" << "\tP3=" << pt3.X << ", " << pt3.Y << "\n";
}
Header file defining a Circle class as a subclass of SetObject.
//===================================================================
// File: Circle.h
// Purpose: Definition of a circle object
// Author: Dr. Rick Coleman
//===================================================================
#ifndef CIRCLE_H
#define CIRCLE_H
#include "SetObject.h"
#include "Sets2.h"
class Circle:SetObject
{
private:
SetPoint2D center;
double radius;
public:
Circle(); // Class constructors
Circle(double CX, double CY,
double radius);
~Circle(); // Class destructor
double getCenterX(); // Return point pt X coordinate
void setCenterX(double x); // Set point pt X coordinate
double getCenterY(); // Return point pt Y coordinate
void setCenterY(double y); // Set point pt Y coordinate
double getRadius(); // Return width of this rectangle
void setRadius(double r); // Set width
SetObject *copy(); // Copy this sub class object
void printData(); // Print data in this object
protected:
void setKey(); // Set the key for this object
};
#endif
Implementation file defining a Circle class as a subclass of SetObject.
//===================================================================
// File: Circle.cpp
// Purpose: Implementation of a circle object
// Author: Dr. Rick Coleman
//===================================================================
#include "Circle.h"
#include <iostream>
using namespace std;
//--------------------------------
// Default class constructor
//--------------------------------
Circle::Circle()
{
center.X = center.Y = 0.0;
radius = 0.0;
this->type = CIRCLE;
this->description = dupString("CIRCLE");
}
//--------------------------------
// Class constructor
//--------------------------------
Circle::Circle(double CX, double CY, double radius)
{
center.X = CX;
center.Y = CY;
this->radius = radius;
this->type = CIRCLE;
this->description = dupString("CIRCLE");
}
//--------------------------------
// Get the center X coordinate
//--------------------------------
double Circle::getCenterX()
{
return center.X;
}
//--------------------------------
// Set the center X coordinate
//--------------------------------
void Circle::setCenterX(double x)
{
center.X = x;
}
//--------------------------------
// Get the center Y coordinate
//--------------------------------
double Circle::getCenterY()
{
return center.Y;
}
//--------------------------------
// Set the center Y coordinate
//--------------------------------
void Circle::setCenterY(double y)
{
center.Y = y;
}
//--------------------------------
// Get the radius
//--------------------------------
double Circle::getRadius()
{
return radius;
}
//--------------------------------
// Set the radius
//--------------------------------
void Circle::setRadius(double r)
{
radius = r;
}
//-----------------------------------------------
// Set the key.
//-----------------------------------------------
void Circle::setKey()
{
static int keybase = 1;
key = keybase;
keybase++;
}
//-----------------------------------------------
// Copy this object
//-----------------------------------------------
SetObject *Circle::copy()
{
Circle *cir = new Circle(this->center.X, this->center.Y, this->radius);
return (SetObject *)cir;
}
//-----------------------------------------------
// Print data in this circle
//-----------------------------------------------
void Circle::printData()
{
cout << "CIRCLE" << "\tcenter=" << center.X << ", " << center.Y << "\tradius="
<< radius <<"\n";
}
Header file defining a Rectangle class as a subclass of SetObject.
//===================================================================
// File: Rectangle.h
// Purpose: Definition of a rectangle object
// Author: Dr. Rick Coleman
//===================================================================
#ifndef RECTANGLE_H
#define RECTANGLE_H
#include "SetObject.h"
#include "Sets2.h"
class Rectangle:SetObject
{
private:
SetPoint2D ULCorner;
double width;
double height;
public:
Rectangle(); // Class constructors
Rectangle(double ulX, double ulY,
double width, double height);
~Rectangle(); // Class destructor
double getPointX(); // Return point pt X coordinate
void setPointX(double x); // Set point pt X coordinate
double getPointY(); // Return point pt Y coordinate
void setPointY(double y); // Set point pt Y coordinate
double getWidth(); // Return width of this rectangle
void setWidth(double wd); // Set width
double getHeight(); // Return height of this rectangle
void setHeight(double ht);// Set height
SetObject *copy(); // Copy this sub class object
void printData(); // Print data in this object
protected:
void setKey(); // Set the key for this object
};
#endif
Implementation file defining a Rectangle class as a subclass of SetObject.
//===================================================================
// File: Rectangle.cpp
// Purpose: Implementation of a rectangle object
// Author: Dr. Rick Coleman
//===================================================================
#include "Rectangle.h"
#include <iostream>
using namespace std;
//--------------------------------
// Default class constructor
//--------------------------------
Rectangle::Rectangle()
{
ULCorner.X = ULCorner.Y = 0.0;
width = height = 0.0;
this->type = RECTANGLE;
this->description = dupString("RECTANGLE");
}
//--------------------------------
// Class constructor
//--------------------------------
Rectangle::Rectangle(double ulX, double ulY,
double width, double height)
{
ULCorner.X = ulX;
ULCorner.Y = ulY;
this->width = width;
this->height = height;
this->type = RECTANGLE;
this->description = dupString("RECTANGLE");
}
//--------------------------------
// Get the upper left X coordinate
//--------------------------------
double Rectangle::getPointX()
{
return ULCorner.X;
}
//--------------------------------
// Set the upper left X coordinate
//--------------------------------
void Rectangle::setPointX(double x)
{
ULCorner.X = x;
}
//--------------------------------
// Get the upper left Y coordinate
//--------------------------------
double Rectangle::getPointY()
{
return ULCorner.Y;
}
//--------------------------------
// Set the upper left Y coordinate
//--------------------------------
void Rectangle::setPointY(double y)
{
ULCorner.Y = y;
}
//--------------------------------
// Get the width
//--------------------------------
double Rectangle::getWidth()
{
return width;
}
//--------------------------------
// Set the width
//--------------------------------
void Rectangle::setWidth(double wd)
{
width = wd;
}
//--------------------------------
// Get the height
//--------------------------------
double Rectangle::getHeight()
{
return height;
}
//--------------------------------
// Set the height
//--------------------------------
void Rectangle::setHeight(double ht)
{
height = ht;
}
//-----------------------------------------------
// Set the key.
//-----------------------------------------------
void Rectangle::setKey()
{
static int keybase = 1;
key = keybase;
keybase++;
}
//-----------------------------------------------
// Copy this object
//-----------------------------------------------
SetObject *Rectangle::copy()
{
Rectangle *rect = new Rectangle(this->ULCorner.X, this->ULCorner.Y,
this->width, this->height);
return (SetObject *)rect;
}
//-----------------------------------------------
// Print data in this rectangle
//-----------------------------------------------
void Rectangle::printData()
{
cout << "RECTANGLE" << "\tUL Corner=" << ULCorner.X << ", " << ULCorner.Y <<
"\tWidth=" << width << " Height=" << height << "\n";
}
Header file defining generic information used by all SetObject subclasses.
//===================================================================
// File: SetInfo.h
// Purpose: Miscellaneous defines for Set demo
// Author: Dr. Rick Coleman
//===================================================================
#ifndef SETINFO_H
#define SETINFO_H
// Object type defines
#define TRIANGLE 1
#define RECTANGLE 2
#define CIRCLE 3
// 2D Point structure
struct Point2D
{
double X;
double Y;
};
#endif
Demonstration program using sets and demonstrating the Union,
Intersection, and Difference set operations.
//===================================================================
// File: Code402_Sets.cpp
// Purpose: Main file for a demonstration of a set class.
// Author: Dr. Rick Coleman
//===================================================================
#include "SetObject.h"
#include "Sets2.h"
#include "Set.h"
#include "Triangle.h"
#include "Rectangle.h"
#include "Circle.h"
#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
Set *Set1, *Set2, *aSet;
SetObject *newItem;
// In this demonstration we will create two sets. The first contains
// int 1, int 2, long 4, float 5.6. The second contains int 1, long 2,
// float 3.4, double 4.5
Set1 = new Set();
// Add item 1 to this set
newItem = (SetObject *)(new Triangle(0.0, 0.0, 1.0, 1.0, 1.0, 0.0));
Set1->addItem(newItem);
// Add item 2 to this set
newItem = (SetObject *)(new Rectangle(0.0, 5.0, 5.0, 5.0));
Set1->addItem(newItem);
// Add item 3 to this set
newItem = (SetObject *)(new Circle(5.0, 5.0, 5.0));
Set1->addItem(newItem);
Set2 = new Set();
// Add a duplicate of the last circle in Set 1 to Set 2
Set2->addItem(Set1->copy(2));
// Add item 1 to this set
newItem = (SetObject *)(new Triangle(0.0, 0.0, 2.0, 2.0, 2.0, 0.0));
Set2->addItem(newItem);
// Add item 2 to this set
newItem = (SetObject *)(new Rectangle(0.0, 10.0, 10.0, 10.0));
Set2->addItem(newItem);
// Add item 3 to this set
newItem = (SetObject *)(new Circle(5.0, 5.0, 3.0));
Set2->addItem(newItem);
// Print both Sets
cout << "Set 1 contains \n";
Set1->PrintSet();
cout << "\nSet 2 contains \n";
Set2->PrintSet();
// Create and print a union of the two
aSet = Set1->Union(Set2);
cout << "Union of Set 1 and Set 2 contains \n";
aSet->PrintSet();
// Delete that set to avoid a memory leadk
delete aSet;
// Create and print an intersection of the two
aSet = Set1->Intersection(Set2);
cout << "Intersection of Set 1 and Set 2 contains \n";
aSet->PrintSet();
// Delete that set to avoid a memory leadk
delete aSet;
// Create and print a difference of the two
aSet = Set1->Difference(Set2);
cout << "Difference of Set 1 and Set 2 contains \n";
aSet->PrintSet();
delete Set1;
delete Set2;
delete aSet;
return 0;
}
Sample screen output from running this program.
Set 1 contains
==================== Objects in Set ====================
TRIANGLE P1=0, 0 P2=1, 1 P3=1, 0
RECTANGLE UL Corner=0, 5 Width=5 Height=5
CIRCLE center=5, 5 radius=5
======================================================
Set 2 contains
==================== Objects in Set ====================
CIRCLE center=5, 5 radius=5
TRIANGLE P1=0, 0 P2=2, 2 P3=2, 0
RECTANGLE UL Corner=0, 10 Width=10 Height=10
CIRCLE center=5, 5 radius=3
======================================================
Union of Set 1 and Set 2 contains
==================== Objects in Set ====================
TRIANGLE P1=0, 0 P2=1, 1 P3=1, 0
RECTANGLE UL Corner=0, 5 Width=5 Height=5
CIRCLE center=5, 5 radius=5
CIRCLE center=5, 5 radius=5
TRIANGLE P1=0, 0 P2=2, 2 P3=2, 0
RECTANGLE UL Corner=0, 10 Width=10 Height=10
CIRCLE center=5, 5 radius=3
======================================================
Intersection of Set 1 and Set 2 contains
==================== Objects in Set ====================
CIRCLE center=5, 5 radius=5
======================================================
Difference of Set 1 and Set 2 contains
==================== Objects in Set ====================
TRIANGLE P1=0, 0 P2=1, 1 P3=1, 0
RECTANGLE UL Corner=0, 5 Width=5 Height=5
======================================================