π REVIEW
deep copy: μ€μ λ°μ΄ν°λ₯Ό 볡μ¬
shallow copy: μ£Όμλ§ λ³΅μ¬
#include <iostream>
// using namespace std; // λλ§μ κ³ μ ν μ΄λ¦
int main()
{
printf("%d\n", 1234);
std::cout << 1234; // stdλΌλ namespaceμ λ§λ€μ΄μ Έ μλ κ±°
// --> global namespace
return 0;
}
int main()
{
std::cout << 1234 << ' ' << 3.14 << ' '<< "abc" << '\n';
}
[μΆλ ₯κ°]
1234 3.14 abc
int main()
{
cout << 5678 << endl;
}
[μΆλ ₯κ°]
5678
-> endl = '\n'
void show_my_age(int age=20){ // κΈ°λ³ΈμΈμ default parameter
cout << "age : " << age << endl;
}
int main()
{
show_my_age();
show_my_age(30);
}
[μΆλ ₯κ°]
20
30
void change(int& z){
z = 33;
}
int main()
{
int& t = k; // & : μ°Έμ‘°(reference)
t = 99;
change(k);
cout << k << endl;
}
int main()
{
string s = "my name is kim"; //stringμ classμ.. νμ§λ§ μ κ²½μ°μ§ μμλ λ¨
cout << s << endl;
cout << s.length() <<endl;
cout << s[0] << s.operator[](0) << endl;
}
[μΆλ ₯κ°]
my name is kim
14
mm
ν΄λμ€λͺ = μΉ΄λ© νκΈ°λ²
ν΄λμ€ = ν¨μ + λ³μ
class Point // struct -> class
{
int x, y;
};
int main()
{
Point pt;
return 0;
}
public: λͺ¨λ μ½λ(λꡬλ) μ κ·Ό
protected: κ΄λ ¨ μλ μ½λ(μμ) μ κ·Ό
private: λ΄ μ½λ(λλ§) μ κ·Ό
class Point // struct -> class
{
public:
int x, y;
};
void show(Point pt){
cout << pt.x << ' ' << pt.y << endl;
}
int main()
{
Point pt;
show(pt);
return 0;
}
class Point // struct -> class
{
public:
int x, y;
// μμ±μ(constructor)
Point()
{
cout << "c`tor" << endl;
x = y = 0;
}
};
μλ³Έ μμ λ°©μ§
Point(const Point &pt)
{
cout << "c`tor" << endl;
x = pt.x;
y = pt.y;
}
κ°μ²΄μ μλͺ μ΄ λλ λ μλμΌλ‘ νΈμΆ
~Point()
{
cout << "d`tor" << endl;
}
λ§€κ°λ³μμ μ΄λ¦μ΄ κ°μ κ²½μ°, νΌλμ νΌνκΈ° μν΄μ μ¬μ©
class Rect
{
public:
Point p1, p2;
Rect(const Point &p1, const Point p2) : p1(p1), p2(p2)
{
cout << "Rect" << endl;
this->p1.show();
this->p1 = p1;
this->p2 = p2;
}
};