「独習C++」(改訂版)を(本自体は、ずいぶん前に買ったんですが)やり始めているので、書いていくことにしてみようと思う。(いつまで続くやらw)
いきなりだが、P.46 練習問題2.1から。
- キュークラスを改良して、初期化関数をコンストラクタに置き換え。
- stopwatchクラスの作成
- 次に示すコンストラクタの誤りは?
#include
using namespace std;
#define SIZE 100
class q_type {
int queue[SIZE];
int head, tail;
public:
q_type();
void q(int num);
int deq();
};
q_type::q_type()
{
head = tail = 0;
}
void q_type::q(int num)
{
if(tail + 1 == head || (tail + 1 == SIZE && !head)) {
cout << "Queue is full\n";
return;
}
tail ++;
if (tail == SIZE ) {
tail = 0;
}
queue[tail] = num;
}
int q_type::deq()
{
if (head == tail) {
cout << "Queue is empty\n";
return 0;
}
head++;
if (head == SIZE) {
head = 0;
}
return queue[head];
}
int main()
{
q_type q1, q2;
int i;
for (i = 1; i <= 10; i++) {
q1.q(i);
q2.q(i*i);
}
for (i = 1; i <= 10; i++) {
cout << "Dequeue 1: " << q1.deq() << "\n";
cout << "Dequeue 2: " << q2.deq() << "\n";
}
return 0;
}
#include
#include
using namespace std;
class stopwatch {
time_t past_time;
time_t start_time;
public:
void start();
void stop();
void show();
stopwatch();
~stopwatch();
};
stopwatch::stopwatch()
{
past_time = 0;
start_time = 0;
}
stopwatch::~stopwatch()
{
cout << "past time:" << past_time << "\n";
}
void stopwatch::start()
{
start_time = time(NULL);
}
void stopwatch::stop()
{
time_t end;
end = time(NULL);
past_time = past_time + end - start_time;
}
int main()
{
int i;
int j;
char c;
stopwatch sw;
sw.start();
cout << "Press a key folloewd by Enter key.";
cin >> c;
sw.stop();
return 0;
}
class hoge {
double foo, bar, comic;
public:
double hoge(); // なぜエラーになる?
};
うーん。コンストラクタなのに、戻り値の型が指定されてるから。
関連記事:
- 独習C++ 改訂版日記2
- 独習C++ 改訂版日記 P.52 練習問題2.2 2.〜
- LVM xfs使用時の領域拡張方法
- 6メガピクセル、実売9万円台「ニコンD50」登場 – D70"s"も
- Linux(Ubuntu)で、Objective-C
Facebook Comments
