'학과수업들/C++ 프로그래밍'에 해당되는 글 4건
- 2008/10/12 C++ 프로그래밍 기초, 8장 연습문제
- 2008/10/10 C++ 프로그래밍 기초, 7장 연습문제
- 2008/09/19 C++ 프로그래밍 기초, 6장 연습문제 (7번, 8번)
- 2008/09/19 C++ 프로그래밍 기초, 5장 연습문제
// 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();
}
#include<iostream>
using namespace std;
struct sungjuk{
char name[20];
int kor;
int eng;
int mat;
int tot;
double ave;
};
void totAve(sungjuk &s);
void prn(sungjuk s);
void main()
{
sungjuk s = {"성윤정",100,80,95};
totAve(s); //총점과 평균
prn(s); //출력
}
void totAve(sungjuk &s)
{
s.tot = s.kor + s.eng + s.mat;
s.ave = s.tot / 3.0;
}
void prn(sungjuk s)
{
cout<<"\n이름\t국어\t영어\t수학\t총점\t평균";
cout<<"\n -------------------------------------------";
cout<<"\n"<<s.name<<"\t"<<s.kor<<"\t"<<s.eng<<"\t"<<s.mat<<"\t"<<s.tot<<"\t"<<s.ave<<"\n";
}
#include<iostream>
using namespace std;
struct sungjuk{
char name[20];
int kor;
int eng;
int mat;
int tot;
double ave;
};
void init(sungjuk &s);
void totAve(sungjuk &s);
void prn(sungjuk s);
void main()
{
sungjuk s;
init(s);
totAve(s); //총점과 평균
prn(s); //출력
}
void init(sungjuk &s)
{
cout<<"이름을 입력하세요.-> ";
cin>>s.name;
cout<<"국어 점수를 입력하세요.-> ";
cin>>s.kor;
cout<<"영어 점수를 입력하세요.-> ";
cin>>s.eng;
cout<<"수학 점수를 입력하세요.-> ";
cin>>s.mat;
}
void totAve(sungjuk &s)
{
s.tot = s.kor + s.eng + s.mat;
s.ave = s.tot / 3.0;
}
void prn(sungjuk s)
{
cout<<"\n이름\t국어\t영어\t수학\t총점\t평균";
cout<<"\n -------------------------------------------";
cout<<"\n"<<s.name<<"\t"<<s.kor<<"\t"<<s.eng<<"\t"<<s.mat<<"\t"<<s.tot<<"\t"<<s.ave<<"\n";
}
#include<iostream>
using namespace std;
struct sungjuk{
char name[20];
int kor;
int eng;
int mat;
int tot;
double ave;
};
void init(sungjuk s);
void totAve(sungjuk &s);
void prn(sungjuk s);
void main()
{
sungjuk s[5];
for(int i=0; i<5; i++){
init(s[i]);
}
}
void init(sungjuk s)
{
cout<<"이름을 입력하세요.-> ";
cin>>s.name;
cout<<"국어 점수를 입력하세요.-> ";
cin>>s.kor;
cout<<"영어 점수를 입력하세요.-> ";
cin>>s.eng;
cout<<"수학 점수를 입력하세요.-> ";
cin>>s.mat;
cout<<"\n";
totAve(s);
prn(s);
}
void totAve(sungjuk &s)
{
s.tot = s.kor + s.eng + s.mat;
s.ave = s.tot / 3.0;
}
void prn(sungjuk s)
{
cout<<"\n이름\t국어\t영어\t수학\t총점\t평균";
cout<<"\n -------------------------------------------";
cout<<"\n"<<s.name<<"\t"<<s.kor<<"\t"<<s.eng<<"\t"<<s.mat<<"\t"<<s.tot<<"\t"<<s.ave<<"\n";
}
7. 2차원 배열을 이용하여 두 행렬에 대한 차를 구하는 함수를 작성하라.
#include<iostream>
#include<iomanip>
using namespace std;
// 7_1. 두 행렬에 대한 차를 구하는 함수의 원형 정의
void sub(int (*a)[4], int (*b)[4], int (*c)[4]);
void main()
{
int a[3][4]={ {10,20,30,40}, {20,40,60,80}, {10,30,50,70} };
int b[3][4]={ {1,2,3,4}, {5,6,7,8}, {9,10,11,12} };
int c[3][4];
int row,col;
sub(a,b,c);
cout<<" 두 행렬의 차를 출력하기";
cout<<"\n==============================\n";
for(row=0; row<3; row++){
for(col=0; col<4; col++)
cout<<setw(7)<<c[row][col];
cout<<'\n';
}
}
// 7_2. 두 행렬에 대한 차를 구하는 함수 정의
void sub(int (*a)[4], int (*b)[4], int (*c)[4])
{
int row,col;
for(row=0; row<3; row++){
for(col=0; col<4; col++)
c[row][col] = a[row][col] - b[row][col];
}
}
[실행결과]
두 행렬의 차를 출력하기
==============================
9 18 27 36
15 34 53 72
1 20 39 58
8. 다음 프로그램에서 잘못된 곳을 수정하라.
#include<iostream>
using namespace std;
void main()
{
int a[4][5]={0,};
int r,c;
for(r=0; r<4; r++){ //for(r=0; r<5; r++){
cout<<"\n";
for(c=0; c<5; c++) //for(c=0; c<4; c++)
cout<<a[r][c]<<"\t";
}
}
[실행결과]
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
1. 다음 프로그램의 실행 결과를 적어라.
#include<iostream>
using namespace std;
void main()
{
int x=10,y=15,xvalue,yvalue;
int *ptrx=&x, *ptry=&y;
xvalue=*ptrx, yvalue=*ptry;
cout<<"\n xvalue = "<< xvalue <<" *ptrx = "<<*ptrx;
cout<<"\n yvalue = "<< yvalue <<" *ptry = "<<*ptry;
ptrx = ptry;
cout<<"\n *ptrx = "<< *ptrx <<" *ptry = "<<*ptry;
cout<<"\n xvalue = "<< xvalue <<" yvalue = "<<yvalue<<"\n";
}
[실행결과]
xvalue = 10 *ptrx = 10
yvalue = 15 *ptry = 15
*ptrx = 15 *ptry = 15
xvalue = 10 yvalue = 15
2. 다음 프로그램의 실행 결과를 적어라.
#include<iostream>
using namespace std;
void func01(int value)
{
value+=20;
cout<< "\n func01 = "<<value;
}
void func02(int *address)
{
*address+=20;
cout<< "\n func02 = "<<*address;
}
void main()
{
int a=20;
func01( a );
cout << "\n main 함수에서 func01 호출후 a =" << a <<"\n";
func02( &a );
cout << "\n main 함수에서 func02 호출후 a =" << a;
}
[실행결과]
func01 = 40
main 함수에서 func01 호출후 a =20
func02 = 40
main 함수에서 func02 호출후 a =40
3. 절대값 구하는 함수를 주소 호출 방식으로 작성해서 실 매개변수의 값이 변경되도록 해보아라.
#include<iostream>
using namespace std;
// 3_1. 절대값을 구하는 함수 absolute의 선언(주소 호출 방식)
void absolute(int *a);
void main()
{
int a=-10;
cout<<" main 에서 함수 호출 전 a 값 = "<< a <<"\n";
absolute(&a);
cout<<" main 에서 함수 호출 후 a 값 = "<< a <<"\n";
}
// 3_2. 절대값을 구하는 함수 absolute의 정의(주소 호출 방식)
void absolute(int *a)
{
if(*a<0)
*a=-*a;
cout<<" absolute 함수에서의 a 값 = "<< *a <<"\n";
}
[실행결과]
main 에서 함수 호출 전 a 값 = -10
absolute 함수에서의 a 값 = 10
main 에서 함수 호출 후 a 값 = 10
4. 절대값을 구하는 함수를 레퍼런스 호출 방식으로 작성해서 실 매개변수의 값이 변경되도록 해보아라.
#include<iostream>
using namespace std;
// 4_1. 절대값을 구하는 함수 absolute의 선언(레퍼런스 호출 방식)
void absolute(int &a);
void main()
{
int a=-10;
cout<<" main 에서 함수 호출 전 a 값 = "<< a <<"\n";
absolute(a);
cout<<" main 에서 함수 호출 후 a 값 = "<< a <<"\n";
}
// 4_2. 절대값을 구하는 함수 absolute의 정의(레퍼런스 호출 방식)
void absolute(int &a)
{
if(a<0)
a=-a;
cout<<" absolute 함수에서의 a 값 = "<< a <<"\n";
}
[실행결과]
main 에서 함수 호출 전 a 값 = -10
absolute 함수에서의 a 값 = 10
main 에서 함수 호출 후 a 값 = 10






Recent Comment