EECE 330 Homework, Spring 2008
American University of Beirut · Data Structures and Algorithms · 11 pages
Showing the first 10 of 11 pages. Download the PDF to see all of it.
More EECE 330 files
- Homework 1 · Fall 2015HomeworkView
- Homework 3 Assignment · Fall 2011HomeworkView
- Homework 2 Assignment · Fall 2011HomeworkView
- Homework 4 Assignment · Fall 2011HomeworkView
- Homework 1 Assignment · Fall 2011HomeworkView
- Homework · Spring 2008HomeworkView
- Other Extra Lecture About Dynamic Programming · Spring 2017LabView
- Useful File MCQ · Fall 2015FileView
Show the text of this homework
American University of Beirut
Faculty of Engineering and Architecture
ECE Department
Spring 2007
EECE 330: Lab # 1
Objective : review EECE 230 concepts ( classes, inheritance, array list, pointer,
virtual functions, overloading,.. )
1)In this exercise, you will design the invoice class using the productType class, the
personType class, the itemType class and arraylistType class. (Download from
Moodle the arraylistType, personType and productType).
Reuse the array listType (the elements of the list are of itemType) and
overload the stream insertion and extraction operators for it.
Reuse the class personType and overload the stream insertion and extraction
operators for it.
Reuse the class productType and overload the stream insertion and extraction
operators for it. The productType specifies the product description and product
unit price.
The itemType class specifies the quantity of the product. It is composed of the
following:
a-private member:
i- product object: is of productType data type;
ii- quantity: is the quantity of the product object.
b- Public member:
i- getTotalPrice function: Return the total price of the item (calculated
using the product unit price and the quantity)
ii-print function: to print the product, the quantity of the product and the
total price of the item.
iii- constructor with default parameters.
c-Operator functions to overload the stream insertion and extraction operators
The invoice class contains the following:
a-Private data member:
i- items object: is of arraylistType data type
ii-customer name object: is of personType data type
b-Public member:
i- amountDue function: To calculate the invoice amount due (based on the
total price of all item objects of the items object)
ii- add function: to add itemType object to items object
iii- print function: to print the customer name, the items object and the
total invoice amountDue
iv-constructor with default parameters;
c-Operator function to overload the stream insertion operator
1
In your main program you need to:
i-declare invoice1 of invoice dataType with (firstName=”James”,
LastName=”Malik” and maxsize of list=5);
ii-Declare item1 & item2 object of itemType datatype
iii-input item1 using the stream extraction operator then print it using the stream
insertion operator.
iv-Add the item1 to the invoice1.
vi- output the invoice1 using the stream insertion operator.
v-input item2 using the stream extraction operator then print it using the stream
insertion operator.
vi- output the invoice1 using the print function.
2) In this exercise you will design and implement the class studentType using
personType, extPersonType, dateType. (Download from Moodle the dateType and the
personType classes)
Derive the class extPersonType ( as public) from the class personType by
adding the following:
a- private data members:
i-Person nationality
iii-Person sex: female or male (0= female, 1= male)
iv-Person dob: date of birth object of dateType data type // date
b-public members:
i-Constructor with default parameter
ii- Function print (to print the Person name, nationality, dob, sex)
iii- Function setData (to print the Person name, nationality, dob, sex)
Derive the class studentType (as public) from the class extPersonType. It
contains the following:
a-private members:
i- Student id
iii-gpa
b-public members:
i- Constructor with default parameters
ii- Function print (to print the data members of PersonType,
extPersonType and studentType)
iii- Function SetData (to set the data members of PersonType,
extPersonType and studentType)
Your driver program will contain the following functions:
a- Declare person as pointer of personType and student1 as object of
studentType. Assign the address of student1 to person. Let person
invokes the print of the personType class. What will be the ouput?
Answer and comment:
2
b- Repeat step a- with the print member function (of the personType base
class ) as virtual. What will be the ouput?
Good Luck
3
#ifndef H_invoiceType
#define H_invoiceType
#include <iostream>
#include "arrayListType.h"
#include "personType.h"
//using namespace std;
class invoice {
private:
arrayListType *items; //items object: is of arraylistType data type
personType *person; //customer name object: is of personType data type
};
#endif
#ifndef H_itemType
#define H_itemType
#include <string>
#include <iomanip>
#include "productType.h"
using namespace std;
class itemType
{
friend ostream& operator <<(ostream &out, const itemType& anitem) {
out<<anitem.product.getDescription()<<anitem.quantity;
//
return out;
//
}
friend istream& operator >>(istream &in, itemType& anitem) {
in>>anitem.quantity;
return in;
}
public:
double getTotalPrice();
void print() const; //print the product, the quantity of the product and
the total price of the item.
itemType(productType& prod, double quan = 0);
//constructor
//Sets product and quantity according to the parameters.
//Postcondition: product = prod; quantity = quan
private:
productType *product; //- product object: is of productType data type
double quantity; //is the quantity of the product object
};
#endif
#include <iostream>
#include <string>
#include "item.h"
using namespace std;
double itemType::getTotalPrice() {
return product.getPrice()*quantity;
}
string itemType::print() const {
string pr;
cout<<"Product Description: "<<product.getDescription()<<endl;
cout<<"Quantity: "<<quantity<<endl;
cout<<"Toltal Price: "<<getTotalPrice()<<endl;
return pr="Product Description: "+product.getDescription()+"\nQuantity:
"+quantity+"\nToltal Price: "+getTotalPrice()+"\n";
} //print the product, the quantity of the product and the total price
of the item.
itemType::itemType(productType &prod, double quan) {
product.setData(prod.getDescription,prod.getPrice);
product.quantity = quan;
}
//personType.h
#include <string>
#include <iomanip>
//using namespace std;
class personType
{
friend ostream& operator <<(ostream &out, const personType& aperson) {
out<<aperson.getFirstName()<<" "<<aperson.getLastName();
return out;
}
friend istream& operator >>(istream &in, personType& aperson) {
in>>aperson.firstName>>aperson.lastName;
return in;
}
public:
void print() const;
//Function to output the first name and last name
//in the form firstName lastName.
void setName();
//Function to input firstName and lastName
string getFirstName() const;
//Function to return the first name.
//Postcondition: The value of the data member firstName
// is returned.
string getLastName() const;
//Function to return the last name.
//Postcondition: The value of the data member lastName
// is returned.
personType(string first = "", string last = "");
//constructor
//Sets firstName and lastName according to the parameters.
//The default values of the parameters are empty strings.
//Postcondition: firstName = first; lastName = last
private:
string firstName; //variable to store the first name
string lastName; //variable to store the last name
};
//personTypeImp.cpp
#include <iostream>
#include <string>
#include "personType.h"
using namespace std;
void personType::print() const
{
cout << firstName << " " << lastName;
}
void personType::setName()
{
cout<<"Enter the first and Last name of the student"<<endl;
cin>>firstName>>lastName ;
}
string personType::getFirstName() const
{
return firstName;
}
string personType::getLastName() const
{
return lastName;
}
//constructor
personType::personType(string first, string last)
{
firstName = first;
lastName = last;
}
//#include <iostream>
//#include <iomanip>
#include <string>
#include "productType.h"
using namespace std;
void productType::setData(string des, double pri)
{
description=des;
price=pri;
}
string productType::getDescription()
{
return description;
}
double productType::getPrice()
{
return price;
}
productType::productType(string des, double pri)
{
description=des;
price=pri;
}
#ifndef H_productType
#define H_productType
#include <iostream>
#include <iomanip>
//#include <fstream>
#include <string>
using namespace std;
class productType
{
friend ostream& operator <<(ostream& out, productType& aproduct) {
out<<aproduct.getDescription()<<" "<<aproduct.getPrice();
return out;
}
friend istream& operator >>(istream& in, productType& aproduct) {
in>>aproduct.description>>aproduct.price;
return in;
}
public:
void setData(string des, double pri);
productType(string des="", double pri=0.0);
string getDescription();
double getPrice();
private:
string
description;
double
price;
};
#endif
#include <iostream>
#include "personType.h"
using namespace std;
void main() {
personType ps;
cin>>ps;
cout<<ps;
}