C++ Question
Modify the class by changing private members to protected members and viewing Account class as a base class. Define two derived classes Checking and RRSP. The Checking class has one additional private data: total(double). It has five public member functions: a constructor Checking (string n, string addr, double b, int p, double tot) (note you must call base constructor by using initialization list), double getTotal() to return total, void setTotal() to updata data, void withdraw(double w) which requires w ≤ total, if the condition is satisfied, the balance should be reduced by w dollars, otherwise, output message “invalid withdraw”. It also has a print function (first call base print function defined in Part 1 and then print Total: $1010.23). RRSP class has additional private data rate(double) and four public member functions: a constructor RRSP (string n, string addr, double b, int a, double r), void deposit(double w) which requires w ≤ total, if the condition is satisfied, then total reduced by w dollars, otherwise, output message “invalid withdraw”, void calInterest() to calculate the interest (=(balance-getTotal())*rate) and add it to the balance in base class (here we assume the total in RRSP account is balance – total). and a print function (first call base print function defined in Part 1 and then print Interest Rate: 2.5% \n Total: balance-getTotal()). In the main function, declare one Checking and one RRSP objects with some data. Call print function for both objects. Call withdraw function with some amount for Checking object and call deposit and calInterest functions for RRSP object. Call print function for both objects again.
CODE:
#include <iostream>
using namespace std;
//account class, BASE class
class Account
{
//defined all members protected
protected:
string n;
string addr;
double b;
int p;
//constructor of Account class
Account(string s1, string s2, double d, int
i)
{
n = s1;
addr = s2;
b = d;
p = i;
}
public:
//print function prints all the data
void print()
{
cout<<"Name:
"<<n<<endl
<<"Address: "<<addr<<endl
<<"b: "<<b<<endl
<<"p: "<<p<<endl;
}
};
//checking class
class Checking : public Account
{
//one extra variable "total"
double total;
public:
//parameterized constructor assigns the value of
total and pas the rest of them to account class constructor
Checking(string s1, string s2, double d, int i,
double t) : Account(s1, s2, d, i)
{
total = t;
}
//returns total
double getTotal()
{
return total;
}
//sets the value of total
void setTotal(double d)
{
total = d;
}
//prints all the data
void print()
{
Account::print();
cout<<"Total:
$"<<total<<endl;
}
//withdraw function withdraws from the total if
possible, otherwise prints an error message
void withdraw(double w)
{
if(w > total)
cout<<"invalid withdraw\n";
else
total -= w;
}
};
//RRSP class, CHILD class of Account class
class RRSP : public Account
{
//one extra variable "rate"
double rate;
public:
//parameterized constructor initializes rate,
passes rest to Account class constructor
RRSP(string s1, string s2, double d, int i,
double r) : Account(s1, s2, d, i)
{
rate = r;
}
//deposit function adds in "b" of account
class
void deposit(double d)
{
Account::b += d;
}
//calculates and adds interest in "b" of Account
class
void calIntrest()
{
double intrest =
Account::b * rate;
Account::b +=
intrest;
}
//prints all the details of RRSP variable
void print()
{
Account::print();
cout<<"Rate:
"<<rate<<endl;
}
};
int main()
{
// define one variable for each class
Checking c("John","washington DC", 12000, 25000,
35000);
RRSP r("Farad","washington DC", 12000, 25000,
0.02);
//print their value, then manipulate as asked
then again print their values
c.print();
r.print();
c.withdraw(2500);
r.deposit(2500);
r.calIntrest();
c.print();
r.print();
return 0;
}



OUTPUT:
Name: John
Address: washington DC
b: 12000
p: 25000
Total: $35000
Name: Farad
Address: washington DC
b: 12000
p: 25000
Rate: 0.02
Name: John
Address: washington DC
b: 12000
p: 25000
Total: $32500
Name: Farad
Address: washington DC
b: 14790
p: 25000
Rate: 0.02

Ask any questions if you have in comment section below.
Please rate the answer.
C++ Question Modify the class by changing private members to protected members and viewing Account class...
C++ Define the class HotelRoom. The class has the following private data members: the room number (an integer) and daily rate (a double). Include a default constructor as well as a constructor with two parameters to initialize the room number and the room’s daily rate. The class should have get/set functions for all its private data members [3pts]. The constructors and the set functions must throw an invalid_argument exception if either one of the parameter values are negative. The exception...
C++ Design a class bankAccount that defines a bank account as an ADT and implements the basic properties of a bank account. The program will be an interactive, menu-driven program. a. Each object of the class bankAccount will hold the following information about an account: account holder’s name account number balance interest rate The data members MUST be private. Create an array of the bankAccount class that can hold up to 20 class objects. b. Include the member functions to...
C++
Could you check my code, it work but professor said that
there are some mistakes(check virtual functions, prin() and others,
according assignment) .
Assignment:
My code:
#include<iostream>
#include<string>
using namespace std;
class BasicShape { //Abstract base class
protected:
double area;
private:
string name;
public:
BasicShape(double a, string n) {
area=a;
name=n;
}
void virtual calcArea()=0;//Pure Virtual Function
virtual void print() {
cout<<"Area of "<<getName()<<" is:
"<<area<<"\n";
}
string getName(){
return name;
}
};
class Circle : public...
Please show it in C++. Thank you!
Problem Definition Create an inheritance hierarchy containing base class Account and derived class Savings-Account. Base class Account should include one data member of type double to represent the account balance. The class should provide a constructor that receives an initial baiance and uses it to initialize the data member. The class should provide three member functions. Member function credit should add an amount to the current balance. Member function debit should withdraw money...
A hard c++ problem ◎Write a c++ program”Student.h”include Private data member, string firstName; string lastName; double GPA(=(4.0*NumberOfAs+3.0*NumberOfBs+2.0*NumberOfCs+1.0*NumberOfDs)/( As+Bs+Cs+Ds+Fs)); Public data member, void setFirstName(string name); string getFirstName() const; void printFirstName() const; void getLastName(string name); string getLastName() const; void printLastName() const; void computeGPA(int NumberOfAs,int NumberOfBs,int NumberOfCs,int NumberOfDs,int NumberOfFs); double getGPA() const; double printGPA() const; A destructor and an explicit default constructor initializing GPA=0.0 and checking if GPA>=0.0 by try{}catch{}. Add member function, bool operator<(const Student); The comparison in this function based on...
C++ design a class named Technician that contains private data members to store the following: -technician's name (a string) - number of service calls - total time of all service calls - average service call time the technician class should also have the following public member functions: - a constructor function that initializes all data members by obtaining their values from the users. with the exception of the average service call time which will be calculated as: total_time/number_of_call - a...
in c++
Define and implement the class Employee with the following requirements: private data members string type name a. b. double type hourlyRate 2. public member functions a. default constructor that sets the data member name to blank"and hourlyRate to zero b. A constructor with parameters to initialize the private data members c. Set and get methods for all private data members d. A constant method weeklyPay() that receives a parameter representing the number of hours the employee worked per...
C++
Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the class point that should have the following Private data members x and y (of type int), with default values of 0 A constant ID of type int A private static integer data member named numOfPoints This data member should be o Incremented whenever a new point object is created. o Decremented whenever a point object is destructed. A default constructor An...
In C++ Create an inheritance hierarchy that a bank might use to represent customers’ bank accounts. All customers at this bank can deposit (i.e., credit) money into their accounts and withdraw (i.e., debit) money from their accounts. More specific types of accounts also exist. Savings accounts, for instance, earn interest on the money they hold. Checking accounts, on the other hand, charge a fee per transaction (i.e., credit or debit). Create an inheritance hierarchy containing base class Account and derived...