can anyone please tell me what's wrong with my code?
I have made this code but the compiler is giving the error that all member functions are non-class type. I can't fix this. Here is my code:

#include<iostream>
#include<string>
using namespace std;

/*class Invoice which demonstrates capabilties
*/
class invoice
{
 private:
  string part_num;
  string part_desc;
  int quantity;
  int price;
 public:
  invoice(string p_n, string p_d, int q, int p) //constructor
  {
   part_num=p_n;
   part_desc=p_d;
   quantity=q;
   price=p;
  }

  void setpart_num(string number) //set function for part_num
   {
   part_num=number;
   }
   string getpart_num()
   {
   return part_num;
   }

   void setpart_desc(string description) //set function for part_desc
   {
   part_desc=description;
   }
   string getpart_desc()
   {
   return part_desc;
   }

   void setquantity(int quant) //set function for quantity
   {
    if(quant>0)
     quantity=quant;
    else
     quantity=0;
   }
   int getquantity()
   {
   return quantity;
   }

   void setprice(int pr) //set function for price
   {
   if(pr>0)
    price=pr;
   else
    price=0;
   }
   int getprice()
   {
   return price;
   }

   int GetInvoiceAmmount()
   {
    return quantity*price;
   } 

};

/*main body
*/
int main()
{
 invoice In();
 string number;
 string description;
 int quant, pr,ans;
 cout<<"enter part number"<<endl;
 getline(cin,number);
 cout<<"enter part description"<<endl;
 getline(cin,description);
 cout<<"enter the quantity of stated part"<<endl;
 cin>>quant;
 cout<<"enter price"<<endl;
 cin>>pr;
 In.setpart_num(number);
 In.getpart_num();
 In.setpart_desc(description);
 In.getpart_desc();
 In.setquantity(quant);
 In.getquantity();
 In.setprice(pr);
 In.getprice();
 ans=In.GetInvoiceAmmount();
 cout<<"invoice ammount is: "<<ans;

 return 0;
}

Recommended Answers

All 16 Replies

Oh i got one syntax error on line 76 :P

inline In;

any further mistakes?

Line 76, invoice In(); , is stating that a function exists named In that takes no input parameters and returns an object of type invoice.

Did you mean that, or are you trying to create an invoice object named In?

I created an object named In.

Did you?

yeah i know there is a slight syntax error ...If u can find it then please help.

This is the invoice constructor: invoice(string p_n, string p_d, int q, int p) //constructor

So to create an invoice object, you need to provide those parameters.

Oh Okay I'll try it :)

Oh my God I'm not getting it. Can you please explain the working of set function and Object initializing together?

int main()
{
 invoice In;
 string number;
 string description;
 int quant, pr,ans;
 cout<<"enter part number"<<endl;
 getline(cin,number);
 cout<<"enter part description"<<endl;
 getline(cin,description);
 cout<<"enter the quantity of stated part"<<endl;
 cin>>quant;
 cout<<"enter price"<<endl;
 cin>>pr;
 In.setpart_num(number);
 In.getpart_num();
 In.setpart_desc(description);
 In.getpart_desc();
 In.setquantity(quant);
 In.getquantity();
 In.setprice(pr);
 In.getprice();
 ans=In.GetInvoiceAmmount();
 cout<<"invoice ammount is: "<<ans;
 return 0;
}

I'm taking the input from user and wanna pass it to constructor through object +passing those values to set functions as well. Now my point is how to pass these parameters at the same time? Can anyone ellaborate? :/

invoice In(number, description, quant, pr);

There are two ways to approach this:

Create a new "empty" object using a default constructor, then call its set methods to populate all its data (no need to call the get methods)

Have a constructor that takes all the data values and creates a fully-populated object

OR any combination of these - eg pass all madatory data to the constructor, then use set methods for optional data

There's no absolute right or wrong here, it depends on the details of the particular case.

Moschops compiler is not taking this line.
Error is "number , description, quant,pr " are not declared in this scope.

To get that error, you must be trying to use the variables number , description, quant,pr before you created them. That won't work. In C++, you have to create the variables before you try to use them.

Can you see what is wrong with the following piece of code?

x++;
int x = 0;

There is an attempt to alter the value of x, before x has been created. That's bad.

Can you see how this fixes it?

int x = 0;
x++;

You have to create the variables, and then you can use them. If this is news to you, then what you're trying is way beyond your current ability and you need to go back to trying to understand what a variable is and how to create one.

JamesCherrill thank you your method worked ^_^
i created an "empty" object and initialized through set functions. But I didn't get the second method u stated ..
explain it please.

Oh i know this thing.
I tried this also :

invoice In(string number, string description, int quant, int pr);

it also didn't work.

Oh thank you Moschops. It helped. I tried this code of block:

int main()
{
 string number;
 string description;
 int quant, pr,ans;
 invoice In(number,description,quant,pr);
 cout<<"enter part number"<<endl;
 getline(cin,number);
 cout<<"enter part description"<<endl;
 getline(cin,description);
 cout<<"enter the quantity of stated part"<<endl;
 cin>>quant;
 cout<<"enter price"<<endl;
 cin>>pr;
 In.setpart_num(number);
 In.getpart_num();
 In.setpart_desc(description);
 In.getpart_desc();
 In.setquantity(quant);
 In.getquantity();
 In.setprice(pr);
 In.getprice();
 ans=In.GetInvoiceAmmount();
 cout<<"invoice ammount is: "<<ans;

 return 0;
}

and it worked :D
Thank you now program is running in a smooth way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.