Need help with Class and User Defined Functions.

My program has to contain a class and user defined functions.
So far I have this,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//Class with private member variables and public member functions.
[code]class Cake{
private:
string Flavor;
double Cost;
double PricePerPound;
double Weight;
public:
Cake();
Cake(double weight, string flavor);
void setFlavor(string f);
void setCost();
void setWeight(double w);
string getFlavor();
double getCost();
double getWeight();
double getPricePerPound();
};

//These are the getter and setter member functions.
void Cake::setFlavor(string f){Flavor = f;}
void Cake::setCost(){
Cost = PricePerPound * Weight;
}
void Cake::setWeight(double w){Weight = w;}
string Cake::getFlavor(){return Flavor;}
double Cake::getCost(){return Cost;}
double Cake::getWeight(){return Weight;}
double Cake::getPricePerPound(){return PricePerPound;}

//This is the default constructor.
Cake::Cake(){
Flavor = "Vanilla";
PricePerPound = 10;
Weight = 0;
Cost = 0;
}

//This is the overloaded constructor.
Cake::Cake(double cakeWeight, string cakeFlavor){
Weight = cakeWeight;
Flavor = cakeFlavor;
}

//This is the main function.
int main(){
Cake cake;
string flavor_input;
double weight_input;
cout << "Here are the cake flavor options: Vanilla, Chocolate, and Lemon." << endl;
cout << "Enter the flavor of the cake that you want: ";
cin >> flavor_input;
cout << "Enter the weight of the cake that you want: ";
cin >> weight_input;
Cake mycake(flavor_input, weight_input);
return 0;
}


The problem is that I don't understand how would you include a user defined function that would validate the user input and another function to display it. Any help is appreciated.
Last edited on
Hello Awsom3Alan3,

To start with:

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.

One thing i did see is that the overloaded ctor is expecting to receive "weight" and "flavor" in that order, but in "main" you are sending thee ctor "flavor" and "weight". The order of the function call must match the order of the function. The compiler should have caught that and flagged it as an error.

The validation function could be a regular function if you are validating user input.

The print function I would likely make a class function, but it is not necessary.

You have already dealt with class functions and have shown that yo have an understanding of how to do that. It is not that hard to add more functions to the class or make functions outside of the class.

Tomorrow I will take another look at the program.

Hope that helps,

Andy
Thanks Andy,
I actually didn't know how to do that.
Last edited on
The thing is that it has to be a user defined function and not one from the class, which I don't know how would you include that here.
Hello Awsom3Alan3,

As I said in my last message with the line Cake mycake(flavor_input, weight_input);. The parameters are in the wrong order to match the ctor. The quick fix is to change this line, but the way I looked at it you are entering flavor and weight and then creating an object of "Cake" with flavor and weight, so the parts of the class should be changed to match. The other reason I changed what is in the class is to be consistent with the way everything is done. This will help you in the end.

You talked about functions for validating and printing, but have yet to actually say what these functions should do. If you were given instructions, or specs as I think of them, please post them so everyone will know what you are working with and what has to be done. Or please give more information on what these two functions should do. Also some kind of sample output will help.

The print function would not take long, so I work on the validating first.

Hope that helps,

Andy
Hello Awsom3Alan3,

A class function would be this:
1
2
3
4
5
//This is the overloaded constructor.
double Cake::getCost()
{
    return Cost;
}

Notice that it starts with "Cake::".

A side note: being defined inside the public area of the class the "Cake::" is not needed. This format is used when you define the function outside the class as you did with the overloaded ctor.

A regular function not of the class would be something like:
1
2
3
4
double ValidateWeight()
{
    // <--- Your code here.
}

Another option you have is:
1
2
3
4
void ValidateWeight(pass "weight_input" by reference)
{
    // <--- Your code here.
}

What is in the () is what you need to do not how to do it.

What you have to consider is if you prompt and get the input in "main" then call the function or call the function in "main" and do the input and validation in the function. Either way works.

My preference is to do everything in the function.

Hope that helps,

Andy
I appreciate the help Andy!
I made the changes you mentioned about the parameters being in the wrong order.
The only problem I have now is that in "main", I created a default cake and a custom cake object. If I create the function how would you go about doing that with the "mycake" object?

1
2
3
4
5
6
7
8
9
10
11
12
int main(){
Cake cake; //default cake;
string flavor_input;
double weight_input;
cout << "Here are the cake flavor options: Vanilla, Chocolate, and Lemon." << endl;
cout << "Enter the flavor of the cake that you want: ";
cin >> flavor_input;
cout << "Enter the weight of the cake that you want: ";
cin >> weight_input;
Cake mycake(flavor_input, weight_input); //custom cake;
return 0;
}
Last edited on
Hello Awsom3Alan3,

Same way it will not matter. As a class function you would use mycake.functionName(). As a non class function you would just call the function as functionName(),

Hope the helps,

Andy
You have a good thing baking.

I'd interpret "validation" differently than Andy. Some other spices too:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>

// A class
class Foo
{
  int x;
public:
  Foo(int);
  int get() const; // const correctness
};

bool bad( int );
std::ostream& operator<< ( std::ostream&, const Foo& );

// you need the declarations (above) to write main()

int main() {
  int y {};
  while ( std::cin >> y and bad(y) ) {}
  if ( not bad(y) ) {
    Foo bar( y );
    std::cout << bar << '\n';
  }
}

// linker needs the implementations:

// Function 1: tells whether answer is valid
bool bad( int answer ) {
  return answer != 42;
}

// Function 2: shows data. Overloaded operator
std::ostream& operator<< ( std::ostream& lhs, const Foo& rhs ) {
  lhs << rhs.get();
  return lhs;
}

Foo::Foo( int x )
: x(x) // member initializer list syntax, note that x is not x, unambiguously
{}

int Foo::get() const
{ return x; }
Topic archived. No new replies allowed.