Been working on this for a while just want to see if i missed anything, I know m
ID: 669227 • Letter: B
Question
Been working on this for a while just want to see if i missed anything, I know my header should be 100% but just want to see if the class itself is working
Implement the class Product and deliver the code in two files product.h and product.cpp
*****HEADER
#include <iostream>
#include <cstdlib>
#include <cstdint>
#ifndef _PRODUCT_H
#define _PRODUCT_H
class Product
{
public:
void SetName ( const char* ); // sets the name field
void SetBarCode ( uint32_t ); // sets the bar code field
void SetCost ( float ); // sets the cost field
const char* GetName () const; // returns a const pointer to the name field
uint32_t GetBarCode () const; // returns the bar code by value
float GetCost () const; // returns cost by value
Product();// name "#" code = 0 cost = 0
Product (const char* name, uint32_t code, float cost);
~Product ();
Product(const Product& p);
Product& operator= (const Product& p);
private:
char * name_; // the product name
uint32_t code_; // the product bar code
float cost_; // the product cost
};
std::ostream& operator<< (std::ostream& os, const Product& p);
#endif
******************the code itself
#include <product.h>
#include <cstring>
void Product::SetName ( const char* name )
{
if (name_ != NULL)
delete [] name_;
size_t size = strlen(name);
name_ = new char [1+size];
name_ [size] = '';
strcpy(name_, name);
}
void Product::SetBarCode ( uint32_t code )
{
code_ = code;
}
void Product::SetCost ( float cost )
{
cost_ = cost;
}
const char* Product:: GetName () const
{
return name_;
}
uint32_t Product::GetBarCode () const
{
return code_;
}
float Product::GetCost () const
{
return cost_;
}
Product::Product() : name_(NULL), code_(0), cost_(0.0)
{
name_ = new char [2];
name_[0] = '#';
name_[1] = '';
}
Product::Product (const char* name, uint32_t code, float cost) :name_(NULL), code_(code), cost_(cost)
{
size_t size = ( name );
name_ = new char [size + 1];
name_[-1] = '';
strcpy (name_, name);
}
Product::~Product ()
{
if (name_ != NULL)
delete [] name_;
}
Product::Product& operator= (const Product& p)
{
if (this != &p)
{
{if ( name_ != 0 )
delete [] name_;
size_t size = strlen( p.GetName);
name_ = new char [size + 1];
name_[-1] = '';
strcpy(name_, p.GetName);
code_ = p.GetCode;
cost_ = p.GetCost;
}
return *this;
}
std::ostream& operator << ( std::ostream& os , const Product& p )
{
std::ios_base::fmtflags flags = os.flags();
os.setf(std::ios::fixed | std::ios::showpoint);
os.precision(2);
os << p.GetName() << ' '
<< std::hex << std::uppercase << std::setfill('0') << std::setw(8)
<< p.GetBarCode()
<< std::dec << std::setfill(' ') << ' '
<< p.GetCost();
os.setf(flags);
return os;
}
Explanation / Answer
I have checked and verified the files. Below is the complete implementation. You have implemented all the methods preset in header. Few minor syntactical and logical errors were there which I have corrected.
File : Product.cpp
#include "product.h"
#include <cstring>
void Product::SetName ( const char* name )
{
if (name_ != NULL)
delete [] name_;
size_t size = strlen(name);
name_ = new char [1+size];
name_ [size] = '';
strcpy(name_, name);
}
void Product::SetBarCode ( uint32_t code )
{
code_ = code;
}
void Product::SetCost ( float cost )
{
cost_ = cost;
}
const char* Product:: GetName () const
{
return name_;
}
uint32_t Product::GetBarCode () const
{
return code_;
}
float Product::GetCost () const
{
return cost_;
}
Product::Product() : name_(NULL), code_(0), cost_(0.0)
{
name_ = new char [2];
name_[0] = '#';
name_[1] = '';
}
Product::Product (const char* name, uint32_t code, float cost) :name_(NULL), code_(code), cost_(cost)
{
size_t size = ( name );
name_ = new char [size + 1];
name_[-1] = '';
strcpy (name_, name);
}
Product::~Product ()
{
if (name_ != NULL)
delete [] name_;
}
Product::Product& operator= (const Product& p)
{
if (this != &p)
{
{if ( name_ != 0 )
delete [] name_;
size_t size = strlen( p.GetName);
name_ = new char [size + 1];
name_[-1] = '';
strcpy(name_, p.GetName);
code_ = p.GetCode;
cost_ = p.GetCost;
}
return *this;
}
std::ostream& operator << ( std::ostream& os , const Product& p )
{
std::ios_base::fmtflags flags = os.flags();
os.setf(std::ios::fixed | std::ios::showpoint);
os.precision(2);
os << p.GetName() << ' '
<< std::hex << std::uppercase << std::setfill('0') << std::setw(8)
<< p.GetBarCode()
<< std::dec << std::setfill(' ') << ' '
<< p.GetCost();
os.setf(flags);
return os;
}
File : product.h
//*****HEADER
#include <iostream>
#include <cstdlib>
#include <cstdint>
#ifndef _PRODUCT_H
#define _PRODUCT_H
class Product
{
public:
void SetName ( const char* ); // sets the name field
void SetBarCode ( uint32_t ); // sets the bar code field
void SetCost ( float ); // sets the cost field
const char* GetName () const; // returns a const pointer to the name field
uint32_t GetBarCode () const; // returns the bar code by value
float GetCost () const; // returns cost by value
Product();// name "#" code = 0 cost = 0
Product (const char* name, uint32_t code, float cost);
~Product ();
Product(const Product& p);
Product& operator= (const Product& p);
private:
char * name_; // the product name
uint32_t code_; // the product bar code
float cost_; // the product cost
};
std::ostream& operator<< (std::ostream& os, const Product& p);
#endif
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.