Please, need help ASAP, not too hard just can\'t figure it out. Inheritance Prog
ID: 3575640 • Letter: P
Question
Please, need help ASAP, not too hard just can't figure it out.
Inheritance Program
Objectives
-Code in C++
-Use Inheritance
-Use the vector class from the standard template library
-Using standard in and standard out
-Implement basic user data validation
Task
For this homework you are going to extend homework 4 and create a simple Bill of Material (BOM) that represents an assembly. We will take the part class and extend it using Inheritance to create an assembly class. Using the is-a relationship test, we know that an assembly IS-A part however a part is not necessarily an assembly. In other words our base class will be a part, and our child class will be an assembly.
Your job is to implement the class defined in Assembly.h. You must use the public methods defined on Part.h to get and set the private variables defined in Part. You will be adding two new methods in Assembly.cpp and overriding the toString method.
In the Assembly toString method you should use the stringstream class to build up a string object to return. Call the toString function in each Part object in the parts vector to get the string representation of each Part.
Your main function should read from standard in an Assembly. The format of the input is defined in the file “data”.
Sample Output
Below is an example session using the “data” file.
Enter the Assembly id: 12-00011
Enter the Assembly drawing id: 12-00011.dwg
Enter the Assembly quantity: 1
Enter the number of Parts that make up this Assembly: 3
Enter the Part id: 12-00012
Enter the Part drawing id: 12-00012.dwg
Enter the Part quantity: 2
Enter the Part id: 12-00013
Enter the Part drawing id: 12-00013.dwg
Enter the Part quantity: 3
Enter the Part id: 12-00014
Enter the Part drawing id: 12-00014.dwg
Enter the Part quantity: 4
This is your completed Assembly:
Id: 12-00012 drawing: 12-00012.dwg qty: 2
Id: 12-00013 drawing: 12-00013.dwg qty: 3
Id: 12-00014 drawing: 12-00014.dwg qty: 4
FILES
Part.h
#ifndef PART_H
#define PART_H
#include
using namespace std;
class Part {
private:
string m_id;
string m_drawing;
int m_quantity;
public:
Part(string id, string dwg, int qty);
string getId();
void setId(string id);
string getDrawing();
void setDrawing(string drawing);
int getQuantity();
void setQuantity(int qty);
virtual string toString();
};
#endif
Part.cpp
#include "Part.h"
Part ::Part(string id, string dwg, int qty)
{
m_id=id;
m_drawing=dwg;
m_quantity=qty;
}
string Part:: getId()
{
return m_id;
}
void Part::setId(string id)
{
m_id=id;
}
string Part:: getDrawing()
{
return m_drawing;
}
void Part:: setDrawing(string drawing)
{
m_drawing=drawing;
}
int Part:: getQuantity()
{
return m_quantity;
}
void Part:: setQuantity(int qty)
{
m_quantity=qty;
}
/**
* Returns a string representation of the part
* Do NOT modify this function or you will
* cause the tests to fail.
*/
string Part::toString()
{
return "Id: " + this->m_id + " drawing: " + this->m_drawing + " qty: " + to_string(this->m_quantity);
}
main.cpp
#include
#include "Part.h"
#include "Assembly.h"
using namespace std;
int main(int argc, char **argv)
{
return 0;
}
data
12-00011
12-00011.dwg
1
3
12-00012
12-00012.dwg
2
12-00013
12-00013.dwg
3
12-00014
12-00014.dwg
4
Assembly.h
#ifndef ASSEMBLY_H
#define ASSEMBLY_H
#include
#include "Part.h"
using namespace std;
class Assembly : public Part{
private:
vector m_parts;
public:
//Assembly constructor
Assembly(string id, string dwg, int qty);
//Add a part to this assembly
void addPart(Part p);
//Print out the BOM
string toString();
};
#endif
Assembly.cpp
#include "Assembly.h"
PROGRAM 4 (if useful to you)
Objectives
Implementing a class
Using a header file
Creating an object from a class you created
Tasks
For this project you are going to create a class that represents a manufactured part on a bill of material (BOM). This part will have the following attributes (represented as a private variable):
identifier - The parts identifier as an alpha numeric string
drawing - The AutoCAD drawing file that represents the part
quantity - The number of parts that are required
In addition to the above attributes your class will provide the following methods.
Part(string id, string dwg, int qty) - Object constructor: Initializes member variables
string getId() - returns the id of the part
void setId(string id) - sets the id of the part
string getDrawing() - returns this parts drawing file name
void setDrawing(string drawing) - sets this parts drawing name
int getQuantity() - returns the quantity of this part
void setQuantity(int qty) - sets the quantity of this part
string toString() - returns a string representation of the part
Your task is to implement the functions for the Part class in the file Part.cpp. The file main.cpp will only be used for testing purposes, no code should be written in main.cpp.
Program 4 answer
#include
#include
#include "part.h"
using namespace std;
Part::Part(string _id, string drw, int quant) {
id = _id;
drawing = drw;
quantity = quant;
}
string Part::getId(){
return id;
}
void Part::setId(string _id){
id = _id;
}
string Part::getDrawing(){
return drawing;
}
void Part::setDrawing(string _drg)
{
drawing = _drg;
}
int Part::getQuantity()
{
return quantity;
}
void Part::setQuantity(int qty){
quantity = qty;
}
/**
* Returns a string representation of the part
* Do NOT modify this function or you will
* cause the tests to fail.
*/
string Part::toString()
{
return "Id: " + this->id + " drawing: " + this->drawing + " qty: " + to_string(this->quantity);
}
Explanation / Answer
Please find below the main.cpp program :
main.cpp:
--------
#include <iostream>
#include "Part.h"
using namespace std;
int main(int argc, char **argv)
{
//This will Constructs a new part
Part p("12-00001","12-00001.dwg",1);
//Print the part out.
cout << p.toString() << endl;
//Test our set Drawing method
p.setDrawing("14-11111.dwg");
cout << p.toString() << endl;
//This will Test our set setId method
p.setId("my_new_id");
cout << p.toString() << endl;
//This will Test our set Quantity method
p.setQuantity(42);
cout << p.toString() << endl;
//This will Test each of the accessor methods
cout << p.getDrawing() << endl;
cout << p.getId() << endl;
cout << p.getQuantity() << endl;
return 0;
}