類的操作練習題二
接類的操作練習題一
編寫一個函數,比較兩個序列。如果Sequence對象有不同的長度,它們就是不同的,如果Sequence對象有相同的長度,但對應的值不同,它們也是不同的。只有Sequence對象有相同的長度,且對應的值也相同,它們才是相同的。把這個函數編寫為Sequence類的一個成員。

Sequence.h
// Sequence.h
#ifndef SEQUENCE_H
#define SEQUENCE_H
// Class encapsulating a sequence of integers
class Sequence {
public:
Sequence(); // Default constructor
Sequence(int start, int length = 2); // Constructor
~Sequence(); // Desctructor
void show(); // Output a sequence
bool equals(const Sequence& seq) const; // Compare this sequence with another
private:
int* pSequence; // Pointer to a sequence
int length; // Sequence length
};
#endif

Sequence.cpp
// Sequence.cpp
// Implementation of the Sequence class
// encapsulating an arbitrary length sequence of integers
#include "Sequence.h"
#include <iostream>
#include <iomanip>
using std::cout;
using std::endl;
// Default constructor
Sequence::Sequence() {
length = 10;
pSequence = new int[length];
for (int i=0; i<length; i++)
pSequence[i] = i;
}
Sequence::Sequence(int start, int length) {
this->length = length < 2 ? 2 : length;
pSequence = new int[length];
for (int i=0; i<length; i++)
pSequence[i] = start+i;
}
// Destructor
Sequence::~Sequence() {
cout << "Destructor called." << endl;
delete[] pSequence;
}
// Output a sequence
void Sequence::show() {
for(int i=0; i<length; i++) {
if (i%10 == 0)
cout << endl;
cout << std::setw(2+(pSequence[0]+length)/10) << pSequence[i];
}
cout << endl;
}
// Comparison
bool Sequence::equals(const Sequence& seq) const {
if (length != seq.length)
return false;
for (int i=0; i<length; i++)
if (pSequence[i] != seq.pSequence[i])
return false;
return true;
}

main.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
using std::cout;
using std::endl;
#include "Sequence.h"
void main() {
const int nSeq = 5;
Sequence** pSequences = new Sequence*[nSeq];
pSequences[0] = new Sequence(5,6);
pSequences[1] = new Sequence(5,5);
pSequences[2] = new Sequence(5,6);
pSequences[3] = new Sequence(5,5);
pSequences[4] = new Sequence(4,8);
for (int i=0; i<4; i++)
for (int j=i+1; j<5; j++)
cout << endl
<< "sequences[" << i << "]"
<< ((pSequences[i]->equals(*pSequences[j])) ? " is " : " is not ")
<< " equal to "
<< "sequences[" << j << "].";
cout << endl;
// First delete the Sequence objects in the free store
for (int j=0; j<nSeq; j++)
delete pSequences[j];
delete[] pSequences; // Now delete the array holding their addresses
}
編寫一個函數,比較兩個序列。如果Sequence對象有不同的長度,它們就是不同的,如果Sequence對象有相同的長度,但對應的值不同,它們也是不同的。只有Sequence對象有相同的長度,且對應的值也相同,它們才是相同的。把這個函數編寫為Sequence類的一個成員。
// Sequence.h
#ifndef SEQUENCE_H
#define SEQUENCE_H
// Class encapsulating a sequence of integers
class Sequence {
public:
Sequence(); // Default constructor
Sequence(int start, int length = 2); // Constructor
~Sequence(); // Desctructor
void show(); // Output a sequence
bool equals(const Sequence& seq) const; // Compare this sequence with another
private:
int* pSequence; // Pointer to a sequence
int length; // Sequence length
};
#endif
// Sequence.cpp
// Implementation of the Sequence class
// encapsulating an arbitrary length sequence of integers
#include "Sequence.h"
#include <iostream>
#include <iomanip>
using std::cout;
using std::endl;
// Default constructor
Sequence::Sequence() {
length = 10;
pSequence = new int[length];
for (int i=0; i<length; i++)
pSequence[i] = i;
}
Sequence::Sequence(int start, int length) {
this->length = length < 2 ? 2 : length;
pSequence = new int[length];
for (int i=0; i<length; i++)
pSequence[i] = start+i;
}
// Destructor
Sequence::~Sequence() {
cout << "Destructor called." << endl;
delete[] pSequence;
}
// Output a sequence
void Sequence::show() {
for(int i=0; i<length; i++) {
if (i%10 == 0)
cout << endl;
cout << std::setw(2+(pSequence[0]+length)/10) << pSequence[i];
}
cout << endl;
}
// Comparison
bool Sequence::equals(const Sequence& seq) const {
if (length != seq.length)
return false;
for (int i=0; i<length; i++)
if (pSequence[i] != seq.pSequence[i])
return false;
return true;
}
#include <iostream>
#include <cstdlib>
#include <ctime>
using std::cout;
using std::endl;
#include "Sequence.h"
void main() {
const int nSeq = 5;
Sequence** pSequences = new Sequence*[nSeq];
pSequences[0] = new Sequence(5,6);
pSequences[1] = new Sequence(5,5);
pSequences[2] = new Sequence(5,6);
pSequences[3] = new Sequence(5,5);
pSequences[4] = new Sequence(4,8);
for (int i=0; i<4; i++)
for (int j=i+1; j<5; j++)
cout << endl
<< "sequences[" << i << "]"
<< ((pSequences[i]->equals(*pSequences[j])) ? " is " : " is not ")
<< " equal to "
<< "sequences[" << j << "].";
cout << endl;
// First delete the Sequence objects in the free store
for (int j=0; j<nSeq; j++)
delete pSequences[j];
delete[] pSequences; // Now delete the array holding their addresses
}
posted on 2009-03-18 17:53 luqingfei 閱讀(282) 評論(0) 編輯 收藏 引用 所屬分類: C++基礎

