forked from teseoch/CPP-Fall-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path92-fruit1.cpp
103 lines (86 loc) · 1.65 KB
/
92-fruit1.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
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
#include <iostream>
#include <string>
#include <typeinfo>
class Fruit
{
public:
virtual std::string get_name() const
{
return "Generic Fruit";
}
virtual bool is_delicious() const
{
return false;
}
};
void print_fruit(const Fruit &f)
{
std::cout << "Fruit: " << f.get_name();
std::cout << " ";
if (f.is_delicious())
std::cout << "(delicious)";
else
std::cout << "(not delicious)";
//this dosent work
// f.get_num_seeds();
std::cout << std::endl;
}
//Task: Add classes for Raspberry, Pumpkin and Pineapple.
// Have the constructor for Raspberry take a single
// int parameter (storing the number of seeds in the
// raspberry).
class Raspberry : public Fruit
{
public:
Raspberry(int ns) : n_seeds{ns} {}
virtual std::string get_name() const override
{
return "Raspberry";
}
virtual bool is_delicious() const override
{
return true;
}
int num_seeds() const
{
return n_seeds;
}
private:
int n_seeds;
};
class Pumpkin : public Fruit
{
public:
virtual std::string get_name() const override
{
return "Pumpkin";
}
virtual bool is_delicious() const override
{
return true;
}
};
class Pineapple : public Fruit
{
public:
virtual std::string get_name() const override
{
return "Pineapple";
}
virtual bool is_delicious() const override
{
return false;
}
};
int main()
{
Fruit F{};
Raspberry R{6};
Pumpkin J{};
Pineapple P{};
print_fruit(F);
print_fruit(R);
print_fruit(P);
print_fruit(J);
return 0;
}