class c++ - constructors and inheritance!

Shared by: this

cpp

1
#include <iostream>
2

3
using namespace std;
4

5

6
class Food{
7

8
    public:
9
        Food(){
10
            name = "";
11
            brand = "";
12
            calories = 0;
13
            fat = 0;
14
            price = 0.0;
15
        };
16

17
        string something;
18

19
        string getName(){
20
            return name;
21
        };
22

23
        int getCalories(){
24
            return calories;
25
        }; 
26
        int getPrice(){
27
            return price;
28
        }; 
29
        int getFat(){
30
            return fat;
31
        }; 
32
        string getBrand(){
33
            return brand;
34
        };
35

36

37
         void setName(string a){
38
            name = a;
39
        };
40

41
        void setCalories(int a){
42
            calories = a;
43
        }; 
44
        void setPrice(int a){
45
            price = a;
46
        }; 
47
        void setFat(int a){
48
            fat  =a;
49
        };
50

51
        void setBrand(string a){
52
          brand = a;
53
        };
54

55

56
    private:
57
        int calories,fat, price;
58
        string name, brand;
59

60
};
61

62
class Bread:  public Food{
63
    
64
    public:
65
      bool getWheat(){
66
        return wheat;
67
      };
68

69
      string getLoaf(){
70
        return loaf;
71
      }
72

73
      void setWheat(bool a){
74
        wheat = a;
75
      };
76

77
      void setLoaf(string a){
78
        loaf = a;
79
      };
80

81
    
82
    private:
83
        string loaf = "";
84
        bool wheat = false;
85
};
86

87
int main(){
88

89
    Bread bread; 
90

91
    bread.setName("bread");
92
    bread.setBrand("Delight");
93
    bread.setWheat(true);
94
    bread.setLoaf("family size");
95
    bread.something = "something is yourng";
96
    // bread.name = 'games'
97
    cout <<bread.getLoaf() << ' ' << bread.getWheat() << endl;
98
    
99
    return 0;
100

101
}