forked from furkankirac/cs321-2019-20-fall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweek6-midterm.cpp
73 lines (56 loc) · 1.47 KB
/
week6-midterm.cpp
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
#include <iostream>
#include <string>
#include <vector>
enum Color { Red, Yellow, Orange, Blue };
enum Shape { Round, Curved };
struct Fruit
{
std::string name;
Color color;
Shape shape;
Fruit(const std::string& name, Color color, Shape shape)
: name{name}, color{color}, shape{shape}
{ }
Fruit(const Fruit& other)
: name{name}, color{color}, shape{shape}
{ }
};
struct Toy
{
std::string name;
float price;
Toy(const std::string& name, float price)
: name{name}, price{price}
{ }
};
template<typename T>
struct Basket
{
std::vector<T> items;
void add(const T& item) { items.push_back(item); }
const T& operator[](size_t index) const { return items[index]; }
T& operator[](size_t index) { return items[index]; }
};
template<typename T>
void print_name(const T& item)
{
std::cout << item.name << std::endl;
}
auto operator+(const Fruit& f, const Toy& t)
{
return Fruit{f.name+t.name, f.color, f.shape};
}
int main(int argc, char* argv[])
{
auto fruits = Basket<Fruit>{};
fruits.add(Fruit{"Apple", Color::Red, Shape::Round});
fruits.add(Fruit{"Banana", Color::Yellow, Shape::Curved});
fruits.add(Fruit{"Blueberry", Color::Blue, Shape::Round});
auto toys = Basket<Toy>{};
toys.add(Toy{"Lego", 100.0});
toys.add(Toy{"Barbie", 50.0});
fruits[0] = Fruit{"Orange", Color::Orange, Shape::Round};
print_name(fruits[0]);
print_name(fruits[0]+toys[0]);
return 0;
}