I am trying to write some classes and test them manually using c++ 11 in order to understand how to code CPP, but i somehow get this error "exited with non-zero status". I am new to CPP and i do not understand where i might be going wrong. I would like to maintain shallow copy. Can someone please let me know where i am wrong? Or how to modify this to make it better?
This is what i have done:
#include <cctype>
#include <iostream>
using namespace std;
class User {
public:
enum class Status { UNKNOWN, ONLINE, OFFLINE};
string *userName;
string *userUri;
Status *userStatus;
public:
User () {
userName = new string;
userUri = new string;
*userName = ""; *userUri = ""; *userStatus = Status::UNKNOWN;
}
User (string);
User (string, string);
bool isNull() {
return (userUri->empty() || userName->compare("") == 0);
}
friend bool operator==(User& user1, User& user2) {
return user1.uri().compare(user2.uri()) == 0;
}
User operator=(User& user) {
userUri = user.userUri;
userName = user.userName;
userStatus = user.userStatus;
}
string uri() { return *userUri; }
string name() { return *userName; }
Status status() { return *userStatus; }
void setName(string);
void setStatus(User::Status);
~User();
};
User::User(string uri) {
*userUri = uri;
}
User::User (string uri, string name) {
*userUri = uri;
*userName = name;
}
void User::setName(string name) {
if (isNull()) {
return;
}
*userName = name;
}
void User::setStatus(User::Status status) {
if (isNull()) {
return;
}
*userStatus = status;
}
User::~User() {
delete userName, userUri, userStatus;
userName = NULL;
userUri = NULL;
userStatus = NULL;
}
int main() {
User u, u1;
cout << u.isNull() << "\n";
cout << (u.uri().compare("") > 0) << endl;
cout << (u.name().compare("") > 0)<< endl;
cout << (u.status() == User::Status::UNKNOWN) << endl;
cout << (u == u1) << endl;
u.setName("dhara");
cout << (u.name().compare("") == 0) << endl;
u.setStatus(User::Status::ONLINE);
cout << (u.status() == User::Status::UNKNOWN) << endl;
User u2("a@a.com","Robert");
User u3 = u2;
u3.setName("dhara");
cout << u3.uri() << endl;
cout << u2.name() << endl;
return 0;
}
Aucun commentaire:
Enregistrer un commentaire