Monday, 25 December 2017

ADT: RationalNumber


Define a class for rational numbers. A rational number is “ratio-nal” number, composed of two integers with division indicated.
The division is not carried out; it is only indicated, as in 1/2, 2/3, 15/32, 65/4, 16/5.
You should represent rational numbers by two values.
1. An integer named numerator displayed above a line or before a slash.
2. An integer named denominator displayed below or after that line.
Value should only be assigned to denominator if it is non-zero, 1 otherwise.
1. Provide the implementation of mutators for numerator and denominator data members of the class.
2. Provide the implementation of accessors for numerator and denominator data members of the class.
A principle of abstract data type construction is that constructors must be present to create objects with legal values. You should
provide constructors to make objects out of pairs of integer values;
1. A constructor that accepts Rational Number’s numerator and denominator as arguments and assigns them to the
appropriate member variables.
2. Since every integer is also a rational number, 2/1 or 17/1, you should provide a constructor with single integer parameter
that accept only the value of numerator as argument and assign it to the appropriate member variable.
3. Provide the implementation of following member functions and operators
1. write method to write rational numbers in the form 2/3 or 37/51 on the screen.
2. read method to input rational numbers in the form 2/3 or 37/51 from the keyboard.
3. Overload plus (+) binary operator to perform the addition of two rational numbers.
4. Overload minus (–) binary operator to perform the subtraction of two rational numbers and returns the result.
5. Overload multiply (*) binary operator to perform the multiplication of two rational numbers and returns the result.
6. Overload divide (/) binary operator to perform the division of two rational numbers and returns the result.
7. Overload less than (<) binary operator to perform the comparison of two rational numbers and returns the result.
8. Overload equal (==) binary operator to perform the comparison of two rational numbers and returns the result.
9. Overload minus (–) unary operator to convert a rational number into its negative form, if it is already not and returns the
result.
10. Overload logical not (!) unary operator to return true if the rational number is negative, false otherwise.
4. Once you have written the class, write main function and test its functionality by creating some objects of RationalNumber.
The formulas will be useful in defining functions:
a/b + c/d means (a*d + b*c) / (b*d)
a/b – c/d means (a*d – b*c) / (b*d)
(a/b) * (c/d) means (a*c) / (b*d)
(a/b) / (c/d) means (a*d) / (c*b)
–(a/b) means (–a/b)
(a/b) < (c/d) means (a*d) < (c*b)
(a/b) == (c/d) means (a*d) == (c*d)
Let any sign be carried by the numerator; keep the denominator positive.

Code Link is...
https://www.dropbox.com/s/zdsiupqxtnkvbfa/ADT_RationalNumber.cpp?dl=0

ADT: Circle


Write a Circle class having following functionalities
1. The class should have following four private data members.
1. An integer named x that holds the x-axis of a circle.
2. An integer named y that holds the y-axis of a circle.
3. A float named radius that holds the radius of a circle.
4. A constant double named PI that holds the pi’s value i.e. 3.14159.
Value should only be assigned to data members x and y when it is greater than or equal –50 and lesser than or equal to 50, 0
otherwise and to radius when it is greater than or equal 1 and lesser than or equal to 10, 5 otherwise.
2. Provide the implementation of mutators for x, y and radius data members of the class.
3. Provide the implementation of accessors for all the data members (x, y, radius and PI) of the class.
4. Provide the implementation of following constructors and a destructor
1. A constructor that accepts Circle’s x, y coordinates and radius as arguments and assigns them to the appropriate
member variables.
2. A constructor that accepts Circle’s x, y coordinates as arguments and assigns them to the appropriate member variables.
The radius field should be assigned the default value.
3. A constructor that accepts Circle’s x coordinates and radius as arguments and assigns them to the appropriate member
variables. The y coordinates should be assigned the default value.
4. A default constructor that initializes all the data members of the class with default values.
5. A copy constructor to initialize a circle’s object with already existing object.
6. A destructor that do nothing except displaying a simple message “Destructor executed…” on the screen.
5. Provide the implementation of following member functions
1. setCircle method accepts Circle’s x, y coordinates and radius as arguments and assigns them to the appropriate member
variables.
2. getCircle method to initialize the data of a circle taken from the user.
3. putCircle method to display the information of a particular circle.
4. getArea method calculate and return the area of a circle that is PI * radius2
5. getDiameter method calculate and return the diameter of a circle that is radius * 2
6. getCircumference method calculate and return the circumference of a circle that is 2 * PI * radius that is
7. addCircle method should accept two circle objects and return there sum.
8. isEqual method should accept two circle object and return true if they are having same state, false otherwise.
9. findCircle method should accept an array of Circle objects and return the index of the array which is equal to left hand
side object, –1 otherwise.
10. updateObjects method should accept an array of Circle objects with its size and update the radius of all those objects to
the radius of left hand side object exist in the array having same x, y coordinates as of left hand side object.
6. Once you have written the class, write main function and test its functionality by creating some objects of Circle.

Code Link...
https://www.dropbox.com/s/i7jhiqm3jdd3wl2/ADT_Circle.cpp?dl=0

ADT: Cuboids


Cuboids are three-dimensional shapes having different measurements in each dimension say height, width and depth. Cuboids
shapes are often used for boxes, cupboards, rooms, buildings, etc. So keeping in the mind the importance of Cuboids you have to
implement a class Cuboids having following functionalities
1. The class should have following three private data members to which value should only be assigned to them when it is
greater than 0 and lesser than 35.00, 1 otherwise no matter to which dimension.
1. A float named height that holds the cuboids’ height.
2. A float named width that holds the cuboids’ width.
3. A float named depth that holds the cuboids’ depth.
2. Provide the implementation of mutators for all the data members (width, height and depth) of the class.
3. Provide the implementation of accessors for all the data members (width, height and depth) of the class.
4. Provide the implementation of following constructors and a destructor
1. A constructor that accepts cuboids’ height, width and depth as arguments and assigns them to the appropriate member
variables.
2. A constructor that accepts cuboids’ height and width as arguments and assigns them to the appropriate member
variables. The depth field should be assigned the default value.
3. A default constructor that initializes all the data members of the class with default values.
4. A copy constructor to initialize a cuboids’ object with already existing object.
5. A destructor that do nothing except displaying a simple message “Destructor executed…” on the screen.
5. Provide the implementation of following member functions
1. setCuboids method accepts cuboids’ height, width and depth as arguments and assigns them to the appropriate
member variables.
2. getCuboids method to initialize the data of a cuboids taken from the user.
3. putCuboids method to display the information of a particular cuboids.
4. getSurfaceArea method provide the facility to calculate the surface area of a cuboids that is
2(height * widht) + 2(height * depth) + 2(width * depth)
5. getVolume method provide the facility to calculate the volume of a cuboids that is height * width * depth
6. getSpaceDiagonal method provide the facility to calculate the space diagonal of a cuboids that is
ℎ ℎ + ℎ + ℎ
7. putCuboidsInfo method should display all the dimensions, surface area, volume and space diagonal of a cuboids.
6. Once you have written the class, write main function and test its functionality by creating some objects of Cuboids.

code Link is...
https://www.dropbox.com/s/ae0abd03lyt3hrg/ADT_Cuboids.cpp?dl=0

ADT: Employee


Write a class named Employee for which each object can hold information about a particular employee:
1. The class should have following four private data members
1. A string named name that holds the employee’s name.
2. An integer named id that holds the employee’s ID number.
3. A string named department that holds the name of the department where the employee works.
4. A string named position that holds the employee’s job title.
2. Provide the implementation of following constructors and a destructor
1. A constructor that accepts employee’s name, employee’s ID number, department, and position as arguments and
assigns them to the appropriate member variables.
2. A constructor that accepts employee’s name and ID number as arguments and assigns them to the appropriate member
variables. The department and position fields should be assigned an empty string ("").
3. A default constructor that assigns empty string ("") to the name, department, and position member variables, and 0
to the id member variable.
4. A copy constructor to initialize an employee’s object with already existing object.
5. A destructor that do nothing except displaying a simple message “Destructor executed…” on the screen.
3. Provide the implementation of properties methods (get/set) for all the data members (name, id, department and position) of
the class.
4. Provide the implementation of following member functions
1. setInfo method accepts employee’s name, employee’s ID number, department, and position as arguments and assigns
them to the appropriate member variables.
2. getInfo method to initialize the data of an employee taken from the user.
3. putInfo method to display the information of a particular employee.
5. Once you have written the class, write main function and test its functionality also creates five Employee objects to hold the
following data.
Code link is...



Bank Account Hierarchy


1. ADT: Account
Design an Account class that has the following members:
Protected Member Variable:
firstName, a string used to hold the first name of the account holder.
lastName, a string used to hold the last name of the account holder.
curBalance, a double used to hold the current balance of the account holder.
Public Member Functions:
• Account(const string& fname, const string &lname, double curbalance);
• virtual string getAcctType() const;
Returns "Account" (and that's all it does).
• virtual double bebitTransaction(double debitamount);
Subtract the transaction amount from the account. No condition check is required.
• virtual double creditTransaction(double creditamount);
Add the transaction amount to the account. No condition check is required.
• void print(ostream& os);
Print the three fields to the parameter ostream. The fields must be printed in a formatted way, in a fixed-size column
and appropriate left/right justification for each field as below.
Saud, Yaseen CheckingAccount 200.00
Hunain, Shahid SavingsAccount 1000.00
2. ADT: CheckingAccount
Minimum balance on the account is equal to 100. For every transaction resulting in the amount of the account being lower
than the minimum balance, a transaction fee of 10 is charged. The balance can become negative.
Design a CheckingAccount class that is derived from the Account class. The CheckingAccount class should have the following
members:
Public Member Functions:
• CheckingAccount(const string& fname, const string& lname, double curbalance);
Do not forget to initialize fields other than the three parameters.
• virtual string getAcctType() const;
Returns "CheckingAccount" (and that's all it does).
• virtual double debitTransaction(double debitamount);
Subtracts the transaction amount from the account and possibly charges a transaction fee. You must call the base
class debitTransaction() method inside this method.
• virtual double creditTransaction(double creditamount);
Adds the transaction amount to the account and possibly charges a transaction fee. You must call the base class
creditTransaction() method inside this method.
• void chargeFee();
This is a private method. It is called internally by the debit and credit transaction methods.

3. ADT: SavingsAccount
A maximum of 2 transactions is allowed. After successfully committing any transaction an interest of 2% is paid (i.e., added
to the current balance). The interest is paid on the entire amount of the account.
Design a SavingsAccount class that is derived from the Account class. The SavingsAccount class should have the following
members:
Public Member Functions:
• SavingsAccount(const string& fname, const string& lname, double curbalance);
Do not forget to initialize fields other than the three parameters.
• virtual string getAcctType() const;
Returns "SavingsAccount" (and that's all it does).
• virtual double debitTransaction(double debitamount);
If there are less than two transactions, subtracts the transaction amount from the account and pays interest. You
must call the base class debitTransaction() method inside this method.
• virtual double creditTransaction(double creditamount);
If there are less than two transactions, adds the transaction amount to the account and pays interest. You must call
the base class creditTransaction() method inside this method.
• void payInterest();
This is a private method. It is called internally by the debit and credit transaction methods.
4. Main Function
This application program uses the classes you have developed. It creates one Checking account and one Savings account, and
does some transactions. After every transaction, the account information is displayed, by calling print(cout) in the object.
Requirements:
1. Both objects must be dynamically created and pointed by the base class pointer. For example,
Account* c = new CheckingAccount("Osama", "Jameel", 100.0);

code link .....
https://www.dropbox.com/s/xd12zkzc8wa8q4f/Bank%20Account.cpp?dl=0

Shape Inheritance Hierarchy


1. ADT: BasicShape
Design a BasicShape class that has the following members:
Private Member Variable:
area, a double used to hold the shape’s area.
Public Member Functions:
• getArea – return the value in the member variable area.
• calcArea – a virtual function that display a message “Basic Shape Calculate Area Function…”.
2. ADT: Circle
Design a Circle class that is derived from the BasicShape class. The Circle class should have the following members:
Private Member Variable:
centerX, a long integer used to hold the x coordinate of the circle’s center.
centerY, a long integer used to hold the y coordinate of the circle’s center.
radius, a double used to hold the circle’s radius.
Public Member Functions:
• constructor – accepts values for centerX, centerY, and radius. Should call the overridden calcArea function described
below.
• getCenterX – returns the value in centerX.
• getCenterY – returns the value in centerY.
• calcArea – calculates the area of the circle (area = 3.14159 * radius * radius) and stores the result in the inherited
member area.
3. ADT: Rectangle
Design a Rectangle class that is derived from the BasicShape class. The Rectangle class should have the following members.
Private Member Variable:
width, a long integer used to hold the width of the rectangle.
length, a long integer used to hold the length of the rectangle.
Public Member Functions:
• constructor – accepts values for width and length. Should call the overridden calcArea function described below.
• getWidth – returns the value in width.
• getLength – returns the value in length.
• calcArea – calculates the area of the rectangle (area = length * width) and stores the result in the inherited member area.
4. Main Function
Demonstrate the classes in a program that has a BasicShape pointer. Initialize it with the dynamically created object of Circle
and make call to calcArea function, then assign the BasicShape’s pointer with dynamically created Rectangle objects and
make call to calcArea function

Code Link.....
https://www.dropbox.com/s/bnrhy0rpdf5tr5v/Shape_Inheritance.cpp?dl=0

Case Study: Package Inheritance Hierarchy


Package-delivery services, such as OCS®, TCS® and JCS®, offer a number of different shipping options, each with specific costs
associated.
Create an inheritance hierarchy to represent various types of packages. Use Package as the base class of the hierarchy, and then
include classes TwoDayPackage and OvernightPackage that derive from Package.
1. ADT: Package
1. The class should have following private data members
1. Two strings to represent the name of the sender and receiver.
2. Two strings to hold the address of the sender and receiver.
3. Two strings to contain the city of the sender and receiver.
4. A float named weight to store the weight of the package in ounce.
5. A float named costPerOunce to store the cost per ounce of the package.
2. A constructor which accepts the names, addresses, cities of the sender and receiver and the weight and its cost per ounce
as arguments and assigns them to the appropriate member variables. Ensure that the weight and cost per ounce contain
positive values.
3. calculateCost member function that returns the cost associated with shipping the package, i.e. weight * costPerOunce.
2. ADT: TwoDayPackage
1. The class should have private data member of type float named flatFee to represent the company charges for two-daydelivery
service.
2. A constructor which accepts all the required information for the two-day-delivery package including the flatFee as
arguments and assigns them to the appropriate member variables. Ensure that the flatFee contains positive value.
3. calculateCost member function that computes and return the shipping cost by adding the flat fee to the weight-based
cost calculated by base class Package's calculateCost function.
3. ADT: OvernightPackage
1. The class should have private data member of type float named feePerOunce to represent the additional cost charged for
overnight-delivery service.
2. A constructor which accepts all the required information for the overnight package including the additional fee per ounce
as arguments and assigns them to the appropriate member variables. Ensure that the fee per ounce contains positive
value.
3. calculateCost member function that adds the additional fee per ounce to the standard cost per ounce before calculating
the shipping cost and then computes and return the shipping cost.
4. Main Function
Once you have written your classes, write main function and test the functionality of each of the classes thoroughly by creating
their objects.

Code link.....
https://www.dropbox.com/s/uibbqxwz8kam2g7/Package_Inheritance.cpp?dl=0

ADT: RationalNumber

Define a class for rational numbers. A rational number is “ratio-nal” number, composed of two integers with division indicated. The division...