Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

With the help of another Chegg Expert I was able to get a bit further with finis

ID: 669850 • Letter: W

Question

With the help of another Chegg Expert I was able to get a bit further with finishing this assignment. Still experiencing some problems and just need some help wrapping this up.

The assignment is:

I need to make three files: makefile, product.h, and product.cpp

Overall goal is to design a class based on non-language-specific specifications

1. Create a makefile that builds executables test1.x and test2.x. Look at the #include statements in test1.cpp and test2.cpp to deduce what the intermediate targets and dependencies should be.

The include statements from test1.cpp:

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <product.h>
// #include <product.cpp> // in lieu of makefile

Include statements from test2.cpp:

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <product.h>

2. Design the class Product, placing the definition in file product.h

3. Implement the class Product, placing the class implementation in file product.cpp

The class should implement the following:

Class Name :

Product

Services:

Properties:

Private variables:

1. The class should be a proper type, to include default constructor, 3-argument constructor (that initializes the three data fields), copy constructor, assignment operator, and destructor. Note that the default constructor should set the name to "#", the bar code to 0x00000000, and the cost to 0.0.

2. Be sure to use initialization lists for all of the constructors, including the copy constructor.

3. The output operator operator<< should be overloaded for the type Product. Display the three fields with TAB character between them. (Don't output any newlines.)

This is what I have so far:

File: product.cpp

/*
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 = strlen(name);
name_ = new char [1+size];
name_[size] = '';
strcpy(name_ , name);

}

Product::~Product()
{
if (name_ != NULL)
  delete[] name_;
}

Product::Product (const Product& p) : name_(NULL), code_(p.code_), cost_(p.cost_)
{


size_t size = strlen(p.name_);
name_ = new char [1+size];
name_[size] = '';
strcpy(name_ , p.name_);

}

Product& Product::operator= (const Product& p)
{
if (this != &p)
{
  if (name_ != NULL)
   delete [] name_;
  size_t size = strlen(p.name_);
  name_ = new char [1+size];
  name_[size] = '';
  strcpy(name_ , p.name_);

}
return *this;
}

std::ostream& operator<< (std::ostream& os, const Product& p)
{
os << p.GetName() << ' '
  << p.GetBarCode() << ' '
  << p.GetCost ();

return os;
}


File: product.h

/* product.h */
#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

Makefile:

CPP = $(HOME)/cpp


#CC = g++ -std=c++11 -Wall -Wextra -I. -I$(CPP)
CC = clang++ -std=c++11 -I. -I$(CPP) -Weverything -Wno-old-style-cast
CCC = clang++ -std=c++11 -I. -I$(CPP) -Weverything -Wno-old-style-cast -Wno-sign-conversion

# -Weverything -Wno-old-style-cast

all: product.x test1.x test2.x

test1.x: product.o
$ (CC) -otest1.x product.o


test2.x: product.o
$(CC) -otest2.x product.o


product.x: product.o
$(CC) -oproduct.x product.o

product.o: product.h product.cpp
$(CC) -c product.cpp

-------------------------------------------------------------

I have to be able to use the command "make test1.x" without running into any errors. Everytime I try to run this I get the error:

/usr/lib/gcc/x86_64-pc-linux-gnu/4.8.4/../../../../lib64/crt1.o: In function `_start': (.text+0x20): undefined reference to `main' x86_64-pc-linux-gnu-clang-3.5.0: error: linker command failed with exit code 1 (use -v to see invocation) makefile:15: recipe for target 'test2.x' failed make: *** [test2.x] Error 1

The test1.cpp file I am working with is this: (It can not be modified)

/**
test.cpp

test harness for class Product
*/

#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] << ' ';
}

// */
}

Any help is greatly appreciated.

Explanation / Answer

Answer:

Note: code modified without errors

Main.cpp

#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]

               << ' ';

   }

   system("pause");

}

Product.cpp:

#include "product.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)

{

size_t size = strlen( name );

    name_ = new char [size + 1];

    name_[-1] = '';

    strcpy (name_, name);

}

Product::~Product ()

{

if (name_!= NULL)

    delete[] name_;

}

Product::Product (const Product& p) : name_(NULL), code_(p.code_), cost_(p.cost_)

{

size_t size = strlen(p.name_);

name_ = new char [1+size];

name_[size] = '';

strcpy(name_ , p.name_);

}

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();

       return os;

      

}

Product.h:

#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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote