2일차 정리
using namespace std;
include <iostream>
아무것도 반환하지 않으면 void
반환하는 값이 있으면 반환값 인수형.
<stdafx.h> precompiled header, 재 컴파일이 필요없는 header들
&num 레퍼런스 = 본래 변수와 저장 주소를 공유하는 또다른 변수명을 만듬(포인터와 유사함)
new
같은공간 KIM이름의 함수에서는 KIM::func2에서 앞의 KIM생략가능.
--------------------------
#함수의 오버로딩
동일한 이름의 함수를 만들고 인자를 몇 개 전달 하느냐에 따라서 어떠한 함수를 호출할 것인지를 구분하는 방법
- void 함수의 이름(자료형, 자료형, 자료형){ }
1. 함수의 이름
2. 자료형
3. 매개변수의 갯수
@함수의 오버로딩 ----------
void MyFunc(){
cout << "MyFunc(void) called" << endl;
}
void MyFunc(int a) {
cout << "MyFunc(int a) called"<<endl;
}
void main(void)
{
MyFunc();
MyFunc(11);
}
--------실행결과
MyFunc(void) called
MyFunc(int a) called
계속하려면 아무 키나 누르십시오 . . .
-------------------
@인수와 포인터 //
배열은 첫번째 문자의 주소만 적어도 빈칸까지 출력.
#include "stdafx.h"
#include <iostream>
using namespace std;
void MyFunc(char a){ // 케릭터
cout << "MyFunc(char a) called" << endl;
}
void MyFunc(char * c) { // 케릭터형 포인터
cout << "MyFunc(char *c) called"<<endl;
}
void main(void)
{
MyFunc('A');
MyFunc("Hello");
}
---------실행결과
MyFunc(char a) called
MyFunc(char *c) called
계속하려면 아무 키나 누르십시오 . . .
----------------
@--------bool 형의 데이터 크기
#include "stdafx.h"
#include <iostream>
using namespace std;
void main(void)
{
bool a = false;
cout << "a: " << a<<endl;
cout << "true : " << true << endl;
cout << "sizeof bool " << sizeof(bool) << endl;
}
-------실행결과
a: 0
true : 1
sizeof bool 1
계속하려면 아무 키나 누르십시오 . . .
----------------
@레퍼런스 -- 포인트
#include "stdafx.h"
#include <iostream>
using namespace std;
int & func () {
int num = 10; int &su = num;
return su;
}
void main () {
int su;
su = func();
cout << su << endl;
su++;
cout << su << endl;
}
실행결과--------------
@-----레퍼런스 호출
#include "stdafx.h"
#include <iostream>
using namespace std;
void Swap (int &su1, int &su2) {
int tmp=su1;
su1 = su2;
su2 = tmp;
}
void main () {
int num1= 10, num2=20;
cout<<"num1 : "<<num1 <<",num2:" << num2<< endl;
Swap(num1, num2);
cout<<"num1 : "<<num1 <<",num2:" << num2<< endl;
}
---실행결과
num1 : 10,num2:20
num1 : 20,num2:10
계속하려면 아무 키나 누르십시오 . . .
------------
@레퍼런스로 더 큰 수 반환----------
#include "stdafx.h"
#include <iostream>
using namespace std;
int & Swap (int &su1, int &su2) {
if (su1 >= su2) {
return su1;
}
else return su2;
}
void main () {
int num1, num2;
cin >> num1;
cin >> num2;
cout << Swap(num1, num2);
}
------실행결과
10 20
20계속하려면 아무 키나 누르십시오 . . .
---------------
@레퍼런스로 배수를 출력---------------
#include "stdafx.h"
#include <iostream>
using namespace std;
/*
하나의 수를 입력 받아 1~100사이의 배수의 합을 출력하시오
•5를 입력 받았다면 1~100 사이의 5의 배수의 합 출력
*/
int Swap (int &su) {
int sum=0;
for(int i=1; i <= 100; i++){
if (i%su == 0){sum+=i;}
}
return sum;
}
void main () {
int num;
cin >> num;
cout << Swap(num);
}
-----------실행결과
10
550계속하려면 아무 키나 누르십시오 . . .
------------------
@소문자를 대문자로
#include "stdafx.h"
#include <iostream>
using namespace std;
void func(char & a) {
if('a'<= a && a <= 'z')
a = a -32;
}
void main () {
char A;
cin >> A;
func(A);
cout << A;
}
---------실행결과
d
D계속하려면 아무 키나 누르십시오 . . .
------------------
@동적할당값 입력
#include "stdafx.h"
#include <iostream>
using namespace std;
void main ( ) {
int *ptr;
ptr = new int;
*ptr = 10;
cout << *ptr << endl;
cout << ptr << endl;
delete ptr;
}
------실행결과
10
003774D8
계속하려면 아무 키나 누르십시오 . . .
----
@ malloc 대신 new 연산자로 공간 할당 후 데이터 입력
#include "stdafx.h"
#include <iostream>
using namespace std;
void main ( ) {
int *ptr;
ptr = new int[2];
44
ptr[0] = 10;
ptr[1] = 20;
cout << ptr[0] << ptr[1] <<endl;
delete ptr;
}
--------실행결과
1020
계속하려면 아무 키나 누르십시오 . . .
------------------
@new로 공간 할당 후 배열마다 입력
#include "stdafx.h"
#include <iostream>
using namespace std;
void main ( ) {
int * ptr;
int n, ret;
cin >> n;
ptr = new int[n];
for(int i=0;i<n;i++){
ptr[i]=i;
}
for(int i=0;i<n;i++){
cout << ptr[i]<< endl;
}
delete []ptr; // 배열"[]" 공간을 하나씩 지운다.
}
----------실행결과
3
0
1
2
계속하려면 아무 키나 누르십시오 . . .
------------------
@new연산자로 공간 할당
- for 반복문과 배열로 데이터 입출력
#include "stdafx.h"
#include <iostream>
using namespace std;
void main ( ) {
int len;
int *ptr; //ptr이란 명칭의 포인터
cout << "학생의 숫자를 입력하세요 :";
cin >> len;
ptr = new int[len];
for(int x=0; x <len; x++){
cout << x+1 << "번 학생 점수 입력: ";
cin >> ptr[x];
}
for(int x=0; x<len;x++)
cout << x+1<< "번 학생 : "<<ptr[x] << endl;
delete []ptr;
}
-----------실행결과
학생의 숫자를 입력하세요 :3
1번 학생 점수 입력: 100
2번 학생 점수 입력: 90
3번 학생 점수 입력: 60
1번 학생 : 100
2번 학생 : 90
3번 학생 : 60
계속하려면 아무 키나 누르십시오 . . .
--------------------
@namespace를 활용한 함수 사용
#include "stdafx.h"
#include <iostream>
using namespace std;
namespace KIM{
void Func(){ cout <<"KIM이 정의한 함수"<<endl;}
}
namespace LEE{
void Func(){ cout <<"LEE이 정의한 함수"<<endl;}
}
void main ( ) {
KIM::Func();
LEE::Func();
}
--------실행결과
KIM이 정의한 함수
LEE이 정의한 함수
계속하려면 아무 키나 누르십시오 . . .
------------------
@namespace 맨 위에 사용할 함수 이름을 정의하고 호출
#include "stdafx.h"
#include <iostream>
using namespace std;
namespace KIM{
void Func(); // func와 func2라는 이름의 함수 선언
void Func2();
}
namespace LEE{
void Func();
}
void main ( ) {
KIM::Func();
LEE::Func();
}
void KIM::Func(){
cout <<"KIM이 정의한 함수"<<endl;
Func2();// KIM::func2에서 앞의 KIM생략가능.
}
void LEE::Func(){
cout <<"LEE가 정의한 함수"<<endl;
}
void KIM::Func2(){
cout<<"KIM이 정의한 두번째 함수"<<endl;
}
---------실행결과
KIM이 정의한 함수
KIM이 정의한 두번째 함수
LEE가 정의한 함수
계속하려면 아무 키나 누르십시오 . . .
------------------
@ C언어의 구조체
#include "stdafx.h"
#include <iostream>
using namespace std;
typedef struct handphone{//typedef라는 자료형으로 handphone이란 이름의 구조체 선언
char name[20];
int price;
char phone_num[14];
}HANDPHONE, *PHANDPHONE;//HANDPHONE이란 이름으로 호출, 포인터 호출은 *PHANDPHONE.
void main ( ) {
HANDPHONE phone={"INFISCAP", 8000000, "010-1234-5678"};
int a;
}
--------실행결과
계속하려면 아무 키나 누르십시오 . . .
---------------
@C언어 구조체 복습
#include "stdafx.h"
#include <iostream>
using namespace std;
typedef struct handphone{
char name[20];
int price;
char phone_num[14];
}HANDPHONE, *PHANDPHONE;
void main ( ) {
HANDPHONE phone={"INFISCAP", 8000000, "010-1234-5678"};
cout<<phone.name<< endl;
cout<<phone.price<< endl;
cout<<phone.phone_num<< endl;
/*
for(int i=0; phone.name[i];i++){
cout <<phone.name[i] <<endl;
}
*/
}
----------실행결과
INFISCAP
8000000
010-1234-5678
계속하려면 아무 키나 누르십시오 . . .
-------------------