Question

The class declaration (interface) and the function definitions (implementation) must be in separate files - the...

The class declaration (interface) and the function definitions (implementation) must be in separate files - the interface or "header" file has a .hpp extension and the implementation has a .cpp extension. As usual, all data members should be private.

Write a class called BankAccount that has:

a string data member called customerName, a string data member called customerID, and a double data member called customerBalance

a constructor that takes two strings and a double (name, ID, balance) and uses them to initialize the data members. The data members of this class do not need to be set after they are initialized, so this class doesn't need any set methods - therefore the constructor will directly assign values to the data members instead of calling set methods to do it.

a get method for each data member

a method called withdraw that takes a double parameter and deducts it from the customer balance (the balance is allowed to be negative) - this method should not have a return value

a method called deposit that takes a double parameter and adds it to the current balance - this method should not have a return value

The BankAccount class might be used as follows:

BankAccount account1("Harry Potter", "K4637", 8032.78);
account1.withdraw(244.0);
account1.withdraw(3012.58);
account1.deposit(37.54);
account1.withdraw(1807.12);
account1.deposit(500.00);
double finalBalance = account1.getCustomerBalance();

BankAccount.hpp should have "include guards" as discussed on page 447.

BankAccount.cpp needs to #include BankAccount.hpp. When you include your own .hpp files (header files), put double quotes around them instead of angled brackets. (You should only #include .hpp files, not .cpp files.)

Put your main function in a separate file (this is the "client" code) and give it a name with a .cpp extension.

The file with your main method also needs to #include BankAccount.hpp.

If you named the file with your main method "bankAccountMain.cpp", then you can compile your program with "g++ BankAccount.cpp bankAccountMain.cpp -o bank", for example.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

BankAccount.hpp

#ifndef BANKACCOUNT_HPP
#define BANKACCOUNT_HPP
class BankAccount
{
public:
BankAccount();
BankAccount(string name,string accno,double bal);

// Function declarations
void withdraw(double amt);
void deposit(double amt);
double getCustomerBalance();
  
void setName(string name);
string getName();
void setAccountNo(string accNO);
string getAccountNo();
double getBalance();
void setbalance(double bal);
  

private:
// Declaring variables
string name;
string accno;
double bal;

};
#endif

______________

BankAccount.cpp
#include <iostream>
using namespace std;
#include "BankAccount.hpp"

BankAccount::BankAccount()
{
}

BankAccount::BankAccount(string name, string accno, double bal)
{
this->name = name;
this->accno = accno;
this->bal = bal;
}

void BankAccount::withdraw(double amt)
{
this->bal = bal - amt;
}
void BankAccount::deposit(double amt)
{
this->bal = bal + amt;
}
double BankAccount::getCustomerBalance()
{
return bal;
}

void BankAccount::setName(string name)
{
this->name = name;
}
string BankAccount::getName()
{
return name;
}

void BankAccount::setAccountNo(string accNO)
{
this->accno = accNO;
}
string BankAccount::getAccountNo()
{
return accno;
}
double BankAccount::getBalance()
{
return bal;
}
void BankAccount::setbalance(double bal)
{
this->bal = bal;
}

__________________

main.cpp


#include <iostream>
using namespace std;
#include "BankAccount.hpp"

int main()
{
BankAccount account1("Harry Potter", "K4637", 8032.78);
account1.withdraw(244.0);
account1.withdraw(3012.58);
account1.deposit(37.54);
account1.withdraw(1807.12);
account1.deposit(500.00);
double finalBalance = account1.getCustomerBalance();

cout<<"Final balance is :$"<<finalBalance<<endl;
}

________________

Output:


_____________Could you rate me well.Plz .Thank You

Add a comment
Know the answer?
Add Answer to:
The class declaration (interface) and the function definitions (implementation) must be in separate files - the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1a. Create a class named Computer - Separate declaration from implementation (i.e. Header and CPP files)...

    1a. Create a class named Computer - Separate declaration from implementation (i.e. Header and CPP files) 1b. Add the following private member variables - an int variable named year - a string variable named model - a string variable named purpose 1c Add the following public functions: - Default constructor which sets numeric members to -1 and string members to the empty string - Destructor to print "Removing instance from memory" - A non-default constructor to set the year and...

  • c++ please    Define the class bankAccount to implement the basic properties of a bank account....

    c++ please    Define the class bankAccount to implement the basic properties of a bank account. An object of this class should store the following data: Account holder’s name (string), account number (int), account type (string, checking/saving), balance (double), and interest rate (double). (Store interest rate as a decimal number.) Add appropriate member func- tions to manipulate an object. Use a static member in the class to automatically assign account numbers. Also declare an array of 10 compo- nents of...

  • 1a. Create a class named Inventory - Separate declaration from implementation (i.e. Header and CPP files)...

    1a. Create a class named Inventory - Separate declaration from implementation (i.e. Header and CPP files) 1b. Add the following private member variables - a int variable named itemNumber - an int variable named quantity - a double variable named cost - a double variable named totalCost. Total cost is defined as quantity X cost. - updateTotalCost() which takes no arguments and sets the values of the variable totalCost. 1c. Add the following public functions: - Accessor and Mutators for...

  • C++ Design a class bankAccount that defines a bank account as an ADT and implements the...

    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...

  • In C++ Write a program that contains a BankAccount class. The BankAccount class should store the...

    In C++ Write a program that contains a BankAccount class. The BankAccount class should store the following attributes: account name account balance Make sure you use the correct access modifiers for the variables. Write get/set methods for all attributes. Write a constructor that takes two parameters. Write a default constructor. Write a method called Deposit(double) that adds the passed in amount to the balance. Write a method called Withdraw(double) that subtracts the passed in amount from the balance. Do not...

  • Design a class that contains: we have to make 3 files header file , one .cpp...

    Design a class that contains: we have to make 3 files header file , one .cpp file for function and one more main cpp file 9.3 (The Account class) Design a class named Account that contains: An int data field named id for the account. A double data field named balance for the account. A double data field named annualInterestRate that stores the current interest rate. A no-arg constructor that creates a default account with id 0, balance 0, and...

  • Objectives: 1. Classes and Data Abstraction?2. User-defined classes?3. Implementation of a class in separate files Part...

    Objectives: 1. Classes and Data Abstraction?2. User-defined classes?3. Implementation of a class in separate files Part 1: In this assignment, you are asked: Stage1:?Design and implement a class named memberType with the following requirements: An object of memberType holds the following information: • Person’s first name (string)?• Person’s last name (string)?• Member identification number (int) • Number of books purchased (int)?• Amount of money spent (double)?The class memberType has member functions that perform operations on objects of memberType. For the...

  • Implement the following class in interface and implementation files. A separate file (the main project file)...

    Implement the following class in interface and implementation files. A separate file (the main project file) shall have the main function that exercises this class. Create a class named Student that has three member variables: name - string that stores the name of the student numClasses - integer that tracks how many courses the student is currently enrolled in, this number must be between 1 and 100 ClassList - an array of strings of size 100 used to store the...

  • Requirements:  Your Java class names must follow the names specified above. Note that they are...

    Requirements:  Your Java class names must follow the names specified above. Note that they are case sensitive. See our template below.  BankAccount: an abstract class.  SavingAccount: a concrete class that extends BankAccount. o The constructor takes client's firstname, lastname and social security number. o The constructor should generate a random 6-digit number as the user account number. o The initial balance is 0 and the annual interest rate is fixed at 1.0% (0.01).o The withdraw method signature...

  • Here is an example of how you separate your code into different files add the header...

    Here is an example of how you separate your code into different files add the header file and the .cpp file to the project. ------------------------------------------------------------------ //Header file -- saved as ex1.hpp #pragma once class ex1 { public: ...ex1(){} ...void print(); }; ------------------------------------------------------------------ //.cpp file -- saved as ex1.cpp #include "ex1.hpp" #include void ex1::print() { ...std::cout<<"Hello"< } ------------------------------------------------------------------ //main -- saved as main.cpp #include "ex1.hpp" int main(void) { ...ex1 e(); ...e.print(); ...return 0; } ------------------------------------------------------------------ Why do I have the #pragma...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT