Below the code, if someone can figure out why this code continued to work and all of a sudden decided to crash forever...
please if you can rectify the code...
Thanks a mill
Main.cpp
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include "Product.h"
using namespace std;
int main(int argc, char *argv[])
{Product apple;
system("PAUSE");
return 0;
}
product.h
#ifndef Product_h
#define Product_h
const int max=30;
class Product
{private:
char *_productname; //dynamic allocation (use pointers), prevent NULL or void
char *_productdescription; //dynamic allocation (use pointers) prevent NULL or void
int _price;// x>0 (resolved)
int _uniquenumber;// unique number x>0 (resolved)
static int count; //unsigned would work as well...
protected:
public:
Product();
Product(char*,char*,int);
void setProductname();
void setProductdescription();
void setPrice();
char *getProductname();
char *getProductdescription();
int getPrice();
void printProduct(char*, char*, int, int);
~Product();
};
#endif
Product.cpp
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include "Product.h"
using namespace std;
int Product::count=0;
Product::Product()
{setProductname();
setProductdescription();
setPrice();
_uniquenumber=++count;
printProduct(_productname,
_productdescription, _price, _uniquenumber);
}
Product::~Product()
{
}
Product::Product(char*,cha
r*,int)
{}
void Product::setProductname()
{char *name=new char [30];
cout<<"Enter the products name"<<endl;
cin.get(name,30);
char s;
do{
cin.get(s);
}while(s!='\n');
strncpy(_productname, name, 30);
delete[]name;
}
void Product::setProductdescrip
tion()
{char *description=new char [30];
cout<<"Enter the products description"<<endl;
cin.get(description,30);
char s;
do{
cin.get(s);
}while(s!='\n');
strncpy(_productdescriptio
n, description, 30);
delete[]description;
}
void Product::setPrice()
{
do{cout<<"Enter products price"<<endl;
cin>>_price;
}while(_price<=0);
}
char *Product::getProductname()
{return _productname;}
char *Product::getProductdescri
ption()
{return _productdescription;}
int Product::getPrice()
{return _price;}
void Product::printProduct(char
*, char*, int, int)
{cout<<endl<<endl<<"******
**********
**"<<endl;
cout<<"Product: "<<_productname<<endl;
cout<<"Description: "<<_productdescription<<en
dl;
cout<<"Price: "<<_price<<endl;
cout<<"Productnumber: "<<_uniquenumber<<endl;
cout<<"******************"
<<endl;}
Start Free Trial