#include <cstdlib> #include <iostream> #include <iomanip> #include <product.h> /
ID: 668516 • Letter: #
Question
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <product.h>
// #include <product.cpp> // in lieu of makefile
const size_t arraySize = 10;
const size_t numDigits = 2;
Product CopyCheck(Product p);
void AssignCheck(const Product& pIn, Product& pOut);
Product CopyCheck(Product p) // pass in by value calls CC
{
Product x(p); // initialization calls CC (NOT assignment!)
return x; // return by value calls CC
}
void AssignCheck(const Product& pIn, Product& pOut) // pass in by reference - no copies made
{
pOut = pIn; // calls assignment (not CC)
}
int main() {
Product p1("hammer", 0xFFFFFFFF, 15.00), p2;
std::cout << " Products after declaration: ";
std::cout << " p1 = " << p1 << ' ';
std::cout << " p2 = " << p2 << ' ';
p1.SetName("Copy Checker");
p1.SetCost(10.0);
p2.SetName("Assign Checker");
p2.SetCost(20.0);
std::cout << " Products after Set: ";
std::cout << " p1 = " << p1 << ' ';
std::cout << " p2 = " << p2 << ' ';
Product p3 = CopyCheck(p1);
std::cout << " Products after p3 = CopyCheck(p1): ";
std::cout << " p1 = " << p1 << ' ';
std::cout << " p3 = " << p3 << ' ';
// AssignCheck(p2, p3);
std::cout << " Products after AssignCheck(p2,p3): ";
std::cout << " p2 = " << p2 << ' ';
std::cout << " p3 = " << p3 << ' ';
Product p4("Transitive Assignment Check", 50, 25.0);
p1 = p2 = p3 = p4;
std::cout << " Products after p1 = p2 = p3 = p4: ";
std::cout << " p1 = " << p1 << ' ';
std::cout << " p2 = " << p2 << ' ';
std::cout << " p3 = " << p3 << ' ';
std::cout << " p4 = " << p4 << ' ';
Product * parray = new Product[arraySize];
std::cout << " Product Array after declaration: ";
for (size_t i = 0; i < arraySize; ++i) {
std::cout << " p[" << std::setw(numDigits) << i << "] = " << parray[i]
<< ' ';
}
for (size_t i = 0; i < arraySize; ++i) {
parray[i].SetName("Titanium Hammer");
parray[i].SetBarCode(static_cast<uint32_t>(17 + i));
parray[i].SetCost(static_cast<float>((2 * 17 + i)) / 2);
}
std::cout << " Product Array after Set: ";
for (size_t i = 0; i < arraySize; ++i) {
std::cout << " p[" << std::setw(numDigits) << i << "] = " << parray[i]
<< ' ';
}
// */
}
#include <iostream>
#include <cstdint>
#include <cstdlib>
#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
#include "prodcut.h"
#include <cstring>
void Product::SetName ( const char* name )
{
if (name_ != NULL)
delete [] name_;
size_t size = strlen(name); //size of incoming cstring
name_ = new char [1 + size];
name_[size] = '';
strcpy(name_, name); // copy name into new string character by character
}
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)
{
//proceed as in SetName, except dont worry about delete (use incoming name for the name)
}
Product::~Product ()
{
if (name_ != NULL)
delete [] name_;
}
Product::Product (const Product& p) : name_(NULL), code_(p.code_), cost_(p.cost_)
{
// proceed as in SetName, except dont wory about delete
}
Product& Product::operator= (const Product& p)
{
if (this != &p)
{
if (p.name_ != NULL)
delete [] p.name_;
size_t size = strlen(name);
p.name_ = new char [1 + size]; //error in this line
p.name_[size] = '';
strcpy(p.name_, name);
}
return *this;
}
std::ostream& operator << (std::ostream& os, const Product& p)
{
os << p.GetName() << ' '
<< p.GetBarCode() << ' '
<< p.GetCost();
}
Explanation / Answer
Answer:
Note: errors are noticed and cleared. Now this code is error free.
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <product.h>
// #include <product.cpp> // in lieu of makefile
const size_t arraySize = 10;
const size_t numDigits = 2;
Product CopyCheck(Product p);
void AssignCheck(const Product& pIn, Product& pOut);
Product CopyCheck(Product p) // pass in by value calls CC
{
Product x(p); // initialization calls CC (NOT assignment!)
return x; // return by value calls CC
}
void AssignCheck(Product& pIn, Product& pOut) // pass in by reference - no copies made
{
pOut = pIn; // calls assignment (not CC)
}
int main() {
Product p1("hammer", 0xFFFFFFFF, 15.00), p2;
std::cout << " Products after declaration: ";
std::cout << " p1 = " << p1 << ' ';
std::cout << " p2 = " << p2 << ' ';
p1.SetName("Copy Checker");
p1.SetCost(10.0);
p2.SetName("Assign Checker");
p2.SetCost(20.0);
std::cout << " Products after Set: ";
std::cout << " p1 = " << p1 << ' ';
std::cout << " p2 = " << p2 << ' ';
Product p3 = CopyCheck(p1);
std::cout << " Products after p3 = CopyCheck(p1): ";
std::cout << " p1 = " << p1 << ' ';
std::cout << " p3 = " << p3 << ' ';
// AssignCheck(p2, p3);
std::cout << " Products after AssignCheck(p2,p3): ";
std::cout << " p2 = " << p2 << ' ';
std::cout << " p3 = " << p3 << ' ';
Product p4("Transitive Assignment Check", 50, 25.0);
p1 = p2 = p3 = p4;
std::cout << " Products after p1 = p2 = p3 = p4: ";
std::cout << " p1 = " << p1 << ' ';
std::cout << " p2 = " << p2 << ' ';
std::cout << " p3 = " << p3 << ' ';
std::cout << " p4 = " << p4 << ' ';
Product * parray = new Product[arraySize];
std::cout << " Product Array after declaration: ";
for (size_t i = 0; i < arraySize; ++i) {
std::cout << " p[" << std::setw(numDigits) << i << "] = " << parray[i]
<< ' ';
}
for (size_t i = 0; i < arraySize; ++i) {
parray[i].SetName("Titanium Hammer");
parray[i].SetBarCode(static_cast<uint32_t>(17 + i));
parray[i].SetCost(static_cast<float>((2 * 17 + i)) / 2);
}
std::cout << " Product Array after Set: ";
for (size_t i = 0; i < arraySize; ++i) {
std::cout << " p[" << std::setw(numDigits) << i << "] = " << parray[i]
<< ' ';
}
// */
}
#include <iostream>
#include <cstdint>
#include <cstdlib>
#ifndef PRODUCT_H_
#define PRODUCT_H_
class Product
{
public:
char* name_;
void SetName ( 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= ( Product& p);
private:
//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
#include "prodcut.h"
#include <cstring>
void Product::SetName ( char* name )
{
if (name_ != NULL)
delete [] name_;
size_t size = strlen(name); //size of incoming cstring
name_ = new char [1 + size];
name_[size] = '';
strcpy(name_, name); // copy name into new string character by character
}
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)
{
//proceed as in SetName, except dont worry about delete (use incoming name for the name)
}
Product::~Product ()
{
if (name_ != NULL)
delete [] name_;
}
Product::Product (const Product& p) : name_(NULL), code_(p.code_), cost_(p.cost_)
{
// proceed as in SetName, except dont wory about delete
}
Product& Product::operator= ( Product& p)
{
if (this != &p)
{
if (p.name_ != NULL)
delete [] p.name_;
size_t size = strlen(name_);
p.SetName(name_);
p.name_[size] = '';
strcpy(p.name_, name_);
}
return *this;
}
std::ostream& operator << (std::ostream& os, const Product& p)
{
os << p.GetName() << ' '
<< p.GetBarCode() << ' '
<< p.GetCost();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.