I need to make three files: makefile, product.h, and product.cpp Overall goal is
ID: 669403 • Letter: I
Question
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.)
Thank you for your help.
Explanation / Answer
Answer
Note:As per requirement Code has written in cpp
Code:
//Three files has attached//
//Header files//
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <myproduct.h>
//Declaring the largest array//
const size_t arraySize = 12;
//Pass by value class//
const size_t numDigits = 3;
myproduct CopyCheck(myproduct p);
void AssignCheck(const myproduct& pIn, mymyproduct& pOut);
// passing the value //
myproduct CopyCheck(myproduct p)
{
//Intiating the myproducts//
myproduct x(p);
//Returing value//
return x;
}
//Passing by reference//
void AssignCheck(myproduct& pIn, myproduct& pOut)
{
//Assigning the calls//
pOut = pIn;
}
//Main method declaration//
int main()
{
myproduct mp1("screw", 0xFFFFFFFF, 15.00), mp3;
std::cout << " myproducts behind declaration: ";
std::cout << " mp1 = " << mp1 << ' ';
std::cout << " mp2 = " << mp2 << ' ';
mp1.SetName("Copy Checker");
mp1.SetCost(12.0);
mp2.SetName("Assign Checker");
mp2.SetCost(20.0);
std::cout << " myproducts behind Set: ";
std::cout << " mp1 = " << mp1 << ' ';
std::cout << " mp2 = " << mp2 << ' ';
//Instance//
myproduct mp3 = CopyCheck(mp1);
std::cout << " myproducts behind mp3 = CopyCheck(mp1): ";
std::cout << " mp1 = " << mp1 << ' ';
std::cout << " mp3 = " << mp3 << ' ';
//validate mp2,mp3;//
std::cout << " myproducts behind AssignCheck(mp2,mp3): ";
std::cout << " mp2 = " << mp2 << ' ';
std::cout << " mp3 = " << mp3 << ' ';
myproduct p4("Transitive Assignment Check", 55, 30.0);
mp1 = mp2 = mp3 = mp4;
std::cout << " myproducts behind mp1 = mp2 = mp3 = mp4: ";
std::cout << " mp1 = " << mmp1 << ' ';
std::cout << " mp2 = " << mp2 << ' ';
std::cout << " mp3 = " << mp3 << ' ';
std::cout << " mp4 = " << mp4 << ' ';
myproduct * parray = fresh myproduct[arraySize];
std::cout << " myproduct Array behind declaration: ";
for (size_t m = 0; m < arraySize; ++m)
{
std::cout << " p[" << std::setw(numDmgmts) << m << "] = " << parray[m]
<< ' ';
}
//Loop started//
for (size_t m = 0; m < arraySize; ++m)
{
parray[m].SetName("Match screw");
parray[m].SetBarCode(static_cast<uint32_t>(17 + m));
parray[m].SetCost(static_cast<float>((2 * 17 + m)) / 2);
}
std::cout << " myproduct Array behind Set: ";
for (size_t m = 0; m < arraySize; ++m)
{
std::cout << " p[" << std::setw(numDigits) << m << "] = " << parray[m]<< ' ';
}
}
File 2
#include <iostream>
#include <cstdint>
#include <cstdlib>
//Address your enqiury//
#ifndef MyProduct_H_
#define MyProduct_H_
//Class declaration//
class myProduct
{
public:
//To access from any function//
char* name_;
//To set the name//
void SetName ( char* ) ;
//To print the code//
void SetBarCode ( uint32_t );
//passing by pointer//
void SetCost ( float );
const char* GetName () const;
//pass by code value//
uint32_t GetBarCode () const;
//returns by cost//
float GetCost () const;
MyProduct (); //name "#" code = 0 cost 0
MyProduct (const char* name, uint32_t code, float cost);
~MyProduct ();
MyProduct (const MyProduct& p);
MyProduct& operator= ( MyProduct& p);
private:
//Access controll specifiers//
//the MyProduct name//
//the MyProduct code//
uint32_t code_;
//the MyProduct cost//
float cost_;
};std::ostream& operator << (std::ostream& os, const MyProduct& p);
#endif
File:3
#include "prodcut.h"
#include <cstring>
//Method to copy strcpy//
void MyProduct::SetName ( char* name )
{
//Condition//
if (name_ != NULL)
delete [] name_;
size_t size = strlen(name);
name_ = new char [1 + size];
name_[size] = '';
//Copy the name to the new string//
strcpy(name_, name);
}
//Pass in by value//
void MyProduct::SetBarCode ( uint32_t code )
{
code_ = code;
}
//Set the cost field//
void MyProduct::SetCost ( float cost )
{
cost_ = cost;
}
const char* MyProduct::GetName () const
{
return name_;
}
uint32_t MyProduct::GetBarCode () const
{
return code_;
}
float MyProduct::GetCost () const
{
return cost_;
}
MyProduct::MyProduct() : name_(NULL), code_(0), cost_(0.0)
{
name_ = new char [2];
name_[0] = '#';
name_[1] = '';
}
MyProduct::MyProduct (const char* name, uint32_t code, float cost): name_(NULL), code_(code), cost_(cost)
{
}
MyProduct::~MyProduct ()
{
//condition//
if (name_ != NULL)
delete [] name_;
}
MyProduct::MyProduct (const MyProduct& p) : name_(NULL), code_(p.code_), cost_(p.cost_)
{
}
MyProduct& MyProduct::operator= ( MyProduct& 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;
}
//Overload operator//
std::ostream& operator << (std::ostream& os, const MyMyProduct& p)
{
os << p.GetName() << ' '
<< p.GetBarCode() << ' '
<< p.GetCost();
}