#include <iostream>
using namespace std;
class Point
{
private:
int xpos, ypos;
public:
Point(int x=0, int y=0) : xpos(x), ypos(y)
{
cout<<"Point 객체 생성"<<endl;
}
~Point()
{
cout<<"Point 객체 소멸"<<endl;
}
void SetPos(int x, int y)
{
xpos=x;
ypos=y;
}
friend ostream& operator<<(ostream& os, const Point& pos);
};
ostream& operator<<(ostream& os, const Point& pos)
{
os<<'['<<pos.xpos<<", "<<pos.ypos<<']'<<endl;
return os;
}
class SmartPtr
{
private:
Point * posptr;
public:
SmartPtr(Point * ptr) : posptr(ptr)
{ }
Point& operator*() const
{
return *posptr;
}
Point* operator->() const
{
return posptr;
}
~SmartPtr()
{
delete posptr;
}
};
int main(void)
{
SmartPtr sptr1(new Point(1, 2));
SmartPtr sptr2(new Point(2, 3));
SmartPtr sptr3(new Point(4, 5));
cout<<*sptr1;
cout<<*sptr2;
cout<<*sptr3;
sptr1->SetPos(10, 20);
sptr2->SetPos(30, 40);
sptr3->SetPos(50, 60);
cout<<*sptr1;
cout<<*sptr2;
cout<<*sptr3;
return 0;
}
====================================
#include <iostream>
using namespace std;
class Point
{
private:
int xpos, ypos;
public:
Point(int x=0, int y=0) : xpos(x), ypos(y)
{ }
Point operator+(const Point & pos) const
{
return Point(xpos+pos.xpos, ypos+pos.ypos);
}
friend ostream& operator<<(ostream& os, const Point& pos);
};
ostream& operator<<(ostream& os, const Point& pos)
{
os<<'['<<pos.xpos<<", "<<pos.ypos<<']'<<endl;
return os;
}
class Adder
{
public:
int operator()(const int &n1, const int &n2)
{
return n1+n2;
}
double operator()(const double &e1, const double &e2)
{
return e1+e2;
}
Point operator()(const Point &pos1, const Point &pos2)
{
return pos1+pos2;
}
};
int main(void)
{
Adder adder;
cout<<adder(1, 3)<<endl;
cout<<adder(1.5, 3.7)<<endl;
cout<<adder(Point(3, 4), Point(7, 9));
return 0;
}
=============
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string str1="I like ";
string str2="string class";
string str3=str1+str2;
cout<<str1<<endl;
cout<<str2<<endl;
cout<<str3<<endl;
str1+=str2;
if(str1==str3)
cout<<"동일 문자열!"<<endl;
else
cout<<"동일하지 않은 문자열!"<<endl;
string str4;
cout<<"문자열 입력: ";
cin>>str4;
cout<<"입력한 문자열: "<<str4<<endl;
return 0;
}
'C언어 업무보고' 카테고리의 다른 글
연산자 오버로딩, 스마트 포인터, 펑터, string 클래스 (0) | 2014.10.30 |
---|---|
링크드리스트 중간삽입 20140421 (0) | 2014.04.21 |
140417 연결리스트 : 연결리스트의 삽입 (0) | 2014.04.17 |
140416 분할컴파일 및 소스인사이트 툴 사용법 (0) | 2014.04.16 |
아트메가 불 밝히기 : volatile, 반복문 (0) | 2014.04.11 |