C++ Answers

Questions answered by Experts: 9 913

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Search

(c) Consider the partial class declaration of an Employee class below:

class Employee: protected Person{
public:
//...
double Rate;
double GetRate() const;
double GetHours() const;
private:
double Hours;
//...
protected:
double Pay() const;
//...
};

Line 1: cout << E.GetName();
Line 2: cout << E.GetHours();
Line 3: cout << E.Address;
Line 4: cout << E.GetRate();
Line 5: cout << E.Rate;
Line 6: cout << E.Pay();

(ii) Explain why the following implementation of function Pay()would result in a
compilation error.
(c) Consider the partial class declaration of an Employee class below:
class Employee: protected Person{
public:

//...
double Rate;
double GetRate() const;
double GetHours() const;
private:
double Hours;
//...
protected:
double Pay() const;
//...
};

(i) The code fragment below (extracted from a main() function) has three invalid
statements. Write down the line numbers of the invalid statements and explain
why each of these statements is invalid. E is an instance of an Employee
object.

Line 1: cout << E.GetName();
Line 2: cout << E.GetHours();
Line 3: cout << E.Address;
Line 4: cout << E.GetRate();
Line 5: cout << E.Rate;
Line 6: cout << E.Pay();
Consider the following class definitions and answer the questions that follow:

class Date {
public:
Date(int day, int month, int year);
int GetYear() const;
private:
int Day;
int Month;
int Year;
};
class Person{
public:
Person(const string &name, const string &address, const
Date &date);
string GetAddress() const;
string GetName() const;
private:
string Name;
string Address;
Date DateOfBirth;
};
class Customer{
public:
Customer(const string &name, const string &address,
int day, int month, int year,double creditlimit);
void IncreaseLimit(double amount);
string GetName() const;
string GetAddress() const;
void SetBalance(double balance);
double GetBalance() const;
private:
string Name;
string Address;
int DayOfBirth;
int MonthOfBirth;
int YearOfBirth;
double CreditLimit;
double BalanceDue;
};

(b) Implement the constructor for the derived Customer class.
Consider the following class definitions and answer the questions that follow:

class Date {
public:
Date(int day, int month, int year);
int GetYear() const;
private:
int Day;
int Month;
int Year;
};
class Person{
public:
Person(const string &name, const string &address, const
Date &date);
string GetAddress() const;
string GetName() const;
private:
string Name;
string Address;
Date DateOfBirth;
};
class Customer{
public:
Customer(const string &name, const string &address,
int day, int month, int year,double creditlimit);
void IncreaseLimit(double amount);
string GetName() const;
string GetAddress() const;
void SetBalance(double balance);
double GetBalance() const;
private:
string Name;
string Address;
int DayOfBirth;
int MonthOfBirth;
int YearOfBirth;
double CreditLimit;
double BalanceDue;
};

(a) Provide an improved declaration for the Customer class that exploits the
principles of reusability. Provide the interface only.
Overload the +, - and * operators for objects of class Pairs in question 4, as
member functions. Also define the class Pairs as an ADT that uses separate files
for the interface and the implementation. Use separate compilation and the same
program as in question 4 to test these member functions.
The program below contains an incomplete recursive function raised_to_power().
The function returns the value of the first parameter number of type float raised to
the value of the second parameter power of type int for all values of power greater
than or equal to 0.

1. #include <iostream>
2. using namespace std;
3. float raised_to_power( )
4. {
5. if (power < 0)
6. {
7. cout << "\nError - can't raise to a negative power\n";
8. exit(1);
9. }
10. else if ( )
11. return ( );
12. else
13. return (number * raised_to_power(number, power - 1));
14. }
15. main()
16.
17. float answer = raised_to_power(4.0,3);
18. cout << answer;
19. return 0;
20.}

(a) Complete the function header in line 3.
(b) Using the fact that any value raised to the power of 0 is 1, complete the base
case in line 10 and 11.
(c) Why do we need a base case in a recursive function?
(d) What is the purpose of the general case?

A manufacturer uses 1000 bytes as the value of 1K bytes, 1000K bytes as the value of 1MB, 1000MB as the value of 1GB. However, in computer memory, 1KB is equal to 1024 bytes. So the actual storage on a 40GB hard drive is approximately 37.25GB. Write a program that prompts the user to enter the size of the hard drive specified by the manufacturer, on the hard drive box, and outputs the actual storage capacity of the hard drive.

Input

User will need to input the hard drive size in gigabytes. Input the size after the text "Enter hard drive size in giga bytes: ".

Output

The output will be the hard drive size and the actual hard drive storage capacity. The program shall output the hard drive size inputted by the user after the "Hard drive size = ". It'll be followed by the string "Actual hard drive storage capacity = " and the actual hard drive storage capacity computed.


Sample Output

Hard drive size = 704 GB

Actual hard drive storage capacity = 655.651 GB


Program

Create a program that asks for the student's score and the number of items in a laboratory exercise. It will then compute for the grade using the formula. I recommend you to study and review on our lesson on Datatypes and Variables (Identifier).

Input

The user is required to enter two (2) inputs where the first input is the student score and the second one is the total number of items.

Output

The program will display the solution or formula (Score / Total # of Items) x 60 + 40 followed by an equal sign then the equivalent laboratory grade with two decimal places of the student. (Refer to the sample output)

++++++++++++++++++++++++++++++

Sample Input #1

21 40

Sample Output #1

(21/40)x60+40=71.50


Program

Create a program that asks for the student's score and the number of items in a laboratory exercise. It will then compute for the grade using the formula.

Input

The user is required to enter two (2) inputs where the first input is the student score and the second one is the total number of items.

Output

The program will display the solution or formula (Score / Total # of Items) x 60 + 40 followed by an equal sign then the equivalent laboratory grade with two decimal places of the student. (Refer to the sample output)

++++++++++++++++++++++++++++++

Sample Input #1

15 20

Sample Output #1

(15/20)x60+40=85.00

++++++++++++++++++++++++++++++

Sample Input #2

21 40

Sample Output #2

(21/40)x60+40=71.50


Define a class named ‘Train’ representing following members:

Data members :- - Train Number

- Train Name

- Source

- Destination

- Journey Date

- Capacity

Member functions:

- Initialise members

- Input Train data

- Display data

Write a C++ program to test the train class.


LATEST TUTORIALS
APPROVED BY CLIENTS