简体   繁体   中英

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.

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 . You can extend this if needed, or use it as-is.

2) A container of pointers or, better yet, smart pointers to Expense objects. Look up std::vector or std::map if you need fast lookup by some argument.

3) Create new expenses dynamically with new : new Expense() .

4) Add to the container:

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.

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. And then wrap that all up in a class to create methods for the functionality.

Research the STL vector class. 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)

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.

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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