2020年4月6日 星期一

4.1-Class Point in plane

程式碼:
main.cpp
#include <iostream>
#include "Point.h"
using namespace std;

int main(void) {
 Point point;

 point.Set(0, 5);
 cout << point.RetrieveVertical() << " " << point.RetrieveHorizontal() << endl;

 point.Move(1, 2);
 cout << point.RetrieveVertical() << " " << point.RetrieveHorizontal() << endl;
 for (int i = 0; i < 4; i++) {
  point.Rotate();
  cout << point.RetrieveVertical() << " " << point.RetrieveHorizontal() << endl;
 }

 return 0;
}

Point.cpp
#include "Point.h"

void Point::Set(int x, int y)
{
 vertical = x;
 horizontal = y;
}

void Point::Move(int x, int y)
{
 vertical += x;
 horizontal += y;
}
void Point::Rotate()
{
 int temp = vertical;
 vertical = horizontal;
 horizontal = temp * (-1);
}


Point.h
#ifndef Point_H
#define Point_H
#include <iostream>
using namespace std;

class Point
{
private:
 int vertical;
 int horizontal;

public:
 Point()
 {
  vertical = 0;
  horizontal = 0;
 }

 void Set(int vertical, int horizontal);
 void Move(int x, int y);
 void Rotate();
 int RetrieveVertical() const { return vertical; }
 int RetrieveHorizontal() const { return horizontal; }
};

#endif //Point_H

沒有留言:

張貼留言