Answer the question
In order to leave comments, you need to log in
How to switch from public data in a class to private data?
There is this C++ code:
#include <iostream>
#include <string.h>
using namespace std;
// Одно звено очереди
class Node {
public:
string login;
string password;
Node *Next;
};
class Queue {
Node *Head, *Tail; // Указатели на начало и конец очереди
public:
Queue(); // Конструктор
~Queue(); // Деструктор
void Add(string login, string password);
void Show();
};
// Значения по умолчанию
Queue::Queue() {
Head = Tail = NULL;
}
// Освобождение памяти
Queue::~Queue() {
Node *temp = Head;
while (temp != NULL) {
temp = Head->Next;
delete Head;
Head = temp;
}
}
void Queue::Add(string login, string password) {
Node *temp = new Node;
temp->login = login;
temp->password = password;
temp->Next = NULL;
if (Head != NULL) {
Tail->Next = temp;
Tail = temp;
} else {
Head = Tail = temp;
}
}
void Queue::Show() {
Node *temp = Head;
while (temp != NULL) {
cout << temp->login;
cout << ";";
cout << temp->password;
cout << endl;
temp = temp->Next;
}
cout << endl;
}
int main()
{
Queue o;
o.Add("s","ss");
o.Add("1","op");
o.Add("l","p");
o.Show();
return 0;
}
class Node {
private:
string login;
string password;
Node *Next;
};
Answer the question
In order to leave comments, you need to log in
void Add(Node node);
1. Why does Node not have a constructor?
Node::Node(string aLogin, string aPassword) {
login = aLogin;
password = aPassword;
}
I may break your life now, but here you are
#include <string>
#include <utility>
#include <iostream>
class CredentialsInfo {
std::string _login, _password;
public:
CredentialsInfo() {}
CredentialsInfo(const std::string& login, const std::string& password)
: _login(login)
, _password(password)
{ }
public:
std::string Login() const {
return _login;
}
std::string Password() const {
return _password;
}
void SetLogin(const std::string& newLogin) {
_login = newLogin;
}
void SetPassword(const std::string& newPassword) {
_password = newPassword;
}
friend std::ostream& operator<<(std::ostream&, const CredentialsInfo&);
};
std::ostream& operator<<(std::ostream& out, const CredentialsInfo& info) {
return out << info._login << "; " << info._password;
}
template <class DataClass>
class Queue {
public:
typedef DataClass value_type;
private:
struct QueueNode {
value_type Value;
QueueNode* Next;
QueueNode(value_type&& value)
: Value(std::move(value))
, Next(nullptr)
{ }
};
QueueNode *_head, *_tail;
public:
Queue()
: _head(nullptr)
, _tail(nullptr)
{ }
~Queue() {
while(_head) {
auto tmp = _head;
_head = _head->Next;
delete tmp;
}
}
public:
template <class Data>
void Add(Data&& data) {
auto* newNode = new QueueNode(std::forward<Data>(data));
if (_head) {
_tail->Next = newNode;
_tail = newNode;
} else {
_head = _tail = newNode;
}
}
template<class... Args>
void Add(Args&&... args) {
Add(value_type(std::forward<Args>(args)...));
}
void Show(std::ostream& out) const {
auto it = _head;
while (it) {
out << it->Value << '\n';
it = it->Next;
}
out.flush();
}
};
typedef Queue<CredentialsInfo> CredentialsQueue;
int main(int, char**) {
CredentialsQueue queue;
queue.Add("s", "ss");
queue.Add("1", "op");
queue.Add("1", "p");
queue.Show(std::cout);
return 0;
}
The class has access to all its members, even private ones, friend is not needed here at all. The language does not support properties, so work is done with set/get methods. Variables are made "almost always" (read always) private and made public set/get methods as needed.
I tried to rewrite the class with your instructions:
class Node {
string login;
string password;
Node *next;
public:
void setLogin(string newLogin);
void setPassword(string);
void getLogin();
};
Node::Node(string aLogin, string aPassword) {
login = aLogin;
password = aPassword;
}
void setLogin(string newLogin) {
Node temp;
temp.login= newLogin;
}
class Node {
string login;
string password;
Node *next;
public:
void setLogin(string newLogin);
void setPassword(string);
void getLogin();
};
Node::Node(string aLogin, string aPassword) {
login = aLogin;
password = aPassword;
}
void setLogin(string newLogin) {
Node temp;
temp.login= newLogin;
}
class Node {
string login;
string password;
Node *next;
public:
void setLogin(string newLogin);
void setPassword(string);
void getLogin();
};
Node::Node(string aLogin, string aPassword) {
login = aLogin;
password = aPassword;
}
void setLogin(string newLogin) {
Node temp;
temp.login= newLogin;
}
class Node {
string login;
string password;
Node *next;
public:
void setLogin(string newLogin);
void setPassword(string);
void getLogin();
};
Node::Node(string aLogin, string aPassword) {
login = aLogin;
password = aPassword;
}
void setLogin(string newLogin) {
Node temp;
temp.login= newLogin;
}
class Node {
string login;
string password;
Node *next;
public:
void setLogin(string newLogin);
void setPassword(string);
void getLogin();
};
Node::Node(string aLogin, string aPassword) {
login = aLogin;
password = aPassword;
}
void setLogin(string newLogin) {
Node temp;
temp.login= newLogin;
}
class Node {
string login;
string password;
Node *next;
public:
void setLogin(string newLogin);
void setPassword(string);
void getLogin();
};
Node::Node(string aLogin, string aPassword) {
login = aLogin;
password = aPassword;
}
void setLogin(string newLogin) {
Node temp;
temp.login= newLogin;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question