我很难在 C++ 中实现我想要的数据访问。类

发布于 2024-12-25 10:26:44 字数 2382 浏览 0 评论 0原文

基本上,我有一个包含各种私有数据的“A”类。类“B”是指向“A”对象的指针的单链表。类“C”具有其他(对于我的查询而言不是必需的)数据,以及对象“B”,换句话说,A 的一个列表。我应该用这种类嵌套为学校做一个简单的 C++ 项目。我想我可以通过公开所有数据来“修复”一切,但这是残酷的。

drug.h

#ifndef DRUG_H
#define DRUG_H
#include "clientlist.h"

// forward declaration
class ClientList;

class Drug {

    private:

    ClientList clients;

    std::string name;
    std::string description;
    double price;
    int stock;
    int tobuy;

    public:

    Drug(std::string n = "drug", std::string d = "description", double p = 0.0, int s = 0, int b = 0);

    void print();
};

#endif

drug.cpp

#include <iostream>
#include <string>
#include <iomanip>
#include "drug.h"
using namespace std;

Drug::Drug(string n, string d, double p, int s, int b) {
    name = n;
    description = d;
    price = p;
    stock = s;
    tobuy = b;
}

void Drug::print() {
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout << setprecision(2);
    cout << "price is: " << price << " PLN" << endl;
    cout << "stock is: " << stock << endl;
    cout << "tobuy is: " << tobuy << endl;
    cout << "name is: " << name << endl;
    cout << "description is: " << description << endl;
}

clientlist.h

#ifndef CLIENT_LIST_H
#define CLIENT_LIST_H

//forward declaration
class Client;

class ClientList {

    private:

    struct clientlistelement {
    Client *data;
    clientlistelement *next;
    };

    clientlistelement *head;

    int numberofclients;

    public:

    ClientList();
    void print();
};

#endif

main.cpp

#include <iostream>
#include <string>
#include "drug.h"
#include "druglist.h"
#include "client.h"
#include "clientlist.h"
using namespace std;

int main() {

    Drug d;
    Client c;

    d.print();
    cout << d.clients.numberofclients << endl; // <-- error

    cout << endl;

    c.print();

    return 0;
}

也许我的设计很糟糕,但基本上药物被设计为包含一个“列表”对象,它本质上只是指向客户端对象的指针列表,而客户端对象又具有列表的药物。这种双向镜像交互的全部要点是拥有两种类型的对象的单链表,这些对象相互“分配”(读:指向),以模拟了解其客户的服务,反之亦然。但我很快就发现了几乎无法访问任何数据的问题。我尝试加我的印刷会员为好友,但没有成功,我不知道该往哪里继续。有人可以提供一些想法吗?

编辑:我绝不是一个经验丰富或优秀的 C++ 程序员,这只是我的第一堂课的开始。我想我所追求的是,​​我不明白,是以某种方式吸收对不同类的对象中的私有数据的访问。 =/

Basically, I have a class "A" with various private data. Class "B" is a singly linked list of pointers unto "A" objects. Class "C" has other (non-essential for the purposes of my query) data, and an object "B", in other words one list of A's. I'm supposed to do a simple kind of C++ project for school with this kind of class nesting. I guess I could "fix" everything by making all data public, but that's atrocious.

drug.h

#ifndef DRUG_H
#define DRUG_H
#include "clientlist.h"

// forward declaration
class ClientList;

class Drug {

    private:

    ClientList clients;

    std::string name;
    std::string description;
    double price;
    int stock;
    int tobuy;

    public:

    Drug(std::string n = "drug", std::string d = "description", double p = 0.0, int s = 0, int b = 0);

    void print();
};

#endif

drug.cpp

#include <iostream>
#include <string>
#include <iomanip>
#include "drug.h"
using namespace std;

Drug::Drug(string n, string d, double p, int s, int b) {
    name = n;
    description = d;
    price = p;
    stock = s;
    tobuy = b;
}

void Drug::print() {
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout << setprecision(2);
    cout << "price is: " << price << " PLN" << endl;
    cout << "stock is: " << stock << endl;
    cout << "tobuy is: " << tobuy << endl;
    cout << "name is: " << name << endl;
    cout << "description is: " << description << endl;
}

clientlist.h

#ifndef CLIENT_LIST_H
#define CLIENT_LIST_H

//forward declaration
class Client;

class ClientList {

    private:

    struct clientlistelement {
    Client *data;
    clientlistelement *next;
    };

    clientlistelement *head;

    int numberofclients;

    public:

    ClientList();
    void print();
};

#endif

main.cpp

#include <iostream>
#include <string>
#include "drug.h"
#include "druglist.h"
#include "client.h"
#include "clientlist.h"
using namespace std;

int main() {

    Drug d;
    Client c;

    d.print();
    cout << d.clients.numberofclients << endl; // <-- error

    cout << endl;

    c.print();

    return 0;
}

Maybe my design is just shitty, but basically a drug is designed to contain a "list" object, which is essentially just a list of pointers unto client objects, which in turn have lists of drugs. The whole point of this two-way mirrored interaction is to have singly linked lists of two types of objects which are mutually "assigned" (read: pointing on) each other, to simulate services which are aware of their customers and vice versa. But I've very quickly stumbled upon this issue of not being able to access hardly any data. I tried friending my print members to no avail and I'm not sure where to proceed. Can anybody lend some ideas?

EDIT: I am by no means an experienced or good C++ programmer, this is only the beginning of my first class. I guess what I'm after, that I don't understand, is somehow being to absorb access to private data in an object of a different class. =/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

合久必婚 2025-01-01 10:26:44

我建议分解你的基本设计。 C++ 中的类(以及一般的面向对象设计)应该对一些真实世界的对象进行建模。为此,药物可以具有属性(名称、价格等),但客户(服用药物的人)是< em>不是 Drug 的属性,不应包含在该类定义中。

同样,Client 大概是一个人,它也可以具有特定的属性(姓名、年龄、性别等)。 客户可能服用或不服用的任何药物实际上并不是该客户的属性。

我将对此进行建模,以便 DrugClient 是两个独立的不同类,其中一个类没有任何属性包含在另一个类中。完成后,您可以使用单独的类来建模 DrugClient 之间的关系。

例如:

一个 Prescription 对象可以关联一个 Client 和多个 Drug 对象,而另一个类可以反向处理关联(一个 Drug 和所有关联的 Client 对象)。

作为家庭作业,我实际上不想给您执行此操作的代码,但我想让您以正确的“面向对象”方式思考。

如果正如你所说,这个基本设计是强加给你的(这是一个非常不幸的情况,因为设计本身并不理想,数据耦合度太高,并且确实违背了一些体面的面向对象原则)。您可以简单地为您的数据类型添加访问器(getter)函数。这与公开数据不同,因为您实际上无法更改数据,但可以出于显示输出等目的获取它。

I would suggest breaking apart your basic design. Classes in C++ (and in Object-Oriented design in general) should model some real-world object. To that end, a Drug can have attributes (name, price, etc.), but Client (people that take the Drug) are not attributes of a Drug and should not be included in that class definition.

In that same vein, a Client is presumably a person, which again can have specific attributes (name, age, gender, etc.). Any Drug that a Client may or may not be taking is not really an attribute of that Client.

I would model this so that Drug and Client are two separate distinct classes, with no attributes of one held within the other. Once that is done, you can use separate classes to model the relationships between Drug and Client.

For instance:

A Prescription object can associate one Client and multiple Drug objects, while another class can handle the association in reverse (one Drug and all the associated Client objects).

Being as how this is homework, I don't want to actually give you the code to do this, but I wanted to get you thinking in the right "object-oriented" way.

If as you say, this basic design is forced upon you (a very unfortunate situation, as the design itself is not ideal, the data is too highly coupled and really goes against some decent object-oriented principles). You can simply add accessor (getter) functions for your data types. This is different than making the data public, in that you cannot actually change the data, but you can get it for purposes of displaying output, etc.

书信已泛黄 2025-01-01 10:26:44

我不一定认为具有 ClientList 和 Clients 的 Drug 模型有问题,除了类名可能无法准确反映它们的用途之外。 “药品”似乎与其说是药品本身,不如说是“药品可用性”——库存有多少,需要购买多少。客户端可以是“PrescriptionForDrug”,其中可能包括需要多少数量的计数。

那么从客观角度来看,我不会将客户列为 DrugAvailability 的公共成员。它应该保密,因为没有人应该关心 DrugAvailability 中列出的内容。您只想知道需要多少以及库存有多少。您想尽可能隐藏“它是如何完成的”。

例如,“tobuy”不需要是 int 成员。相反,它可以是一个成员函数 ToBuy(),并且不跟踪 int 成员,当调用此函数时,它可以(私下)运行 PrescriptionForDrug 列表(即客户列表)并计算需要多少个,并将其与库存进行比较。

因此,在这种情况下,DrugAvailability.Stock() 将返回库存数量,DrugAvailability.ToBuy() 将返回需要数量,并且它将说明任何处方。您实际上不需要知道 DrugAvailability 中列出了 PrescriptionForDrug,因此您可以将该详细信息保密。

为事物起一个正确的名称很重要,这样才能准确地找出您想要解决的问题。然后,隐藏细节,只暴露外界真正需要了解的部分。

I don't necessarily see a problem with the model of Drug having ClientList having Clients, other than the class names might not accurately reflect what these are used for. The "Drug" seems not so much the drug itself, as a "DrugAvailability" -- how much is in stock, how much is needed to buy. The Clients could be "PrescriptionForDrug", which would probably include a count of how many are needed.

Then in object terms, I would not make the client list a public member of DrugAvailability. It should be kept private because nobody should care about what is listed inside of DrugAvailability. You just want to know, how many are needed and how many are in stock. You want to hide the "how it's done" as much as possible.

For example, the "tobuy" does not need to be an int member. Instead, it can be a member function, ToBuy(), and instead of tracking an int member, when this function is called it can (privately) run down the list of PrescriptionForDrug (i.e. the client list) and count up how many are needed, and compare that to stock.

So in that case, DrugAvailability.Stock() would return how many are in stock, DrugAvailability.ToBuy() would return how many are needed, and it would account for any prescriptions. You don't really need to know that there are PrescriptionForDrug listed inside of DrugAvailability, so you keep that detail private.

It's important to come up with the right names of things, to find out exactly what you are trying to solve. Then, hide the details and only surface the parts that the outside world really needs to know about.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文