10.24.2017

7주차 과제

<9-1>
#include <iostream>
using namespace std;
class CRectangle {
private :
         double width, height;
public:
         CRectangle() {
                  width = height = 1.0;
         }
         ~CRectangle() {
         }
         CRectangle(double a, double b){
                  width = a, height = b;
         }
         double get_width() {
                  return width;
         }
         double get_height() {
                  return height;
         }
         void set_width(double a) {
                  width = a;
         }
         void set_height(double a) {
                  height = a;
         }
         double getArea() {
                  cout << "면적은 : " << width*height << endl;
                  return width*height;
         }
         double getPerimeter() {
                  cout << "둘레 길이는 : " << 2 * (width + height) << endl;
                  return 2 * (width + height);
         }
};

int main() {
         CRectangle a, b;
         a.set_height(40);
         a.set_width(4);
         cout << "첫 번째 객체의 높이는 : " << a.get_height() << endl;
         cout << "너비는 : " << a.get_width() << endl;
         a.getArea();
         a.getPerimeter();

         b.set_height(35.9);
         b.set_width(3.5);
         cout << "두 번째 객체의 높이는 : " << b.get_height() << endl;
         cout << "너비는 : " << b.get_width() << endl;
         b.getArea();
         b.getPerimeter();
         system("pause");
         return 0;
}

9-2
#include <iostream>
using namespace std;
class Fan {
private :
         int speed;
         bool on;
         double radius;
public:
         Fan() {
                  speed = 1;
                  on = false;
                  radius = 5;
         }
         Fan(int a, bool b, double c)
         {
                  speed = a;
                  on = b;
                  radius = c;
         }
         ~Fan() {
         }
         int get_speed() {
                  return speed;
         }
         bool get_on() {
                  return on;
         }
         double get_radius() {
                  return radius;
         }
         void set_speed(int a) {
                  speed = a;
         }
         void set_on(bool a) {
                  on = a;
         }
         void set_radius(double a) {
                  radius = a;
         }
};

int main() {
 Fan a, b;
 a.set_speed(3);
 a.set_on(true);
 a.set_radius(10);
 b.set_speed(2);
 b.set_on(false);
 b.set_radius(5);
 cout << "Speed is : " << a.get_speed() << endl;
 cout << "Is it on? (on : 1, if off : 0) : " << a.get_on() << endl;
 cout << "Radius is : " << a.get_radius() << endl;
 cout << "Speed is : " << b.get_speed() << endl;
 cout << "Is it on? (on : 1, if off : 0) : " << b.get_on() << endl;
 cout << "Radius is : " << b.get_radius() << endl;
 system("pause");
 return 0;
}

9-3
#include <iostream>
using namespace std;
class Account {
private :
         int id;
         double balance, annualInterestRate;
public :
         Account() {
                  id = 0;
                  balance = annualInterestRate = 0.0;
         }
         Account() {
         }
         int get_id() {
                  return id;
         }
         double get_balance() {
                  return balance;
         }
         double get_annualInterestRate() {
                  return annualInterestRate;
         }
         void set_id(int a) {
                  id = a;
         }
         void set_balance(double a) {
                  balance = a;
         }
         void set_annualInterestRate(double a) {
                  annualInterestRate = a;
         }
         double getMonthlyInterestRate() {
                  return get_annualInterestRate() / 12;
         }
         double withdraw(double amount) {
                  return balance -= amount;
         }
         double deposit(double amount) {
                  return balance += amount;
         }
};

int main() {
         Account a;
         a.set_id(1122);
         a.set_balance(20000);
         a.set_annualInterestRate(4.5);
         a.withdraw(2500);
         a.deposit(3000);
         cout << "잔액 : " << a.get_balance() << endl;
         cout << "월 이자 : " << a.get_balance()*a.getMonthlyInterestRate()/100 << endl;
         system("pause");
         return 0;
}

9-4
#include <iostream>
#include <cmath>
using namespace std;
class MyPoint {
private :
         double x, y;
public :
         MyPoint() {
                  x = y = 0.0;
         }
         MyPoint(double a, double b) {
                  x = a;
                  y = b;
         }
         ~MyPoint() {
         }
         double get_x() {
                  return x;
         }
         double get_y() {
                  return y;
         }
         double distance(double a, double b) {
                  return sqrt(pow(a-x, 2) + pow(b-y,2));
         }
};

int main() {
         MyPoint a(0, 0.0);
         MyPoint b(10, 30.5);

         double distance = a.distance(b.get_x(), b.get_y());
         cout << "두 점 사이의 거리는 : " << distance << endl;
         system("pause");
         return 0;
}

댓글 없음:

댓글 쓰기