目錄
本章小結
程式清單
建立一個Stock類(version 1)
建立一個Stock類(version 2),增加了構造函數和析構函數
建立一個Stock類(version 3),使用了*this指針
建立一個抽象資料類型(ADT)--stack類
課後程式設計習題答案
本章小結
面向對象程式設計強調的是程式如何表示資料。使用OOP方法解決程式設計問題的第一步是根據它程式之間的接口來描述資料,進而指定如何使用資料。然後,設計一個類來實作該接口。一般來說,私有資料成員存儲資訊,公有成員函數(又稱為方法)提供通路資料的唯一途徑。類将資料和方法組合成一個單元,其私有性實作資料隐藏。
通常,将類聲明分成兩部分組成,這兩部分通常儲存在不冋的檔案中。類聲明(包括由函數原型表示的方法)應放到頭檔案中。定義成員函數的源代碼放在方法檔案中。這樣便将接口描述與實作細節分開了。 從理論上說,隻需知道公有接口就可以使用類。當然,可以檢視實作方法(除非隻提供了編譯形式),但程式不應依賴于其實作細節,如知道某個值被存儲為int。隻要程式和 類隻通過定義接口的方法進行通信,程式員就可以随意地對任何部分做獨立的改進,而不必擔心這樣做會導緻意外的不良影響。
類是使用者定義的類型,對象是類的執行個體。這意味着對象是這種類型的變量,例如由new按類描述配置設定的記憶體C++試圖讓使用者定義的類型盡可能與标準類型類似,是以可以聲明對象、指向對象的指針和對象數組。可以按值傳遞對象、将對象作為函數傳回值、将一個對象賦給同類型的另一個對象。如果提供了構造函數,則在建立對象時,可以初始化對象。如果提供了析構函數方法,則在對象消亡後,程式将執行該函數。
每個對象都存儲自己的資料,而共享類方法。如果mr_object是對象名,try_me()是成員函數,則可以使用成員運算符句點調用成員函數:mr_object.try_me()。在OOP中,這種函數調用被稱為将try_me消息發送給mr_object對象。在try_me()方法中引用類資料成員時,将使用mr_object對象相應的資料成員。同樣,函數調用i_objet.try_me()将通路i_object對象的資料成員。
如果希望成員函數對多個對象進行操作,可以将額外的對象作為參數傳遞給它。如果方法需要顯式地引用調用它的對象,則可以使用this指針。由于this指針被設定為調用對象的位址,是以*this是該對象的别名。
類很适合用于描述ADT(abstract data type,抽象資料類型)。公有成員函數接口提供了 ADT描述的服務,類的私有部分和類方法的代碼提供了實作,這些實作對類的客戶隐藏。
程式清單
建立一個Stock類(version 1)
10.1 stock00.h Stock類的聲明
//stock00.h -- Stock class interface
//version 00
#ifndef STOCK00_H_
#define STOCK00_H_
#include "stdafx.h"
#include<string>
class Stock //class declaration
{
private:
std::string company;
long shares;
double share_val;
double total_val;
void set_tot()
{
total_val = shares * share_val;
}
public:
void acquire(const std::string & co, long n, double pr);
void buy(long num, double price);
void sell(long num, double price);
void update(double price);
void show();
}; //note semicolon at the end
#endif // !STOCK00_H_
10.2 stock00.cpp Stock類方法的實作
//stock00.cpp -- implementing the Stock class
//version 00
#include "stdafx.h"
#include "stock00.h"
#include<iostream>
using namespace std;
void Stock::acquire(const std::string & co, long n, double pr)
{
company = co;
if (n < 0)
{
cout << "Number of shares can't be negative; " << company << " shares set to 0.\n";
shares = 0;
}
else
shares = n;
share_val = pr;
set_tot();
}
void Stock::buy(long num, double price)
{
if (num < 0)
{
cout << "Number of shares purchased can't be negative. Transaction is aborted.\n";
}
else
{
shares += num;
share_val = price;
set_tot();
}
}
void Stock::sell(long num, double price)
{
if (num < 0)
{
cout << "Number of shares sold can't be negative. Transaction is aborted.\n";
}
else if (num > shares)
{
cout << "You can't sell more than you have! Transaction is aborted.\n";
}
else
{
shares -= num;
share_val = price;
set_tot();
}
}
void Stock::update(double price)
{
share_val = price;
set_tot();
}
void Stock::show()
{
cout << "Company: " << company;
cout << " Shares: " << shares << endl;
cout << " Share Price: $" << share_val << endl;
cout << " Total Worth: $" << total_val << endl;
}
10.3 usestock0.cpp 使用Stock類
// usestock0.cpp -- the client program
// compile with stock00.cpp
#include "stdafx.h"
#include "stock00.h"
#include<iostream>
int main()
{
Stock fluffy_the_cat;
fluffy_the_cat.acquire("NanoSmart", 20, 12.50);
fluffy_the_cat.show();
fluffy_the_cat.buy(15, 18.125);
fluffy_the_cat.show();
fluffy_the_cat.sell(400, 20.00);
fluffy_the_cat.show();
fluffy_the_cat.buy(300000, 40.125);
fluffy_the_cat.show();
fluffy_the_cat.sell(300000, 0.125);
fluffy_the_cat.show();
return 0;
}
建立一個Stock類(version 2),增加了構造函數和析構函數
10.4 stock10.h stock類的聲明,添加了構造函數和析構函數
//stock10.h -- Stock class declaration with constructors destructor added
//version 10
#ifndef STOCK10_H_
#define STOCK10_H_
#include "stdafx.h"
#include<string>
class Stock //class declaration
{
private:
std::string company;
long shares;
double share_val;
double total_val;
void set_tot()
{
total_val = shares * share_val;
}
public:
Stock();//default constructor
Stock(const std::string & co, long n = 0, double pr = 0.0);
~Stock();//noisy destructor
void buy(long num, double price);
void sell(long num, double price);
void update(double price);
void show();
}; //note semicolon at the end
#endif // !STOCK10_H_
10.5 stock10.cpp stock類方法的實作,在這個版本中輸出精度得到了統一
//stock10.cpp -- Stock class declaration with constructors destructor added
//version 10
#include "stdafx.h"
#include "stock10.h"
#include<iostream>
using namespace std;
//constructors (verbose versions)
Stock::Stock()
{
cout << "Default constructor called\n";
company = "no name";
shares = 0;
share_val = 0.0;
total_val = 0.0;
}
Stock::Stock(const std::string & co, long n /* = 0 */, double pr /* = 0.0 */)
{
cout << "Constructor using " << co << " called\n";
company = co;
if (n < 0)
{
cout << "Number of shares can't be negative: " << company << " shares set to 0.\n";
shares = 0;
}
else
shares = n;
share_val = pr;
set_tot();
}
//class destructor
Stock::~Stock()
{
cout << "Bye. " << company << "!\n";
}
//other methods
void Stock::buy(long num, double price)
{
if (num < 0)
{
cout << "Number of shares purchased can't be negative. Transaction is aborted.\n";
}
else
{
shares += num;
share_val = price;
set_tot();
}
}
void Stock::sell(long num, double price)
{
if (num < 0)
{
cout << "Number of shares sold can't be negative. Transaction is aborted.\n";
}
else if (num > shares)
{
cout << "You can't sell more than you have! Transaction is aborted.\n";
}
else
{
shares -= num;
share_val = price;
set_tot();
}
}
void Stock::update(double price)
{
share_val = price;
set_tot();
}
void Stock::show()
{
ios_base::fmtflags orig = cout.setf(ios_base::fixed, ios_base::floatfield);
streamsize prec = cout.precision(3);//set format to #.###
cout << "Company: " << company;
cout << " Shares: " << shares << endl;
cout << " Share Price: $" << share_val << endl;
cout.precision(2);//set format to #.##
cout << " Total Worth: $" << total_val << endl;
//restore original format
cout.setf(orig, ios_base::floatfield);
cout.precision(prec);
}
10.6 usestock1.cpp 使用stock類
//usestock1.cpp -- using the Stock class
//compile with stock10.cpp
#include "stdafx.h"
#include "stock10.h"
#include<iostream>
int main()
{
using namespace std;
cout << "Using constructors to create new objects\n";
Stock stock1("NanoSmart", 12, 20.0);
stock1.show();
Stock stock2 = Stock("Boffo Objects", 2, 2.0);
stock2.show();
cout << "Assing stock1 to stock2:\n";
stock2 = stock1;
cout << "Listing stock1 and stock2:\n";
stock1.show();
stock2.show();
cout << "Using a constructor to reset an object\n";
stock1 = Stock("Nifty Foods", 10, 50.0);
cout << "Revised stock1:\n";
stock1.show();
cout << "Done\n";
return 0;
}
建立一個Stock類(version 3),使用了*this指針
10.7 stock20.h 添加了新方法topval()并使用了*this指針
//stock20.h -- sugmented version
#ifndef STOCK20_H_
#define STOCK20_H_
#include <string>
class Stock
{
private:
std::string company;
int shares;
double share_val;
double total_val;
void set_tot()
{
total_val = shares * share_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;
const Stock & topval(const Stock & s) const;
};
#endif // !STOCK20_H_
10.8 Stock類的實作,使用了*this指針
//stock20.cpp -- augmented version
//version 20
#include "stdafx.h"
#include "stock20.h"
#include <iostream>
using namespace std;
//constructors (verbose versions)
Stock::Stock()
{
company = "no name";
shares = 0;
share_val = 0.0;
total_val = 0.0;
}
Stock::Stock(const std::string & co, long n, double pr)
{
company = co;
if (n < 0)
{
cout << "Number of shares can't be negative: " << company << " shares set to 0.\n";
shares = 0;
}
else
shares = n;
share_val = pr;
set_tot();
}
//class destructor
Stock::~Stock()
{
}
//other methods
void Stock::buy(long num, double price)
{
if (num < 0)
{
cout << "Number of shares purchased can't be negative. Transaction is aborted.\n";
}
else
{
shares += num;
share_val = price;
set_tot();
}
}
void Stock::sell(long num, double price)
{
if (num < 0)
{
cout << "Number of shares sold can't be negative. Transaction is aborted.\n";
}
else if (num > shares)
{
cout << "You can't sell more than you have! Transaction is aborted.\n";
}
else
{
shares -= num;
share_val = price;
set_tot();
}
}
void Stock::update(double price)
{
share_val = price;
set_tot();
}
void Stock::show() const
{
ios_base::fmtflags orig = cout.setf(ios_base::fixed, ios_base::floatfield);
streamsize prec = cout.precision(3);//set format to #.###
cout << "Company: " << company;
cout << " Shares: " << shares << endl;
cout << " Share Price: $" << share_val << endl;
cout.precision(2);//set format to #.##
cout << " Total Worth: $" << total_val << endl;
//restore original format
cout.setf(orig, ios_base::floatfield);
cout.precision(prec);
}
const Stock & Stock::topval(const Stock & s) const
{
if (s.total_val > total_val)
return s;
else
return *this;
}
10.9 使用stock類建立對象數組
//usestock2.cpp -- using the Stock class
//compile with stock20.cpp
#include "stdafx.h"
#include "stock20.h"
#include <iostream>
const int STKS = 4;
int main()
{
using namespace std;
Stock stocks[STKS] =
{
Stock("NamoSmart",12,20.0),
Stock("Boffo Objects",200,2.0),
Stock("Monolithic Obelisks",130,3.25),
Stock("Fleep Enterprises",60,6.5)
};
cout << "Stock holdings:\n";
int st;
for (st = 0; st < STKS; st++)
stocks[st].show();
const Stock *top = &stocks[0];
for (st = 1; st < STKS; st++)
top = &top->topval(stocks[st]);
cout << "\nMost valuable holding:\n";
top->show();
return 0;
}
建立一個抽象資料類型(ADT)--stack類
10.10 stack.h stack類的聲明,stack(棧)是一種抽象資料類型(ADT)
//stack.h -- class definition for the stack ADT
#ifndef STACK_H_
#define STACK_H_
typedef unsigned long 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
10.11 stack類方法的實作
//stack.cpp -- Stack member functions
#include "stdafx.h"
#include "stack.h"
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)
{
if (top > 0)
{
item = items[--top];
return true;
}
else
return false;
}
10.12 使用stack類
//stacker.cpp -- testing the Stack class
#include "stdafx.h"
#include "stack.h"
#include <iostream>
#include <cctype>
int main()
{
using namespace std;
Stack st;
char ch;
unsigned long po;
cout << "Please enter A to add a purchase order,\nP to process a PO ,or Q to quit.\n";
while (cin >> ch && toupper(ch) != 'Q')
{
while (cin.get() != '\n')
continue;
if (!isalpha(ch))
{
cout << '\a';
continue;
}
switch (ch)
{
case 'A':
case 'a':
cout << "Enter a PO number to add: ";
cin >> po;
if (st.isfull())
cout << "stack already full\n";
else
st.push(po);
break;
case 'P':
case 'p':
if (st.isempty())
cout << "stack already empty\n";
else
{
st.pop(po);
cout << "PO #" << po << " popped\n";
}
break;
}
cout << "Please enter A to add a purchase order,\n"
<< "p to process a PO, or Q to quit.\n";
}
cout << "Bye\n";
return 0;
}
課後程式設計習題答案
習題1
//bankaccount.h
#ifndef BANKACCOUNT_H_
#define BANKACCOUNT_H_
#include <string>
class BankAccount
{
private:
std::string name;
std::string acctnum;
double balance;
public:
BankAccount(const std::string & client,
const std::string & num, double bal=0.0);
void show() const;
void deposit(double cash);
void withdraw(double cash);
};
#endif
//bankaccount.cpp
#include <iostream>
#include "bankaccount.h"
BankAccount::BankAccount(const std::string & client, const std::string & num, double bal)
{
name = client;
acctnum = num;
balance = bal;
}
void BankAccount::show()const
{ using std::cout;
using std::endl;
cout << "Client: " << name << endl;
cout << "Account Number: " << acctnum << endl;
cout << "Balance: " << balance << endl;
}
void BankAccount::deposit(double cash)
{
if (cash >= 0)
balance += cash;
else
std::cout << "Illegal transaction attempted";
}
void BankAccount::withdraw(double cash)
{
if (cash < 0)
std::cout << "Illegal transaction attempted";
else if (cash <= balance)
balance -=cash;
else
std::cout << "Request denied due to insufficient funds.\n";
}
//main.cpp
#include <iostream>
#include "bankaccount.h"
int main()
{
BankAccount ba("Kermit", "
croak322", 123.00);
ba.show();
ba.deposit(20);
ba.show();
ba.withdraw(300);
ba.show();
ba.withdraw(23);
ba.show();
return 0;
}
習題2
//person.h
#ifndef PERSON_H_
#define PERSON_H_
#include <string>
class Person
{
private:
static const int LIMIT=25;
std::string lname;
char fname[LIMIT];
public:
Person() {lname=""; fname[0]='\0';} //#1
Person(const std::string &ln, const char * fn="Heyyou"); //#2
// the following methods display lname and fname
void Show() const; // firstname lastname format
void FormalShow() const; // lastname, firstname format
};
#endif
//person.cpp
#include <iostream>
#include <cstring>
#include "person.h"
Person::Person(const std::string &ln, const char * fn)
{
lname = ln;
strcpy(fname, fn);
}
void Person::Show() const
{
using std::cout;
using std::endl;
cout << "The people's name is " << fname << " "<< lname << endl;
}
void Person::FormalShow() const
{
using std::cout;
using std::endl;
cout << "The people's name is " << lname << ", "<< fname <<endl;
}
//main.cpp
#include <iostream>
#include "person.h"
int main()
{
using std::cout;
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;
}
習題3
//golf.h
#ifndef GOLF_H_
#define GOLF_H_
class Golf
{
private:
static const int Len = 40;
char fullname[Len];
int handicap;
public:
Golf();
Golf(const char * name, int hc);
const Golf & setgolf(const Golf & g);
void showgolf() const;
};
#endif
//golf.cpp
#include <iostream>
#include <cstring>
#include "golf.h"
Golf::Golf()
{
strcpy(fullname, "No Name");
handicap = 0;
}
Golf::Golf(const char *name, int hc)
{
strcpy(fullname, name);
handicap = hc;
}
const Golf &Golf::setgolf(const Golf &g)
{
strcpy(fullname, g.fullname);
handicap = g.handicap;
return *this;
}
void Golf::showgolf() const
{
std::cout << "Golfer: " << fullname << "\n";
std::cout << "Handicap: " << handicap << "\n\n";
}
//main
#include <iostream>
#include "golf.h"
int main()
{
Golf golger1("Ann Birdfree", 5);
golger1.showgolf();
Golf golger2;
golger2.setgolf(golger1);
golger2.showgolf();
return 0;
}
習題4
//sale.h
#ifndef SALE_H_
#define SALE_H_
namespace SALES
{
const int QUARTERS = 4;
class Sales
{
private:
double sales[QUARTERS];
double average;
double max;
double min;
public:
Sales(); // default constructor
// copies the lesser of 4 or n items from the array ar
// to the sales member of s and computes and stores the
// average, maximum, and minimum values of the entered items;
// remaining elements of sales, if any, set to 0
Sales(const double ar[], int n);
// gathers sales for 4 quarters interactively, stores them
// in the sales member of s and computes and stores the
// average, maximum, and minimum values
void setSales();
// display all information in structure s
void showSales() const;
};
}
#endif
//sale.cpp
#include <iostream>
#include "sale.h"
namespace SALES
{
using std::cout;
using std::cin;
using std::endl;
static double calaverage(double arr[], unsigned arrSize)
{
double sum = 0;
for (int i=0; i<arrSize; i++)
sum += arr[i];
return sum/arrSize;
}
static double calmax(double arr[], unsigned arrSize)
{
double max = arr[0];
for (int i=1; i<arrSize; i++)
{
if (max < arr[i])
max = arr[i];
}
return max;
}
static double calmin(double arr[], unsigned arrSize)
{
double min = arr[0];
for (int i=1; i<arrSize; i++)
{
if (min > arr[i])
min = arr[i];
}
return min;
}
Sales::Sales()
{
min = 0;
max = 0;
average = 0;
for (int i = 0; i < QUARTERS; i++)
sales[i] =0;
}
Sales::Sales(const double ar[], int n)
{
unsigned times = n < QUARTERS ? (unsigned)n:QUARTERS;
for (int i=0; i<times; i++)
sales[i] = ar[i];
for (int i=times; i<QUARTERS; i++)
sales[i] = 0;
average = calaverage(sales, times);
max = calmax(sales, times);
min = calmin(sales, times);
}
void Sales::setSales()
{
cout << "Enter 4sales:\n";
for (int i=0; i<QUARTERS; i++)
{
cout << "sales " << i+1 << ": ";
cin >> sales[i];
}
*this = Sales(sales, QUARTERS);
}
void Sales::showSales() const
{
cout << "sales: ";
for (int i=0; i<QUARTERS; i++)
cout << sales[i] << " ";
cout << endl;
cout << "average: " << average << endl;
cout << "max: " << max << endl;
cout << "min: " << min << endl;
}
}
//main.cpp
#include <iostream>
#include "sale.h"
using namespace std;
int main ()
{
using namespace SALES;
double salesList[] = {12.2, 11.16, 10.61, 16.24, 11.53};
Sales salesBook(salesList,
sizeof(salesList)/sizeof(salesList[0]));
salesBook.showSales();
Sales salesPen;
salesPen.setSales();
salesPen.showSales();
return 0;
}
習題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;
// push() returns false if stack already is full, true otherwise
bool push(const Item & item); // add item to stack
// pop() returns false if stack already is empty, true otherwise
bool pop(Item & item); // pop top into item
};
#endif
//stack.cpp
#include <iostream>
#include "stack.h"
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)
{
if (top > 0)
{
item = items[--top];
return true;
}
else
return false;
}
//main.cpp
#include <iostream>
#include <cctype>
#include "stack.h"
void get_customer(customer & cu);
int main()
{
using namespace std;
Stack st;
customer temp;
double payment = 0;
char ch;
cout << "Please enter A to add a customer,\n" << "P to process a customer, and Q to quit.\n";
while (cin >> ch && (ch = toupper(ch)) != 'Q')
{
while (cin.get() != '\n')
continue;
if (ch != 'A' && ch != 'P')
{
cout << "Please respond A, P or Q: ";
continue;
}
switch (ch)
{
case 'A':
if (st.isfull())
cout << "stack already full\n";
else
{
get_customer(temp);
st.push(temp);
}
break;
case 'P':
if (st.isempty())
cout << "stack is empty\n";
else
{
st.pop(temp);
payment += temp.payment;
cout << temp.fullname << " processed. ";
cout << "Payments now total $" << payment << "\n";
}
break;
}
cout << "Please enter A to add a customer,\n" << "P to process a customer, and Q to quit.\n";
}
cout << "Done!\n";
return 0;
}
void get_customer(customer &cu)
{
using namespace std;
cout << "Enter customer name: ";
cin.getline(cu.fullname, 35);
cout << "Enter customer payment: ";
cin >> cu.payment;
while (cin.get() != '\n')
continue;
}
習題6
//move.h
#ifndef MOVE_H_
#define 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;
//this function adds x of m to x of invoking object to get new x
//add y of m to y of invoking object to get new y, creates a new
//move object initialized to new x,y values and returns it
void reset(double a = 0,double b = 0);
};
#endif
//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 << "\n";
}
Move Move::add(const Move &m) const
{
Move temp;
temp.x = x + m.x;
temp.y = y + m.y;
return temp;
}
void Move::reset(double a, double b)
{
x = a;
y = b;
}
//main
#include <iostream>
#include "move.h"
int main()
{
using std::cout;
using std::endl;
Move move1(4,5);
Move move2(2,1);
Move move3;
cout << "The number in move1 is:\n";
move1.showMove();
cout << "The number in move2 is:\n";
move2.showMove();
move3 = move2.add(move1);
cout << "The number in move3 is :\n";
move3.showMove();
cout << "move1+move2, now move2's number is :\n";
move2.showMove();
cout << "After move1 + move2,now move1's number is :\n";
move1.showMove();
move1.reset();
cout << "After reset move1,now move1's number is:\n";
move1.showMove();
return 0;
}
習題7
//plorg.cpp
#ifndef PLORG_H_
#define PLORG_H_
class Plorg
{
private:
char name[20];
int CI;
public:
Plorg();
Plorg(char * na, int n = 50);
void resetCI(int n);
void showplorg() const;
};
#endif
//plorg.cpp
#include <iostream>
#include <cstring>
#include "plorg.h"
Plorg::Plorg()
{
strcpy(name, "Plorga");
CI = 0;
}
Plorg::Plorg(char *na, int n)
{
strcpy(name, na);
CI = n;
}
void Plorg::resetCI(int n)
{
CI = n;
}
void Plorg::showplorg() const
{
std::cout << " The plorg's name is " << name << "\n" <<"The CI is "<< CI <<std::endl;
}
//main.cpp
#include <iostream>
#include "plorg.h"
int main()
{
using namespace std;
Plorg plorg1;
plorg1.showplorg();
Plorg plorg2("heyyroup", 31);
plorg2.showplorg();
plorg1.resetCI(41);
plorg1.showplorg();
return 0;
}
習題8
//list.h
#ifndef LIST_H_
#define LIST_H_
const int TSIZE = 50;
struct film
{
char title[TSIZE];
int rating;
};
typedef struct film Item;
const int LISTMAX = 10;
class List
{
private:
Item items[LISTMAX];
int count;
public:
List();
bool isempty();
bool isfull();
int itemcount();
bool additem(Item item);
void visit(void (*pf)(Item &));
};
#endif
//list.cpp
#include "list.h"
List::List()
{
count = 0;
}
bool List::isempty()
{
return count == 0;
}
bool List::isfull()
{
return count == LISTMAX;
}
int List::itemcount()
{
return count;
}
bool List::additem(Item item)
{
if (count == LISTMAX)
return false;
else
items[count++] = item;
return true;
}
void List::visit(void (*pf)(Item &))
{
for (int i=0; i<count; i++)
(*pf)(items[i]);
}
//main.cpp
#include <iostream>
#include <cstdlib>
#include "list.h"
void showfilm(Item & item);
int main()
{
using namespace std;
List movies;
Item temp;
if (movies.isfull())
{
cout << "No more room in list! Bye!\n";
exit(1);
}
cout << "Enter first movie title:\n";
while (cin.getline(temp.title, TSIZE) && temp.title[0] != '\0')
{
cout << "Enter your rating <1-10>: ";
cin >> temp.rating;
while (cin.get() != '\n')
continue;
if (movies.additem(temp) == false)
{
cout << "List already is full!\n";
break;
}
if (movies.isfull())
{
cout << "You have filled the list.\n";
break;
}
cout << "Enter next movie title (empty line to stop):\n";
}
if (movies.isempty())
cout << "No data entered.";
else
{
cout << "Here is the movie list:\n";
movies.visit(showfilm);
}
cout << "Bye!\n";
return 0;
}
void showfilm(Item &item)
{
std::cout << "Movie: "<< item.title << "Rating: "<< item.rating << std::endl;
}