cLass lunch 
{

    private:
          float cost;
          int  calories;
     public:
          lunch ();
          void set cost (float c);
          void set calories (int c);
};
 luncn: :lunch ()
{
      cost =0.0;
      calories =0;
}
void :  :lunchdisplay()
{
     cout <<"lunch costs"<<cost<<endl;
     cout<<"it has"<<calories<<"calories<<"calories"<<endl;
}
void: :lunch: :setcost (float c)
{
       cost += c;
}
void lunch: :set calories (int c)
{
      calories += c;
}
Dave Sinkula commented: Wow. Code. No code tags. Got a question? -2

Recommended Answers

All 6 Replies

Nice code you posted -- why did you post it ? (my crystle ball is broken tonight)

Read this, then try again.

That being said, I'm going to guess that you would post something like " it doesn't work", so I'll try to go over the obvious mistakes with you: cLass lunch is invalid. Needs to be class lunch . C++ is a case-sensitive language.

In your constructor for lunch, luncn: :lunch () needs to be lunch::lunch() . And on that note, when defining the member functions for your class, they should be in the format type classname::functionname(type1 param1, . . . type(n-1) param(n-1)) , where type is the return type of the function, such as int, bool, or void; classname is the name of the class who's member function you're writing; functionname is the name of the function as declared in the class; and type(n-1) and param(n-1) are just the parameter assigned to the function.

Beyond that, I'd say check out this link for some practice problems to do in C++.

Read this, then try again.

That being said, I'm going to guess that you would post something like " it doesn't work", so I'll try to go over the obvious mistakes with you: cLass lunch is invalid. Needs to be class lunch . C++ is a case-sensitive language.

In your constructor for lunch, luncn: :lunch () needs to be lunch::lunch() . And on that note, when defining the member functions for your class, they should be in the format type classname::functionname(type1 param1, . . . type(n-1) param(n-1)) , where type is the return type of the function, such as int, bool, or void; classname is the name of the class who's member function you're writing; functionname is the name of the function as declared in the class; and type(n-1) and param(n-1) are just the parameter assigned to the function.

Beyond that, I'd say check out this link for some practice problems to do in C++.

I want the user to enter as many food items as they wish.When the user is done entering food items ,display the total number of food items,the total cost of thelunch and the total number of calories .

I want the user to enter as many food items as they wish.When the user is done entering food items ,display the total number of food items,the total cost of thelunch and the total number of calories .

Ok, so what doesn't your program do that it should do? What have you tried to fix those problems ? (Hint: we are not going to write your program for you. That your job, not ours. )

Ok I agree with u but i need some held with member function definition, do i have to use int main after member function definition

you can code member functions either before or after main(). If they are coded after, then you have to provide function prototypes at the beginning of your program. For example lets say you want to write a function foo that takes an int argument and returns an int. Code it like this:

#include <blabla>

// function prototype
int foo(int);

int main()
{
   // blabla
   return 0;
}

int foo(int n)
{
  // blabla
  return 0;
}

see google for more detailed tutorials

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.