Question

In: Computer Science

create a class in C++ having the following 4 private members: one double variable representing the...

 
 
create a class in C++ having 
the following 4 private members:
one double variable representing the balance of a bank account
on string variable representing the bank account number
a function to deposit money from the bank account
a function to withdraw money from the bank account
the following 2 public members:
-two wrapper functions.
- one parameterless constructor
- one constructor initializing the account balance and account number

- sample:

#include <iostream>
using namespace std;

class account
{
    public:
    account();
    account(string s, double m);
    
    void setMoney(double b);
    void getMoney(double b);
    
    private:
    double money;
    string accountNo;
    
    void withdraw(double b);
    void deposit(double b);
    
};

    account::account()
    {
       money=0;
       accountNo="12345";
    }
    account::account(string s, double m)
    {
       money=m;
       accountNo=s;       
    }
    
    void  account::withdraw(double b)
    {
        if(money<b)
        {
           cout<<"not enough"<<endl;
        }
        else
        {
            money = money-b;
        }
        cout<<"withdraw your new balance is "<<money<<endl;
    }
    void account::setMoney(double b)
    {
        deposit(b);
    }
    void  account::getMoney(double b)
    {
        withdraw(b); 
    }
    
    void  account::deposit(double b)
    {
        money = money+b;
        cout<<"deposit: your new balance is "<<money<<endl; 
    }

int main() 
{
   account a;
   
    a.setMoney(1000);
    a.getMoney(500);
    
   account b("23456", 1234);
    b.setMoney(2000);
    b.getMoney(600);
   
   return 0;
}
 

Solutions

Expert Solution

#include <iostream>
using namespace std;

class account
{
public:
account();
account(string s, double m);
  
void setMoney(double b);
void getMoney(double b);
  
private:
double money;
string accountNo;
  
void withdraw(double b);
void deposit(double b);
  
};

account::account()
{
money=0;
accountNo="12345";
}
account::account(string s, double m)
{
money=m;
accountNo=s;   
}
  
void account::withdraw(double b)
{
if(money<b)
{
cout<<"not enough"<<endl;
}
else
{
money = money-b;
}
cout<<"withdraw your new balance is "<<money<<endl;
}
void account::setMoney(double b)
{
deposit(b);
}
void account::getMoney(double b)
{
withdraw(b);
}
  
void account::deposit(double b)
{
money = money+b;
cout<<"deposit: your new balance is "<<money<<endl;
}

int main()
{
account a;

a.setMoney(1000);
a.getMoney(500);
  
account b("23456", 1234);
b.setMoney(2000);
b.getMoney(600);

return 0;
}

Expected output:


Related Solutions

Write in C++ language. (Employee Record): Create a class named 'Staff' having the following members: Data...
Write in C++ language. (Employee Record): Create a class named 'Staff' having the following members: Data members - Id – Name - Phone number – Address - AgeIt also has a function named 'printSalary' which prints the salary of the staff.Two classes 'Employee' and 'Officer' inherits the 'Staff' class. The 'Employee' and 'Officer' classes have data members 'Top Skill' and 'department' respectively. Now, assign name, age, phone number, address and salary to an employee and a officer by making an...
C++ Create a class for working with fractions. Only 2 private data members are needed: the...
C++ Create a class for working with fractions. Only 2 private data members are needed: the int numerator of the fraction, and the positive int denominator of the fraction. For example, the fraction 3/7 will have the two private data member values of 3 and 7. The following methods should be in your class: a. A default constructor that should use default arguments in case no initializers are included in the main. The fraction needs to be stored in reduced...
I. Design and code a class Rectangle that has the following properties: Two private members double...
I. Design and code a class Rectangle that has the following properties: Two private members double length and width A constructor that accepts two parameters for the private members Default constructor that sets both members to 0 Setters/getters for each private member Method area() that returns the area of the rectangle Method Perim() that returns the perimeter of the rectangle Method toString that return the param as a String Method Add that accepts a Rectangle and returns a rectangle with...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y;...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y; public MyPoint() { this(0, 0); } public MyPoint(double x, double y) { this.x = x; this.y = y; } // Returns the distance between this MyPoint and other public double distance(MyPoint other) { return Math.sqrt(Math.pow(other.x - x, 2) + Math.pow(other.y - y, 2)); } // Determines if this MyPoint is equivalent to another MyPoint public boolean equals(MyPoint other) { return this.x == other.x &&...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y;...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y; public MyPoint() { this(0, 0); } public MyPoint(double x, double y) { this.x = x; this.y = y; } // Returns the distance between this MyPoint and other public double distance(MyPoint other) { return Math.sqrt(Math.pow(other.x - x, 2) + Math.pow(other.y - y, 2)); } // Determines if this MyPoint is equivalent to another MyPoint public boolean equals(MyPoint other) { return this.x == other.x &&...
Create a class called Car (Car.java). It should have the following private data members: • String...
Create a class called Car (Car.java). It should have the following private data members: • String make • String model • int year Provide the following methods: • default constructor (set make and model to an empty string, and set year to 0) • non-default constructor Car(String make, String model, int year) • getters and setters for the three data members • method print() prints the Car object’s information, formatted as follows: Make: Toyota Model: 4Runner Year: 2010 public class...
Create a class called Car (Car.java). It should have the following private data members: • String...
Create a class called Car (Car.java). It should have the following private data members: • String make • String model • int year Provide the following methods: • default constructor (set make and model to an empty string, and set year to 0) • non-default constructor Car(String make, String model, int year) • getters and setters for the three data members • method print() prints the Car object’s information, formatted as follows: Make: Toyota Model: 4Runner Year: 2010 public class...
I. Create the class Item with the following members: 1. id, a protected variable of type...
I. Create the class Item with the following members: 1. id, a protected variable of type int 2. name, a protected variable of type string 3. price, a protected variable of type double 4. a public non-default constructor which accepts three parameters to initialize the variables above 5. a copy constructor 6. overload the = operator such that both operands are of type Item 7. overload the = operator such that the right operand is of type double. Assign the...
I. Create the class Item with the following members:   1. id, a protected variable of type...
I. Create the class Item with the following members:   1. id, a protected variable of type int   2. name, a protected variable of type string   3. price, a protected variable of type double   4. a public non-default constructor which accepts three parameters to initialize the variables above II. Create the class Chair. The class publicly inherits from Item and has the following members   1. numLegs, a private variable of type int   2. a non-default constructor to initialize numLegs, id, name,...
Here is the assignment description. * Create a class named 'Account' having the following private attributes...
Here is the assignment description. * Create a class named 'Account' having the following private attributes int accountNumber; double balance; * Write a constructor with parameters for each of the attributes. * Write another constructorwith one parameter for the accountNumber. * Write getter and setter methods for each of the private attributes. * Write a method void credit(double amount) which adds the given amount to the balance. * Write a method void debit(double amount) which subtracts the given amount from...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT