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

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....
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...
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...
Make a function definition in C for the following: void insert (double *b, int c, double...
Make a function definition in C for the following: void insert (double *b, int c, double s, int pos); //Insert value s at position pos in array. //needs: // c > 0, pos >= 0, and pos <= c-1. Elements b[0]...b[c-1] exist. //this will do: //Elements from indexes pos up to c-2 have been moved up to indexes pos+1 up to c-1. The value s has been copied into b[pos]. //Note: the data that was in b[c-1] when the function...
Given the following int variables which represent a date, int day; // 0-6 for Sunday-Saturday int...
Given the following int variables which represent a date, int day; // 0-6 for Sunday-Saturday int month; // 1-12 for January-December int date; // 1-31 int year; // like 2020 Define the function void tomorrow(int& day, int& month, int& date, int& year); which updates the variables to represent the next day. Test in main().
4, Make the table project with C++. Write a function with the following interface: void multiplyTable(int...
4, Make the table project with C++. Write a function with the following interface: void multiplyTable(int num) This function should display the multiplication table for values from 1...num. For example, if the function is passed 10 when it is called, it should display the following: 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT