Class and object


I just learned class and object today, couldn't even get the code compiled. could anyone help please? Thank you
I have three files: Product.h Product.cpp TestProduct.cpp
We haven't learned to put everything in a project yet. so for now, just just these three source files.
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218

class Product
{
private:
      int num;
      string name;
      int units; 
      char category; 
      double cost; 
      double price; 
      double totalCost;
      
public:
       Product();
       Product(const Product& copyFrom);
       Product(int, string, char);
       void setNum(int);
       int getNum();
       void setName(string);
       string getName();
       void setUnits(int);
       int getUnits();
       void setCategory(char);
       char getCategory();
       void setCost(double);
       double getCost();
       void setPrice(double);
       double getPrice();
       void setTotalCost(double);
       double getTotalCost();
       void receipt(int);
       void ship(int);
       void print();       
};





#include "Product.h"
#include <iostream>
#include <string>
#include <iomanip>


       Product::Product()
       {
          num = 00000;
          name = "Skirt";
          units = 10;
          category = 'S';
          cost = 9.98;
          price = 49.98;
          totalCost = cost * units;          
       }
       
       Product::Product(const Product& copyFrom)
       {
          num = copyFrom.num;
          name = copyFrom.name;
          units = copyFrom.units;
          category = copyFrom.category;
          cost = copyFrom.cost;
          price = copyFrom.cost;
          totalCost = copyFrom.totalCost; 
       }
       
       Product::Product(int pNum, string pName, char pCategory)
       {
             num = pNum;
             name = pName;
             category = pCategory;                          
       }
       
       void Product::setNum(int pNum)
       {
            num = pNum;            
       }
       int Product::getNum()
       {
           return num;
       }
       void Product::setName(string pName)
       {
            name = pMame;
        }
       string Product::getName()
       {
              return name;
       }
       void Product::setUnits(int pUnits)
       {
            units = pUnits;
        }
       int Product::getUnits()
       {
           return units;
       }
       void Product::setCategory(char pCategory)
       {
            category = pCategory;
        }
       char Product::getCategory()
       {
            return category;
        }
       void Product::setCost(double pCost)
       {
            cost = pCost;
        }
       double Product::getCost()
       {           
           return cost;         
       }
       void Product::setPrice(double pPrice)
       {
            price = pPrice;
        }
        
       double Product::getPrice()
       {
            return price;  
       }
       void Product::setTotalCost(double totalCost)
       {
            totalCost = cost * units;
        }
       double Product::getTotalCost()
       {
              return totalCost;
       }
       void Product::receipt(int pNum)
       {
            num = pNum;
            units++;
        }
       void Product::ship(int pNum)
       {
            num = pNum;
            units--;
        }
       void Product::print()
       {
            cout << fixed << showpoint << setprecision (2);
            cout << num << "   " << name << "   "  << units << "  "
                  << category << "   " << cost << "   " << price << "   " 
                  << totalPrice << endl;
        }      





#include "Product.cpp"
#include <string>
#include <iostream>
using namespace std;

int main()
{
      int num;
      string name;
      int units; 
      char category; 
      double cost; 
      double price; 
      double totalCost;
      
      Product obj1;
      Product obj2(00001, "shirt",'m');
      Product obj3;
      
      obj2.setCost(9.99);
      obj2.getCost();
      obj2.setPrice(39.99);
      obj2.getPrice();
      obj2.setTotalCost(totalCost);
      obj2.getTotalCost();
      
      cout << "Enter the number: " << endl;
      cin >> num;
      cout << "Enter the name: " << endl;
      cin >> name;
      cout << "Enter the units: " << endl;
      cin >> units;
      cout << "Enter the category: " << endl;
      cin >> category;
      cout << "Enter the cost: " << endl;  
      cin >> cost;
      cout << "Enter the price: " << endl;
      cin >> price;
      
      obj3.setNum(num);
      obj3.getNum();
      obj3.setName(name);
      obj3.getName();
      obj3.setUnits(units);
      obj3.getUnits();
      obj3.setCost(cost);
      obj3.getCost();
      obj3.setPrice(price);
      obj3.getPrice();
      obj3.setTotalCost(totalCost);
      obj3.getTotalCost();
      
      Product obj4(obj3);
      
    obj1.print();
    obj2.print();
    obj3.print();
    obj4.print();
        
    system ("pause");
    return 0;
}


Try to put:

#include "Product.h"

instead of:

#include "Product.cpp"
Thanks for the reply. Because today we haven't learned to create a project yet. So my professor said we should include "Product.cpp" to make it work. These three soure files are not in a project format.

Any other suggestions?

Thank you
Not enough information.

What was your compile error? What is your environment/OS?
This is the first compile error:

1 F:\DPR226\Homework\PRODUCT\Product.cpp:1, from F:\DPR226\Homework\PRODUCT\testProduct.cpp In file included from F:\DPR226\Homework\PRODUCT\/Product.cpp:1, from F:\DPR226\Homework\PRODUCT\testProduct.cpp

I am using windows xp and Dev C++

In Product.h, you need:

1
2
3
#include <string>

using namespace std;


Now, what kind of errors are you getting?
Thanks kfmfe04 :) No error any more! The comiler is happy and the program works perfectly!
Thanks again
np

Also, you may want to get into the habit of putting safeguards in your header files:

1
2
3
4
5
6
#ifndef __H_PRODUCT_H__
#define __H_PRODUCT_H__

// put your header code here

#endif // __H_PRODUCT_H__ 


Without this, you will get plenty of seemingly obscure compiler errors upon multiple inclusion of headers, when you go to a bigger project.
*Disch vomits all over the horrid get/set abuse*
*Disch vomits all over the horrid get/set abuse*

rofl - too bad C++ doesn't have properties:

http://en.wikipedia.org/wiki/Property_(programming)

That is get/set abuse there indeed. I only use them when I need access to a variable to be limited in some way. When you have simple getters and setters without any extra code in them besides that for getting and setting, you know you're abusing 'em. ;)

That said, if your instructor commanded you to do it, then I can still blame you for not standing up to him or her I guess Disch and I will go after him or her with a #define #divine shotgun.

-Albatross
Last edited on
*Disch vomits all over the horrid get/set abuse*

@yingkang:

Don't misunderstand him. He just happens to have a very sensitive stomach...
kfmfe04 wrote:
too bad C++ doesn't have properties

Actually, you can make it have properties, but it's quite dangerous, so I'll get myself some protection before attempting to move on...

[vomit-proof suit]

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
#include <iostream>
using namespace std;

class SomeClass
{
    int a;
    int b;
    int c;

    void calculate()
    {
        cout << "calculating..." << endl;
        c=a*a-b;
    }

    class IntProxy
    {
        int & ref;
        SomeClass * psc;

    public:
        IntProxy(int & n,SomeClass * psc_):
            ref(n),psc(psc_){}

        operator int(){return ref;}

        int operator=(int n)
        {
            ref=n;
            psc->calculate();
            return n;
        }
    };

public:
    SomeClass(int a_, int b_):
        a(a_),b(b_){calculate();}

    IntProxy A() {return IntProxy(a,this);}
    IntProxy B() {return IntProxy(b,this);}
    int C() const {return c;}
};

int main()
{
    cout << "creating some_object..." << endl;
    SomeClass some_object(3,5);

    cout << "\nprinting a and b members..." << endl;
    int temp=some_object.A();
    cout << temp << endl;
    cout << some_object.B() << endl;

    cout << "\nprinting c member..." << endl;
    cout << some_object.C() << endl;

    cout << "\nsetting a and b members..." << endl;
    some_object.A()=4;
    some_object.B()=1;

    cout << "\nprinting c member..." << endl;
    cout << some_object.C() << endl;

    cout << "\nhit enter to quit..." << endl;
    cin.get();
    return 0;
}

[/vomit-proof suit]

The trick is to behave in one way when the return value is treated as an L-value and in another way when it is treated as an R-value (or, more properly, 'not as an L-value'). But to make that possible you have to wrap the return type in a custom type.
Last edited on
I added the safeguards in the file. Thanks kfmfe04

I know all the getters and setters here seem so unnecessary, but we t have to practice using them now. Not sure about the shotgun though, our professor is quite a nice guy :)
Topic archived. No new replies allowed.