I need to make three files: makefile, product.h, and product.cpp Overall goal is
ID: 669450 • 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>
The makefile should follow this type of template (this is just an example but non functioning code):
# makefile for class project
HOME = /home/classes/cs1000/LIB
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: test1.x test2.x product.x
product.x: main.o products.o
$(CC) -oproduct.x product.o main.o
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
Complete Program:
// File: product.h
#ifndef PRODUCT_H
#define PRODUCT_H
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <stdint.h>
using namespace std;
class Product
{
public:
Product();
Product(char*, uint32_t, float);
void SetName(const char*);
void SetBarCode(uint32_t);
void SetCost(float);
const char* GetName() const;
uint32_t GetBarCode() const;
float GetCost() const;
Product& operator =(const Product& b);
private:
char* name_;
uint32_t code_;
float cost_;
};
ostream& operator <<(ostream& out, const Product& obj);
#endif
// File: product.cpp
#include "product.h"
Product::Product()
{
name_ = new char[100];
strcpy(name_, "#");
code_ = 0x00000000;
cost_ = 0.0;
}
Product::Product(char* aName, uint32_t aCode, float aCost)
{
name_ = new char[100];
strcpy(name_, aName);
code_ = aCode;
cost_ = aCost;
}
void Product::SetName(const char* aName)
{
strcpy(name_, aName);
}
void Product::SetBarCode(uint32_t aCode)
{
code_ = aCode;
}
void Product::SetCost(float aCost)
{
cost_ = aCost;
}
const char* Product::GetName() const
{
return name_;
}
uint32_t Product::GetBarCode() const
{
return code_;
}
float Product::GetCost() const
{
return cost_;
}
Product& Product::operator=(const Product& other)
{
strcpy(name_, other.name_);
code_ = other.code_;
cost_ = other.cost_;
return *this;
}
ostream& operator <<(ostream& out, const Product& obj)
{
out << setprecision(2) << fixed << left;
out << setw(17) << obj.GetName();
out << setw(17) << obj.GetBarCode();
out << setw(17) << obj.GetCost();
out << endl;
return out;
}
// File: makefile.cpp
#include <iostream>
#include <iomanip>
#include "product.h"
using namespace std;
int main()
{
Product p1("product_1", 0xFEDCBA98, 100);
Product p2("product_2", 0x89ABCDEF, 200);
cout << "Products after declaration:" << endl;
cout << " p1 = " << p1;
cout << " p2 = " << p2;
p2 = p1;
cout << " Products after assignment p2 = p1" << endl;
cout << " p1 = " << p1;
cout << " p2 = " << p2;
cout << endl;
system("pause");
return 0;
}