cpp primier plus homework c10
程序员文章站
2022-07-13 23:53:52
...
//c10_01
//header
#ifndef ACCOUNT_H_
#define ACCOUNT_H_
#include <string>
class Account
{
//using std::string;
private:
std::string m_username;//prefix m_
std::string m_accname;
long double m_money;
public:
// void init();
void show_account()const;
void deposit(long double m);
void withdraw(long double n);
Account();//default constructor
Account(const std::string &username,const std::string &accname,long double money);//constructor
~Account();//destructor
};
#endif // ACCOUNT_H_
//memberfunc
#include<iostream>
#include"Account.h"
#include<string>
using std::cout;
using std::endl;
using std::string;
//typename std::string string;
void Account::show_account()const
{
cout<<"#Account owner: "<<m_username<<endl;
cout<<"#Account_name: "<<m_accname<<endl;
cout<<"#The money you have: "<<m_money<<endl;
cout<<"-------------------------------------"<<endl;
}
void Account::deposit(long double m)
{
m_money+=m;
}
void Account::withdraw(long double n)
{
m_money-=n;
}
//Account()
Account::Account(const string &username,const string &accname,long double money)
{
m_username=username;
m_accname=accname;
m_money=money;
}
Account::~Account()
{
}
//main
#include <iostream>
#include<string>
#include"Account.h"
int main(void)
{
using namespace std;
Account user1("w","Saury",100000);
user1.show_account();
user1.deposit(20000);
user1.show_account();
user1.withdraw(3000);
user1.show_account();
}
//c10_o2
//header
#ifndef PERSON_H_
#define PERSON_H_
#include<string>
using std::string;
class Person
{
private:
static const int LIMIT=25;
string lname;
char fname[LIMIT];
public:
Person() {lname="";fname[0]='\0';}
Person(const string &ln,const char*fn="Heyyou");
void show() const;
void FormalShow() const;
~Person();
};
#endif // PERSON_H_
//cpp
#include<iostream>
#include<string>
#include<cstring>
#include"Person.h"
using std::string;
using std::cout;
using std::endl;
Person::Person(const string &ln,const char*fn)
{
lname=ln;
strcpy(fname,fn);
}
//Person
void Person::show() const
{
cout<<fname<<" "<<lname;
}
void Person::FormalShow() const
{
cout<<lname<<" "<<fname;
}
Person::~Person()
{
}
//main
#include <iostream>
#include"Person.h"
#include<string>
using namespace std;
int main()
{
Person one;
Person two("Smythecraft");
Person three("Dimwiddy","Sam");
three.show();
cout<<endl;
three.FormalShow();
}