C++primer Plus Programming Practice 10.10__linux

Source: Internet
Author: User

Programming Exercises:
1. Define a class to represent a bank account. Data members include names, accounts

Numbers (using strings) and deposits. member functions perform the following actions:
* Create an object and initialize it;
* Displays the name, account number and deposit of the depositor;
* Depositing the specified deposit in the parameter;
* Take out the specified amount of the parameter;
Write a small program to show all the features.
The procedure is written as follows:

File Bankaccount.h class Bankaccount {Private:char name[40];
    Char acctnum[25];
Double balance;
    Public:bankaccount (const char* client, const char* num, double bal = 0.0);
    void Show (void) const;
    void deposit (double cash);
void withdraw (double cash);

}; File Bankaccount.cpp #include "Bankaccount.h" #include <string.h> #include <iostream> Bankaccount::
    Bankaccount (const char* client, const char* num, double bal) {strncpy (name, client,39);
    Name[39]= ' ";
    strncpy (acctnum,num,24);
    Acctnum[24]= ' ";
    } void Bankaccount::show (void) const {using Std::cout;
cout<< "Name:" <<name<< "Acctnum:" <<acctnum<< "Balance:" <<balance<< "\ n";
    } void Bankaccount::d eposit (double cash) {using Std::cout; if (cash<0) cout<< "Number of balance deposited can" t be negative.

else Balance+=cash;
 } void Bankaccount::withdraw (double cash) {using Std::cout;   if (cash<0) cout<< "Number of balance withdrawed can" t be negative.
    "; else if (cash>balance) cout<< "can ' t withdraw more than you have."

"; File Bankaccount1.cpp #include <iostream> #include <cctype> #include "Bankaccount.h" int main () {using NAMESP
    Ace STD;
    Bankaccount banka1 ("Zhang San", "24536", 19804);
    Banka1.show ();
    Double cash1;
    Char ch; cout<< want to deposit or withdraw? "<<" Please choose D to deposit, choose W to withdraw money or choose Q to quit.
        while (Cin>>ch&&toupper (CH)!= ' Q ') {while (Cin.get ()!= ' \ n ') continue;
            if (!isalpha (ch)) {cout<< ' \a ';
        Continue Switch (CH) {case ' d ': Case ' d ': cout<< ' Please enter the number of Mon
            EY you deposit: ";
            Banka1.deposit (CASH1); banka1.show ();
            Case ' W ': Case ' W ': cout<< ' Please enter the number of your withdraw: ";
            Banka1.withdraw (CASH1);
            Banka1.show ();  
    } system ("Pause");
return 0;

The results of the program run meet the expected requirements.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.