Problem Statement: Expense class
You are required to write a class name “Expenses “. Expenses class has
• Constructors
• Destructor
• Write a function to add expenses for a month
• Write function which displays these expenses.
• Write a function which calculates savings for a month.
• Expense class has following data members
o Total Income // total income of person/family
o Gbill //Gas bill
o Ebill //Electricity bill
o Wbill // water Bill
o OExpenses // any other expenses
o Month //Month for which you are calculating these expenses

You have to populate all these record on monthly basis; to populate records; you have to use getter and setters for all data members

#include <iostream>
using namespace std;



      class expense
      {
            private:
                    int income;
                    int gbill;
                    int ebill;
                    int wbill;
                    int saving;
                    public:
                           void display();
                           void getTotalIncome();
                           void getEBill();
                           void getWBill();
                           void getGBill(); 
                           
                           }
                           
void expense::display()
{
     cout <<"expense"<<income<<","<<ebill<<","<<gbill<<","<<wbill;
void expense::getTotalIncome(int i)
{
     income=i;
     return income;                            
}
void expense::getEBill(int i)
{
     ebill=i;
     return ebill;
     }
void expense::getGBill(int i)
{
     gbill=i;
     return gbill;
     
     }
void expense::getWBill(int i)
{
     wbill=i;
     return wbill;
     }
     
int main()
{
    expense m1,m2,m3,m4,m5,m6;
    m1.display();
    m2.display();
    m3.display();
    m4.display();
    m5.display();
    m6.display();
}

please guide me wat to do in this program and how to do it.i have tried it wat i have given bt it has a lot of errors in it tell me how to improve it

You need a ; on the end of your class declaration.

class expenses {


};

You have it right that "getters" return something but you need to indicate that on their declarations and definitions (i.e., what are they returning-- they're not void). You need to just get the private values and return them. You'll need separate "setter" methods that take in a parameter and set your private variables to that value. In turn those won't return anything.

EDIT: Do NOT bump your post 2 minutes after posting it! There is no point and it makes people less likely to want to help.

Edited 6 Years Ago by jonsca: n/a

Here's one of your methods.

void expense::getGBill(int i)
{
     gbill=i;
     return gbill;
     
     }

It neither gets nor sets it's somewhere in the middle so it does nothing. If you have a private member variable such as gbill in the above case you need gatekeepers to make sure you can access the information but only on your terms. Your getter for gbill simply returns the value of gbill to the caller. It doesn't need to take in any parameters as it's returning what already exists (in this case gbill) within the object of the class. So, you need a getter that's returning something. What does void return? Nothing. So your get method should return an int (the type of gbill).

The setter, which has to be a separate method from the getter, takes in a value (of the same type as gbill and sets the private variable gbill to that value. This method will be void.

I'll leave the implementation up to you since I've given you quite a bit of it but your calls in main() would look something like this:

int gbill_in;
int gbill_out;
expense exp;
gbill_in = 20; //I'm just making this one up could come from input
exp.setGBill(gbill_in);    //void method
//say here you invoked another method that changed
//that private value internally
exp.someMethod();
//now get gbill again
gbill_out = exp.getGBill();  //returns an int

plz sir nw chk it again

#include <iostream>
#include <stdlib.h>
using namespace std;



      class expense
      {
            private:
                    int income;
                    int gbill;
                    int ebill;
                    int wbill;
                    int saving;
                    public:
                           void display();
                           void setTotalIncome();
                           void setEBill();
                           void setWBill();
                           void setGBill();
                            
                           int getEBill();
                           int getWBill();
                           int getGBill(); 
                           };
                           
void expense::display()
{
     cout <<"expense"<<","<<getEBill<<","<<getGBill<<","<<getWBill;
     }
void expense::setTotalIncome(int i)
{
     income=i;                            
}
void expense::setEBill(int i)
{
     ebill=i;
     }
void expense::setGBill(int i)
{
     gbill=i;
     
     }
void expense::setWBill(int i)
{
     wbill=i;
     }
int expenses::getEBill()
{
     return ebill;                            
}
void expense::getEBill()
{
     return ebill;
     }
void expense::getWBill()
{
     return wbill;
            }

int main()
{
    expense m1,m2,m3,m4,m5,m6;
    m1.display();
    m2.display();
    m3.display();
    m4.display();
    m5.display();
    m6.display();
}

The declaration of these methods is incorrect (the definitions are fine)

void setEBill();
void setWBill();
void setGBill();
[B]Declaration:[/B] -- what it will take in and return
void display();  //line 16 -- part of your class declaration lines 7-25

[B]Definition: [/B] -- the actual mechanics of the method
void expense::display()  //line 27
{
        cout <<"expense"<<","<<getEBill<<","<<getGBill<<","<<getWBill;
}

So fix up lines 18-21 appropriately. What parameter do they all take in?

Edited 6 Years Ago by jonsca: n/a

now i have make that correction nw how can i get that total amount n saving in function

#include <iostream>
#include <stdlib.h>
using namespace std;



      class expense
      {
            private:
                    int income;
                    int gbill;
                    int ebill;
                    int wbill;
                    int saving;
                    public:
                           expense(int, int, int);
                           int display();
                           void setTotalIncome(int);
                           void setEBill(int);
                           void setWBill(int);
                           void setGBill(int);
                            
                           int getEBill();
                           int getWBill();
                           int getGBill(); 
                           int addBills();
                           };
 expense::expense(int eb,int gb, int wb)
 {
                      ebill=eb;
                      wbill=wb;
                      gbill=gb;
                      }                          
int expense::display()
{
     cout <<"expense"<<","<<getEBill()<<","<<getGBill()<<","<<getWBill();
     }
void expense::setTotalIncome(int i)
{
     income=i;                            
}
void expense::setEBill(int i)
{
     ebill=i;
     }
void expense::setGBill(int i)
{
     gbill=i;
     
     }
void expense::setWBill(int i)
{
     wbill=i;
     }
int expense::getEBill()
{
     return ebill;                            
}
int expense::getGBill()
{
     return gbill;
     }
int expense::getWBill()
{
     return wbill;
            }
int addBills(int i,int j, int k)
{
    int income;
    cout <<"please enter the total income";
    cin >> income;
    int saving;
    saving =income-(i+j+k);
    cout<< "the saving is:"<<saving;
}
int main()
{
    expense m1(2,3,4),m2(1,2,3),m3(4,5,1),m4(1,2,3),m5(2,1,3),m6(1,2,4);
    m1.display();
    m2.display();
    m3.display();
    m4.display();
    m5.display();
    m6.display();
}

You have the income and you have the expenses... All this method will do is update your savings variable, nothing in or out.

Also, it says to have a month member variable, you are missing that.

sir chk line 27 wats wrong in that

#include <iostream>
#include <stdlib.h>
using namespace std;



      class expense
      {
            private:
                    int income;
                    int gbill;
                    int ebill;
                    int wbill;
                    int saving;
                    friend int addBills(int, int, int);
                    public:
                           expense(int, int, int);
                           int display();
                           void setTotalIncome(int);
                           void setEBill(int);
                           void setWBill(int);
                           void setGBill(int);
                            
                           int getEBill();
                           int getWBill(); 
                           int getGBill(); 
                           int addBills (ebill, gbill, wbill);
                           };
 expense::expense(int eb,int gb, int wb)
 {
                      ebill=eb;
                      wbill=wb;
                      gbill=gb;
                      }                          
int expense::display()
{
     cout <<"expense"<<","<<getEBill()<<","<<getGBill()<<","<<getWBill();
     }
void expense::setTotalIncome(int i)
{
     income=i;                            
}
void expense::setEBill(int i)
{
     ebill=i;
     }
void expense::setGBill(int i)
{
     gbill=i;
     
     }
void expense::setWBill(int i)
{
     wbill=i;
     }
int expense::getEBill()
{
     return ebill;                            
}
int expense::getGBill()
{
     return gbill;
     }
int expense::getWBill()
{
     return wbill;
            }
int addBills(int i,int j, int k)
{
    int income;
    cout <<"please enter the total income";
    cin >> income;
    int saving;
    saving =income-(i+j+k);
    cout<< "the saving is:"<<saving;
}
int main()
{
    expense m1(2,3,4),m2(1,2,3),m3(4,5,1),m4(1,2,3),m5(2,1,3),m6(1,2,4);
    m1.display();
    m2.display();
    m3.display();
    m4.display();
    m5.display();
    m6.display();
}

Any time you are declaring a method it should have the type in front of each of the parameters (e.g., int gBill). But I think what you've meant to do is leave the friend declaration right where it is and move line 27 out with your method definitions, but you won't precede it with a expense:: since it is a friend function. Bear in mind that if your friend function is private you will only be able to run it from one of the other methods in your class.

This article has been dead for over six months. Start a new discussion instead.