Please note that this study guide was not meant to be exhaustive. It certainly does NOT have everything that is on the midterm, and probably contains a number of items not found on the midterm. Please advise the instructor of any errors found so they can be corrected as soon as possible.
// test code was not required, but is included
#include <string>
#include <iostream>
using std::cout;
using std::ostream;
using std::string;
class MyClass {
private:
static int count;
string id;
void init(const string str) { setId(str); count++; }
public:
MyClass() { init(""); }
MyClass(string str) { init(str); }
MyClass(const MyClass& obj) { init(obj.id); }
~MyClass() { count--; }
static int getCount() { return count; }
string getId() const { return string(id); }
void setId(const string str) { id = str; }
friend ostream& operator<<(ostream& strm, const MyClass& obj);
};
int MyClass::count = 0;
ostream& operator<<(ostream& strm, const MyClass& obj) {
strm << obj.id;
return strm;
}
int main(int argc, char* argv[]) {
MyClass m1;
MyClass m2(string("12345678901234567890"));
MyClass m3(m2);
cout << "m1 = " << m1 << '\n';
cout << "m2 = " << m2 << '\n';
cout << "m3 = " << m3 << '\n';
cout << "count = " << MyClass::getCount() << '\n';
{
MyClass m4(string("Temporary item"));
cout << "m4 = " << m4 << '\n';
cout << "count = " << MyClass::getCount() << '\n';
}
m3.setId(string("New id"));
cout << "m3 = " << m3 << '\n';
cout << "count = " << MyClass::getCount() << '\n';
return 0;
}
/*
Sample output:
m1 =
m2 = 12345678901234567890
m3 = 12345678901234567890
count = 3
m4 = Temporary item
count = 4
m3 = New id
count = 3
*/