第十章编程题

1

为复习题5描述的类提供方法定义,并编写一个小程序来演示所有的特性。

#include <iostream>
#include <cstring>
using namespace std;
class BankAccount
{
private:
    string name;
    string account;
    long long money;

public:
    BankAccount(string name = "", string account = "", long long money = 0)
    {
        this->name = name;
        this->account = account;
        this->money = money;
    }
    void display()
    {
        cout << "name: " << name << endl;
        cout << "account: " << account << endl;
        cout << "money: " << money << endl;
    }
    void add(int x)
    {
        money += x;
    }
    void get(int x)
    {
        money -= x;
    }
};
int main()
{
    // BankAccount myaccount = {"CUMT","101101",0};
    // BankAccount myaccount{"CUMT","101101",0};
    // BankAccount myaccount = BankAccount("CUMT","101101",0);
    BankAccount myaccount("CUMT", "101101", 10);
    myaccount.display();
    myaccount.add(666);
    myaccount.display();
    myaccount.get(100);
    myaccount.display();
    return 0;
}

在这里插入图片描述

2

注意构造函数的声明和定义的两个参数列表中,默认值不能重复出现 (重定义默认参数)

下面是一个非常简单的类定义:
在这里插入图片描述
它使用了一个string对象和一个字符数组,让您能够比较它们的用
法。请提供未定义的方法的代码,以完成这个类的实现。再编写一个使
用这个类的程序,它使用了三种可能的构造函数调用(没有参数、一个
参数和两个参数)以及两种显示方法。下面是一个使用这些构造函数和
方法的例子:
在这里插入图片描述

#include <iostream>
#include <cstring>
using namespace std;
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::Person(const string &ln,const char *fn){ // 注意此处不能带有默认值,否则会报错:(重定义默认参数)
    lname = ln;
    strcpy(fname,fn);
}
void Person::Show()const{
    cout<<fname<<"\t"<<lname<<endl;
}
void Person::FormalShow()const{
    cout<<lname<<"\t"<<fname<<endl;
}

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

    return 0;

}


在这里插入图片描述

3

完成第9章的编程练习1,但要用正确的golf类声明替换那里的代
码。用带合适参数的构造函数替换setgolf(golf &, const char , int),
以提供初始值。保留setgolf( )的交互版本,但要用构造函数来实现它
(例如,setgolf( )的代码应该获得数据,将数据传递给构造函数来创建
一个临时对象,并将其赋给调用对象,即
this)。

// golf.h
const int Len = 40;
class Golf {
private:
	char fullname[Len];
	int handicap;
public:
	Golf(const char* name = "", int hc = 0);
	void setGolf();
	void handcap(int hc);
	void showGolf();
};


// golf.cpp
#define _CRT_SECURE_NO_WARNINGS

#include "golf.h";
#include<cstring>
#include<string>
#include<iostream>
using namespace std;

Golf::Golf(const char* name, int hc) 
{ strcpy(fullname, name); handicap = hc; }
void Golf::setGolf() {
	cout << "enter fullname : ";
	char fname[Len];
	int hcap;
	fname[0] = '\0';
	string str;
	getline(cin, str);
	strncpy(fname, str.c_str(), Len);
	cout << "enter handicap : ";
	cin >> hcap;
	getchar();//吸收回车
	*this = Golf(fname, hcap);

}
void Golf::handcap(int hc) {
	this->handicap = hc;
}
void Golf::showGolf() {
	cout << "fullname : " << fullname << endl;
	cout << "handicap : " << handicap << endl;
}
// main.cpp
#define _CRT_SECURE_NO_WARNINGS

// #include<bits/stdc++.h>
#include <iostream>
#include <cstring>
#include <vector>
#include "golf.h"

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

        
int main()
{
    Golf g1("golf 1", 32);
    g1.showGolf();
    g1.handcap(20);
    cout << "handicap from 32 to 20" << endl;
    g1.showGolf();

    Golf g2("golf 2");
    g2.showGolf();
    g2.setGolf();
    cout << "after setGolf" << endl;
    g2.showGolf();

    Golf g3;
    g3.showGolf();
    g3.setGolf();
    cout << "after setGolf" << endl;
    g3.showGolf();

    return 0;

}


在这里插入图片描述

4

完成第9章的编程练习4,但将Sales结构及相关的函数转换为一个类及其方法。用构造函数替换setSales(sales &,double [ ],int)函数。用构造函数实现setSales(Sales &)方法的交互版本。将类保留在名称空间SALES中。

代码:

// sale.h
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();
    };
}

// sale.cpp
#define _CRT_SECURE_NO_WARNINGS
#include "sale.h"
#include<iostream>
#include<string>
#include<cstring>


namespace SALES {
    using std::cin;
    using std::cout;
    using std::endl;
	Sales::Sales(const double ar[], int n) {
        // 初始化
        for (int i = 0; i < QUARTERS; i++)sales[i] = 0;
        average = 0;
        // 数组为空时
        if (n == 0) {
            max = 0;
            min = 0;
        }
        // 数组非空时
        else{
            max = ar[0];
            min = ar[0];
            int less = std::min(QUARTERS, n);
            for (int i = 0; i < less; i++)
            {
                sales[i] = ar[i];
                max = std::max(ar[i], max);
                min = std::min(ar[i], min);
                average += ar[i];
            }
            average /= less;
        }
        
	}
    Sales::Sales() {
        // 初始化
        int i = 0, n = 0;
        average = 0;
        while (n < QUARTERS) {
            cout << "enter sale#" << n + 1 << " : ";
            cin >> sales[n];
            n++;
        }
        max = sales[0];
        min = sales[0];
        for (i = 0; i < QUARTERS; i++)
        {
            max = std::max(sales[i], max);
            min = std::min(sales[i], min);
            average += sales[i];
        }
        average /= QUARTERS;
    }
    void Sales::showSales() {
        for (int i = 0; i < QUARTERS; i++) {
            if (sales[i] == 0)break;
            cout << "sale #" << i + 1 << " : " << sales[i] << endl;
        }
        cout << "max : " << max << endl;
        cout << "min : " << min << endl;
        cout << "average : " << average << endl;
    }
}
// main.cpp
#define _CRT_SECURE_NO_WARNINGS


#include <iostream>
#include <cstring>
#include <vector>

#include "sale.h"

using namespace std;
using namespace SALES;

        
int main()
{
    double arr1[6] = { 1.1,2.2,3.3,4.4,5.5,6.6 };
    double arr2[3] = { 1.1,2.2,3.3 };

    Sales s1(arr1, 6);
    s1.showSales();
    cout << "------------------" << endl;
    Sales s2(arr2, 3);
    s2.showSales();
    cout << "------------------" << endl;
    Sales s3; // 使用交互构造;
    s3.showSales();
    return 0;

}


结果:

在这里插入图片描述

5

考虑下面的结构声明:
在这里插入图片描述
编写一个程序,它从栈中添加和删除customer结构(栈用Stack类声明表示)。每次customer结构被删除时,其payment的值都被加入到总数中,并报告总数。注意:应该可以直接使用Stack类而不作修改;只需修改typedef声明,使Item的类型为customer,而不是unsigned long即可。

6

下面是一个类声明:
在这里插入图片描述
请提供成员函数的定义和测试这个类的程序。

7

Betelgeusean plorg有这些特征。
数据:

  • plorg的名称不超过19个字符;
  • plorg有满意指数(CI),这是一个整数。

操作:

  • 新的plorg将有名称,其CI值为50;
  • plorg的CI可以修改;
  • plorg可以报告其名称和CI;
  • plorg的默认名称为“Plorga”。

请编写一个Plorg类声明(包括数据成员和成员函数原型)来表示plorg,并编写成员函数的函数定义。然后编写一个小程序,以演示Plorg类的所有特性。

8

  • 可以将简单列表描述成下面这样:
  • 可存储0或多个某种类型的列表;
  • 可创建空列表;
  • 可在列表中添加数据项;
  • 可确定列表是否为空;
  • 可确定列表是否为满;

可访问列表中的每一个数据项,并对它执行某种操作。可以看到,这个列表确实很简单,例如,它不允许插入或删除数据项。
请设计一个List类来表示这种抽象类型。您应提供头文件list.h和实现文件list.cpp,前者包含类定义,后者包含类方法的实现。您还应创建一个简短的程序来使用这个类。
该列表的规范很简单,这主要旨在简化这个编程练习。可以选择使用数组或链表来实现该列表,但公有接口不应依赖于所做的选择。也就是说,公有接口不应有数组索引、节点指针等。应使用通用概念来表达创建列表、在列表中添加数据项等操作。对于访问数据项以及执行操作,通常应使用将函数指针作为参数的函数来处理:

在这里插入图片描述
其中,pf指向一个将Item引用作为参数的函数(不是成员函数),Item是列表中数据项的类型。visit( )函数将该函数用于列表中的每个数据项。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值