I
I
ivan8m82014-09-18 00:56:05
C++ / C#
ivan8m8, 2014-09-18 00:56:05

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;
}

There are two classes, Node is one element of the queue. Queue - the queue itself.
I am forced to do this:
class Node {
private:
    string login;
    string password;
    Node *Next;
};

But how do I record logins and passwords in Node if they are private there?
Please explain with my example. (What did I hear about friendly functions, but in practice it did not work out in this example)
PS It is obligatory that there are 2 classes.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
_
_ _, 2014-09-18
@AMar4enko

getLogin(), setLogin(value), getPassword(), setPassword(value)

V
vdem, 2014-09-18
@vdem

void Add(Node node);
1. Why does Node not have a constructor?

Node::Node(string aLogin, string aPassword) {
    login = aLogin;
    password = aPassword;
}

2. Why does the Queue need to know about the private fields (and any fields at all) of the Node? Will this help her in any way?
UPD: Make a NON-SPECIALIZED queue. I don't see the point in linking it to a username/password.
UPD2: The login/password pair of ONE user should not know about the NEXT user at all.

X
xandox, 2014-09-19
@xandox

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;
}

A
AxisPod, 2014-09-18
@AxisPod

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
ivan8m8, 2014-09-18
@ivan8m8

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;
}

Swears:
1) /main.cpp:16: error: out-of-line definition of 'Node' does not match any declaration in 'Node'
Node::Node(string aLogin, string aPassword) {
^~~~
2) main.cpp:23: error: 'login' is a private member of 'Node'
temp.login= newLogin;
^
It didn't work for me to put the data in private...
Help? Just started OOP. 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;
}

Swears:
1) /main.cpp:16: error: out-of-line definition of 'Node' does not match any declaration in 'Node'
Node::Node(string aLogin, string aPassword) {
^~~~
2) main.cpp:23: error: 'login' is a private member of 'Node'
temp.login= newLogin;
^
It didn't work for me to put the data in private...
Help? Just started OOP. 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;
}

Swears:
1) /main.cpp:16: error: out-of-line definition of 'Node' does not match any declaration in 'Node'
Node::Node(string aLogin, string aPassword) {
^~~~
2) main.cpp:23: error: 'login' is a private member of 'Node'
temp.login= newLogin;
^
It didn't work for me to put the data in private...
Help? Just started OOP. 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;
}

Swears:
1) /main.cpp:16: error: out-of-line definition of 'Node' does not match any declaration in 'Node'
Node::Node(string aLogin, string aPassword) {
^~~~
2) main.cpp:23: error: 'login' is a private member of 'Node'
temp.login= newLogin;
^
It didn't work for me to put the data in private...
Help? Just started OOP. 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;
}

Swears:
1) /main.cpp:16: error: out-of-line definition of 'Node' does not match any declaration in 'Node'
Node::Node(string aLogin, string aPassword) {
^~~~
2) main.cpp:23: error: 'login' is a private member of 'Node'
temp.login= newLogin;
^
It didn't work for me to put the data in private...
Help? Just started OOP. 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;
}

Swears:
1) /main.cpp:16: error: out-of-line definition of 'Node' does not match any declaration in 'Node'
Node::Node(string aLogin, string aPassword) {
^~~~
2) main.cpp:23: error: 'login' is a private member of 'Node'
temp.login= newLogin;
^
It didn't work for me to put the data in private...
Help? Just started OOP.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question