-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfruits.cpp
51 lines (40 loc) · 1.09 KB
/
fruits.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
#include <iostream>
#include <string>
using namespace std;
class Fruit{
private:
string m_name;
string m_color;
public:
Fruit(const string& name, const string& color): m_name{name}, m_color{color}{};
string get_name(void)const{return m_name;}
string get_color(void)const{return m_color;}
};
class Apple: public Fruit{
private:
float m_fiber;
public:
Apple(const string& name="", const string& color="", float fiber=0):
Fruit{name, color}, m_fiber{fiber}{};
float get_fiber(void)const{return m_fiber;}
friend ostream& operator<<(ostream& out, const Apple& obj){
out << "Apple(" << obj.get_name() << ", "<< obj.get_color() << ", " <<
obj.get_fiber() << ")\n";
return out;
}
};
class Banana: public Fruit{
public:
Banana(const string& name="", const string& color=""): Fruit{name, color}{};
friend ostream& operator<<(ostream& out, const Banana& obj){
out << "Banana(" << obj.get_name() << ", "<< obj.get_color() << ")\n";
return out;
}
};
int main(){
const Apple a("Red delicious", "red", 4.2);
std::cout << a;
const Banana b("Cavendish", "yellow");
std::cout << b;
return 0;
}