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.

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;
}

Edited 6 Years Ago by YingKang: n/a

Make this change :

#include "Product.h"
#include <string>
#include <iostream>
using namespace std;
 
int main()
{
 //...
}

Make this change :

#include "Product.h"
#include <string>
#include <iostream>
using namespace std;
 
int main()
{
 //...
}

Hi FirstPerson,
Thanks for reply
I haven't put every thing in a project yet. They are three seperatefiles. So it's supposed to use #include "Product.cpp"

I guess it's some other problems.

Hi FirstPerson,
Thanks for reply
I haven't put every thing in a project yet. They are three seperatefiles. So it's supposed to use #include "Product.cpp"

I guess it's some other problems.

Exactly, you separated the project into files right? One a .h, .cpp and the main.cpp file, right?

So normally, one would include #include "fileName.h" where fileName is the name of the .h file, in the main.cpp. Using .cpp can cause multiple definition error. Post the error message as well.

Exactly, you separated the project into files right? One a .h, .cpp and the main.cpp file, right?

So normally, one would include #include "fileName.h" where fileName is the name of the .h file, in the main.cpp. Using .cpp can cause multiple definition error. Post the error message as well.

I changed to #include"Product.h" in the main file Product.cpp. still not compiling :(
More suggestions?
Thanks

Instructions to get the above to compile:

1) add #include <string> at the start of the Product.h file.
2) replace each use of type string with std::string in the Product.h file.
3) add "using namespace std;" just after the includes in the Product.cpp file
4) as said by firstPerson, include the Product.h in the main.cpp (TestProduct.cpp)
5) compile both cpp files together, say you have a gcc (g++), the command-line is (no .exe if in Linux):
g++ TestProduct.cpp Product.cpp -o TestProduct.exe
If it is not gcc or another command-line compiler, then look in the project menus of your IDE for "add source file to project/build" (or something of the like).

That should do it.

Thanks for all the reply. The problem is solved. I added using namespace std in Product.h file.

>>I added using namespace std in Product.h file.

There is a good reason why I didn't suggest that "simpler" solution in my previous post. That is because it is considered bad practice to import a namespace in a header file (which is what "using namespace std;" does). It is considered bad practice because one untold rule when writing header files is that the "trail" of it should be minimal. In other words, besides the stuff that is actually declared in the header file and the other essential dependencies, nothing more should get imported when you include that header file somewhere else (minimizes code bloat, decrease compilation time and avoid possible name-clashes). But for your simple program that won't make a difference, but it is a good thing to get used to.

This article has been dead for over six months. Start a new discussion instead.