In: Computer Science
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.
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: