Skip to content

c++ 캡슐화 및 캡슐화 안된버전  #11

Description

@KangHoyong
#include <iostream>

using namespace std;
class Point2d
{
public :
	void show(); 
	void setX(int x);
	void setY(int y);
	int getX() { return x; }
	int getY() { return y; }
 

private :
	int x; // x좌표의 범위 : 0 ~ 100 
	int y; // y좌표의 범위 : 0 ~ 100
};
// 캡슐화 된 경우 
void Point2d::show()
{
	cout << "x 좌표 : " << x << endl;
	cout << "y 좌표 : " << y << endl;

}
// 캡슐화 되지 않은경우 !! 
class PointShow
{
public:
	void showData(Point2d p); 
};
void PointShow::showData(Point2d p)
{
	cout << "외부클래스 만든경우" << endl;
	cout << "x좌표 : " << p.getX() << endl;
	cout << "y좌표 : " << p.getY() << endl;
}
void Point2d::setX(int x)
{
	if (x < 0 || x>100)
	{
		cout << "x 좌표 입력 오류, 확인 요망 0 ~ 100 까지 " << endl;
		return;
	}
	this->x = x; 
	// x = x ; 

}
void Point2d::setY(int y)
{
	if (x < 0 || x>100)
	{
		cout << "x 좌표 입력 오류, 확인 요망 0 ~ 100 까지 " << endl;
		return;
	}
	this->y = y; 
	// y = y; 
}
int main()
{
	int x, y; 
	cout << "좌표입력 : "; 
	cin >> x >> y; 

	Point2d p1;
	p1.setX(x);
	p1.setY(y);
	p1.show();

	// 캡슐화를 하지 않은경우
	PointShow show;
	show.showData(p1);
	return 0; 
}


Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions