vectors

i need help with add topping function , can someone help. im new to vectors .
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
  #include<iostream>
#include<string>
#include<vector>
using namespace std;

class MyPizza
{
    private: 
    string name;
    string size;
    vector<string> toppings;
    
    
    public:
    void setSize(string);
    string getSize();
    void setName(string);
    string getName();
    string addTopping();
    
    MyPizza()
    {
        name="";
        size="";
        toppings.push_back("");
    }//default constructor
    MyPizza(string Cname, string PizzaSize,vector<string> P_toppings) //parametized constructor
    {
        name=Cname;
        size=PizzaSize;
        toppings=P_toppings;
    }
    
};
void MyPizza::setSize(string Psize)
{
    size=Psize;
}

string MyPizza::getSize()
{
    return size;
}


void MyPizza::setName(string Pname)
{
    name=Pname;
}


string MyPizza::getName()
{
    return name;
}

string MyPizza::addTopping()
{
   // addTopping – this function receives a string representing a single topping, and adds it to
//to the vector of toppings in the class
 }
 
int main()
{
    
}
/*Create a class called Pizza that represents a single pizza order. Required class attributes and
functions are as follows:
Class attributes:
 Customer name (you can make it a single name if you want, no spaces)
 Size of pizza (you decide whether to use string or char)
 A VECTOR of strings that represents the toppings
Class Functions:
 Default constructor (choose appropriate values)
 set/get for size (validate that size is only S, M, L – upper or case)
 set/get for customer name
 addTopping – this function receives a string representing a single topping, and adds it to
to the vector of toppings in the class
 clearTopping – this function resets the topping vector to empty (use .clear() function)
 getPrice – this function calculates are returns the price of the pizza
 display – this function displays the pizza order in the format shown below:
*/



You don’t need to initialise objects like std::string and std::vector: they are already initialised by their constructors.

setSize() should “validate that size is only S, M, L – upper or case”.

The assignment says: “addTopping – this function receives a string”, not returns a std::string.

Hints:
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
// Create a class called Pizza that represents a single pizza order.
// Required class attributes and functions are as follows:
// Class attributes:
//    Customer name (you can make it a single name if you want, no spaces)
//    Size of pizza (you decide whether to use std::string or char)
//    A VECTOR of std::strings that represents the toppings
// Class Functions:
//    Default constructor (choose appropriate values)
//    set/get for size (validate that size is only S, M, L – upper or case)
//    set/get for customer name
//    addTopping – this function receives a std::string representing a single 
//       topping, and adds it to the std::vector of toppings in the class
//    clearTopping – this function resets the topping std::vector to empty 
//       (use .clear() function)
//    getPrice – this function calculates are returns the price of the pizza
//    display – this function displays the pizza order in the format shown below:

#include <iostream>
#include <string>
#include <vector>

class MyPizza   /* ?? Create a class called Pizza */
{
private: 
    std::string name;
    std::string size;
    std::vector<std::string> toppings;

public:
    MyPizza() //default constructor
    {
    }

    MyPizza(std::string c_name,
    std::string pizza_size,
    std::vector<std::string> p_toppings) //parametized constructor
    {
        name = c_name;
        size = pizza_size;
        toppings = p_toppings;
    }
    void setSize(const std::string & p_size);
    std::string getSize() const;
    void setName(const std::string & p_name);
    std::string getName() const;
    void addTopping(const std::string & topping);
};


void MyPizza::setSize(const std::string & p_size)
{
    // Assignement requires:
    /* validate that size is only S, M, L – upper or case */
    size = p_size;
}


std::string MyPizza::getSize() const
{
    return size;
}


void MyPizza::setName(const std::string & p_name)
{
    name = p_name;
}


std::string MyPizza::getName() const
{
    return name;
}


void MyPizza::addTopping(const std::string & topping)
{
    // addTopping – this function receives a std::string representing a single
    // topping, and adds it to the std::vector of toppings in the class
}


int main()
{
}

Topic archived. No new replies allowed.