C++ Primer Plus 第十章答案 对象和类

简介: 只有聪明人才能看见的摘要~( ̄▽ ̄~)~

复习题

//10.9
//1
类是用户定义的类型的定义。类声明指定了数据将如何储存
同时指定了用来访问和操作这些数据的方法(类成员函数)
//2
类表示人们可以通过类方法的公有接口对类对象执行的操作,这是抽象
类的数据成员可以是私有的(private为默认),这意味着只能通过成员函数来访问这些数据,这是数据隐藏
实现的具体细节(如数据的显示和方法的代码)都是隐藏的,这是封装
//3
类定义了一种类型,包括如何使用它。对象是一个变量或其他数据对象(如由new产生的)
并根据类定义被创建和使用。类和对象的关系和标准类型和它的变量的关系相同
//4
如果创建给定类的多个对象,则每个对象都有其自己的数据内存空间,但是所有对象都
使用同一组成员函数(通常,方法是公有的而数据是私有的,但这只是策略方面的问题,并不是对类的要求)
//5
class Bank_account {
private:
  string b_name;
  string b_accountnum;
  double b_deposit;
public:
  Bank_account();
  Bank_account(const string& name, const string& accountnum, double deposit = 0.0);
  ~Bank_account();
  void show()const;
  void deposit(double deposit);
  void take(double take);
};
//6
创建类对象或显式调用构造函数时,类的构造函数被调用
对象过期时,调用类的析构函数
//7
Bank_account::Bank_account(const string& name, const string& account, double deposit = 0.0) {
  b_name = name;
  b_account = account;
  if (deposit < 0) {
    cout << "Error!Set to 0.\n";
    b_deposit = 0;
  }
  else
    b_deposit = deposit;
}
//8
默认构造函数是没有参数或者所有参数都有默认值的构造函数。拥有默认构造函数后
可以声明对象而不初始化它,即使已经定义了初始化构造函数。它还使得能声明数组
//9
class Stock {
private:
  std::string company;
  int shares;
  double share_val;
  double total_val;
  void set_tot() { total_val = shares * shara_val; }
public:
  Stock();
  Stock(const std::string& co, long n = 0, double pr = 0.0);
  ~Stock();
  void buy(long num, double price);
  void sell(long num, double price);
  void update(double price);
  void show();
  const Stock& topval(const Stock& s)const;
  int shares()const { return shares; }
  double share_val()const { return share_val; }
  double total_val()const { return total_val; }
  const std::string& company()const { return company; }
};
//10
this指针是类方法可以使用的指针,它指向用于调用方法的对象
因此,this是对象的地址,*this是对象本身

image.gif

编程练习

practice 1

#pragma once//避免此头文件被包含多次,和#ifndef的作用类似
//Bank.h
#ifndef BANK_H_
#define BANK_H_
#include<string>
using namespace std;
class Bank_account {
private:
  string b_name;
  string b_accountnum;
  double b_deposit;
public:
  Bank_account();
  Bank_account(const string& name, const string& accountnum, double deposit);
  ~Bank_account();
  void show()const;
  void deposit(double deposit);
  void take(double take);
};
#endif
//Bank.cpp
#include"Bank.h"
#include<iostream>
#include<string>
using namespace std;
Bank_account::Bank_account() {
  b_name = "no name";
  b_accountnum = "\0";
  b_deposit = 0.0;
}
Bank_account::Bank_account(const string& name, const string& accountnum, double deposit) {
  b_name = name;
  b_accountnum = accountnum;
  if (deposit < 0) {
    cout << "Error!Set to 0.\n";
    b_deposit = 0;
  }
  else
    b_deposit = deposit;
}
Bank_account::~Bank_account() {}
void Bank_account::show()const {
  cout << "The information of " << b_name << "'s bank account: \n";
  cout << "name: " << b_name << " accountnum: " << b_accountnum << endl;
  cout << "deposit: " << b_deposit << endl;
}
void Bank_account::deposit(double deposit) {
  if (deposit < 0)
    cout << "Number of deposit can't be nagative.\n";
  else
    b_deposit += deposit;
}
void Bank_account::take(double take) {
  if (take < 0)
    cout << "Number of take from account can't be nagative.\n";
  else if (take > b_deposit)
    cout << "You can't take from account more than it has.\n";
  else
    b_deposit -= take;
}
//main.cpp
#include"Bank.h"
int main() {
  using namespace std;
  Bank_account yue;
  yue.show();
  yue = { "yue","1234567",-100.0 };
  yue.show();
  yue = { "yue","1234567",100.0 };
  yue.show();
  yue.deposit(-12.1);
  yue.show();
  yue.deposit(12.1);
  yue.show();
  yue.take(-25.5);
  yue.show();
  yue.take(112.1);
  yue.show();
  yue.take(25.5);
  yue.show();
  return 0;
}

image.gif

practice 2

#pragma once
//person.h
#ifndef PERSON_H_
#define PERSON_H_
#include<string>
#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;
};
#endif
//person.cpp
#include"person.h"
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
Person::Person(const string& ln, const char* fn) {
  lname = ln;
  strcpy_s(fname, fn);
}
void Person::show()const {
  cout << "Show the name for person: " << this->fname << endl;//或者使用(*this).fname
  cout << "The firstname: " << fname << endl;
  cout << "The lastname: " << lname << endl;
}
void Person::formalshow()const {
  cout << "Show the name for person: " << this->fname << endl;
  cout << "The lastname: " << lname << endl;
  cout << "The firstname: " << fname << endl;
}
//main.cpp
#include"person.h"
#include<iostream>
int main() {
  using std::endl;
  Person one;
  Person two("Smythecraft");
  Person three("Dimwiddy", "Sam");
  one.show();
  one.formalshow();
  cout << endl;
  two.show();
  two.formalshow();
  cout << endl;
  three.show();
  three.formalshow();
  cout << endl;
  return 0;
}

image.gif

practice 3

#pragma once
//golf.h
#ifndef GOLF_H_
#define GOLF_H_
const int Len = 40;
class Golf {
private:
  char fullname[Len];
  int g_handicap;
public:
  Golf(const char* name, int hc);
  Golf();
  void setgolf();
  const char* rtfname();//要实现输入名字为空时结束循环,因此要一个成员函数返回私有数据比较
  void handicap(int hc);//此处函数名和私有数据同名
  //后面的赋值会报错,所以给私有数据名加了前缀g_handicap
  void showgolf()const;
};
#endif
//golf.cpp
#include"golf.h"
#include<iostream>
#include<cstring>
using namespace std;
Golf::Golf(const char* name, int hc) {
  strcpy_s(fullname, name);
  g_handicap = hc;
}
Golf::Golf() {
  strcpy_s(fullname, "no name");
  g_handicap = 0;
}
void Golf::setgolf() {
  cout << "Please enter the information for golf" << endl;
  Golf golf;
  cout << "The fullname: ";
  cin.getline(golf.fullname, Len);
  cout << "The handicap: ";
  cin >> golf.g_handicap;
  cin.get();//凡是输入使用getline()使都要考虑之前的输入是否有
  //换行符留在输入队列中,使用cin.get()丢弃换行符
  *this = golf;
}
const char* Golf::rtfname() {
  return fullname;
}
void Golf::handicap(int hc) {
  g_handicap = hc;
}
void Golf::showgolf()const {
  cout << "The information of " << fullname << ": \n";
  cout << "Fullname: " << fullname << endl;
  cout << "Handicap: " << g_handicap << endl;
}
//main.cpp
#include"golf.h"
#include<iostream>
int main() {
  Golf g1;
  g1.showgolf();
  Golf g[3];
  for (int i = 0; i < 3; i++) {
    g[i].setgolf();
    if (g[i].rtfname()[0]== '\0')//这里在调用setgolf输入名字时使用了cin.getline()
      //它会把换行符变为空字符储存在字符串里,因此检查第一个字符
      // 我不会太具体的解释具体为什么==""不对
      break;
      g[i].showgolf();
  }
  g1.handicap(100);
  g1.showgolf();
  return 0;
}

image.gif

practice 4

//sale.h
#ifndef SALE_H_
#define SALE_H_
namespace SALES {
  const int QUARTERS = 4;
  class Sales {
  private:
    double sales[QUARTERS];
    double average;
    double min;
    double max;
  public:
    Sales(const double ar[], int n=4);
    Sales();
    void setsales();
    void showsales();
  };
}
#endif
//sale.cpp
#include"sale.h"
#include<iostream>
namespace SALES {
  using namespace std;
  Sales::Sales(const double ar[], int n) {
    double mx = ar[0];
    double mn = ar[0];
    double total = 0;
    for (int i = 0; i < n; i++) {
      sales[i] = ar[i];
      total += ar[i];
      if (mx < ar[i])
        mx = ar[i];
      if (mn > ar[i])
        mn = ar[i];
    }
    average = total / n;
    max = mx; min = mn;
  }
  Sales::Sales() {
    sales[0] = sales[1] = sales[2] = sales[3] = 0;
    min = max = average = 0;
  }
  void Sales::setsales() {
    cout << "Enter an array treat as sales of quarters:(most at 4)";
    double arr[4] = {};
    int i = 0;
    for (i; i < 4; i++) {
      if (cin >> arr[i]);
      else
        break;
    }
    *this = Sales(arr, i);
  }
  void Sales::showsales() {
    cout << "Here is the information of sales of quarters: \n";
    cout << "Every sale: " << sales[0] << "    " << sales[1] << "    " << sales[2]
      << "    " << sales[3] << endl;
    cout << "Average: " << average << endl;
    cout << "The max sale: " << max << endl;
    cout << "The min sale: " << min << endl;
    cout << endl;
  }
}
//main.cpp
#include"sale.h"
int main() {
  using namespace SALES;
  double ar[4] = { 90,80,120,110 };
  Sales s1(ar, 4);
  s1.showsales();
  Sales s2;
  s2.setsales();
  s2.showsales();
  return 0;
}

image.gif

practice 5

//stack.h
#ifndef STACK_H_
#define STACK_H_
struct customer {
  char fullname[35];
  double payment;
};
typedef customer Item;
class Stack {
private:
  enum{MAX=10};
  Item items[MAX];
  int top;
public:
  Stack();
  bool isempty()const;
  bool isfull()const;
  bool push(const Item& item);
  bool pop(Item& item);
};
#endif
//stack.cpp
#include"stack.h"
#include<iostream>
Stack::Stack() { top = 0; }
bool Stack::isempty()const {
  return top == 0;
}
bool Stack::isfull()const {
  return top == MAX;
}
bool Stack::push(const Item& item) {
  if (top < MAX) {
    items[top++] = item;
    return true;
  }
  else
    return false;
}
bool Stack::pop(Item& item) {
  static double total = 0;
  if (top > 0) {
    item = items[--top];
    total += item.payment;
    std::cout << "The total payment is: " << total << std::endl;
    return true;
  }
  else return false;
}
//main.cpp
#include"stack.h"
#include<iostream>
int main() {
  Stack stack;
  customer c[5]{
    {"yue",120},
    {"yue yeui",500},
    {"yue aishai",200.5},
    {"yue yue",101},
    {"yue ",82.3}
  };
  for (int i = 0; i < 5; i++)
    stack.push(c[i]);
  for (int i = 4; i >= 0; i--)
    stack.pop(c[i]);
  return 0;
}

image.gif

practice 6

//move.h
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);
};
//move.cpp
#include<iostream>
#include"move.h"
Move::Move(double a,double b) {
  x = a;
  y = b;
}
void Move::showmove()const {
  std::cout << "x = " << x << ", y = " << y << std::endl;
}
Move Move::add(const Move& m)const {
  Move mm;
  mm.x = x + m.x;
  mm.y = y + m.y;
  return mm;
}
void Move::reset(double a, double b) {
  x = a;
  y = b;
}
//main.cpp
#include"move.h"
int main() {
  Move m1(1.1, 2.2);
  m1.showmove();
  Move m2(2.2, 3.3);
  m2.showmove();
  Move m3;
  m3 = m2.add(m1);
  m3.showmove();
  m3.reset(10, 11);
  m3.showmove();
  return 0;
}

image.gif

practice 7

//plorg.h
#ifndef PLORG_H_
#define PLORG_H_
class Plorg {
private:
  char name[20];
  int CI;
public:
  Plorg(const char* n = "Plorga", int ci = 50);
  void resetci(int ci);
  char* rtname();
  int rtci();
};
#endif
//plorg.cpp
#include"plorg.h"
#include<cstring>
Plorg::Plorg(const char* n, int ci) {
  strcpy_s(name, n);
  CI = ci;
}
void Plorg::resetci(int ci) {
  CI = ci;
}
char* Plorg::rtname() {
  return name;
}
int Plorg::rtci() {
  return CI;
}
//main.cpp
#include"plorg.h"
#include<iostream>
using std::cout;
using std::endl;
int main() {
  Plorg p1("Binlang");
  cout << "p1: name: " << p1.rtname() << ", CI: " << p1.rtci() << endl;
  Plorg p2("yue", 100);
  cout << "p2: name: " << p2.rtname() << ", CI: " << p2.rtci() << endl;
  p2.resetci(150);
  cout << "p2: name: " << p2.rtname() << ", CI: " << p2.rtci() << endl;
  Plorg p3;
  cout << "p3: name: " << p3.rtname() << ", CI: " << p3.rtci() << endl;
  return 0;
}

image.gif

practice 8

//list.h
#ifndef LIST_H_
#define LIST_H_
const int MAX = 5;
struct people {
  char name[30];
  int age;
};
typedef people Item;
class List {
private:
  Item items[MAX];
  int head;
  int tail;
public:
  List();
  bool add(Item& item);
  bool get(Item& item);
  void visit(void(*pf)(Item&));
  void show()const;
};
void agedouble(Item&);
#endif
//list.cpp
#include"list.h"
#include<iostream>
using namespace std;
List::List() {
  for(int i=0;i<5;i++)
    items[i] = { "NONE",0 };
  head = 0;
  tail = 0;
}
bool List::add(Item& item) {
  if ((tail + 1) % MAX == head) {
    cout << "The list is fall!\n";
    return false;
  }
  else {
    items[tail] = item;
    tail = (tail + 1) % MAX;
  }
  return true;
}
bool List::get(Item& item) {
  if (tail == head) {
    cout << "The list is empty!\n";
    return false;
  }
  else {
    item = items[head];
    items[head] = { "NONE",0 };
    head = (head + 1) % MAX;
  }
  return true;
}
void List::visit(void(*pf)(Item&)) {
  for (int i = 0; i < MAX; i++)
    (*pf)(items[i]);
}
void List::show()const {
  for (int i = 0; i < MAX; i++)
    cout << "items[" << i << "]= " << items[i].name
    << ", " << items[i].age << endl;
  cout << endl;
}
void agedouble(Item& item) {
  item.age *= 2;
}
//main.cpp
#include"List.h"
#include<iostream>
int main() {
  using std::cout;
  using std::endl;
  List list;
  people p[5]{
    {"yue",12},
    {"yuei",13},
    {"yues",10},
    {"yuea",20},
    {"yuejs",18}
  };
  for (int i = 0; i < 5; i++) {// 队列只能读取四个元素,需要一个空间作为队满的判断
      list.add(p[i]);
    cout << p[i].name << "    " << p[i].age << endl;
  }
  list.show();
  void (*pdouble)(Item&) = agedouble;
  list.visit(pdouble);
  list.show();
  for (int i = 4; i >= 0; i--) {
    list.get(p[i]);
    cout << p[i].name << "    " << p[i].age << endl;
  }cout << endl;
  list.show();
  return 0;
}

image.gif

image.gif编辑

创建一个people[5]的数组。然后for循环,每次循环add一个people结构到List类,每次循环显示people结构的内容。add失败会显示列表已满。show()。然后调用一个以函数指针为参数(指向一个翻倍people结构的age成员的函数)的成员函数,show()。再for循环,每次循环get一个people结构,每次循环显示get的people的信息。get失败会显示列表已空。show()

目录
相关文章
|
2天前
|
编译器 C++
【C++】继续学习 string类 吧
首先不得不说的是由于历史原因,string的接口多达130多个,简直冗杂… 所以学习过程中,我们只需要选取常用的,好用的来进行使用即可(有种垃圾堆里翻美食的感觉)
7 1
|
2天前
|
算法 安全 程序员
【C++】STL学习之旅——初识STL,认识string类
现在我正式开始学习STL,这让我期待好久了,一想到不用手撕链表,手搓堆栈,心里非常爽
9 0
|
2天前
|
存储 安全 测试技术
【C++】string学习 — 手搓string类项目
C++ 的 string 类是 C++ 标准库中提供的一个用于处理字符串的类。它在 C++ 的历史中扮演了重要的角色,为字符串处理提供了更加方便、高效的方法。
7 0
|
3天前
|
Java C++ Python
【C++从练气到飞升】06---重识类和对象(二)
【C++从练气到飞升】06---重识类和对象(二)
|
3天前
|
编译器 C++
【C++从练气到飞升】06---重识类和对象(一)
【C++从练气到飞升】06---重识类和对象(一)
|
3天前
|
存储 编译器 C语言
【C++从练气到飞升】02---初识类与对象
【C++从练气到飞升】02---初识类与对象
|
3天前
|
设计模式 安全 算法
【C++入门到精通】特殊类的设计 | 单例模式 [ C++入门 ]
【C++入门到精通】特殊类的设计 | 单例模式 [ C++入门 ]
14 0
|
4天前
|
C语言 C++
【C++】string类(常用接口)
【C++】string类(常用接口)
13 1
|
3天前
|
设计模式 算法 编译器
【C++入门到精通】特殊类的设计 |只能在堆 ( 栈 ) 上创建对象的类 |禁止拷贝和继承的类 [ C++入门 ]
【C++入门到精通】特殊类的设计 |只能在堆 ( 栈 ) 上创建对象的类 |禁止拷贝和继承的类 [ C++入门 ]
8 0
|
3天前
|
算法 安全 调度
【C++入门到精通】 线程库 | thread类 C++11 [ C++入门 ]
【C++入门到精通】 线程库 | thread类 C++11 [ C++入门 ]
13 1