c++ primer plus 第十章答案

1.这个文件名不要在意。。懒得去新建。。

sale.h

#ifndef SALE_H_INCLUDED
#define SALE_H_INCLUDED
#include 
class BankAccount
{
private:
    std::string name;
    int id;
    double deposit;
public:
    BankAccount(const std::string name_ = "Rail Gun",const int id_ = 0, double deposit_ = 0);
    void show() const;
    void input(double money);
    void output(double money);
};

#endif // SALE_H_INCLUDED
sale.cpp

#include 
#include "sale.h"
using namespace std;
BankAccount::BankAccount(const string name_, const int id_, double deposit_)
{
    name = name_;
    id = id_;
    deposit = deposit_;
}
void BankAccount::show() const
{
    cout << "name :   " << name << endl;
    cout << "id:   " << id << endl;
    cout << "deposit: " << deposit << endl;
}
void BankAccount::input(double money)
{
    deposit += money;
}
void BankAccount::output(double money)
{
    if(deposit < money)
        cout << "You have no enough money!" << endl;
    else
        deposit -= money;
}

main.cpp

#include 
#include "sale.h"

int main()
{
    BankAccount user1("Rail Gun", 111, 888);
    user1.show();
    user1.input(112);
    user1.output(1111);
    user1.show();
    return 0;
}

2.头文件

#ifndef SALE_H_INCLUDED
#define SALE_H_INCLUDED
#include 
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;
};
#endif // SALE_H_INCLUDED

函数实现

#include 
#include "sale.h"
#include 
using namespace std;

Person::Person(const string & ln, const char * fn)
{
    lname = ln;
    strcpy(fname, fn);
}
void Person::show() const
{
    cout << lname << ' ' << fname << endl;
}
void Person::FormalShow() const
{
    cout << fname << ' ' << lname << endl;
}

main.cpp

#include 
#include "sale.h"

int main()
{
    Person one;
    Person two("Smythecraft");
    Person three("Dimwiddy", "Sam");
    one.show();
    one.FormalShow();
    two.show();
    two.FormalShow();
    three.show();
    three.FormalShow();
    return 0;
}

3.golf.h

#ifndef GOLF_H_INCLUDED
#define GOLF_H_INCLUDED
const int Len = 40;
class golf
{
private:
    char fullname[Len];
    int handicap;
public:
    golf(const char * name = "", int hc = 0);
    int setgolf();
    void Handicap(int hc);
    void showgolf();
};

#endif // GOLF_H_INCLUDED
golf.cpp

#include 
#include "golf.h"
#include 
using namespace std;
golf::golf(const char * name, int hc)
{
    strcpy(fullname, name);
    handicap = hc;
}
int golf::setgolf()
{
    cout << "Please enter the fullname";
    if(cin.getline(fullname,40) && strlen(fullname) != 0)
    {
        cout << "Please enter the number";
        cin >> handicap;
        cin.get();
        return 1;
    }
    else
    {
        cout << "error";
        return 0;
    }
}
void golf::Handicap(int hc)
{
    handicap = hc;
}
void golf::showgolf()
{
    cout << fullname << ' ';
    cout << handicap;
    cout << endl;
}
main.cpp

#include 
#include "golf.h"
using namespace std;
int main()
{
    golf test;
    if(!test.setgolf())
            return 0;
    golf otherone("hello world!", 3);
    otherone.showgolf();
    otherone.Handicap(888);
    otherone.showgolf();
}

4.这个搞的是真的脑壳晕。。命名空间什么的类什么的都一毛一样。。main.cpp的写的什么不要太在意,我已经晕了T A T,头文件和实现倒是都改过来了_(:зゝ∠)_

sale.h

#ifndef SALE_H_INCLUDED
#define SALE_H_INCLUDED
namespace SALES
{
    const int QUARTERS = 4;
    class Sales
    {
    private:
        double sales[QUARTERS];
        double average;
        double max;
        double min;
    public:
        Sales(const double ar[], int n);
        Sales();
        void showSales() const;
    };
}



#endif // SALE_H_INCLUDED

sale.cpp

#include "sale.h"
#include 
using namespace std;
namespace SALES
{
    Sales::Sales(const double ar[], int n)
    {
        int sum = 0;
        int avg, max ,min;
        for(int i = 0; i < n; i++)
        {
            sales[i] = ar[i];
            sum += sales[i];
        }
        max = min = sales[0];
        for(int i = 1; i < n; i++)
        {
            max = max > sales[i] ? max : sales[i];
            min = min < sales[i] ? min : sales[i];
        }
        avg = sum / n;
    }
    Sales::Sales()
    {
        cout << "Please enter sales[i]" << endl;
        double max = 999;
        double min = -999;
        double avg = 0;
        for(int i = 0 ; i < 4 ; ++i)
        {
            cin >> sales[i];
            max = max < sales[i] ? sales[i] : max;
            min = min > sales[i] ? sales[i] : min;
            avg += sales[i];
        }
        max = max;
        min = min;
        average = avg / 4;

    }
    void Sales::showSales() const
    {
        for(int i = 0 ; i < 4 ; ++i)
            cout << sales[i] << endl;
        cout << "MAX: " << max << endl << "MIN: " << min  << endl << "AVG: " << average << endl;
    }
}

main.cpp

#include 
#include "sale.h"

int main()
{
    double a[3] = {1.1, 2.2, 3.3};
    SALES::Sales x(a, 3);
    x.showSales();
    SALES::Sales y;
    y.showSales();
    return 0;
}

5.

我明明昨天写着放这的,不见了!!!(>_<)MD,不写了,好不容易才写出来的啊!!!!!

6.test.h

#ifndef TEST_H_INCLUDED
#define TEST_H_INCLUDED
class Move
{
private:
    double x;
    double y;
public:
    Move(double a = 0, double b = 0);
    void showmove() const;
    Move add(const Move & m) const;
    void reset(double a = 0, double b = 0);
};
#endif // TEST_H_INCLUDED
test.cpp

#include "test.h"
#include 
Move::Move(double a, double b)
{
    x = a;
    y = b;
}
void Move::showmove() const
{
    std::cout << "X: " << x << std::endl;
    std::cout << "Y: " << y << std::endl;
    std::cout << "______________________________________" << std::endl;
}
Move Move::add(const Move & m) const
{
    Move temp((*this).x + m.x, (*this).y + m.y);
    return temp;
}
void Move::reset(double a, double b)
{
    x = a;
    y = b;
}
main.cpp
#include 
#include "test.h"

int main()
{
    Move emm(1, 2);
    emm.showmove();
    Move aha(2, 1);
    aha.showmove();
    emm.add(aha).showmove();
    emm.reset(8, 8);
    emm.showmove();
    return 0;
}

7.test.h
#ifndef TEST_H_INCLUDED
#define TEST_H_INCLUDED

class Plorg{
private:
    char name[19];
    int CI;
public:
    Plorg();
    void changePlorg(char *name_, int CI_);
    void showPlorg();
};
#endif // TEST_H_INCLUDED
test.cpp

#include 
#include "test.h"
#include "cstring"
Plorg::Plorg()
{
    strcpy(name, "Plorg");
    CI = 50;
}
void Plorg::changePlorg(char *name_, int CI_)
{
    strcpy(name,name_);
    CI = CI_;
}
void Plorg::showPlorg()
{
    std::cout << name << ' ' << CI << std::endl;
}
main.cpp

#include 
#include "test.h"
int main()
{
    Plorg a;
    a.showPlorg();
    a.changePlorg("T_T",666);
    a.showPlorg();
    return 0;
}












你可能感兴趣的:(c++,primer,plus)