2014年4月30日水曜日

開発環境

C++実践プログラミング (スティーブ オウアルライン (著)、Steve Oualline (原著)、Steve Oualline(原著)、望月 康司(翻訳)、クイープ(翻訳) 、オライリー・ジャパン)のⅣ部(高度なプログラミング概念)の21章(高度なクラス)、21.8(プログラミング実習)、実習 21-3.を解いてみる。

その他参考書籍

実習 21-3.

コード(BBEdit, Emacs)

shape.cpp

#include <iostream>
#include <cmath>

class shape {
protected:
  int size;
public:
  shape (int n) {
    size = n;
  }
  virtual double area() = 0;
};

class square : public shape {
public:
  square (int n) : shape (n){}

  double area () {
    return size * size;
  }
};

class circle : public shape {
public:
  circle (int n) : shape(n) {}
  double area() {
    return size * size * M_PI;
  }    
};

class equilateral_triangle : public shape  {
public:
  equilateral_triangle (int n) : shape (n){}
  double area () {
    return size * (size * sqrt(3) / 2) / 2;
  }
};

int main(int argc, char *argv[])
{
  square s(5);
  circle c(5);
  equilateral_triangle t(5);

  std::cout << s.area() << std::endl;
  std::cout << c.area() << std::endl;
  std::cout << t.area() << std::endl;
  return (0);
}

Makefile

CC=g++
CFLAGS=-g -Wall
all: shape

shape: shape.cpp
 ${CC} ${CFLAGS} -o shape shape.cpp

clean:
 rm shape

入出力結果(Terminal)

$ make && ./shape
g++ -g -Wall -o shape shape.cpp
25
78.5398
10.8253
$

0 コメント:

コメントを投稿