#include<iostream>
using namespace std;
class product
{
private:
int code;
char name[20];
double price;
int instock;
double soldperyear[12];
public:
product(int,char[],double,int);
void setproduct(int c , char n[],double p,int i)
{
setcode(c);
setname(n);
setprice(p);
setinstock(i);
}
void peryear(int i)
{
for ( i=1;i<=12;i++)
soldperyear[i]=0;}
void setcode(int c)
{
if(c>=2500&&c<=2750)
code=c;
else
code=2500;
}
void setname(char n[])
{
strncpy(name, n,19);
name[19]='\0';}
void setprice(double p)
{
price = p> 0.0?p:0.0;
}
void setinstock(int i)
{i=0;
instock=i;
}
int getcode()
{ return code;}
char * getname()
{
return name;
}
double getprice()
{
return price;
}
int getstock()
{
return instock;
}
void increment(int inc)
{
instock=instock +inc ;
}
void decrement(int ins,int month)
{if(instock>=ins){
instock-=ins;
soldperyear[month]+=ins;}
else cout<<" not enough products in stock "<<endl;
}
int total (){
int total =0;
for (int i=1;i<=12;i++)
total +=soldperyear[i];
return total ;
}
void print() {
cout<<" Product: "<<endl;
cout<<"code :"<<getcode()<<endl;
cout<<"Name : "<<getname()<<endl;
cout<<"price: "<<getprice()<<endl;
cout<<"sold per year : " <<total()<<endl;
cout<<" In stock : " <<getstock()<<endl;
}
};
product::product(int c, char n[], double p, int i){
setproduct( c , n, p, i);
}
void main()
{
}
Create a class product with the following data members:
Code – The product’s code
The valid values are any number between 2500 and 2750
(Defaults to 2500)
Name - The product’s name. Ex. “Japanese Corner” or “Rustic Table”….
(string of 20 characters. Defaults to “New Product”)
Price - The product’s price
A double value representing the product’s price. The valid values should be greater than 0.0. (Defaults to 0.0)
SoldPerYear – A 12 elements array of double that saves the sales per month over the span of a year. (The elements’ values must default to 0)
InStock – The number of items available (Defaults to 0)
And the following member functions:
A constructor with default arguments for Code, Name, Price and InStock
Set functions with integrity checks for Code, Name, Price and InStock
Get functions for Code, Name, Price and InStock
A member function that increments InStock by a certain value.
A member function that decrements InStock by a certain value while it increments one of the elements of SoldPerYear. Make sure that the value is less than or equal to Available; if not display an error message.
A member function that calculates the total sales for the year.
A print function to print all attributes of a product.
Write a program to test your class by:
Creating two objects, a pointer and a reference.
Using the two objects, pointer and reference test all the member functions of your class
samer.aboufakher.3 0 Newbie Poster
ddanbe 2,724 Professional Procrastinator Featured Poster
DaveAmour 160 Mmmmmm beer Featured Poster
samer.aboufakher.3 0 Newbie Poster
DaveAmour 160 Mmmmmm beer Featured Poster
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster
samer.aboufakher.3 0 Newbie Poster
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster
rubberman 1,355 Nearly a Posting Virtuoso Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.