Hi there. I'm very new to C++ (or any C related language) and have been thrown a rather difficult assignment. The ides is to implement a set class, with functions for unions and intersections of sets. The first file, set.h has been given as follows:
#ifndef SET_H
#define SET_H
#include <iostream>
#include <vector>
usingnamespace std;
class Set
{
// friend operator functions
friend ostream &operator<<(ostream & out, const Set & s);
friend istream &operator>>(istream & in, Set & s);
public:
// Constructors
Set(); // default constructor
Set(const Set & s); // copy constructor
// destructor
~Set();
// operator functions
Set operator||(Set & s); // union of the set with the set s
Set operator&&(Set & s); // intersection of the set with the set s
const Set& operator=(const Set & s); // assignment
booloperator==(const Set & s); // equality
int& operator[](int index); // returns modifiable lvalue
intoperator[](int index) const; // returns rvalue
// Set membership functions
bool member(int m); // The integer m is a member
void add(int m); // add the integer m to the set
void remove(int m); // remove the integer m from the set
private:
vector<int> _s;
};
#endif
So I'm trying (and failing) to write the functions for that class,
std::vector's .at() doesn't return true or false; it returns the character that is there.
As for your union/intersection, I'd rather you use named functions for them instead of overloading. Other than that, you are doing some evil stuff in there. Operators like that shouldn't be modifying their arguments, but you are screwing with s in there so if I do:
Thing is, the file set.h was given, so I have no choice but to overload the operators, something I'd really rather avoid doing if I could. I've also had another look at the .at() thing, and I'm now wondering what the heck I was thinking there!