Need help creating c++ set class?
I'm creating a new class for c++ based on Set theory. I was given Set.h and have to create Set.cpp. I need help with the cardinality function. I'm uncertain how to do it. Here is the program:
#include <iostream>
#include "Set.h"
// Constructor, sets _cardinality to zero
Set::Set(){
_cardinality=0;
}
// returns true if its argument is an element of the set
bool Set::isElement(const int num) const{
for(int i=0; i<_cardinality; i++){
if(_element[i]==num){
return true;
}
else{
return false;
}
}
}
// adds the argument to the set
void Set::add( int num){
for(int i=0; i< _cardinality; i++){
if(_element[i]==num){
return;
}
else{
_element[_cardinality]=num;
_cardinality++;
i++;
}
}
}
// removes the element from the set
void Set::remove(int num){
for(int i=0; i< _cardinality; i++){
if(_element[i]!=num){
return;
}
else if(_cardinality==1){
clear();
}
else{
_element[i]=_element[i+1];
_cardinality--;
}
}
}
// resets cardinality to zero (effectively emptying the set)
void Set::clear(){
for(int i=0; i<_cardinality-1; i++){
_element[i]=0;
}
}
// returns the cardinality of the set
More Related Questions and Answers ...
The information post by website user , we not guarantee correctness.
Dental Treatment Skin Whitening Skin Problems Skin Rashes Shoes
