728x90
반응형
#include <iostream>

int main(void)
{
	int num = 20;
	std::cout << "Hello World!" << std::endl;
	std::cout << "Hello " << "World!" << std::endl;
	std::cout << num << ' ' << 'A';
	std::cout << ' ' << 3.14 << std::endl;
	return 0;
}

// HelloWorld.cpp
#include <iostream>

int main(void)
{
	int val1;
	std::cout << "첫번째 숫자입력 :";
	std::cin >> val1;

	int val2;
	std::cout << "두번째 숫자입력 :";
	std::cin >> val2;

	int result = val1 + val2;
	std::cout << "덧셈 결과: " << result << std::endl;

	return 0;
}

// SimpleAdder.cpp
#include <iostream>

int main(void) {

	int val1, val2;
	int result = 0;
	std::cout << "두개의 숫자 입력";
	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 << "두 수 사이의 정수 합: " << result << std::endl;

	return 0;
}

// BetweenAdder.cpp
#include <iostream>

int main(void) {

	char name[100];
	char lang[100];

	std::cout << "What is your name?";
	std::cin >> name;

	std::cout << "What is your favorite language?";
	std::cin >> lang;

	std::cout << "My name is " << name << ".\n";
	std::cout << "My favorite language is " << lang << "." << std::endl;

	return 0;
}

// StringIO.cpp
#include <iostream>

void MyFunc(void);
void MyFunc(char c);
void MyFunc(int a, int b);


int main(void)
{
	MyFunc();
	MyFunc('A');
	MyFunc(12, 13);

	return 0;
}

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;
}

// FunctionOverloading.cpp
#include <iostream>

void swap(int*, int*);
void swap(char*, char*);
void swap(double*, double*);


int main(void) {
	int num1 = 20, num2 = 30;
	swap(&num1, &num2);
	std::cout << num1 << ' ' << num2 << std::endl;

	char ch1 = 'A', ch2 = 'Z';
	swap(&ch1, &ch2);
	std::cout << ch1 << ' ' << ch2 << std::endl;

	double dbl1 = 1.111, dbl2 = 5.555;
	swap(&dbl1, &dbl2);
	std::cout << dbl1 << ' ' << dbl2 << std::endl;

	return 0;
}

void swap(int* pa, int* pb) {
	int temp;
	temp = *pa;
	*pa = *pb;
	*pb = temp;
}

void swap(char* pa, char* pb) {
	int temp;
	temp = *pa;
	*pa = *pb;
	*pb = temp;
}

void swap(double* pa, double* pb) {
	int temp;
	temp = *pa;
	*pa = *pb;
	*pb = temp;
}

//26p 01-2 [Function Overloading]
#include <iostream>

int Adder(int num1 = 1, int num2 = 2) {
	return num1 + num2;
}

int main(void) {

	std::cout << Adder() << std::endl;
	std::cout << Adder(5) << std::endl;
	std::cout << Adder(3, 5) << std::endl;

	return 0;
}

// DefaultValue1.cpp
#include <iostream>

int Adder(int num1 = 1, int num2 = 2);

int main(void) {

	std::cout << Adder() << std::endl;
	std::cout << Adder(5) << std::endl;
	std::cout << Adder(3, 5) << std::endl;

	return 0;
}

int Adder(int num1, int num2) {
	return num1 + num2;
}

// DefaultValue2.cpp
#include <iostream>

int BoxVolume(int length, int width = 1, int height = 1);

int main(void) {
	std::cout << "[3, 3, 3] :" <<BoxVolume(3, 3, 3)<< std::endl;
	std::cout << "[5, 5, D]:" << BoxVolume(5, 5) << std::endl;
	std::cout << "[7, D, D] : " << BoxVolume(7) << std::endl;
	//std::cout << "[D, D, D]: " << BoxVolume() << std::endl;

	return 0;
}

int BoxVolume(int length, int width, int height) {
	return length * width * height;
}

// DefaultValue3.cpp
#include <iostream>

namespace BestCompImpl {
	void SimpleFunc(void) {
		std::cout << "BestCom이 정의한 함수" << std::endl;
	}
}

namespace ProgCompImpl {
	void SimpleFunc(void) {
		std::cout << "ProgCom이 정의한 함수" << std::endl;
	}
}


int main(void) { 
	
	BestCompImpl::SimpleFunc();
	ProgCompImpl::SimpleFunc();

	return 0; 
}
#include <iostream>

namespace BestCom {
	void SimpleFunc(void);
}

namespace ProgCom {
	void SimpleFunc(void);
}

int main(void) {
	BestCom::SimpleFunc();
	ProgCom::SimpleFunc();

	return 0;
}

void BestCom::SimpleFunc(void) {
	std::cout << "BestCom이 정의한 함수" << std::endl;
}

void ProgCom::SimpleFunc(void) {
	std::cout << "ProgCom이 정의한 함수" << std::endl;
}

// NameSp2.cpp
#include <iostream>

namespace BestCom {
	void SimpleFunc(void);
}

namespace BestCom {
	void PrettyFunc(void);
}

namespace ProgCom {
	void SimpleFunc(void);
}

namespace ProgCom {
	void SimpleFunc(void);
}

int main(void) {
	BestCom::SimpleFunc();

	return 0;
}

void BestCom::SimpleFunc(void) {
	std::cout << "BestCom이 정의한 함수" << std::endl;
	PrettyFunc();
	ProgCom::SimpleFunc();
}

void BestCom::PrettyFunc(void) {
	std::cout << "So Pretty" << std::endl;
}

void ProgCom::SimpleFunc(void) {
	std::cout << "ProgCom이 정의한 함수" << std::endl;
}

// NameSp3.cpp
#include <iostream>

namespace SamSung {
	int math(int, int);
}

namespace Apple {
	int math(int, int);
}

int main(void) {

	int result = Apple::math(2, 4);

	std::cout << "" << result << "" << std::endl;

	return 0;
}

int SamSung::math(int a, int b) {
	return a + b;
}

int Apple::math(int a, int b) {
	return a - b;
}

/*삼성이 만든 math 라는 함수와 Apple에서 만든 Math 라는 함수의 이름이 중복되지만
범위지정연산자(scope resolution operator) :: 를 이용하여 회사명::함수명 으로 구분지었으며
메인함수에서 이를 활용하여 호출하였다.*/

// My Application Examples
#include <iostream>

namespace Hybrid {
	void HybFunc(void) {
		std::cout << "So simple function!" << std::endl;
		std::cout << "In namespace Hybrid!" << std::endl;
	}
}

int main(void) {
	using Hybrid::HybFunc;
	Hybrid::HybFunc();

	return 0;
}

// UsingDcl1.cpp
#include <iostream>

using std::cout;
using std::endl;

int main(void) {

	cout << "Hello World" << endl;

	return 0;
}

// My application example
#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int main(void) {
	int num = 20;
	cout << "Hello World!" << endl;
	cout << "Hello " << "World" << endl;
	cout << num << ' ' << 'A';
	cout << ' ' << 3.14 << endl;

	return 0;
}

// UsingDcl2.cpp
#include <iostream>

using namespace std;

int main(void) {
	int num = 20;
	cout << "Hello World!" << endl;
	cout << "Hello " << "World" << endl;
	cout << num << ' ' << 'A';
	cout << ' ' << 3.14 << endl;

	return 0;
}

// UsingDcl3.cpp
#include <iostream>

//using namespace std;

namespace AAA {
	namespace BBB {
		namespace CCC {
			int num1;
			int num2;
		}
	}
}

int main(void) {
	AAA::BBB::CCC::num1 = 20;
	AAA::BBB::CCC::num2 = 30;

	namespace ABC = AAA::BBB::CCC;
	std::cout << ABC::num1 << std::endl;
	std::cout << ABC::num2 << std::endl;

	return 0;
}

// NameAlias.cpp
#include <iostream>
using namespace std;

int main(void) {
	int num = 10;
	int i = 0;

	cout << "true: " << true << endl;
	cout << "false: " << false << endl;

	while (true) {
		cout << i++ << ' ';
		if (i > num)
			break;
	}
	cout << endl;

	cout << "size of 1 : " << sizeof(1) << endl;
	cout << "size of 0: " << sizeof(0) << endl;
	cout << "size of true : " << sizeof(true) << endl;
	cout << "size of false: " << sizeof(false) << endl;

	return 0;
}

// TruAndFalse.cpp
#include <iostream>
using namespace std;

bool IsPositive(int num) {
	if (num <= 0)
		return false;
	else
		return true;
}

int main(void) {
	bool isPos;
	int num;

	cout << "Input number: ";
	cin >> num;

	isPos = IsPositive(num);
	if (isPos)
		cout << "Positie number" << endl;
	else
		cout << "Negative number" << endl;

	return 0;
}

// DataTypeBool.cpp
#include <iostream>
using namespace std;

int main(void) {
	int num1 = 1020;
	int& num2 = num1;

	num2 = 3047;
	cout << "VAL: " << num1 << endl;
	cout << "REF: " << num2 << endl;

	cout << "VAL: " << &num1 << endl;
	cout << "REF: " << &num2 << endl;

	return 0;
}
// Reference.cpp
#include <iostream>
using namespace std;

int main(void) {

	int arr[3] = { 1, 3, 5 };
	int& ref1 = arr[0];
	int& ref2 = arr[1];
	int& ref3 = arr[2];

	cout << ref1 << endl;
	cout << ref2 << endl;
	cout << ref3 << endl;

	return 0;
}

//RefArrElem.cpp
#include <iostream>

using namespace std;

int main(void) {
	int num = 12;
	int* ptr = &num;
	int** dptr = &ptr;

	int& ref = num;
	int* (&pref) = ptr;
	int** (dpref) = dptr;

	cout << ref << endl;
	cout << *pref << endl;
	cout << **dpref << endl;

	return 0;
}

//Refptr.cpp
#include <iostream>
//void swap(int& rfa, int& rfb);
void swap(int* pa, int* pb);
using namespace std;


int main(void) {

	int a = 10;
	int b = 20;
	
	swap(a, b);

	cout << a << b << endl;

	return 0;
}

//void swap(int& rfa, int& rfb) {
//	int temp;
//
//	temp = rfa;
//	rfa = rfb;
//	rfb = temp;
//}

void swap(int* pa, int* pb) {
	int temp;
	
	temp = *pa;
	*pa = *pb;
	*pb = temp;
}

// swap (pointer, reference)
#include <iostream>
using namespace std;

int& RefRetFuncOne(int& ref) {
	ref++;
	return ref;
}

int main(void) {
	int num1 = 1;
	int& num2 = RefRetFuncOne(num1);

	num1++;
	num2++;

	cout << "num1: " << num1 << endl;
	cout << "num2: " << num2 << endl;

	return 0;
}
// RefReturnOne.cpp
728x90
반응형

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

Class  (0) 2022.09.26
BankingSystemver01.cpp  (0) 2022.09.26
Pointer Examples  (0) 2022.09.10
Transfer an array to a function's arguments  (0) 2022.09.09
User-Defined Type  (0) 2022.09.08

+ Recent posts