C++ 프로그래밍 기초, 8장 연습문제
Posted 2008/10/12 17:45, Filed under: 학과수업들/C++ 프로그래밍
// 1. 다음 프로그램을 실행하면 에러가 발생한다. 에러가 발생하지 않도록 수정하라.
#include<iostream>
using namespace std;
class CRect
{
public :
void print();
//int left; //public으로 정의
//int top; //public으로 정의
//int right; //public으로 정의
//int bottom; //public으로 정의
};
void CRect::print()
{
cout << "(" << left << ", " << top<< ", " << right << ", " << bottom << " )" <<endl;
}
void main()
{
CRect obj_1;
obj_1.left=0; obj_1.top=0; //error
obj_1.right=20; obj_1.bottom='20'; //error
obj_1.print();
}
// 1. 다음 프로그램을 실행하면 에러가 발생한다. 에러가 발생하지 않도록 수정하라.
// (데이터 은닉 private 접근 지정자를 유지하면서 private 멤버 변수값을 수정하기
// 위해서 멤버함수를 추가하는 방법)
#include<iostream>
using namespace std;
class CRect
{
int left;
int top;
int right;
int bottom;
public :
void print();
void setLeft(int l); //멤버함수 추가
void setTop(int t); //멤버함수 추가
void setRight(int r); //멤버함수 추가
void setBottom(int b); //멤버함수 추가
};
void CRect::setLeft(int l){left = l;} //멤버함수 추가
void CRect::setTop(int t){top = t;} //멤버함수 추가
void CRect::setRight(int r){right = r;} //멤버함수 추가
void CRect::setBottom(int b){bottom = b;} //멤버함수 추가
void CRect::print()
{
cout << "(" << left << ", " << top<< ", " << right << ", " << bottom << " )" <<endl;
}
void main()
{
CRect obj_1;
obj_1.setLeft(0); obj_1.setTop(0); //error
obj_1.setRight(20); obj_1.setBottom(20); //error
obj_1.print();
}
// 2. SetRect()를 멤버함수로 정의하라.
#include <iostream>
using namespace std;
class CRect
{
int left;
int top;
int right;
int bottom;
public :
void print();
void SetRect(int l, int t, int r, int b);
};
// SetRect()를 멤버함수로 정의
void CRect::SetRect(int l, int t, int r, int b)
{
this->left = l;
this->top = t;
this->right = r;
this->bottom = b;
}
// SetRect()를 멤버함수로 정의 끝
void CRect::print()
{
cout << "(" << left << ", " << top<< ", " << right << ", " << bottom << " )" <<endl;
}
void main()
{
CRect obj_1;
obj_1.SetRect(0, 0, 20, 20); //error
obj_1.print();
}
// 3. 객체를 초기화할 수 있도록 생성자를 오버로딩하라.
#include <iostream>
using namespace std;
class CRect
{
int left;
int top;
int right;
int bottom;
public :
void prn();
CRect(int l, int t, int r, int b);
CRect(int r, int b);
CRect();
};
CRect::CRect(int l, int t, int r, int b)
{
left=l;
top=t;
right=r;
bottom=b;
}
CRect::CRect(int r, int b)
{
left=0;
top=0;
right=r;
bottom=b;
}
CRect::CRect()
{
left=0;
top=0;
right=0;
bottom=0;
}
void CRect::prn()
{
cout << "(" << left << ", " << top<< ", " << right << ", " << bottom << " )" <<endl;
}
void main()
{
CRect obj_1(0, 0, 20, 20);
CRect obj_2(20, 20);
CRect obj_3;
obj_1.prn();
obj_2.prn();
obj_3.prn();
}
// 4. 객체를 초기화할 수 있도록 생성자에 디폴드 매개변수 값으로 할당받도록 정의하라.
#include <iostream.h>
#include <iomanip.h>
class CRect
{
int left;
int top;
int right;
int bottom;
public :
void prn();
CRect(int l=0, int t=0, int r=0, int b=0);
};
CRect::CRect(int l, int t, int r, int b)
{
left=l;
top=t;
right=r;
bottom=b;
}
void CRect::prn()
{
cout << "(" << left << ", " << top<< ", " << right << ", " << bottom << " )" <<endl;
}
void main()
{
CRect obj_1(20, 20, 20, 20);
CRect obj_2(20, 20, 20);
CRect obj_3(20, 20);
CRect obj_4(20);
CRect obj_5;
obj_1.prn(); obj_2.prn(); obj_3.prn();
obj_4.prn(); obj_5.prn();
}
Response :
0 Trackback
,
0 Comment
Trackback URL : http://mysilpir.net/trackback/318
