calling my objects

good day everyone. am writing a little code that uses classes and after writing, I declarede some objects but when I tried using the objects it showed errors. pls can some one help me out. am using microsoft visual C++ 2010 express editio. below is the code for both the header class and the source code.

header class
class product
{
private:
int numberSold, numberOfProduct;
int criticalValue;
public:
product();
product (int numProduct, int numSold, int critVal)
{
numberOfProduct=numProduct;
numberSold=numSold;
criticalValue=critVal;
};
int numberSell()
{
int sold=0;
sold=sold+numberSold;
return sold;
};
int numberRemaining()
{
int numberRemain;
numberRemain = numberOfProduct-numberSold;
return numberRemain;
};
bool checkCritical()
{
bool reply;
if (numberOfProduct <= criticalValue)
return true;
else
return false;
};
virtual ~product(void);
};



source code:


#include <iostream>
#include <string>
#include "product.h"
#include <fstream>
using namespace std;

void chooseProduct();
void sellProduct(product prodt);

int main()
{
product television, radio,dvd;
product homeTheater, laptop, camcoder;
ifstream inData;
ofstream outData;
string name;
int choice, NP=100, CV=20, NS;
inData.open("C:\\Users\\Public\\C+ Code.txt");
outData.open("C:\\Users\\Public\\C++ Code.txt");
cout<<"Enter the name of the Product";
getline(cin,name);
chooseProduct();
cin>> choice;
cout<<"Enter number of item to be sold";
cin>>NS;
if (NS>=NP)
{
cout<<"Number of Product in inventory is not enough to service customer";
return 1;
}
if (choice >=1 || choice<=6)
{
switch(choice)
{
case 1:
television(NP,NS,CV);
sellProduct(television);
break;
case 2:
radio(NP,NS,CV);
sellProduct(radio);
break;
case 3:
dvd(NP,NS,CV);
sellProduct(dvd);
break;
case 4:
homeTheater(NP,NS,CV);
sellProduct(homeTheater);
case 5:
laptop(NP,NS,CV);
sellProduct(laptop);
break;
case 6:
camcoder(NP,NS,CV);
sellProduct(camcoder);
break;
default:
cout<<"Product is not in inventory";
}
}
else
cout<<"please make a valid selection";
inData.close();
outData.close();
return 0;
}


void showProduct()
{
cout<<" Enter 1 for television";
cout<<" Enter 2 for radio";
cout<<" Enter 3 for dvd";
cout<<" Enter 4 for hometheater";
cout<<" Enter 5 for latops";
cout<<" Enter 6 for camcoders";
}

void sellProduct(product prodt)
{
int numbaSold,numLeft;
char answer;
ofstream outData;

numbaSold=prodt.numberSell();
numLeft=prodt.numberRemaining();
prodt.checkCritical();
cout<<"Number of prduct sold is "<<numbaSold;
cout<<"Number of product remaining is "<<numLeft;
cout<<"Do you wish to save the file now? Enter Y for Yes, Or N for No";
cin>>answer;
if (answer="Y" || answer="y")
{
outData<<numLeft;
outData<<numbaSold;
cout<<"File is saved";
}
else
cout<<" You have not saved this file";
}


in the source code, the part where I passed arguments to the objects:
television(NP,NS,CV), radio(NP,NS,CV), laptops(NP,NS,CV), hometheater(NP,NS,CV), dvd(NP,NS,CV) and camcoders(NP,NS,CV) are underlined red. not the arguments itself but the objects television, radio, laptops,hometheater, dvd, camcoders are the ones underlined.
The function you made, product (int numProduct, int numSold, int critVal), is a constructor. You can use this function ONLY when creating the object. For example,

product television(NP, NS, CV);
would create the product object named television, using that function.

After you have created the object, you cannot use its constructor functions. If you need to change its internal private variables after creation, you'll need to write a different member function for the class. For example,

1
2
3
4
5
6
void setVariables(int numProduct, int numSold, int critVal)
{
numberOfProduct=numProduct;
numberSold=numSold;
criticalValue=critVal;
};

Last edited on
thanks alot. I tried it and it worked well. I also did some little improvising on the code and it also executed perfectly well. here is the improvised version. just the part where the object is being created.

int choice, NP=100, CV=20, NS;
cout<<"Enter number of item to be sold"<<endl;
cin>>NS;
product television(NP,NS,CV);
product radio(NP,NS,CV);
product dvd(NP,NS,CV);
product laptop(NP,NS,CV);
product camcoder(NP,NS,CV);
product hometheater(NP,NS,CV);

I decided to get all the variables in place before creating the objects and it executed well. please is this proper in C++.
Topic archived. No new replies allowed.