π νΌμνλ C++ κ³΅λΆ -1-
π C++ 곡λΆ..
2νλ 2νκΈ° μκ° κ³Όλͺ© μ€ νλμΈ κ°μ²΄μ§ν₯ μ€κ³μμ C++μ μ¬μ©νκΈ°μ, μμ΅μ λͺ©μ μΌλ‘ μ¬λ¦λ°©νμ C++μ 곡λΆνμλ€. λΉμμ 곡λΆνλ λ΄μ©λ€μ μ 리νμ¬ ν¬μ€ν νλ€.
μν€λ μ€λ₯Ό μ£Όλ‘ μ°Έμ‘°νμ¬ κ³΅λΆνλ€.
Input, Output, Overloading
#include<iostream>
void helloworld(void);
void helloworld(int a, int b);
int main(void){
char temp[10];
int a, b;
std::cout << "input any string" << std::endl;
std::cin >> temp;
std::cout << "you typed : " << temp << std::endl;
helloworld();
std::cin >> a;
std::cin >> b;
helloworld(a, b);
return 0;
}
void helloworld(void){
std::cout << "HelloWorld" << std::endl;
std::cout << "type two integer to add" << std::endl;
}
void helloworld(int a, int b){
std::cout << "result : " << a + b << std::endl;
}
input any string
hello!
you typed : hello!
HelloWorld
type two integer to add
3 5
result : 8
cinμ ν΅ν΄ λ¬Έμμ΄κ³Ό μ μλ₯Ό μ
λ ₯λ°κ³ coutμ ν΅ν΄ λ¬Έμμ΄κ³Ό λ μμ μ°μ° κ²°κ³Όλ₯Ό μΆλ ₯νλλ‘ νλ€.
μ΄ λ helloworldν¨μλ overloadingμ ν΅ν΄ μΈμμ λ°λΌ λ€λ₯΄κ² μλνλλ‘ μ½λλ₯Ό μ§ νμ©ν΄λ³΄μλ€.
Default parameter
#include<iostream>
int adder(int a = 1, int b = 2, int c = 3);
int main(void){
int a, b, c;
std::cout << "type three integers to add" << std::endl;
std::cin >> a;
std::cin >> b;
std::cin >> c;
std::cout << "method with default values " << adder() << std::endl;
std::cout << "method without default values " << adder(a, b, c) << std::endl;
std::cout << "method with and without default values #1 " <<adder(a) << std::endl;
std::cout << "method with and without default values #2 " <<adder(a, b) << std::endl;
std::cout << "method with and without default values #3 " <<adder(a, b, c) << std::endl;
return 0;
}
int adder(int a, int b, int c){
return a + b + c;
}
type three integers to add
7 8 9
method with default values 6
method without default values 24
method with and without default values #1 12
method with and without default values #2 18
method with and without default values #3 24
default parameterλΌλ μ²μ보λ νΉμ±μ΄ μμλ€. ν¨μλ₯Ό μ μΈν λ 미리 μΈμλ‘ κ°μ μ£Όλ©΄, μΈμλ‘ ν΄λΉ λΆλΆμ κ°μ λ£μ§ μμμ λ κΈ°λ³ΈμΌλ‘ μ£Όμ΄μ§ μΈμκ° μ리λ₯Ό λμ νλ€. κ²°κ³Όλ₯Ό 보면, μΈμ μμ΄ μ€νλκ²½μ°μ κΈ°λ³ΈμΈμμΈ 1, 2, 3μ΄, μ λ ₯ν μΈμλ₯Ό λ£μ΄μ£Όλ©΄ ν΄λΉλΆλΆμ μ λ ₯ν μΈμλ‘, μ λ ₯λμ§ μμ μΈμλ κΈ°λ³ΈμΈμλ‘ μ°μ°λλκ²μ νμΈν μ μλ€.
Macro function
#include<iostream>
#define SQUARE(X) ((X)*(X))
int main(void){
int a;
std::cout << "type any integer to square" << std::endl;
std::cin >> a;
std::cout << "result : " << SQUARE(a) << std::endl;
return 0;
}
type any integer to square
3
result : 9
Cμμμ λ§μ°¬κ°μ§λ‘ macroλ‘ μ μ²λ¦¬ν ꡬ문μ ν¨μμ²λΌ μ¬μ©ν μ μλ€.
Inline function
#include<iostream>
inline void helloWorld();
int main(void){
helloWorld();
return 0;
}
inline void helloWorld(){
std::cout << "Hello World!" << std::endl;
}
Hello World!
inline functionμ μ€μ λ‘ ν¨μλ₯Ό μ€ννλ λμ , ν¨μ μμ λͺ λ Ήμ ν¨μμ μΉνν΄μ μ€ννλ€. ν¨μμ λ©λͺ¨λ¦¬μμμΈ stackλ©λͺ¨λ¦¬λ₯Ό μ°Έμ‘°νλ©° λ°μν μ μλ μκ°λλΉλ₯Ό μ€μ¬μ€ μ μλ€. λ§€ν¬λ‘λ μ μ²λ¦¬κΈ°κ° μ²λ¦¬νμ§λ§ inline functionμ μ»΄νμΌλ¬κ° μ²λ¦¬λ₯Ό νλ€. μ λ¬λλ μΈμλ μ°¨μ΄κ° μλλ°, λ§€ν¬λ‘λ μλ£νμ μ νμ΄ μμ§λ§ inline functionμ μλ£νμ μ ν΄μ€ μ μλ€λ κ²μ΄λ€. μμ¦μ μ»΄νμΌλ¬κ° μμμ μΈλΌμΈμ μ±λ₯ ν₯μμ¬λΆλ₯Ό νλ¨ν΄ μ²λ¦¬νκΈ°λλ¬Έμ ν¬κ² μλ―Έμμ§λ μλ€.
String and template
#include<iostream>
using std::string;
template <typename T>
T adder(T a, T b){
return a + b;
}
int main(void){
int a, b;
string c, d;
std::cout << "type two integers" << std::endl;
std::cin >> a;
std::cin >> b;
std::cout << "you typed " << a << " and " << b << " the result is " << adder<int>(a, b) << std::endl;
std::cout << "type two strings" << std::endl;
std::cin >> c;
std::cin >> d;
std::cout << "you typed " << c << " and " << d << " the result is " << adder<string>(c, d) << std::endl;
return 0;
}
type two integers
8 11
you typed 8 and 11 the result is 19
type two strings
hello world
you typed hello and world the result is helloworld
templateμ Javaμ genericκ³Ό κ°λ€. ν¨μλ ν΄λμ€μ μ μΈμμ μλ£νμ λͺ
μνλκ²μ΄ μλλΌ, μ¬μ©ν λ λͺ
μνμ¬ λ€μν λ³μ, κ°μ²΄μ λμνμ¬ μλνλλ‘ ν΄μ€λ€. μμ μμ ν¨μ adderλ μ μμ λ¬Έμμ΄μ λν΄ λνκΈ° μ°μ°μ μννκ³ κ·Έ κ²°κ³Όλ₯Ό 보μ¬μ€λ€.
Namespace
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
namespace parents{
void func();
namespace son{
void func();
}
namespace daughter{
void func();
}
}
std::string test = "GLOBAL"; //VARIABLES CAN BE DEFINENED WITH SAME NAME BOTH GLOBALLY AND LOCALLY
int main(void){
std::string test = "LOCAL";
cout << test << endl;
cout << ::test <<endl;
parents::func();
parents::son::func();
parents::daughter::func();
namespace seunghun = parents::son; //NEW NAMESPACE CAN BE DEFINED
seunghun::func();
using namespace parents;
func();
son::func();
daughter::func();
return 0;
}
void parents::func(){
cout << "PARENTS" << endl;
}
void parents::son::func(){
cout << "SON" << endl;
}
void parents::daughter::func(){
cout << "DAUGHTER" << endl;
}
LOCAL
GLOBAL
PARENTS
SON
DAUGHTER
SON
PARENTS
SON
DAUGHTER
namespaceλ scopeλ₯Ό λΆλ¦¬νμ¬ λμΌν μ΄λ¦μ κ°λ λ³μλ ν¨μκ° λ€λ₯Έ namespaceμ μ‘΄μ¬ν μ μλλ‘ ν΄μ€λ€. namespaceλ₯Ό λͺ
μνμ¬ λ€λ₯Έ scopeμ μ‘΄μ¬νλ μ½λλ₯Ό μ¬μ©ν μ μλ€. λ§μ½ λͺ
μνμ§ μμΌλ©΄ λμΌν scopeμ μ‘΄μ¬νλ λ³μλ ν¨μλ₯Ό μ¬μ©νκ²λλ€. usingμ μ¬μ©νμ¬ namespaceλͺ
μλ₯Ό μλ΅ν΄μ€ μ μλ€.
Basic reference
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
void swap(int& a, int& b);
int main(void){
int num1 = 10;
int& num2 = num1;
int num3 = 20;
cout << num1 << endl;
cout << num2 << endl;
cout << num3 << endl;
cout << &num1 << endl;
cout << &num2 << endl;
cout << &num3 << endl;
num1++;
cout << num1 << endl;
cout << num2 << endl;
cout << num3 << endl;
cout << &num1 << endl;
cout << &num2 << endl;
cout << &num3 << endl;
swap(num1, num3);
cout << num1 << endl;
cout << num2 << endl;
cout << num3 << endl;
cout << &num1 << endl;
cout << &num2 << endl;
cout << &num3 << endl;
return 0;
}
void swap(int& a, int& b){
int temp = b;
b = a;
a = temp;
}
10
10
20
0x7ffe09549080
0x7ffe09549080
0x7ffe09549084
11
11
20
0x7ffe09549080
0x7ffe09549080
0x7ffe09549084
20
20
11
0x7ffe09549080
0x7ffe09549080
0x7ffe09549084
μλ£ν λ€μμ€λ &λ referenceλ₯Ό μλ―Ένλ€. κ°μ λ©λͺ¨λ¦¬κ³΅κ°μ ν λΉλ κ°μ λ€λ₯Έ λ³μ λͺ
μ λΆμ¬μ£Όλ κ²μ΄λ€. μ΄λͺ
μ΄λ λ³λͺ
μ΄λΌκ³ μκ°νλ©΄ λλ€.
λκ°μ λ³μμ§λ§ κ°μ λ©λͺ¨λ¦¬λ₯Ό μ°Έμ‘°νλ€. λ°λΌμ κ°μ΄ λ³κ²½λλ©΄ μ°Έμ‘°νλ κ°λ λ³νλ€. μ£Όμμ°μ°μ &μλ λ€λ₯΄λ€! μ°Έμ‘°λ₯Ό μ΄μ©ν΄ ν¨μλ₯Ό μ§λ©΄ μ€νμμμ λ§€κ°λ³μλ₯Ό μν λ©λͺ¨λ¦¬λ₯Ό λ³λλ‘ ν λΉνμ§ μκ³ μ€μ λ³μμ μ§μ μ κ·Όν΄μ λ ν¨μ¨μ μ΄λ€.
Return reference
#include <iostream>
using namespace std;
int& RefRetFunc2(int& ref){
ref++;
return ref;
}
int main(void){
int num1 = 1;
int num2 = RefRetFunc2(num1);
int& num3 = num1;
int& num4 = num2;
cout << "num1: " << num1 << endl;
cout << "num2: " << num2 << endl;
cout << "num3: " << num3 << endl;
cout << "num4: " << num4 << endl;
cout << "num1: " << &num1 << endl;
cout << "num2: " << &num2 << endl;
cout << "num3: " << &num3 << endl;
cout << "num4: " << &num4 << endl;
num1 += 2;
num2 += 100;
cout << "num1: " << num1 << endl;
cout << "num2: " << num2 << endl;
cout << "num3: " << num3 << endl;
cout << "num4: " << num4 << endl;
cout << "num1: " << &num1 << endl;
cout << "num2: " << &num2 << endl;
cout << "num3: " << &num3 << endl;
cout << "num4: " << &num4 << endl;
return 0;
}
num1: 2 num2: 2 num3: 2 num4: 2 num1: 0x7ffcb81482f4 num2: 0x7ffcb81482f8 num3: 0x7ffcb81482f4 num4: 0x7ffcb81482f8 num1: 4 num2: 102 num3: 4 num4: 102 num1: 0x7ffcb81482f4 num2: 0x7ffcb81482f8 num3: 0x7ffcb81482f4 num4: 0x7ffcb81482f8
referenceλ₯Ό λ°ννλλ‘ ν¨μλ₯Ό μ§€ μ μλ€. μ°Έμ‘°λ₯Ό λ°ννκ±°λ μΌλ° λ³μμ μ°Έμ‘°λ³μλ₯Ό μ§μ΄λ£κ²λλ©΄ μ°Έμ‘° λ³μκ° κ°μ§ κ°μ΄ 볡μ¬λλ€. μμ μμ λ₯Ό 보면, num1μ referenceμ 1μ λν΄ num2λ‘ μ λ¬νμ¬ num1κ³Ό num2λͺ¨λ 2κ°λλ€. κ·Έλ¦¬κ³ κ·Έ κ°μ referenceλ‘ κ°μ Έκ°λ num3μ num4λ κ°μ΄ λ³νλ€. μ΄νμ num1μ 2λ₯Ό λνμ¬ num1κ³Ό κ·Έλ₯Ό μ°Έμ‘°νλ num3μ κ°μ 4κ° λκ³ , num2μ 100μ λνμ¬ num2μ κ·Έλ₯Ό μ°Έμ‘°νλ num4λ 102κ° λλ€.
Conventional malloc and free methods
#include<iostream>
#include<string.h>
char* createString(int length);
using std::cout;
using std::endl;
int main(void){
char* myString = createString(20);
strcpy(myString, "I AM SO HAPPY");
cout << myString << endl;
cout << &myString << endl;
free(myString);
cout << myString << endl;
cout << &myString << endl;
return 0;
}
char* createString(int length){
char* newString = (char*)malloc(sizeof(char) * length);
return newString;
}
I AM SO HAPPY
0x7ffea83500a80x7ffea83500a8
CμΈμ΄μμ μ¬μ©νλ μ ν΅μ μΈ mallocκ³Ό freeμ C++μμλ κ·Έλλ‘ μ¬μ© κ°λ₯νλ€.
β¦ are replaceable with new and delete in C++
#include<iostream>
#include<string.h>
char* createString(int length);
using std::cout;
using std::endl;
int main(void){
char* myString = createString(20);
strcpy(myString, "I AM SO HAPPY");
cout << myString << endl;
cout << &myString << endl;
delete[](myString);
cout << myString << endl;
cout << &myString << endl;
return 0;
}
char* createString(int length){
char* newString = new char[length];
return newString;
}
I AM SO HAPPY
0x7ffea83500a80x7ffea83500a8
C++μμλ mallocκ³Ό freeλ₯Ό λ체νλ newμ deleteκ° μλ€. κ²°κ³Όλ μμ λμΌν¨μ νμΈν μ μλ€.
λ€μ κΈ π νΌμνλ C++ κ³΅λΆ -2- 보λ¬κ°κΈ°
Comments