1.
#include <iostream>

void myfunc(void) {
        std::cout << "myfunc(void) called" << std::endl;
}
void myfunc(char c) {
        std::cout << "myfunc(char c) called" << std::endl;
}
void myfunc(int a, int b) {
        std::cout << "myfunc(int a, int b) called" << std::endl;
}

int main(void) {
        myfunc();
        myfunc('A');
        myfunc(12, 13);
        return 0;
}

myfunc(void) called
myfunc(char c) called
myfunc(int a, int b) called



2.
#include <iostream>

int adder(int num1 = 1, int num2 = 3) {
        return num1 + num2;
}

int main(void) {
        std::cout << adder() << std::endl;
        std::cout << adder(5) << std::endl;
        std::cout << adder(8, 5) << std::endl;
        return 0;
}

4
8
13

'Programming > C++' 카테고리의 다른 글

표준입출력  (0) 2011.04.08

1.
#include <iostream> // .h 를 안붙여도 된다
int main(void) {
        int num = 20;
        std::cout << "hello world!" << std::endl;  // 개행문자 = std::endl
        std::cout << "hello" << " world" << std::endl;
        std::cout << num << "" << 'A';
        std::cout << "" << 3.14 << std::endl;
        return 0;
}

hello world!
hello world
20A3.14



2.
#include <iostream>
int main(void) {
        int val1;
        std::cout << "Insert first number : ";
        std::cin >> val1;
        int val2;
        std::cout << "Insert second number : ";
        std::cin >> val2;
        int result = val1 + val2;
        std::cout << "result : " << result << std::endl;
        return 0;
}

Insert first number : 5
Insert second num : 6
result : 11



3.
#include <iostream>
int main(void) {
        int val1, val2;
        int result = 0;
        std::cout << "Insert Two Number : ";
        std::cin >> val1 >> val2;
        if(val1 < val2) {
                for(int i = val1 + 1;i < val2;i++)
                        result += i;
        }
        else {
                for(int i = val2 + 1;i < val1;i++)
                        result += i;
        }
        std::cout << "Sum between two number : " << result << std::endl;
        return 0;
}

Insert Two Number : 0 11
Sum between two number : 55



4.
#include <iostream>
int main(void) {
        char name[100];
        char lang[100];
        std::cout << "Insert your name : ";
        std::cin >> name;
        std::cout << "Insert your favorite language : ";
        std::cin >> lang;
        std::cout << "Your name is " << name << std::endl;
        std::cout << "Your favorite language is " << lang << std::endl;
        return 0;
}

Insert your name : gaffel
Insert your favorite language : KOREAN
Your name is gaffel
Your favorite language is KOREAN

'Programming > C++' 카테고리의 다른 글

함수오버로딩  (0) 2011.04.08

+ Recent posts