Question

In: Computer Science

c++ Exercise 1: Make a struct “Fate” that contains the following variables: int month, int year,...

c++

Exercise 1: Make a struct “Fate” that contains the following variables: int month, int year, int day. Make another struct Product that would contain the following variables: 1- Product name. 2- An array for the product ingredients. Set its max size to 3. 3- Product date of expiration. Use the date struct you made. Use those structs in entering the data for two products of your choice. Make a function printProduct(Product product) that prints out the contents of the struct as shown below:

Show your work to the TA and print the full details of two different products.

Exercise 2: Make a class Product that contains the variables in Exercise 1, add setter and getter methods to those variables and the method printProduct as well. Define your own constructor alongside the default constructor: Product(string name, Date date). Make sure to split your work into 3 separate files, as seen in the lab:  The header file (Product.h) for the function prototypes and the variables split into two categories (public, private).  The (Product.cpp) file that implements those functions.  The main .cpp file that uses that class to print out the details of two different products. Take the product details from cin. Construct two products: One using the default constructor with the setter methods, the second with your own constructor.

Solutions

Expert Solution

Exercise 1:

/*
C++ program that demonstrates the Product structure .Prompt
user to enter the product name, ingredients and expiration date .
Print the product details on console window.
*/
//main.cpp
//include header files
#include<iostream>
using namespace std;
//Fate structure
struct Fate
{
   int month;
   int year;
   int day;
};
//Product structure
struct Product
{
   char prodName[50];
   char ingredients[3][50];
   Fate expDate;
};
//function prototype
void printProduct(Product product);
/*start of main function*/
int main()
{
   /*Create a variable of Produdct structure*/
   Product product;

   cout<<"Enter product Name: ";
   //Read product name
   cin.getline(product.prodName,'\n');

   cout<<"Enter product ingredients[1]: ";
   //Read product ingredients
   cin.getline(product.ingredients[0],'\n');

   cout<<"Enter product ingredients[2]: ";
   //Read product ingredients
   cin.getline(product.ingredients[1],'\n');

   cout<<"Enter product ingredients[3]: ";
   //Read product ingredients
   cin.getline(product.ingredients[2],'\n');

   //Read product expiration date
   cout<<"Enter expiration date: ";
   cin>>product.expDate.day
       >>product.expDate.month
       >>product.expDate.year;

   //call printProduct function
   printProduct(product);

   system("pause");
   return 0;

}
void printProduct(Product product)
{
   cout<<"Product Name: "<<
       product.prodName<<endl;
   cout<<"Product ingredients: "<<endl;
   cout<<"1."<<product.ingredients[0]<<endl;
   cout<<"2."<<product.ingredients[1]<<endl;
   cout<<"3."<<product.ingredients[2]<<endl;
   cout<<"Expiration Date:";
   cout<<product.expDate.day<<","
       <<product.expDate.month<<","
       <<product.expDate.year<<endl;

}

Sample Output:

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

Exercise 2:


//Product.h
#ifndef PRODUCT_H
#define PRODUCT_H
#include<iostream>
#include<string>
using namespace std;
//Date structure
struct Date
{
   int month;
   int year;
   int day;
};
//Product class
class Product
{
private:
   string name;
   Date date;

public:
   Product();
   Product(string name, Date date);
   void setName(string);
   void setDate(Date date);

   string getName();
   Date getDate();

   void printProduct();
};
#endif PRODUCT_H
------------------------------------------------------------
//Product.cpp
//Implemenation file
#include<iostream>
#include "Product.h"
using namespace std;
//default constructor
Product::Product()
{
   name="";
   date.day=0;
   date.month=0;
   date.year=0;
}
//parameterized constructor
Product::Product(string name, Date date)
{
   this->name=name;
   this->date.day=date.day;
   this->date.month=date.month;
   this->date.year=date.year;
}
//setter methods
void Product::setName(string name)
{
   this->name=name;
}
void Product::setDate(Date date)
{
   this->date.day=date.day;
   this->date.month=date.month;
   this->date.year=date.year;
}
//Getter methods
string Product::getName()
{
   return name;
}
Date Product::getDate()
{
   return date;
}
//Method that print the product details
void Product::printProduct()
{
   cout<<"Product name: "<<name<<endl;
   cout<<"Date, DD/MM/YYYY : ";
   cout<<date.day<<"/"
       <<date.month<<"/"
       <<date.year<<endl;
}

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

//main.cpp
#include<iostream>
#include "Product.h"
using namespace std;
int main()
{

   //Create an object of Product class
   Product p;

   cout<<"Default constructor"<<endl;
   p.printProduct();

   //create Date structure variable
   //and set date values
   Date date;
   date.day=12;
   date.month=12;
   date.year=2019;

   //Set date and name of product,p
   p.setDate(date);
   p.setName("Pizaa");

   cout<<"Parameterized constructor"<<endl;
   p.printProduct();

   system("pause");
   return 0;
}

------------------------------------------------------------
Sample Output:


Related Solutions

In c++: Code Challenge Consider the following code: struct ListNode { int value; struct ListNode *next;...
In c++: Code Challenge Consider the following code: struct ListNode { int value; struct ListNode *next; }; ListNode *head; // List head pointer Assume that a linked list has been created and head points to the first node. Write code that traverses the list displaying the contents of each node’s value member Now write the code that destroys the linked list
1)What is the output of the following code? struct someType { int a; int b; };...
1)What is the output of the following code? struct someType { int a; int b; }; void f1(someType &s) { s.a = 1; s.b = 2; } someType f2(someType s) { someType t; t = s; s.a = 3; return t; } int main() { someType s1, s2; f1(s1); s2 = f2(s1); cout << s1.a << '-' << s1.b << '-' << s2.a << '-' << s2.b << '-' << endl; return 0; } ------------------------------------------------------------------------------------------------------------------ 2) struct dateType { int...
Implement stack in C Struct: struct patients{ int id; int severity; char *firstName; char *lastName; char...
Implement stack in C Struct: struct patients{ int id; int severity; char *firstName; char *lastName; char *state; int time_spent; }; Given Array: struct patients* patientsArray[4] = {&p1, &p2, &p3, &p4}; Create two functions one for pushing this array to the stack and one for popping the variables such as p1 p2 p3 p4 by its ID
Date - month: int - day: int - year: int +Date() +Date(month: int, day: int, year:...
Date - month: int - day: int - year: int +Date() +Date(month: int, day: int, year: int) +setDate(month: int, day: int, year: int): void -setDay(day: int): void -setMonth(month: int): void -setYear(year: int): void +getMonth():int +getDay():int +getYear():int +isLeapYear(): boolean +determineSeason(): string +printDate():void Create the class Constructor with no arguments sets the date to be January 1, 1900 Constructor with arguments CALLS THE SET FUNCTIONS to set the Month, then set the Year, and then set the Day - IN THAT ORDER...
Consider the following struct that represents a node within a binary tree: struct Node { int...
Consider the following struct that represents a node within a binary tree: struct Node { int data; // Data of interest Node *left // Link to left subtree (nullptr if none) Node *right ; // Link to right subtree (nullptr if none) }; Complete the following function that computes the number of elements in a binary tree: // Counts the number of elements in the binary tree to which t points. // Returns the number of elements. int size(Node *t)...
Using the following definitions for a binary tree, typedef struct bintreenode {     int data;     struct bintreenode*...
Using the following definitions for a binary tree, typedef struct bintreenode {     int data;     struct bintreenode* left;     struct bintreenode* right; } btreenode; // Used for a node in the queue. typedef struct node {     btreenode* nodePtr;     struct node* next; } node; // Used to represent the queue efficiently. typedef struct queue {     node* front;     node* back; } queue; Implement the following functions: void bfs(btreenode* root) // Prints a breadth first search traversal of the binary search tree rooted at root....
C++ language. struct Node {    int data;    Node *next; } Write a function to...
C++ language. struct Node {    int data;    Node *next; } Write a function to concatenate two linked lists. Given lists A* = (4, 6) and B* = (3, 7, 12), after return from Concatenate_Lists(Node A*, Node B*) the list A should be changed to be A = (4, 6, 3, 7, 12). Your function should not change B and should not directly link nodes from A to B (i.e. the nodes inserted into A should be copies of...
IN C++ Given a struct Node { int value; Node *left, *right;}; , implement the functions...
IN C++ Given a struct Node { int value; Node *left, *right;}; , implement the functions below. a) int getSmallest(Node * r); // return smallest value in the BST with root r. Assume r not null. b) int getSecondSmallest(Node * r); // return 2nd smallest value in BST with root r. Assume r not null and r has a nonnull left or right child. c) void removeSecondSmallest(Node * r); // remove 2nd smallest value in BST with root r. Assume...
Write a C program with a struct that contains a string of 12 characters and two...
Write a C program with a struct that contains a string of 12 characters and two integers: number1 and number2. Read all 3 values in from the keyboard using scanf. Print the string and the sum of the two integers using the fully qualified struct names.
Using C++ language, create a program that uses a struct with array variables that will loop...
Using C++ language, create a program that uses a struct with array variables that will loop at least 3 times and get the below information: First Name Last Name Job Title Employee Number Hours Worked Hourly Wage Number of Deductions Claimed Then, determine if the person is entitled to overtime and gross pay. Afterwards, determine the tax and net pay. Output everything to the screen. Use functions wherever possible. Bonus Points: Use an input file to read in an unknown...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT