第十章:对象和类
一、复习题
1. 类是用户定义的类型的定义.类声明指定了数据将如何存储,同时指定了用来访问和操作这些数据的方法(类成员函数).
2.类表示人们可以通过类方法的公有接口对类对象执行的操作,这是抽象.
类的数据成员可以是私有的,这意味着只能通过成员函数来进行访问这些数据,这是数据隐藏
实现的具体细节(如数据表示和方法的代码)都是隐藏的,这是封装.
3. 类是一种类型,而对象是对类的实例化(类与对象之间的关系同标准类型与其变量之间的关系相同.)
4. 如果创建给定类的多个对象,则每个对象都有其自己的数据内存空间,但所有的对象都使用同一组成员函数.
5.
class Account
{
private:
string name;
string id;
double deposit;
public:
Account();
void showAccount() const;
void setDeposit(double cash);
void outDeposit(double cash);
}
6. 类构造函数在创建类的对象时调用;析构函数在对象被销毁时自动被调用
7.
Account();
using namespace std;
Account::Account(const string & name, const string & id, double deposit){
this->name = name;
this->id = id;
this->deposit = deposit;
}
注:默认参数位于原型中,而不是函数定义中.
8. 默认构造函数是不含形参的构造函数,或提供默认值的含形参的构造函数.可以声明对象而不用初始化它,还是得能够声明数组.
9.
const string & getCompany() const { return company; }
const long getShares() const { return shares; }
const double getValue() const { return share_Val; }
const double getTotal() const { return total_Val; }
10. this是一个指向调用成员函数对象的指针,而*this是这个对象本身
二、编程练习
1.
#ifndef __ACCOUNT_H__
#define __ACCOUNT_H__
#include
using namespace std;
class Account
{
private:
string name;
string id;
double deposit;
public:
Account(const string& name = "Error", const string& id = "00000", double deposit = 0.0);
void showAccount() const;
void setDeposit(double cash);
void outDeposit(double cash);
};
#endif
#include
#include "Account.h"
Account::Account(const string& name, const string& id, double deposit) {
this->name = name;
this->id = id;
this->deposit = deposit;
}
void Account::showAccount() const {
cout << "Name: " << name << endl;
cout << "Id: " << id << endl;
cout << "Deposit: $" << deposit << endl;
}
void Account::setDeposit(double cash) {
deposit += cash;
}
void Account::outDeposit(double cash) {
if (cash > deposit) {
cout << "Do not have enough money!" << endl;
}
else {
deposit -= cash;
}
}
#include "Account.h"
int main()
{
Account a1("David", "832203230", 100);
a1.showAccount();
a1.outDeposit(120);
a1.showAccount();
a1.setDeposit(20);
a1.showAccount();
a1.outDeposit(60);
a1.showAccount();
return 0;
}
2.
#include
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;
};
#include
#include
#include "Person.h"
using namespace std;
Person::Person(const string& ln, const char* fn) {
lname = ln;
strcpy_s(fname, fn);
}
void Person::show() const {
cout << fname << " " << lname << endl;
}
void Person::FormalShow() const {
cout << lname << " " << fname << endl;
}
#include "Person.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.
class Golf
{
static const int Len = 40;
private:
char fullname[Len];
int handicap;
public:
Golf(const char*, int);
Golf();
void sethandicap(int hc);
void showgolf() const;
};
#include "Golf.h"
#include
#include
using namespace std;
Golf::Golf(const char* name, int hc) {
strcpy_s(fullname, name);
handicap = hc;
}
Golf::Golf() {
cout << "Enter the name: " << endl;
cin.getline(fullname, Len);
cout << "Enter the handicap: " << endl;
cin >> handicap;
cin.get();
}
void Golf::sethandicap(int hc) {
handicap = hc;
}
void Golf::showgolf() const {
cout << "Fullname: " << fullname << endl;
cout << "Handicap: " << handicap << endl;
}
#include "Golf.h"
#include
using namespace std;
int main()
{
Golf ann("Ann Birdfree", 24);
Golf arrg[3];
arrg[0] = ann;
for (int i = 0; i < 3; i++) {
arrg[i].showgolf();
}
return 0;
}
4.
namespace SALES
{
class Sales
{
static const int QUARTERS = 4;
private:
double sales[QUARTERS];
double average;
double max;
double min;
public:
Sales(const double ar[], int n);
Sales();
void showSales() const;
};
}
#include
#include "Sales.h"
using namespace std;
namespace SALES {
Sales::Sales(const double ar[], int n) {
double sum = 0;
double max = ar[0];
double min = ar[0];
for (int i = 0; i < n && i < QUARTERS; i++) {
sales[i] = ar[i];
sum += ar[i];
max = max < ar[i] ? ar[i] : max;
min = min > ar[i] ? ar[i] : min;
}
this->max = max;
this->min = min;
average = n == 0 ? 0 : sum / n;
}
Sales::Sales() {
double ar[QUARTERS];
for (int i = 0; i < QUARTERS; i++) {
cout << "Enter the num: " << endl;
cin >> ar[i];
}
double sum = 0;
double max = ar[0];
double min = ar[0];
for (int i = 0; i < QUARTERS; i++) {
sales[i] = ar[i];
sum += ar[i];
max = max < ar[i] ? ar[i] : max;
min = min > ar[i] ? ar[i] : min;
}
this->max = max;
this->min = min;
average = sum / QUARTERS;
}
void Sales::showSales() const {
for (int i = 0; i < QUARTERS; i++) {
cout << sales[i] << " ";
}
cout << average << " " << max << " " << min;
}
}
#include "Sales.h"
#include
using namespace SALES;
int main() {
Sales s1;
double ar[4] = { 1.1,2.2,3.3,4.4 };
Sales s2(ar, 4);
s1.showSales();
s2.showSales();
return 0;
}
5.
#include
struct customer
{
char fullname[35];
double payment;
};
typedef customer Customer;
class Stack
{
static const int LEN = 4;
private:
Customer customers[LEN];
int top = 0;
int total = 0;
void show_tot() { std::cout << total << std::endl; }
public:
Stack();
void push(Customer& c);
void out(Customer& c);
bool isEmpty();
bool isFull();
};
#include "customer.h"
#include
using namespace std;
Stack::Stack()
{
top = 0;
}
void Stack::push(Customer& c)
{
if (isFull()) {
cout << "Full" << endl;
}
else {
customers[top++] = c;
}
}
void Stack::out(Customer& c)
{
if (isEmpty())
{
cout << "Empty" << endl;
}
else
{
c = customers[--top];
total += c.payment;
show_tot();
}
}
bool Stack::isEmpty()
{
return top == 0;
}
bool Stack::isFull()
{
return top == LEN;
}
#include "customer.h"
int main()
{
Stack stack;
Customer test;
Customer c1 = { "David",20 };
Customer c2 = { "Eason",10 };
Customer c3 = { "Tanger",30 };
Customer c4 = { "Angel",40 };
Customer c5 = { "Bart",50 };
stack.out(test);
stack.push(c1);
stack.push(c2);
stack.push(c3);
stack.push(c4);
stack.push(c5);
stack.out(test);
stack.out(test);
stack.out(test);
stack.out(test);
return 0;
}
6.
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);
};
#include "Move.h"
#include
using namespace std;
Move::Move(double a, double b)
{
x = a;
y = b;
}
void Move::showmove() const
{
cout << "x = " << x << endl;
cout << "y = " << y << endl;
}
Move Move::add(const Move& m) const
{
Move nm;
nm.x = m.x + x;
nm.y = m.y + y;
return nm;
}
void Move::reset(double a, double b)
{
x = a;
y = b;
}
#include "Move.h"
int main()
{
Move m1;
Move m2(1.1, 2.2);
Move m3 = m1.add(m2);
m1.showmove();
m2.showmove();
m3.showmove();
m3.reset(3.3,4.4);
m3.showmove();
return 0;
}
7.
class Plorg
{
private:
char fullname[20];
int CI;
public:
Plorg(const char* = "Plorga");
void setCI(int);
void showPlorg() const;
};
#include
#include
#include "Plorg.h"
using namespace std;
Plorg::Plorg(const char* name)
{
strcpy_s(fullname, name);
CI = 50;
}
void Plorg::setCI(int n)
{
CI = n;
}
void Plorg::showPlorg() const
{
cout << fullname << ": " << CI << endl;
}
#include "Plorg.h"
int main()
{
Plorg p1;
Plorg p2("David");
p1.showPlorg();
p2.showPlorg();
p1.setCI(100);
p1.showPlorg();
return 0;
}
8.
class List
{
static const int MAX = 5;
private:
int arr[MAX];
int len = 0;
int count = 0;
public:
List(int = 0);
void add(int);
bool isFull();
bool isEmpty();
void visit(void (*)(int&));
};
#include "List.h"
#include
using namespace std;
List::List(int n)
{
len = n;
}
void List::add(int n)
{
if (isFull())
{
cout << "Full" << endl;
}
else
{
arr[count++] = n;
}
}
bool List::isFull()
{
return count == len;
}
bool List::isEmpty()
{
return count == 0;
}
void List::visit(void (*pf)(int&))
{
for (int i = 0; i < len; i++)
{
pf(arr[i]);
}
}
#include "List.h"
#include
void show(int& n)
{
std::cout << n << " ";
}
void increase(int& n)
{
n++;
}
int main()
{
List l1;
l1.add(1);
List l2(3);
l2.add(1);
l2.add(2);
l2.add(3);
l2.visit(show);
l2.visit(increase);
l2.visit(show);
return 0;
}