0

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(){

}

 };
Bogdan Maier
  • 663
  • 2
  • 13
  • 19
  • Have you already tried anything to do? – Dmitriy Kachko Mar 21 '12 at 14:12
  • Before going too deep into any type of explanation, are you supposed to follow certain rules for how you implement your solution? For example, are you supposed to allocate memory for your array using new? Or can you use the STL? –  Mar 21 '12 at 14:13
  • I don't know what a dynamic constructor is, but a dynamic array is best realized with [`std::vector`](http://en.cppreference.com/w/cpp/container/vector), and learning C++ is best realized with [a good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Benjamin Lindley Mar 21 '12 at 14:15
  • I know some stuff.... but still oop ain't that easy, i know and understand principlee, but the use of pointers under classes is pretty hard for me. And i`m always checking google, so anything that i should know or get better i look up on the internet. – Bogdan Maier Mar 21 '12 at 14:21
  • After seeing your code, I must reinforce my point. Get a [book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Why are all your members pointers? Why are you declaring objects in your constructor that hide the members in your class? Get a [book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Benjamin Lindley Mar 21 '12 at 14:27

3 Answers3

2

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.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
1

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.

  • I need to make this oop, so i`ll use a class foe daily expenses. There for i will take a look over the vectors deeper into c++, i`m familiar with them from python so i understand what you mean :) thank you, useful answare for storing. – Bogdan Maier Mar 21 '12 at 14:27
  • i have to use my one array structure..... i know about vector but i am not allowed to use it – Bogdan Maier Mar 28 '12 at 09:51
1

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.

Dmitriy Kachko
  • 2,804
  • 1
  • 19
  • 21