C++Primer Plus(第六版)第十章编程练习:

本文介绍了C++ Primer Plus第六版第十章的编程练习,涉及string类型使用、结构体转换为类、命名空间应用等知识点,对部分题目进行了简要解析,如第二题和第四题因与前题相似未详述,第七题也被认为相对简单。对于暂时不理解的抽象数据类型,作者计划后续再进行学习和解答。
摘要由CSDN通过智能技术生成

第一题:
知识点:

  • string类型的使用也是在namespace std中的,要使用std命名空间
    head
#include <string>
#ifndef GOLF_H_INCLUDED
#define GOLF_H_INCLUDED

class Bankaccount
{
    std::string m_name;
    std::string m_account;
    double m_deposit;
public:
    Bankaccount(std::string name, std::string account, double deposit)
    {
        m_name = name;
        m_account = account;
        m_deposit = deposit;
    }
    Bankaccount(){}
    void Show();
    void Indeposit(double money);
    void Outdeposit(double money);
};

#endif // GOLF_H_INCLUDED

definition

#include <iostream>
#include "golf.h"


void Bankaccount::Show()
{
    using namespace std;

    cout << m_name << "\n";
    cout << m_account << "\n";
    cout << fixed << cout.precision(2)<< m_deposit << "\n";
}

void Bankaccount::Indeposit(double money)
{
        m_deposit += money;
}

void Bankaccount::Outdeposit(double money)
{
        m_deposit -= money;
}

main

#include <iostream>
#include "golf.h"

int main()
{
    Bankaccount User1("Tony Stark", "3050006", 36000.50);
    User1.Indeposit(500);
    User1.Outdeposit(200);
    User1.Show();

    return 0;
}

第二题:
知识点:

  • 没什么东西
    head
#include <string>
#include <cstring>
#ifndef GOLF_H_INCLUDED
#define GOLF_H_INCLUDED

class Person
{
    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")
{
    lname = ln;
    strcpy(fname, fn);
}
    void Show() const;
    void FormalShow() const;
};
#endif // GOLF_H_INCLUDED

difination

#include <iostream>
#include "golf.h"




void Person::Show()const
{
    using namespace std;
    cout << lname << " ";
    cout << fname << "\n";
}

void Person::FormalShow()const
{
    std::cout << fname << " ";
    std::cout << lname << "\n";
}

main

#include <iostream>
#include "golf.h"

int main()
{
    Person one;
    Person two("Smythecraft");
    Person three("Dimwiddy", "Sam");
    one.Show();
    one.FormalShow();
    std::cout << std::endl;
    two.Show();
    two.FormalShow();
    std::cout << std::endl;
    three.Show();
    three.FormalShow();

    return 0;
}

第三题:
知识点

  • 就是把结构体换成类,没什么意思
    改的地方就是类定义和构造函数,具体如下
#ifndef GOLF_H_INCLUDED
#define GOLF_H_INCLUDED
const int Len = 40;
class golf
{
    char fullname[Len];
    int handicap;
public:
    golf(const char * name, int hc);
    golf();
    void Handicap(int hc);
    void showgolf()const;
};

#endif // GOLF_H_INCLUDED

// contain function called in file1
#include <iostream>
#include "golf.h"
#include <cstring>

golf::golf(const char * name, int hc)
{
    strcpy(fullname, name);
    handicap = hc;
}

golf::golf()
{
    using namespace std;
    cout << "Enter the name and the value:"<< endl;
    cin.getline(fullname,Len);
    cin >> handicap;
    cin.get();
}

void golf::showgolf()const
{
    std::cout << fullname << std::endl;
    std::cout << handicap << std::endl;
}

void golf::Handicap(int hc)
{
    handicap = hc;
}

第四题:
知识点

  • 和上一题太像,不做了,就是放在命名空间里定义而已

第七题:
知识点

  • 其实也很简单

head

#ifndef GOLF_H_INCLUDED
#define GOLF_H_INCLUDED
#include <cstring>
class Plorg
{
    char m_name[19];
    int m_CI;
public:
    Plorg(const char *name , int CI = 50);
    Plorg() {strcpy(m_name, "Plorga"); m_CI = 50;}
    void setCI(int CI);
    void showCI();

};

#endif // GOLF_H_INCLUDED

definition

// contain function called in file1
#include <iostream>
#include <cstring>
#include "golf.h"
Plorg::Plorg(const char *name , int CI)
{
    strcpy(m_name, name);
    m_CI = CI;
}
void Plorg::setCI(int CI)
{
    m_CI = CI;
}

void Plorg::showCI()
{
    std::cout << m_name << " CI = " << m_CI << "\n";
}

main

#include <iostream>
#include "golf.h"

int main()
{
    Plorg test1;
    Plorg test2("Romance", 60);
    test1.showCI();
    test1.setCI(40);
    test1.showCI();
    test2.showCI();
    return 0;
}

暂时抽象数据类型的不太懂,过后再回来做

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值