Native/C++
연산자 관련 소스들 (operator)
aucd29
2013. 10. 2. 18:58
[code]#include <Turboc.h>
#include <iostream>
using namespace std;
class Time
{
friend ostream &operator <<(ostream &c, const Time &T);
friend ostream &operator <<(ostream &c, const Time *pT);
private:
int hour,min,sec;
public:
Time() { }
Time(int h, int m, int s) { hour=h; min=m; sec=s; }
void OutTime() {
printf("%d:%d:%d\n",hour,min,sec);
}
};
ostream &operator <<(ostream &c, const Time &T)
{
c << T.hour << "시" << T.min << "분" << T.sec << "초";
return c;
}
ostream &operator <<(ostream &c, const Time *pT)
{
c << *pT;
return c;
}
void main()
{
Time A(1,1,1);
Time *p;
p=new Time(2,2,2);
cout << "현재 시간은 " << A << "입니다." << endl;
cout << "현재 시간은 " << p << "입니다." << endl;
delete p;
}
[/code]
Question
Person 을 cout 할 수 있는 ostream 함수를 생성 하라
[code]
#include <Turboc.h>
#include <iostream>
using namespace std;
class Person
{
friend ostream &operator <<(ostream &c, const Person &target);
friend ostream &operator <<(ostream &c, const Person *target);
private:
char *Name;
int Age;
public:
Person(const char *aName, int aAge)
{
Name = new char[strlen(aName)+1];
strcpy(Name, aName);
Age = aAge;
}
Person(const Person &Other)
{
Name = new char[strlen(Other.Name)+1];
strcpy(Name, Other.Name);
Age = Other.Age;
}
~Person()
{
delete[] Name;
}
void OutPerson()
{
printf("Name : %s Age : %d\n", Name, Age);
}
};
ostream &operator <<(ostream &c, const Person &target)
{
c << "Name : " << target.Name << " Age : " << target.Age;
return c;
}
ostream &operator <<(ostream &c, const Person *target)
{
c << *target;
return c;
}
void main()
{
Person c("choi", 27);
Person *d;
d = new Person("cho", 27);
cout << c << endl;
cout << d << endl;
delete d;
}
[/code]
[code]
#include <Turboc.h>
#include <IOSTREAM>
using namespace std;
class Time
{
private:
int hour,min,sec;
public:
Time() { }
Time(int h, int m, int s) { hour=h; min=m; sec=s; }
void OutTime() {
printf("%d:%d:%d\n",hour,min,sec);
}
// 클래스에서 배열로 이용을 할 수 있도록 해주는 연산자로서
// 만약에 Instance를 A로 주었다면 A[0]을 선택시 hour값을
// 리턴하게 끔 되어 있다.
int &operator [](int what) {
switch (what) {
case 0:
return hour;
case 1:
return min;
case 2:
default:
return sec;
}
}
const int &operator [](int what) const {
switch (what) {
case 0:
return hour;
case 1:
return min;
default:
case 2:
return sec;
}
}
};
void main()
{
Time A(1,1,1);
const Time B(7,7,7);
cout << A[0] << A[1] << A[2] << endl;
A[0]=12;
printf("현재 %d시입니다.\n",A[0]);
B[0]=8;
printf("현재 %d시입니다.\n",B[0]);
}
[/code]
[code]
#include <Turboc.h>
class StuList
{
private:
struct Student {
char Name[10];
int StNum;
} S[30];
public:
StuList() {
strcpy(S[0].Name,"이승만");S[0].StNum=1;
strcpy(S[1].Name,"박정희");S[1].StNum=3;
strcpy(S[2].Name,"전두환");S[2].StNum=6;
strcpy(S[3].Name,"노태우");S[3].StNum=9;
strcpy(S[4].Name,"김영삼");S[4].StNum=15;
strcpy(S[5].Name,"김대중");S[5].StNum=17;
strcpy(S[6].Name,"노무현");S[6].StNum=20;
strcpy(S[7].Name,"??????");S[7].StNum=100;
}
// []을 응용해서 간단하게 만든 배열 indexing이다.
// 우선은 구조체에 값을 넣어두고는 struct의 Name과 동일한
// 인덱스 값을 찾고 찾게 된다면 StNum값을 return 하는 형식이다.
int operator[](const char *Name) {
for (int i=0;;i++) {
if (strcmp(S[i].Name,Name)==0) return S[i].StNum;
if (S[i].Name[0]=='?') return -1;
}
}
};
void main()
{
StuList SL;
printf("김영삼 학생의 학번은 %d번입니다.\n",SL["김영삼"]);
}
[/code]
[code]
#include <Turboc.h>
struct Author {
char Name[32];
char Tel[24];
int Age;
};
class Book
{
private:
char Title[32];
Author Writer;
public:
Book(const char *aTitle,const char *aName,int aAge) {
strcpy(Title,aTitle);
strcpy(Writer.Name,aName);
Writer.Age=aAge;
}
Author *operator->() { return &Writer; }
const char *GetTitle() { return Title; }
};
void main()
{
Book Hyc("혼자 연구하는 C/C++","김상형",25);
printf("제목:%s, 저자:%s, 저자 나이:%d세\n",Hyc.GetTitle(),Hyc->Name,Hyc->Age);
}[/code]
#include <iostream>
using namespace std;
class Time
{
friend ostream &operator <<(ostream &c, const Time &T);
friend ostream &operator <<(ostream &c, const Time *pT);
private:
int hour,min,sec;
public:
Time() { }
Time(int h, int m, int s) { hour=h; min=m; sec=s; }
void OutTime() {
printf("%d:%d:%d\n",hour,min,sec);
}
};
ostream &operator <<(ostream &c, const Time &T)
{
c << T.hour << "시" << T.min << "분" << T.sec << "초";
return c;
}
ostream &operator <<(ostream &c, const Time *pT)
{
c << *pT;
return c;
}
void main()
{
Time A(1,1,1);
Time *p;
p=new Time(2,2,2);
cout << "현재 시간은 " << A << "입니다." << endl;
cout << "현재 시간은 " << p << "입니다." << endl;
delete p;
}
[/code]
Question
Person 을 cout 할 수 있는 ostream 함수를 생성 하라
[code]
#include <Turboc.h>
#include <iostream>
using namespace std;
class Person
{
friend ostream &operator <<(ostream &c, const Person &target);
friend ostream &operator <<(ostream &c, const Person *target);
private:
char *Name;
int Age;
public:
Person(const char *aName, int aAge)
{
Name = new char[strlen(aName)+1];
strcpy(Name, aName);
Age = aAge;
}
Person(const Person &Other)
{
Name = new char[strlen(Other.Name)+1];
strcpy(Name, Other.Name);
Age = Other.Age;
}
~Person()
{
delete[] Name;
}
void OutPerson()
{
printf("Name : %s Age : %d\n", Name, Age);
}
};
ostream &operator <<(ostream &c, const Person &target)
{
c << "Name : " << target.Name << " Age : " << target.Age;
return c;
}
ostream &operator <<(ostream &c, const Person *target)
{
c << *target;
return c;
}
void main()
{
Person c("choi", 27);
Person *d;
d = new Person("cho", 27);
cout << c << endl;
cout << d << endl;
delete d;
}
[/code]
[code]
#include <Turboc.h>
#include <IOSTREAM>
using namespace std;
class Time
{
private:
int hour,min,sec;
public:
Time() { }
Time(int h, int m, int s) { hour=h; min=m; sec=s; }
void OutTime() {
printf("%d:%d:%d\n",hour,min,sec);
}
// 클래스에서 배열로 이용을 할 수 있도록 해주는 연산자로서
// 만약에 Instance를 A로 주었다면 A[0]을 선택시 hour값을
// 리턴하게 끔 되어 있다.
int &operator [](int what) {
switch (what) {
case 0:
return hour;
case 1:
return min;
case 2:
default:
return sec;
}
}
const int &operator [](int what) const {
switch (what) {
case 0:
return hour;
case 1:
return min;
default:
case 2:
return sec;
}
}
};
void main()
{
Time A(1,1,1);
const Time B(7,7,7);
cout << A[0] << A[1] << A[2] << endl;
A[0]=12;
printf("현재 %d시입니다.\n",A[0]);
B[0]=8;
printf("현재 %d시입니다.\n",B[0]);
}
[/code]
[code]
#include <Turboc.h>
class StuList
{
private:
struct Student {
char Name[10];
int StNum;
} S[30];
public:
StuList() {
strcpy(S[0].Name,"이승만");S[0].StNum=1;
strcpy(S[1].Name,"박정희");S[1].StNum=3;
strcpy(S[2].Name,"전두환");S[2].StNum=6;
strcpy(S[3].Name,"노태우");S[3].StNum=9;
strcpy(S[4].Name,"김영삼");S[4].StNum=15;
strcpy(S[5].Name,"김대중");S[5].StNum=17;
strcpy(S[6].Name,"노무현");S[6].StNum=20;
strcpy(S[7].Name,"??????");S[7].StNum=100;
}
// []을 응용해서 간단하게 만든 배열 indexing이다.
// 우선은 구조체에 값을 넣어두고는 struct의 Name과 동일한
// 인덱스 값을 찾고 찾게 된다면 StNum값을 return 하는 형식이다.
int operator[](const char *Name) {
for (int i=0;;i++) {
if (strcmp(S[i].Name,Name)==0) return S[i].StNum;
if (S[i].Name[0]=='?') return -1;
}
}
};
void main()
{
StuList SL;
printf("김영삼 학생의 학번은 %d번입니다.\n",SL["김영삼"]);
}
[/code]
[code]
#include <Turboc.h>
struct Author {
char Name[32];
char Tel[24];
int Age;
};
class Book
{
private:
char Title[32];
Author Writer;
public:
Book(const char *aTitle,const char *aName,int aAge) {
strcpy(Title,aTitle);
strcpy(Writer.Name,aName);
Writer.Age=aAge;
}
Author *operator->() { return &Writer; }
const char *GetTitle() { return Title; }
};
void main()
{
Book Hyc("혼자 연구하는 C/C++","김상형",25);
printf("제목:%s, 저자:%s, 저자 나이:%d세\n",Hyc.GetTitle(),Hyc->Name,Hyc->Age);
}[/code]