help with Implementing class Item

Pages: 1234
I did in my code above....
is it on
L58-L62

if not could you point me to what line its on? I want to look at it and try to understand
it for future reference.


just confused because of this part of the instructions
constructor #2 The new fruit's name, amount, weight, etc. are arguments.
Calls the individual setters (below) to validate and set the
variable.

set_name Accept a string argument and copies it into the name variable
set_amount Accepts an int argument and copies it into amount variable, but ONLY if >= 0
set_weight Accept a float argument and copies it into the weight variable if valid
set_price Accepts a double argument and copies it into the price, if reasonable


Retrieval of data is done with getters:

Member Function/Method Description 

get_name Returns the value in name

get_amount Returns the value of rooms

get_weight Returns the value of square_feet

get_prie Returns the list_price of the house




Provide two constructors: one for a “default” object, and one for an object where all the initial
values are provided. Provide setters that validate new values and changes data members if OK.
Provide getters. One of the getters should be a show / display / print / cout getter that shows
all information about the object. Another getter should show some combination of data members,
such as: get_price_per_sqft, which performs a calculation and then produces a result:
get_price_per_sqft() returns list_price / square_feet.


After the class is working, create at least 5 different, separate instances of the class,
such as: fruit1, fruit2, ... fruit5. Create 3 instances with all valid data and 2 instances
with some (attempted) invalid data. .
(apple, orange, banana, grapes, avocado)


Setters should NOT change existing valid member values to invalid values. If given
invalid data in a setter, reject it. Do not change the value.

Modify the program to ask: "How many objects do you want?". Loop as many times specified
by the user (5 is enough for testing) to create that many objects. You can use 5 individual
variables, or you can use an array or vector. Submit one .cpp file. Do not create the class
in a separate file.
Last edited on
¿why do you have Fruit and Fruit_t?
¿what's your current problem with that wall of text?
fruit_t is for my 1st constructor (default with no parameters)
You do not need two classes with essentially the same data.
1
2
    Fruit() {} // this is your default constructor
    Fruit(const std::string& z, int qty, double wgt, float prc, bool isp) // this is a non-default constructor 


PS: 'z' is a bad name for a variable. Also, abbreviations like 'prc' and 'isp' are not obvious and should be expanded more.
Last edited on
does this look any better?



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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream> // for cin, cout, endl
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

// provide at least 4 attributes of different types
class Fruit {
public:
    
    //default constructor/sets everything to 0
    Fruit() {}
    
    
    // non-default constructor
    Fruit(const std::string& fruitName, int quantity,
          double weight, float price, bool ispurchased) :
    name(fruitName), is_purchased(ispurchased) {
        setQuantity(quantity), setWeight(weight), setPrice(price);
    }

    void setName(const std::string& x) { name = x; }
    auto getName() const { return name; }

    void setQuantity(int q) { if (q >= 0) quantity = q; }
    auto getQuantity() const { return quantity; }

    void setWeight(double w) { if (w > 0) weight = w; }
    auto getWeight() const { return weight; }

    void setPrice(float p) { if (p >= 0) price = p; }
    auto getPrice() const { return price; }

    void setIs_purchased(bool i) { is_purchased = i; }
    auto getIs_purchased() const { return is_purchased; }

private:
    string name;
    int quantity {};
    double weight {};
    float price {};
    bool is_purchased {};
};
    

int main() {

// to retrieve data it says to use getters...is it correctly done on L22-L35
/*
     Member Function/Method       Description 

     get_name                             Returns the value in name

     get_amount                          Returns the value of amount

     get_weight                           Returns the value of weight
     get_price                              Returns the price 

*/

// this must be wrong V
    Fruit apple("apple", 4, 4.56, 4, true),
        orange("orange", 7, 2.65, 5, false),
        banana("banana", 4, 2.34, 2, true),
        avocado("avocado", 150, -3.45, -150000, true),
        grapes("grapes", -3, -1000, -10000, false);

    
    
    cout << "This program creates at least 5 separate, co-existing instances of a\n"
            "custom designed object. Information about each object is displayed.\n"
            "Each constructor, setter and getter is tested at least once.\n";
    
    
    cout <<"\nname:" <<apple.getName() <<"   price:"<< apple.getPrice()
    << "  weight:"<< apple.getWeight() << "  quantity:" << apple.getQuantity()
    <<"  was it purchased?:"<< boolalpha << apple.getIs_purchased() << '\n';
    
    
    cout << "name:" << orange.getName() << "  price:" << orange.getPrice()
    << "  weight:"<< orange.getWeight() << "  quantity:" << orange.getQuantity()
    << "  was it purchased?:"<< boolalpha << orange.getIs_purchased() << '\n';
    
    cout << "name:" << banana.getName() << "  price:" << banana.getPrice()
    << "  weight:"<< banana.getWeight() << "  quantity:" << banana.getQuantity()
    << "  was it purchased?:"<< boolalpha << banana.getIs_purchased() << '\n';
    
    cout << "name:" << avocado.getName() << " price:" << avocado.getPrice()
    << "  weight:"<< avocado.getWeight() << "     quantity:" << avocado.getQuantity()
    << "  was it purchased?:"<< boolalpha << avocado.getIs_purchased() << '\n';
    
    cout << "name:" << grapes.getName() << "  price:" << grapes.getPrice()
    << "  weight:"<< grapes.getWeight() << "     quantity:" << grapes.getQuantity()
    << "  was it purchased?:"<< boolalpha << grapes.getIs_purchased() << '\n';
    
    
    
    cout << "\nThis ends the class design, implementation, test program. Goodbye!\n";
    return EXIT_SUCCESS;
}   // end of main 
Last edited on
does this look any better?


What do you think?
Is there a reason your constructor uses an initialiser list to set some data members, and setter methods to set other?

Why the inconsististency?
no specific reason. I'm using YouTube videos and the feedback from here to build
this code. I guess it would be best to only use setter methods


going to edit my code then I will post again
Last edited on
No destructor?
I'd say there's no point in a destructor, at least for this exercise.
just looked over the instructions..theres no mention of adding a destructor.
inside main how would I return my int, double, float, and bool?

I tried using this same format but got errors

Fruit fruit1;
fruit1.set_Name("Apple");
cout << fruit1.get_Name();


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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Compiler directives
#include <iostream> // for cin, cout, endl
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

// class
class Fruit {
    //access specifier
public:
    //default constructor
    Fruit() {}
    // non-default constructor
    Fruit(const std::string& fruitName, int quantity, double weight, float price, bool ispurchased) :
    name(fruitName), is_purchased(ispurchased) {
        set_Quantity(quantity), set_Weight(weight), set_Price(price);
    }
    void set_Name(const std::string& x) { name = x; }
    auto get_Name() const { return name; }

    void set_Quantity(int q) { if (q >= 0) quantity = q; }
    auto get_Quantity() const { return quantity; }

    void set_Weight(double w) { if (w > 0) weight = w; }
    auto get_Weight() const { return weight; }

    void set_Price(float p) { if (p >= 0) price = p; }
    auto get_Price() const { return price; }

    void set_Is_purchased(bool i) { is_purchased = i; }
    auto get_Is_purchased() const { return is_purchased; }

private:
    string name;
    int quantity {};
    double weight {};
    float price {};
    bool is_purchased {};
};
    

// need to add Constructors and setters to ensure that invalid values are not accepted for attributes.
    //For example, some values cannot be negative; a list price would not be negative, etc.
    //Other validation may apply, such as the number of quantity could be only 1 to 10, perhaps.

// If given invalid data in a constructor, modify it to some reasonable default value. An easy, quick,
    //safe default is to use a default number, like 0. Your choice. Constructors do not interact with
    //the user. Don’t ask the user to correct invalid data inside the constructor. The constructor has
    //to work independently of whether there is a user sitting at a keyboard or not. Constructors,
    //setters and getters need to work under many circumstances – as when there is NO user sitting
    // at a screen with a keyboard.


// Setters should NOT change existing valid member values to invalid values.
    // If given invalid data in a setter, reject it. Do not change the value.


// for testing: The testing code should test each member function: constructors, setters and getters.
// To get the values for the data, you can “hard code” the data – using literals,
    // or you can ask the user for data using a get_input function. Allow the user to enter negative
    // values so invalid values can be presented to the class constructors and setters.
    //Data validation is a key reason for having constructors and setters.

// Your driver code should create at least 5 instances of your class and display the contents of each instance.

int main() {

    
// create at least 5 different, separate instances of the class, such as: pet1, pet2, ... pet5.
    
    cout << "This program creates at least 5 separate, co-existing instances of a\n"
            "custom designed object. Information about each object is displayed.\n"
    "Each constructor, setter and getter is tested at least once.\n"<<endl;
    
    // fruit 1
    cout << "Name: ";
    Fruit fruit1;
    fruit1.set_Name("Apple");
    cout << fruit1.get_Name();
    cout << "    Quantity:" <<endl;
    
    // fruit 1
    cout << "Name: ";
    Fruit fruit2;
    fruit1.set_Name("Orange");
    cout << fruit1.get_Name();
    cout << "   Quantity:" <<endl;
    
    // fruit 3
    cout << "Name: ";
    Fruit fruit3;
    fruit1.set_Name("Banana");
    cout << fruit1.get_Name();
    cout << "   Quantity:" <<endl;
    
    // fruit 4
    cout << "Name: ";
    Fruit fruit4;
    fruit1.set_Name("Avocado");
    cout << fruit1.get_Name();
    cout << "  Quantity:" <<endl;
    
    // fruit 5
    Fruit fruit5;
    cout << "Name: ";
    fruit1.set_Name("Grapes");
    cout << fruit1.get_Name();
    cout << "   Quantity:" <<endl;
    
    cout << "\nThis ends the class design, implementation, test program. Goodbye!\n";
    return EXIT_SUCCESS;
}   // end of main


// Modify the program to ask: "How many objects do you want?". Loop as many times specified by the user
// (5 is enough for testing) to create that many objects. You can use 5 individual variables, 
//or you can use an array or vector.  
Last edited on
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
int main() {


	// create at least 5 different, separate instances of the class, such as: pet1, pet2, ... pet5.

	cout << "This program creates at least 5 separate, co-existing instances of a\n"
		"custom designed object. Information about each object is displayed.\n"
		"Each constructor, setter and getter is tested at least once.\n" << endl;

	// fruit 1
	cout << "Name: ";
	Fruit fruit1;
	fruit1.set_Name("Apple");
	cout << fruit1.get_Name();
	cout << "    Quantity: " << fruit1.get_Quantity() << endl;

	// fruit 1
	cout << "Name: ";
	Fruit fruit2;
	fruit1.set_Name("Orange");
	cout << fruit1.get_Name();
	cout << "   Quantity: " << fruit2.get_Quantity() << endl;

	// fruit 3
	cout << "Name: ";
	Fruit fruit3;
	fruit1.set_Name("Banana");
	cout << fruit1.get_Name();
	cout << "   Quantity: " << fruit3.get_Quantity() << endl;

	// fruit 4
	cout << "Name: ";
	Fruit fruit4;
	fruit1.set_Name("Avocado");
	cout << fruit1.get_Name();
	cout << "  Quantity: " << fruit4.get_Quantity() << endl;

	// fruit 5
	Fruit fruit5;
	cout << "Name: ";
	fruit1.set_Name("Grapes");
	cout << fruit1.get_Name();
	cout << "   Quantity: " << fruit5.get_Quantity() << endl;

	cout << "\nThis ends the class design, implementation, test program. Goodbye!\n";
	return EXIT_SUCCESS;
}   // end of main 




This program creates at least 5 separate, co-existing instances of a
custom designed object. Information about each object is displayed.
Each constructor, setter and getter is tested at least once.

Name: Apple    Quantity: 0
Name: Orange   Quantity: 0
Name: Banana   Quantity: 0
Name: Avocado  Quantity: 0
Name: Grapes   Quantity: 0

This ends the class design, implementation, test program. Goodbye!


Obviously the quantity displayed is 0 as this isn't set - just the name.
Last edited on
what do I need to change here to set the quantity to a custom int, custom weight, custom price, custom bool option?

1
2
3
4
5
6
7
8
9
10
11

    Fruit fruit1;
    cout << "Name: ";
    fruit1.set_Name("Apple");
    cout << fruit1.get_Name();

cout << "    Quantity: "
    fruit1.set_Quantity ();
    << fruit1.get_Quantity();

Last edited on
Well for the .set_xxx() functions you need to provide the value to set. eg

 
fruit1.set_Quantity(5);


sets the quantity of fruit1 to 5.
I was trying that and it wasn't working but I found my error. I was using variables under 10 and I had it set as q >= 10 instead of q <= 10

1) How can I go to my next step? it says..
Modify the program to ask: "How many objects do you want?". Loop as many times specified by the user (5 is enough for testing) to create that many objects. You can use 5 individual variables, or you can use an array or vector.

not sure where to place the counter


2) what can I change on L23 to set a limit form 0-10.
if I put a negative number for the quantity it should set it to the default which is 0 but in this code it just takes on the negative number

This doesn't allow values below 0 but it allows any number when I want the limit to be set so the highest number possible is 10.
void set_Quantity(int q) { if (q >= 0) quantity = q; }
auto get_Quantity() const { return quantity; }

This doesn't allow negative values and only allows values above 10
void set_Quantity(int q) { if (q <= 10) quantity = q; }
auto get_Quantity() const { return quantity; }

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <iostream> // for cin, cout, endl
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

// class
class Fruit {
    //access specifier
public:
    //default constructor
    Fruit() {}
    
    // non-default constructor
    Fruit(const std::string& fruitName, int quantity, double weight, float price, bool ispurchased) :
    name(fruitName), is_purchased(ispurchased) {
        set_Quantity(quantity), set_Weight(weight), set_Price(price);
    }
    // name
    void set_Name(const std::string& x) { name = x; }
    auto get_Name() const { return name; }
    // quantity
    void set_Quantity(int q) { if (q <= 10) quantity = q; }
    auto get_Quantity() const { return quantity; }
    // weight
    void set_Weight(double w) { if (w > 0) weight = w; }
    auto get_Weight() const { return weight; }
    // price
    void set_Price(float p) { if (p >= 0) price = p; }
    auto get_Price() const { return price; }
    // is it purchased?
    void set_Is_purchased(bool i) { is_purchased = i; }
    auto get_Is_purchased() const { return is_purchased; }

private:
    string name;
    int quantity {};
    double weight {};
    float price {};
    bool is_purchased {};
};
    

// need to add Constructors and setters to ensure that invalid values are not accepted for attributes.
    //For example, some values cannot be negative; a list price would not be negative, etc.
    //Other validation may apply, such as the number of quantity could be only 1 to 10, perhaps.

// If given invalid data in a constructor, modify it to some reasonable default value. An easy, quick,
    //safe default is to use a default number, like 0. Your choice. Constructors do not interact with
    //the user. Don’t ask the user to correct invalid data inside the constructor. The constructor has
    //to work independently of whether there is a user sitting at a keyboard or not. Constructors,
    //setters and getters need to work under many circumstances – as when there is NO user sitting
    // at a screen with a keyboard.


// Setters should NOT change existing valid member values to invalid values.
    // If given invalid data in a setter, reject it. Do not change the value.


// for testing: The testing code should test each member function: constructors, setters and getters.
// To get the values for the data, you can “hard code” the data – using literals,
    // or you can ask the user for data using a get_input function. Allow the user to enter negative
    // values so invalid values can be presented to the class constructors and setters.
    //Data validation is a key reason for having constructors and setters.

// Your driver code should create at least 5 instances of your class and display the contents of each instance.

int main() {
// create at least 5 different, separate instances of the class, such as: pet1, pet2, ... pet5.
    cout << "This program creates at least 5 separate, co-existing instances of a\n"
            "custom designed object. Information about each object is displayed.\n"
    "Each constructor, setter and getter is tested at least once.\n"<<endl;
    
    
    
    // fruit 1
    Fruit fruit1;
    cout << "Name: ";
    fruit1.set_Name("Apple");
    cout << fruit1.get_Name();
    // sets quantity has to be less than 10
    fruit1.set_Quantity(4);
    cout << "    Quantity: "
    << fruit1.get_Quantity();
    // sets weight
    fruit1.set_Weight(4.56);
    cout << "   Weight: "
    << fruit1.get_Weight();
    // sets price
    fruit1.set_Price(4);
    cout << "   Price: $"
    <<fruit1.get_Price();
    // sets if its purchased or not
    fruit1.set_Is_purchased(true);
    cout<< "   Did you purchase?: "<<fruit1.get_Is_purchased()
    << endl;
    
    // fruit 2
    cout << "Name: "; Fruit fruit2; fruit2.set_Name("Orange"); cout << fruit2.get_Name();
    fruit2.set_Quantity(7); cout << "   Quantity: " << fruit2.get_Quantity();
    fruit2.set_Weight(2.65); cout << "   Weight: " << fruit2.get_Weight();
    fruit2.set_Price(5); cout << "   Price: $" <<fruit2.get_Price();
   
    fruit2.set_Is_purchased(true); cout << "   Did you purchase?: "
    <<fruit2.get_Is_purchased() << endl;
    
    // fruit 3
    cout << "Name: "; Fruit fruit3; fruit3.set_Name("Banana"); cout << fruit3.get_Name();
    fruit3.set_Quantity(4); cout << "   Quantity: " << fruit3.get_Quantity();
    fruit3.set_Weight(2.34); cout << "   Weight: " << fruit3.get_Weight();
    fruit3.set_Price(2); cout << "   Price: $" <<fruit3.get_Price();
   
    fruit3.set_Is_purchased(true); cout << "   Did you purchase?: "
    <<fruit3.get_Is_purchased() << endl;
    
    // fruit 4
    cout << "Name: "; Fruit fruit4; fruit4.set_Name("Avocado"); cout << fruit4.get_Name();
    fruit4.set_Quantity(6); cout << "  Quantity: " << fruit4.get_Quantity();
    fruit4.set_Weight(2.59); cout << "   Weight: " << fruit4.get_Weight();
    fruit4.set_Price(6); cout << "   Price: $" <<fruit4.get_Price();
   
    fruit4.set_Is_purchased(true); cout << "   Did you purchase?: "
    <<fruit4.get_Is_purchased() << endl;
    
    // fruit 5
    cout << "Name: "; Fruit fruit5; fruit5.set_Name("Grape"); cout << fruit5.get_Name();
    fruit5.set_Quantity(6); cout << "    Quantity: " << fruit5.get_Quantity();
    fruit5.set_Weight(2.59); cout << "   Weight: " << fruit5.get_Weight();
    fruit5.set_Price(6); cout << "   Price: $" <<fruit5.get_Price();
    
    fruit5.set_Is_purchased(false); cout << "   Did you purchase?: "
    <<fruit5.get_Is_purchased() << endl;
    
    cout << "\nThis ends the class design, implementation, test program. Goodbye!\n";
    return EXIT_SUCCESS;
}   // end of main
Last edited on
A few small changes whereby you can use functions to save a lot of repetition and take advantage of the several ways to construct a fruit object and retrieve the details and using <iomanip> to setout columns - once and for all cases


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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <iostream> // for cin, cout, endl
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

// class
class Fruit
{
    //access specifier
public:
    //default constructor
    Fruit() {}
    
    // non-default constructor
    Fruit(
          const std::string& fruitName, int quantity, double weight,
          float price, bool ispurchased) :
    name(fruitName), is_purchased(ispurchased)
    {
        set_Quantity(quantity);
        set_Weight(weight);
        set_Price(price);
    }
    
    ~Fruit(){cout << "Fruit object deleted. \n";} // <---
    
    // name
    void set_Name(const std::string& x) { name = x; }
    auto get_Name() const { return name; }
    // quantity
    void set_Quantity(int q)
    { if (q < 0)
        
        quantity = 0;
    else
        quantity = q;
    }
    
    auto get_Quantity() const { return quantity; }
    // weight
    void set_Weight(double w) { if (w > 0) weight = w; }
    auto get_Weight() const { return weight; }
    // price
    void set_Price(float p) { if (p >= 0) price = p; }
    auto get_Price() const { return price; }
    // is it purchased?
    void set_Is_purchased(bool i) { is_purchased = i; }
    auto get_Is_purchased() const { return is_purchased; }
    
    void print()
    {
        cout
        << "Name: " << setw(10) << left << name
        << "Quantity: " << setw(3) << right << quantity << ' '
        << "Weight: " << setw(4) << right << weight << ' '
        << "Price: $" << setw(3) << left << price << ' '
        << "Did you purchase? "  << setw(6) << right << boolalpha
        << is_purchased << '\n';
    }
    
private:
    string name;
    int quantity {};
    double weight {};
    float price {};
    bool is_purchased {};
};

int main()
{
    cout
    << "This program creates at least 5 separate, co-existing instances of a\n"
    << "custom designed object. Information about each object is displayed.\n"
    << "Each constructor, setter and getter is tested at least once.\n\n";
    
    // fruit 1 - set's method
    Fruit fruit1;
    fruit1.set_Name("Apple ");
    fruit1.set_Quantity(4);
    fruit1.set_Weight(4.56);
    fruit1.set_Price(4);
    fruit1.set_Is_purchased(true);
    
    fruit1.print(); // <----
    
    
    
    // fruit 2 - construuctor method
    Fruit fruit2("Orange", 7, 2.65, 5, true); // <---
    fruit2.print(); // <--
    
    // fruit 2a - construuctor method
    Fruit fruit2a("Negative", -7, 2.65, 5, true); // <---
    fruit2a.print(); // <--
    
    
    cout
    << '\n'
    << "And then it can be done this way ...\n";
    // fruit 3
    cout << "Name: "; Fruit fruit3; fruit3.set_Name("Banana"); cout << fruit3.get_Name();
    fruit3.set_Quantity(4); cout << "   Quantity: " << fruit3.get_Quantity();
    fruit3.set_Weight(2.34); cout << "   Weight: " << fruit3.get_Weight();
    fruit3.set_Price(2); cout << "   Price: $" <<fruit3.get_Price();
    
    fruit3.set_Is_purchased(true); cout << "   Did you purchase?: "
    <<fruit3.get_Is_purchased() << endl;
    
    // fruit 4
    cout << "Name: "; Fruit fruit4; fruit4.set_Name("Avocado"); cout << fruit4.get_Name();
    fruit4.set_Quantity(-6); cout << "  Quantity: " << fruit4.get_Quantity(); // <--
    fruit4.set_Weight(2.59); cout << "   Weight: " << fruit4.get_Weight();
    fruit4.set_Price(6); cout << "   Price: $" <<fruit4.get_Price();
    
    fruit4.set_Is_purchased(true); cout << "   Did you purchase?: "
    <<fruit4.get_Is_purchased() << endl;
    
    // fruit 5
    cout << "Name: "; Fruit fruit5; fruit5.set_Name("Grape"); cout << fruit5.get_Name();
    fruit5.set_Quantity(6); cout << "    Quantity: " << fruit5.get_Quantity();
    fruit5.set_Weight(2.59); cout << "   Weight: " << fruit5.get_Weight();
    fruit5.set_Price(6); cout << "   Price: $" <<fruit5.get_Price();
    
    fruit5.set_Is_purchased(false); cout << "   Did you purchase?: "
    <<fruit5.get_Is_purchased() << endl;
    
    cout
    << '\n'
    << "This ends the class design, implementation, test program. Goodbye!\n\n"
    << "Now for the object/memory cleanup via the destructor\n";
    
    
    return EXIT_SUCCESS;
}   // end of main




This program creates at least 5 separate, co-existing instances of a
custom designed object. Information about each object is displayed.
Each constructor, setter and getter is tested at least once.

Name: Apple     Quantity:   4 Weight: 4.56 Price: $4   Did you purchase? true     
Name: Orange    Quantity:   7 Weight: 2.65 Price: $5   Did you purchase? true     
Name: Negative  Quantity:   0 Weight: 2.65 Price: $5   Did you purchase? true     

And then it can be done this way ...
Name: Banana   Quantity: 4   Weight: 2.34   Price: $2   Did you purchase?: true
Name: Avocado  Quantity: 0   Weight: 2.59   Price: $6   Did you purchase?: true
Name: Grape    Quantity: 6   Weight: 2.59   Price: $6   Did you purchase?: false

This ends the class design, implementation, test program. Goodbye!

Now for the object/memory cleanup via the destructor
Fruit object deleted. 
Fruit object deleted. 
Fruit object deleted. 
Fruit object deleted. 
Fruit object deleted. 
Fruit object deleted. 
Program ended with exit code: 0
Last edited on
@againtry thank you that's really helpful. I was starting to think my code was getting a bit long


Is there a way to solve this problem? Is setting a 0-10 limit possible?


2) what can I change on L28 to set a limit form 0-10.
if I put a negative number for the quantity it should set it to the default which is 0 but in this code it just takes on the negative number

This doesn't allow values below 0 but it allows any number when I want the limit to be set so the highest number possible is 10.
void set_Quantity(int q) { if (q >= 0) quantity = q; }
auto get_Quantity() const { return quantity; }

This doesn't allow negative values and only allows values above 10
void set_Quantity(int q) { if (q <= 10) quantity = q; }
auto get_Quantity() const { return quantity; }

here is the updated code:
I did remove the destructor since there is no mention of adding one in the instructions

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
// class
class Fruit
{
    //access specifier
public:
    //default constructor
    Fruit() {}
    // non-default constructor
    Fruit(
          const std::string& fruitName, int quantity, double weight,
          float price, bool ispurchased) :
    name(fruitName), is_purchased(ispurchased)
    {
        set_Quantity(quantity);
        set_Weight(weight);
        set_Price(price);
    }
    
    // name
    void set_Name(const std::string& x) { name = x; }
    auto get_Name() const { return name; }
    // quantity
    void set_Quantity(int q)
    { if (q < 0)
        quantity = 0;
    else
        quantity = q;
    }
    auto get_Quantity() const { return quantity; }
    // weight
    void set_Weight(double w) { if (w > 0) weight = w; }
    auto get_Weight() const { return weight; }
    // price
    void set_Price(float p) { if (p >= 0) price = p; }
    auto get_Price() const { return price; }
    // is it purchased?
    void set_Is_purchased(bool i) { is_purchased = i; }
    auto get_Is_purchased() const { return is_purchased; }
    
    void print()
    {
        cout
        << "Name: " << setw(10) << left << name
        << "Quantity: " << setw(3) << left << quantity << ' '
        << "Weight: " << setw(4) << left << weight << ' '
        << "  Price: $" << setw(3) << left << price << ' '
        << "Did you purchase? "  << setw(6) << right << boolalpha
        << is_purchased << '\n';
    }
    
private:
    string name;
    int quantity {};
    double weight {};
    float price {};
    bool is_purchased {};
};

int main()
{
    cout
    << "This program creates at least 5 separate, co-existing instances of a\n"
    << "custom designed object. Information about each object is displayed.\n"
    << "Each constructor, setter and getter is tested at least once.\n\n";
    
    /*
     Modify the program to ask: "How many objects do you want?". Loop as many times specified by the user
     (5 is enough for testing) to create that many objects. You can use 5 individual variables,
     or you can use an array or vector.
     */
    
    cout << "How many objects do you want?\n"<< endl;
    
    // fruit 1 - set's method
    Fruit fruit1;
    fruit1.set_Name("Apple ");
    fruit1.set_Quantity(4);
    fruit1.set_Weight(4.56);
    fruit1.set_Price(4);
    fruit1.set_Is_purchased(true);
    fruit1.print(); // <----
    
    // fruit 2 - construuctor method
    Fruit fruit2("Orange", 7, 2.65, 5, true); // <---
    fruit2.print(); // <--
    // fruit 3 - construuctor method
    Fruit fruit3("Banana", 4, 2.34, 2, false); // <---
    fruit3.print(); // <--
    // fruit 4 - construuctor method
    Fruit fruit4("Avocado", 16, 2.59, 6, false); // <---
    fruit4.print(); // <--
    // fruit 5 - construuctor method
    Fruit fruit5("Grapes", -7, -2.65, 5, true); // <---
    fruit5.print(); // <--

    
    cout
    << "\nThis ends the class design, implementation, test program. Goodbye!"<< endl;
    
    

    return EXIT_SUCCESS;
}   // end of main 
Last edited on

void set_Quantity(int q) { if (q >= 0) quantity = q; }

You had it almost right. All you need is a compound (or) condition.
33
34
35
36
37
38
39
40
41
42
void set_Quantity(int q) 
{	if (q < 0 || q > 10)
        {   //	Perhaps emit an error message
    	   quantity = 0;
        }
        else
        {   //	within range
            quantity = q;
        }
}


If you want to get fancy (terse), you can use the ternary operator:
 
    quantity = (q < 0 || q > 10) ? 0 : q;	

Last edited on
Pages: 1234