코딩 및 기타

c++ 클래스 복습

정지홍 2023. 2. 16. 23:49

#include <iostream>
using namespace std;

class Triangle {
int z = 0;
public:
int x, y;
Triangle() { this->x = 1, this->y = 1; };
Triangle(int x, int y);
~Triangle();
double getArea();
void setX(int a);
void setY(int b);
void printXY();
void plusZ() { z += 1; };
int getZ() { return this->z; };
};

Triangle::Triangle(int x, int y) {
this->x = x;
this->y = y;
}

Triangle::~Triangle() {
plusZ();
cout << "소멸 x:" << this->x << " y:" << this->y<<" z:"<<getZ() << endl;
}

double Triangle::getArea() {
return x * y * 0.5;
}

void Triangle::setX(int a) {
this->x = a;
}

void Triangle::setY(int b) {
this->y = b;
}

void Triangle::printXY() {
cout << "x:" << this->x << " y:" << this->y << endl;
}

int main(void) {

Triangle a;
Triangle b(10, 10);
cout << "a area:" << a.getArea() << endl;
a.setX(5);
a.setY(6);
cout << "a area:" << a.getArea() << endl;
cout << "b area:" << b.getArea() << endl;
a.printXY();
b.printXY();
return 0;
}