The objectives of the questions are to:

  • assess if students have understood the basic concepts of object-oriented class
  • write the class specification, constructor, accessors and mutators
  • write test driver to test the written methods

Examine the following class diagram, additional information and answer the questions that follow:

CarSpeed

acceleration: float
engine_number: string
start: bool

CarSpeed(float = 0, string = "", bool = false)
start_car():void
stop_car():void
set_acceleration(float): void
set_engine_number(string): void get_velocity(float): float

Additional Information:

  • CarSpeed(float = 0, string = "", bool = false); Constructor with default values. Set the data members, acceleration, engine_number and start accordingly.
  • start_car():void Mutator of the data member, start. Set it to true. Display the message Car started successfully.
  • stop_car():void Mutator of the data member, start. Set it to false. Display the message Car stopped successfully.
  • set_acceleration(float): void Mutator of acceleration. Set and display the data member, acceleration accordingly.
  • set_engine_number(string): void Mutator of engine_number. Set and display the data member, engine_number accordingly.
  • get_velocity(float): float Computes the velocity using the formula: acceleration * time. Prompts the user to enter the value for time. Before the computation of velocity, check that the car has started. If car is started, perform the computation. Return the velocity. If car not started display message "start the car first then retrieve velocity after acceleration. Set the velocity to 0;

(a)List FOUR (4) characteristics of a constructor

(b)Develop C++ code to implement the CarSpeed Class specification.

(c)Develop C+ code to implement ALL the methods found in the CarSpeed class.

(d)Inspect the sample output, as shown below and write a test driver to test and fulfil the following requirements:

Uses a loop that repeats 3 times to prompt the user to enter the engine number, acceleration and time and use the various mutators to set the engine number, acceleration & time. It further invokes the method to compute and display the respective velocities.. The velocity is displayed with 2 decimal places. See image.

Question 2

The objectives of the question are to:

  • Implement the constructor, accessors, mutators and methods
  • understand object composition
  • write test driver to test the methods
You are required to write the declaration and implementation code in separate files. Your submission is expected to reflect this.

Examine the following class specifications, additional information and answer the questions that follow:

class Date
{
int dd;
int mm;
int yy;

public:
Date(int = 1, int=1, int=2015);
void setDate(int, int, int);
int getDD();
int getMM();
int getYY();
};
class Appointment
{
private:
string NRIC;
string name;
double fees;
Date visit;
Date nextVisit;

public:
Appointment();
void setAppt(string, string, double, Date, Date);
Date getVisit();
Date getnextVisit();
string getName();
void displayAppt();
void displayAppt(string);
};

Additional Information:

  • Date(int = 1, int=1, int=2015) Constructor with default values.
  • void setDate(int, int, int) Mutator. pass-in values accordingly.
  • int getDD() int getMM() int getYY() Accessors. respectively.
  • Appointment() Default Constructor of Appointment class. Set data members, NRIC and name to null, fees to 0.0
  • void setAppt(string, string, double, Date, Date) Mutator. arguments values accordingly.
  • Date getVisit() Date getnextVisit() string getName() Accessors.Return the data members,visit / nextVisit / name respectively.
  • void displayAppt() Use cout to display the data members of Appointment class. If the data member of Date class is 0, display a dash1.
  • void displayAppt(string) Use cout to display the name and fees of Appointment class when the NRIC data member of the instance matches the pass-in value. Invokes displayAppt();

Note1: When there is no next visit, the data members, dd, mm and yy in Date class are set to 0.

(a)Define method overloading. Identify the overloaded method(s) found in the given class(es).

(b)Develop C++ code to implement ALL the methods found in the above-mentioned class specifications:

  • (i)Date Class
  • (ii)Appointment Class

(c)Write a test driver to test ALL the methods in the Date and Appointment classes. The test driver should include the following:

  • (i)declare an array of 4 Appointment
  • (ii)initialised the array of Appointment objects with the data from the test data appended below:
  • (iii)display all appointment objects, using the default displayAppt () method
  • (iv)display a single appointment object, using displayAppt(string) method, passing in the NRIC value, S56783.

Include a screen output similar to the sample screen output shown below in your submission.

Test Data

NRIC Name Fees Date of Visit Date of next visit
S12345 Tan Ah Sam 45.00 2 March 2015 8 June 2015
S34567 Lee Tao San 60.00 3 March 2015 -
S56783 Sam Ting 120.50 10 March 2015 31 March 2015
S98761 Anthony Chin 78.50 31 March 2015 30 April 2015

Sample Screen Output See image.

Question 3

The objectives of the question are to:

  • (a)implement the methods as per given class requirements
  • (b)write the test driver to test the given methods
  • (c)write file processing code

Examine the given class specification, data files, additional information and answer the questions that follows:

class Format
{
private:
int number;

public:
void writeFile();
};

Additional Information:

  • number The number of characters read in
  • void writeFile() Performs file processing code, reads in the data from input file, IN.dat and store the read-in data in an array of characters. The read-in data is formatted and written to OUT.dat file. See sample OUT.dat.

Input File(IN.dat)Output File (OUT.dat) See image.

(a)Define ifstream and ofstream.

(b)Using C++ code, develop the writeFile method, fulfilling the requirement stated in the Additional Information above.

(c)Write a test driver to test the following:

  • delare one instance of Format,
  • invoke the writeFile method

Question 4

The objectives of the question are to:

  • (a)assess students understanding of the concept of inheritance in C++.
  • (b)write code demonstrating their understanding of inheritance
  • (c)assess if students have understood the concept of dynamic binding.

Study the given class diagrams, additional information and answer questions that follow:

Pledge
amt1: float
Pledge();
Pledge(float = 1.00);
virtual add_pledge(float): void
virtual get_pledge():float;
virtual display():void
SpecialPledge
*amt2: float
SpecialPledge(float);
add_pledge(float): void
get_pledge():float
display():void

Additional Information:

  • Class : Pledge
    • Pledge() Pledge(float = 1.00) Default Constructor Overloaded Constructor with default values as indicated in function prototype.
    • virtual add_pledge(float): void Add pass-in argument to the data member, amt1.
    • virtual get_pledge():float Accessor fordata member, amt1. Returns this value.
    • virtual display():void Use cout to display the data member, amt1.
  • Class: Special Pledge
    • SpecialPledge(float) Constructor.Invokes the base class constructor with the pass-in argument.. Uses the new operator to declare amt2 as an array of type float. The array size is 12. Sets each amt2 element in the array to the pass-in argument.
    • add_pledge(float): void Prompts the user to enter the month. Add the pass- in argument to the selected months amt2 Element.
    • get_pledge():float Finds the grant total of the values in array, amt2. Uses a for loop to traverse each element in amt2. Adds the value in each element of amt2 to the grand total. Returns the grant total.
    • display():void Uses a for loop to display each element in amt2.

(a)Using C++ code, construct the class specification for

  • (i)Base class: Pledge
  • Derived class: SpecialPledge

(b)Write C++ code to implement the constructor and methods found in

  • (i)Pledge
  • SpecialPledge

(c)Write a test driver to fulfil the following requirements:

  • Declare a vector array to contain 2 Pledge objects
  • Use the new operator to instantiate two objects:
  • (i)one Pledge object with the value 200
  • (ii)one SpecialPledge object with the value 50
  • Invoke ALL the methods
  • See sample output (bold and highlighted) to see the testing of ALL methods.

Sample Output

Test Display Method
Pledge Object:

========================
Pledged Amount $:200

Special Pledge Object:
========================

Pledged Amount for month 1 $:50
Pledged Amount for month 2 $:50
Pledged Amount for month 3 $:50
Pledged Amount for month 4 $:50
Pledged Amount for month 5 $:50
Pledged Amount for month 6 $:50
Pledged Amount for month 7 $:50
Pledged Amount for month 8 $:50
Pledged Amount for month 9 $:50
Pledged Amount for month 10 $:50
Pledged Amount for month 11 $:50
Pledged Amount for month 12 $:50

Test add_pledge Method

Enter the pledged amount for Pledge Object:
========================================================
23

Enter the pledged amount for Special Pledge Object:
========================================================
78

Enter the month: 9


Test get_pledge Method

Pledged Amount
==============
Total for Pledge Amount: 223

Total for Special Pledge Amount: 678


Test Display Method
Pledge Object:

===============
Pledged Amount $:223

Special Pledge Object:
===============

Pledged Amount for month 1 $:50
Pledged Amount for month 2 $:50
Pledged Amount for month 3 $:50
Pledged Amount for month 4 $:50
Pledged Amount for month 5 $:50
Pledged Amount for month 6 $:50
Pledged Amount for month 7 $:50
Pledged Amount for month 8 $:50
Pledged Amount for month 9 $:128
Pledged Amount for month 10 $:50
Pledged Amount for month 11 $:50
Pledged Amount for month 12 $:50
Press any key to continue . . .
Academic Honesty!
It is not our intention to break the school's academic policy. Projects posted are only used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly fill out the form. Please provide a valid email address and we'll get back to you in less than 24 hours. We will be sending an invoice through PayPal upon confirmation. We are a non profit organization however we need an amount to keep this organization running, and to be able to complete our research and development.