Question

In: Computer Science

4. Create a new project AccountPolymorphism and copy and paste the code from the attached AccountPolymorphism.cpp...

4. Create a new project AccountPolymorphism and copy and paste the code from the attached AccountPolymorphism.cpp file. Compile and run. You will see that it is not showing output for Base class pointer to base class object data, not showing derived class output completely(missing word "Saving" in the output) and not showing any output for Base class pointer to derived class object(Saving). Fix these issue and submit the output.

// AccountPolymorphism.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

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

class Account
{
private:
int id;
double balance;
double annualInterestRate;

public:
Account()
{
id = 0;
balance = 0;
annualInterestRate = 0;
}

Account(int id, double balance, double annualInterestRate)
{
this->id = id;
this->balance = balance;
this->annualInterestRate = annualInterestRate;
}

  
double getMonthlyInterest() const
{
return balance * (annualInterestRate / 1200);
}

void withdraw(double amount)
{
balance -= amount;
}

void deposit(double amount)
{
balance += amount;
}

virtual string toString() const
{
stringstream ss;
ss << "Account id: " << id << " balance: " << balance;
return ss.str();
}
};

class Checkings : public Account
{
protected:
int overdraftLimit;

public:
Checkings(int id, double balance, double annualInterestRate) :Account(id, balance, annualInterestRate)
{
overdraftLimit = 5000;
}

string toString(Account Acct) const
{
stringstream ss;

cout << Acct.toString() << endl;
ss << "Account Type : " << "Checking" << endl;
return ss.str();
}
};

class Saving : public Account
{
protected:
int overdraftLimit;

public:

Saving(int id, double balance, double annualInterestRate) :Account(id, balance, annualInterestRate)
{
overdraftLimit = 5000;
}

string toString(Account Acct) const
{
stringstream ss;

cout << Acct.toString() << endl;
ss << "Account Type : " << "Saving" << endl;
return ss.str();
}
};

int main()
{
// Saving saving;
// Checkings checking;

cout << "Testing Account Class object " << endl << endl;
Account TestAccount{ 121,2000.00,0.5 };
cout << TestAccount.toString() << endl;
cout << "Depositing $1000 to Account object" << endl << endl;
TestAccount.deposit(1000.00);
cout << "Amount after depositing $1000 to Account object " << endl << endl;
cout << TestAccount.toString() << endl;
cout << "Withdrawing $2000 from the Account object " << endl << endl;
TestAccount.withdraw(2000.00);
cout << "Amount after witdrawing $2000 from the Account object " << endl << endl;
cout << TestAccount.toString() << endl << endl;

cout << "Creating Savings object " << endl << endl;
Saving saving{ 122, 44000, 0.5 };
cout << saving.toString(saving) << endl;

cout << "Creating Checking object " << endl << endl;
Checkings checking{ 444, 88000, 0.8 };
cout << checking.toString(checking) << endl << endl;

// Using Base Class pointer at Base Class object

const Account* AccountPtr{ &TestAccount };
cout << "Base Class pointer at Base Class object" << endl << endl;
AccountPtr->toString();
cout << endl << endl;

// Using Derived Class pointer at Derived Class object

const Saving* SavingPtr{ &saving };
cout << "Derived Class pointer at Derived Class object" << endl << endl;
SavingPtr->toString(saving);
cout << endl << endl;

// Using Base Class pointer at Derived Class object

cout << "Base Class pointer at Derived Class object" << endl << endl;
AccountPtr = &saving;
AccountPtr->toString();
cout << endl << endl;

cout << "" << endl;

system("pause");

return 0;
}

Solutions

Expert Solution

Program needs to be corrected :

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

class Account
{
private:
int id;
double balance;
double annualInterestRate;

public:
Account()
{
id = 0;
balance = 0;
annualInterestRate = 0;
}

Account(int id, double balance, double annualInterestRate)
{
this->id = id;
this->balance = balance;
this->annualInterestRate = annualInterestRate;
}

  
double getMonthlyInterest() const
{
return balance * (annualInterestRate / 1200);
}

void withdraw(double amount)
{
balance -= amount;
}

void deposit(double amount)
{
balance += amount;
}

string toString() const // no need of virtual keyword since no functions with same prototype exist in child class
{
stringstream ss;
ss << "Account id: " << id << " balance: " << balance;
return ss.str();
}
};

class Checkings : public Account
{
protected:
int overdraftLimit;

public:
Checkings(int id, double balance, double annualInterestRate) :Account(id, balance, annualInterestRate)
{
overdraftLimit = 5000;
}

string toString(Account Acct) const
{
stringstream ss;

cout << Acct.toString() << endl;
ss << "Account Type : " << "Checking" << endl;
return ss.str();
}
};

class Saving : public Account
{
protected:
int overdraftLimit;

public:

Saving(int id, double balance, double annualInterestRate) :Account(id, balance, annualInterestRate)
{
overdraftLimit = 5000;
}

string toString(Account Acct) const
{
stringstream ss;

cout << Acct.toString() << endl;
ss << "Account Type : " << "Saving" << endl;
return ss.str();
}
};

int main()
{
// Saving saving;
// Checkings checking;

cout << "Testing Account Class object " << endl << endl;
Account TestAccount{ 121,2000.00,0.5 };
cout << TestAccount.toString() << endl;
cout << "Depositing $1000 to Account object" << endl << endl;
TestAccount.deposit(1000.00);
cout << "Amount after depositing $1000 to Account object " << endl << endl;
cout << TestAccount.toString() << endl;
cout << "Withdrawing $2000 from the Account object " << endl << endl;
TestAccount.withdraw(2000.00);
cout << "Amount after witdrawing $2000 from the Account object " << endl << endl;
cout << TestAccount.toString() << endl << endl;

cout << "Creating Savings object " << endl << endl;
Saving saving{ 122, 44000, 0.5 };
cout << saving.toString(saving) << endl;

cout << "Creating Checking object " << endl << endl;
Checkings checking{ 444, 88000, 0.8 };
cout << checking.toString(checking) << endl << endl;

// Using Base Class pointer at Base Class object

const Account *AccountPtr =&TestAccount ;
cout << "Base Class pointer at Base Class object" << endl << endl;
cout<<AccountPtr->toString(); //cout is used because here toString() is returning a string object with a copy of the current contents of the stream
cout << endl << endl;

// Using Derived Class pointer at Derived Class object

const Saving* SavingPtr{ &saving };
cout << "Derived Class pointer at Derived Class object" << endl << endl;
SavingPtr->toString(saving);
cout << endl << endl;

// Using Base Class pointer at Derived Class object

cout << "Base Class pointer at Derived Class object" << endl << endl;
AccountPtr = &saving;
cout<<AccountPtr->toString();   //cout is used because here toString() is returning a string object with a copy of the current contents of the stream
cout << endl << endl;

cout << "" << endl;

system("pause");

return 0;
}

Output :

Testing Account Class object

Account id: 121 balance: 2000
Depositing $1000 to Account object

Amount after depositing $1000 to Account object

Account id: 121 balance: 3000
Withdrawing $2000 from the Account object

Amount after witdrawing $2000 from the Account object

Account id: 121 balance: 1000

Creating Savings object

Account id: 122 balance: 44000
Account Type : Saving

Creating Checking object

Account id: 444 balance: 88000
Account Type : Checking


Base Class pointer at Base Class object

Account id: 121 balance: 1000

Derived Class pointer at Derived Class object

Account id: 122 balance: 44000


Base Class pointer at Derived Class object

Account id: 122 balance: 44000

Explanation :

  • The virtual keyword is not necessary here because the prototype used for function toString() in Account and its child classes namely Checking and Saving class are different.
  • Checking and Saving Class have toString(Account Acct) const definition while Account class has toString() const definition. Both are different functions not because the child contains one parameter for function toString() while the parent does not have any parameter for function toString().
  • So while defining toString() in Account Class, I have removed the virtual keyword.
  • LIne having statement   " AccountPtr->toString(); " is being replaced by " cout<<AccountPtr->toString(); ". The reason is that toString() of Account Class is returning a string object with a copy of the current contents of the stream which need to be printed.
  • Now Base Class pointer pointing to any object either of is own type or its chid type can use the toString() method to obtain a string object and print it.

Related Solutions

Download the attached file/s, copy and paste the code segment/s into your visual studio or any...
Download the attached file/s, copy and paste the code segment/s into your visual studio or any other C++ IDE and run it. You will have to implement a small intentional bug in your program // This program uses a function that returns a value. #include <iostream> using namespace std; // Function prototype int sum(int num1, int num2); int main() {    int value1 = 20,   // The first value        value2 = 40,   // The second value        total;         //...
Run the following R code (copy and paste) to create some data: out1 <- rep( c(0,0,1),...
Run the following R code (copy and paste) to create some data: out1 <- rep( c(0,0,1), 3 ) out2 <- rep( c(1,0,1), 3 ) counts <- c(18,17,15,20,10,20,25,13,12) This is a variation on the data in the first example on the “glm” help page in R. The counts variable is our response variable and will be modeled as a Poisson variable, the out1 predictor variable will measure the difference between outcomes 2 (baseline) and 3, and out2 will measure the difference...
It is straightforward to copy-paste code to achieve repetitive actions, what is the downside of such...
It is straightforward to copy-paste code to achieve repetitive actions, what is the downside of such an approach?
R studio questions Write up your answers and paste the R code Copy and paste all...
R studio questions Write up your answers and paste the R code Copy and paste all plots generated. First create a sample drawn from a normal random variable. R has many distributions for which you can get probabilities and draw random numbers. We are going to use the normal. Go to help in R and type in rnorm. You will see a write up for functions associated with the normal distribution. dnorm is the density; pnorm is the probability distribution...
Having a difficult time writing this code up. ( JAVA based ) Will Copy and paste...
Having a difficult time writing this code up. ( JAVA based ) Will Copy and paste the whole solution I was left with. Thank you in advance ! Lab 5 – Class Statistics Write a program which will uses the file Lab5Data.txt containing student names and the points they had earned at the end of the class. The program should use one or more arrays to create a report with the following information: -A table containing the student name, total...
JAVA Copy the attached code into your IDE or an online compiler and test an additional...
JAVA Copy the attached code into your IDE or an online compiler and test an additional type with the generic class. Submit your code and execution display. JAVA The test cases for Integer and String is given in the code. Please add test cases for Character, Boolean and Double type etc. // A Simple Java program to show working of user defined // Generic classes    // We use < > to specify Parameter type class Test<T> {     //...
Directly copy the source code and paste into the Word file. Screenshot of running result must...
Directly copy the source code and paste into the Word file. Screenshot of running result must be presented. 1. (20 points) Write the “Hello, world!” program. 2. (30 points) Write a program to calculate the sum from -5 to10. Use the for loop to do the calculation. 3. (20 points) Write a complete C++ program that asks the user to enter the necessary information about the cylinder, calculate the volume in a function (named as calculate_vol, using reference to pass...
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a...
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a package named threeDimensional and put all 3 of the classes discussed below in this package Include a header comment to EACH OF your files, as indicated in your instructions. Here's a link describing the header. Note that headers are not meant to be in Javadoc format. Note that Javadoc is a huge part of your grade for this assignment. Javadoc requires that every class,...
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a...
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a package named threeDimensional and put all 3 of the classes discussed below in this package Include a header comment to EACH OF your files, as indicated in your instructions. Here's a link describing the header. Note that headers are not meant to be in Javadoc format. Note that Javadoc is a huge part of your grade for this assignment. Javadoc requires that every class,...
Using R Studio Use the two iid samples. (You can copy and paste the code into...
Using R Studio Use the two iid samples. (You can copy and paste the code into R). They both come from the same normal distribution. X = c(-0.06, 1.930, 0.608 -0.133,0.657, -1.284, 0.166, 0.963, 0.719, -0.896) Y = c(0.396, 0.687, 0.809, 0.939, -0.381, -0.042, -1.529, -0.543, 0.758, -2.574, -0.160, -0.713, 0.311, -0.515, -2.332, -0.844, -0.942, 0.053, 0.066, 0.942, -0.861, -0.186, -0.947, -0.110, 0.634, 2.357, 0.201, -0.428, -1.661, 0.395) (a) Report 95% confidence interval for the mean of X. Should we...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT