0% found this document useful (0 votes)
58 views

Weekly Assignment-6-OOP-C++

The document provides instructions for a weekly coding assignment that is due on July 3rd, 2020. It instructs students to create two C++ programs, adding comments to classes following a specific style and including the output of each program. It also asks students to draw UML class diagrams for each program. The first program creates a Pizza class with constants, constructors, and methods to set values and display output. The second program creates classes for Name, CreditData, and Customer, with constructors and methods to display output.

Uploaded by

riwa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

Weekly Assignment-6-OOP-C++

The document provides instructions for a weekly coding assignment that is due on July 3rd, 2020. It instructs students to create two C++ programs, adding comments to classes following a specific style and including the output of each program. It also asks students to draw UML class diagrams for each program. The first program creates a Pizza class with constants, constructors, and methods to set values and display output. The second program creates classes for Name, CreditData, and Customer, with constructors and methods to display output.

Uploaded by

riwa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Weekly Assignment-6

Due date: 3rd July 2020 – [12:00AM] Total marks: 100

Create the following C++ programs as per the instructions given.

1. Add comments to every class following the same style you have done in week-4 and
week-5 assignments.
2. Your answer should also include output of every program in your system.
3. Also draw the UML – class diagram of each program.

Instructions of programs are as follows:


Figure -1

Figure - 2
SOLUTION:

#include<iostream>
#include<string>
using namespace std;
// Pizza class declaration
class Pizza
{
//Private variables
private:
string topping;
int diameter;
double price;
// Constant member fields
const static string STDTOP;
const static int STDSIZE;
const static double STDPRICE;
public:
// Constructor
Pizza(const string = STDTOP, const int = STDSIZE,const double = STDPRICE);
void setValues();

// Function prototype
void displayValues();
};

// Assigning the values for three class constants


const string Pizza::STDTOP = "cheese";
const int Pizza::STDSIZE = 12;
const double Pizza::STDPRICE = 8.99;

// Constructor
// assigning the values in the approprite class field
Pizza::Pizza(const string top, const int size,const double price)
{
topping = top;
diameter = size;
// Calling the member function
this->price = price;
}

// display function
void Pizza::displayValues()
{
cout << "a " << diameter << " inch " << topping <<
" pizza. Price $" << price << endl;
}

// setvalues function
void Pizza::setValues()
{
const double TOPPINGPREMIUM = 1.00;
const double SIZEPREMIUM = 1.50;
cout << "Enter topping ";
cin >> topping;

// Comparing the objects


if(topping != STDTOP)
price = STDPRICE + TOPPINGPREMIUM;
cout << "Enter size ";
cin >> diameter;

if(diameter > STDSIZE)


price += SIZEPREMIUM;
}

//************************************************
// Function main
//************************************************
int main()
{
// creating instance object of the class pizza
Pizza aPizza;
char standard;
cout << "The standard pizza is: ";

aPizza.displayValues();
cout << "Let me take your order" << endl;
cout << "Do you want the standard pizza - y or n? ";// Asks the user to respond yes or no

if(standard != 'y')
aPizza.setValues();//assigning new values to pizza's data file
cout << "Your order is ";
aPizza.displayValues();// displays the user's complete order
return 0;
}
OUTPUT 1:
OUTPUT 2:

UML CLASS- DIAGRAM:


SOLUTION:

#include<iostream>
#include<string>
using namespace std;
class Pizza
{
private:
string topping;
int diameter;
double price;
const static string STDTOP;
const static int STDSIZE;
const static double STDPRICE;
public:
Pizza(const string = STDTOP, const int = STDSIZE,const double = STDPRICE);
void setValues();
void displayValues();
};

const string Pizza::STDTOP = "cheese";


const int Pizza::STDSIZE = 12;
const double Pizza::STDPRICE = 8.99;
Pizza::Pizza(const string top, const int size,
const double price)
{
topping = top;
diameter = size;
this->price = price;
}

void Pizza::displayValues()
{
cout << "a " << diameter << " inch " << topping <<
" pizza. Price $" << price << endl;
}

void Pizza::setValues()
{
const double TOPPINGPREMIUM = 1.00;
const double SIZEPREMIUM = 1.50;
cout << "Enter topping ";
cin >> topping;

if(topping != STDTOP)
price = STDPRICE + TOPPINGPREMIUM;
cout << "Enter size ";
cin >> diameter;

if(diameter > STDSIZE)


price += SIZEPREMIUM;
}

int main()
{
Pizza stdPizza;
Pizza special("pineapple");
Pizza deluxeSpecial("sausage", 16);
Pizza veryDeluxeSpecial("lobster", 20, 17.99);

cout << "The standard pizza is: ";


stdPizza.displayValues();
cout << "Today's special is ";
special.displayValues();
cout << "The deluxe special is ";
deluxeSpecial.displayValues();
cout << "And the very deluxe special is ";
veryDeluxeSpecial.displayValues();

return 0;
}

OUTPUT:
UML CLASS- DIAGRAM:
SOLUTION:

#include<iostream>
#include<string>
using namespace std;

// Name class declaration


class Name
{
//Private variables
private:
string first;
string middle;
string last;

public:
// Constructor
Name(string, string, string);
// Function prototype for display function
void displayFullName();
};

// assigning the values in the approprite class field


Name::Name(string first, string middle, string last)
{
this->first = first; // Calling the member function
this->middle = middle;// Calling the member function
this->last = last;// Calling the member function
}

// display function
void Name::displayFullName()
{
cout << first << " " << middle <<
" " << last << endl;
}

// CreditData class declaration


class CreditData
{
//Private variables
private:
double currentBalance;
double maxBalance;

public:
// Constructor
CreditData(double, double = 0);
// Function prototype for display function
void displayCreditData();
};

// Stores private fields for a current balance and a maximum balance


CreditData::CreditData(double currBal, double maxBal)
{
// Assigning current balance argument to the current Balance field
currentBalance = currBal;
// Compare maxBal and currBal
if(maxBal < currBal)
maxBalance = currBal;
else
maxBalance = maxBal; //Assigning maxbalance argument to the
maxBalance field
}

// displayCreditData()function
void CreditData::displayCreditData()
{
double creditLeft = maxBalance - currentBalance;
cout << "Current balance: $" << currentBalance <<"\nMaximum balance $" <<
maxBalance <<"\nCredit left: $" << creditLeft << endl;
}

// Customer class declaration


class Customer
{
//Private variables
private:
Name name;
CreditData credit;
string phoneNumber;
public:

// constructor
Customer(string, string, string,double, double, string);
// Function prototype for display function
void showCustomer();
};

// assigning the values in the approprite class field


Customer::Customer(string firstName, string middleName,string lastName, double bal,
double max, string phone) :name(firstName, middleName, lastName), credit(bal, max)
{
phoneNumber = phone;
}

// display function
void Customer::showCustomer()
{
cout << "Customer data:" << endl;
name.displayFullName();
cout << phoneNumber << endl;
credit.displayCreditData();
}

//************************************************
// Function main
//************************************************
int main()
{
// Creating a customer object
int x;
const int TIMES = 2;
string first, middle, last, phone;
double bal, lim;

// Using loops two times


for(x = 0; x < TIMES; ++x)
{
cout << endl <<"Please enter first name for customer #" << (x + 1) << " ";
cin >> first;

cout << "Please enter middle name ";


cin >> middle;

cout << "Please enter last name ";


cin >> last;

cout << "Enter current balance ";


cin >> bal;

cout << "Enter credit limit ";


cin >> lim;

cout << "Enter phone number ";


cin >> phone;

Customer cust(first, middle, last, bal, lim, phone);


cust.showCustomer();
}
return 0;
}
OUTPUT:
UML CLASS- DIAGRAM:

You might also like