简体   繁体   English

动态数组,动态构造函数

[英]Dynamic array, dynamic constructor

I would like to get some good information about the themes mentioned in the title. 我想获得有关标题中提到的主题的一些很好的信息。

So, I would like to get some good notes/tutorials about how to create a dynamic class constructor and save the class instances in a dynamic array which will be defined by the user. 因此,我想获得一些有关如何创建动态类构造函数并将类实例保存在用户定义的动态数组中的良好注释/教程。 I'd like to achive the fallowing problem: 我想解决这个休闲问题:

"A family wants to manage its monthly expenses. In order to complete this task, the family needs an application to store, for a certain month, all the family's expenses. Each expense will be stored in the application through the following elements: day (of the month in which it was made), amount of money and the type of the expense (the family wants to group its expenses in the following categories: house keeping, food, transport, clothing, telephone&internet, others – books, films, sports, etc). The family needs an application in order to repeatedly execute the following functionalities (each functionality is exemplified):" “一个家庭希望管理其每月支出。为了完成此任务,该家庭需要一个应用程序来存储该家庭的所有支出在特定月份内。每个支出将通过以下元素存储在应用程序中:天(费用,支出金额和支出类型(家庭希望将支出归为以下几类:房屋,食品,交通,服装,电话和互联网,以及其他–书籍,电影,体育家族需要一个应用程序来重复执行以下功能(每个功能都得到示例):”

Thank you. 谢谢。 PS: I'd like to mention i`m new to c++, but having knowledge about OOP from python. PS:我想提一下我是C ++的新手,但是对python的OOP有一定的了解。

EDIT: i got this far by now. 编辑:我到现在为止。

 class ExpC
  {
private:
int *days;
int *houseK;
int *food;
int *transp;
int *cloth;
int *telNet;
int *others;

public:
/* constructor */

ExpC()                 //Constructor
{
int *days,* houseK,*food,*transp,*cloth,*telNet,*others;
}

~ExpC()                //Deconstructor
{

}

void add(){

}

 };

It seems to me like you need: 在我看来,您需要:

1) A base class - Expense . 1)基类- Expense You can extend this if needed, or use it as-is. 您可以根据需要扩展它,也可以原样使用。

2) A container of pointers or, better yet, smart pointers to Expense objects. 2)指向Expense对象的指针或更好的智能指针的容器。 Look up std::vector or std::map if you need fast lookup by some argument. 如果需要通过某些参数快速查找,请查找std::vectorstd::map

3) Create new expenses dynamically with new : new Expense() . 3)使用newnew Expense()动态创建新费用。

4) Add to the container: 4)添加到容器中:

std::vector<Expense*> expenses;
expenses.push_back(new Expense());

and free the memory when you are done. 并在完成后释放内存。

EDIT: 编辑:

Since your code is completely wrong, I suggest reading a good C++ book or tutorial, and then take the approach I suggested. 由于您的代码是完全错误的,因此建议阅读一本不错的C ++书籍或教程,然后采用我建议的方法。

It sounds to me like you need to look into creating a structure that holds the budget information (month, food, transportation, etc). 在我看来,您需要考虑创建一个包含预算信息(月,食物,交通等)的结构。 Then, if you can use the STL, look into creating a vector of your structure which would easily allow you to create expense data for as many months as the user requires. 然后,如果可以使用STL,请研究创建结构的向量,该向量可以轻松地创建用户所需的长达数月的费用数据。 And then wrap that all up in a class to create methods for the functionality. 然后将所有内容包装在一个类中以创建该功能的方法。

Research the STL vector class. 研究STL向量类。 A vector is a dynamic array that can be of any type from a basic data type such as an integer to something more complex like a user-defined data type (in your case, an expense structure). 向量是动态数组,可以是任何类型,从基本数据类型(例如整数)到更复杂的东西(例如用户定义的数据类型)(在您的情况下为费用结构)。 Research vectors. 研究载体。 They are fairly easy to implement and use if you are already familiar with arrays. 如果您已经熟悉数组,它们将非常容易实现和使用。

But please provide a little more information as to what you can and can't do. 但是,请提供更多有关您可以做什么和不能做什么的信息。

My DB coding experience says me to put each of expences in separate record (object) 我的DB编码经验表明我将每种费用都放在单独的记录(对象)中

So better would be like that 这样会更好

class Expence {
public:
  enum Type {k_food, k_house, k_transport /*, etc*/};

  Expence (int date, Type type, float amount) :
  date_(date),
  type_(type),
  amount_(amount)
  {}

private:
  int date_; 
  Type type_; 
  float amount_;
};

Add to it all methods you need. 在其中添加您需要的所有方法。

The rest of the program will be look like Luchian Grigore says. 该程序的其余部分将看起来像Luchian Grigore所说。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM