//第一题
//count.h
#include <string>
class Count {
private:
std::string m_name;
std::string m_id;
double m_money;
public:
Count();
Count(const std::string name, const std::string id, const double money);
void disp() const;
void add(const double money);
void sub(const double money);
};
//count.cpp
#include <iostream>
#include "count.h"
Count::Count()
{
m_name = "";
m_id = "";
m_money = 0;
}
Count::Count(const std::string name, const std::string id, const double money)
{
m_name = name;
m_id = id;
m_money = money;
}
void Count::disp() const
{
std::cout << "Name: " << m_name << std::endl << "ID: " << m_id << std::endl << "Money: " << m_money << std::endl;
}
void Count::add(const double money)
{
m_money += money;
}
void Count::sub(const double money)
{
m_money -= money;
}
//main.cpp
#include "count.h"
int main()
{
Count a("Tree", "33673504", 66666.66);
a.disp();
a.add(666.66);
a.disp();
a.sub(666.66);
a.disp();
return 0;
}
//第二题
//Person.h
#include <string>
class Person {
private:
static const int LIMIT = 25;
std::string lname;
char fname[LIMIT];
public:
Person() { lname = ""; fname[0] = '\0'; }
Person(const std::string &ln, const char *fn = "Heyyou");
void Show() const;
void FormalShow() const;
};
//Person.cpp
#include "Person.h"
#include <iostream>
#include <cstring>
Person::Person(const std::string &ln, const char *fn)
{
lname = ln;
strcpy_s(fname, fn);
}
void Person::Show() const
{
std::cout << fname << " " << lname << std::endl;
}
void Person::FormalShow() const
{
std::cout << lname << ", " << fname << std::endl;
}
//main.cpp
#include "Person.h"
int main()
{
Person one;
Person two("Smythecraft");
Person three("Dimwiddy", "Sam");
one.Show();
one.FormalShow();
two.Show();
two.