프랜드함수(friend funciton)
friend함수는 어떤 클래스 내에 선언된 모든 멤버들을 접근 지정자에 관계 없이 사용 할 수있는 권한을 부여 받은 함수를 말한다.
특정 함수에서 클래스내의 멤버들을 자유롭게 사용하고자 한다면 그 특정함수를 class 선언부 안에서 선언해주면 된다.
friend함수는 클래스 자료형 속의 어느 부분에서 선언해도 관계없다.
friend함수는 상속되지 않는다.
friend 함수 만들기
#include <iostream> #include <cstring>
using namespace std;
class TestClass { int age; double height; public: void setInof(int _age, double _height) { age = _age; height = _height; } void show() { cout << "나이 " << age << ", 키 " << height << endl; }
friend void pub(); }; void pub() { TestClass free1; free1.age = 25; free1.height = 275.6; free1.show(); } int main() { TestClass test1; test1.setInof(15, 145.8); test1.show(); pub(); } |
실행 결과 나이 : 15, 키 : 145.8 나이 : 25, 키 : 275.6 |
friend 클래스 만들기
#include <iostream> #include <cstring>
using namespace std;
class TestClass { int age; double height; public: void setInof(int _age, double _height) { age = _age; height = _height; } void show() { cout << "나이 : " << age << ", 키 : " << height << endl; }
friend class TestClass2; }; class TestClass2 { public: void pub() { TestClass free1; free1.age = 23; free1.height = 156.7; free1.show(); } void pub2() { TestClass free1; free1.age = 22; free1.height = 166.7; free1.show(); } };
int main() { TestClass2 test1; test1.pub(); test1.pub2(); } |
실행 결과 나이 : 23, 키 : 156.7 나이 : 22, 키 : 166.7 |
#include <iostream> #include <ctime>
using namespace std;
class Date; class Time { friend void OutToday(Date&, Time&); private: int hour, min, sec; public: Time(int h, int m, int s) { hour = h; min = m; sec = s; } };
class Date { friend void OutToday(Date&, Time&); private: int year, month, day; public: Date(int y, int m, int d) { year = y; month = m; day = d; } };
void OutToday(Date& d, Time& t) { cout << "오늘날짜 " << d.year << "년" << d.month << "월" << d.day << "일" << endl; cout << "현재시간 " << t.hour <<"시" << t.min << "분" << t.sec << "초" << endl; }
void main() { time_t curr_time; struct tm* curr_tm; // 시간표시를위한구조체
curr_time = time(NULL); // 현재날짜 curr_tm = localtime(&curr_time); // 현재시간
Date D(curr_tm->tm_year + 1900, curr_tm->tm_mon + 1, curr_tm->tm_mday); Time T(curr_tm->tm_hour, curr_tm->tm_min, curr_tm->tm_sec); OutToday(D, T); } |
Date는 날짜를 표현하는 클래스이며 Time은 시간을 표현하는 클래스이다. 정보를 기억하는 주요 변수들은 모두 private 영역에 선언되어 있어 외부에서 함부로 액세스하지 못하도록 하였다. OutToday함수는 이 두 클래스의 객체를 인수로 전달받아 날짜와 시간을 동시에 출력한다. 그러기 위해서 OutToday는 양쪽 클래스의 모든 멤버를 읽을 수 있어야 하는데 Date나 Time의 멤버 함수로 포함되면 한쪽밖에 읽을 수 없을 것이다. 한 함수가 동시에 두 클래스의 멤버 함수가 될 수는 없기 때문이다.
이럴 때 OutToday를 멤버 함수가 아닌 전역 함수로 정의하고 양쪽 클래스에서 이 함수를 프렌드로 지정하면 된다. 이렇게 되면 OutToday는 Date의 year, month, day와 Time의 hour, min, sec을 모두 읽을 수 있다. 마치 양쪽 클래스의 멤버 함수인 것처럼 숨겨진 멤버를 자유롭게 액세스한다.
#include <iostream> #include <ctime> using namespace std; class Time { friend class Date; private: int hour, min, sec; public: Time(int h, int m, int s) { hour = h; min = m; sec = s; } }; class Date { private: int year, month, day; public: Date(int y, int m, int d) { year = y; month = m; day = d; } void OutToday(Time& t) { cout << "오늘 날짜 : " << year << "년 " << month << "월 " << day << "일" << endl; cout << "현재 시간 : " << t.hour << "시 " << t.min << "분 " << t.sec << "초" << endl; } }; void main() { time_t curr_time; struct tm* curr_tm; // 시간 표시를 위한 구조체 curr_time = time(NULL); // 현재 날짜 curr_tm = localtime(&curr_time); // 현재 시간 Date D(curr_tm->tm_year + 1900, curr_tm->tm_mon + 1, curr_tm->tm_mday); Time T(curr_tm->tm_hour, curr_tm->tm_min, curr_tm->tm_sec); D.OutToday(T); } |
OutToday 함수는 Date 클래스의 멤버 함수로 선언되었지만 Date가 Time의 프렌드 클래스로 지정되어 있으므로 OutToday는 Time 객체의 모든 멤버를 읽을 수 있다.
static 변수와 static 함수
정적 멤버 변수는 클래스의 바깥에 선언되어 있지만 클래스에 속하며 객체별로 할당되지 않고 모든 객체가 공유하는 멤버이다.
정적 맴버 함수는 클래스의 다른 정적 멤버에 의해서만 액세스할 수 있고 어떤 객체의 인스턴스도 존재하지 않아도 호출할 수 있다.
#include <iostream> #include <ctime> using namespace std;
int cnt = 0; class Count { private: int Value;
public: Count() { cnt++; } ~Count() { cnt--; } void OutNum() { cout << "현재객체개수 " << cnt << endl; } };
int main() { Count t1, *p1; t1.OutNum(); p1 = new Count p1->OutNum(); delete p1; t1.OutNum(); } |
#include <iostream> #include <ctime> using namespace std;
class Count { private: int Value; static int cnt; public: Count() { cnt++; } ~Count() { cnt--; } void OutNum() { cout << "현재객체개수 " << cnt << endl; } }; int Count::cnt = 0; int main() { Count t1, *p1; t1.OutNum(); p1 = new Count p1->OutNum(); delete p1; t1.OutNum(); } |
실행 결과
현재 객체 개수 = 1
현재 객체 개수 = 2
현재 객체 개수 = 1
#include <iostream> #include <ctime> using namespace std;
class Count { private: int Value; static int cnt; public: Count() { cnt++; } ~Count() { cnt--; } static void InitCnt() { cnt = 0; } static void OutNum() { cout << "현재객체개수 " << cnt << endl; } }; int Count::cnt; int main() { Count::InitCnt(); Count::OutNum(); Count t1, *p1; t1.OutNum(); p1 = new Count p1->OutNum(); delete p1; t1.OutNum(); } |
실행 결과 현재 객체 개수 = 0 현재 객체 개수 = 1 현재 객체 개수 = 2 현재 객체 개수 = 1 |
#include <iostream> #include <cstring> using namespace std;
class DBQuery { private: char *sql; public: DBQuery() { }; static void DBConnect(const char* Server, const char* ID, const char* Pass); static void DBDisConnect(); void RunQuery(const char* SQL); }; void DBQuery::DBConnect(const char* Server, const char* ID, const char* Pass) { cout << "서버접속성공 << endl; } void DBQuery::DBDisConnect() { cout << "서버해제성공 << endl; } void DBQuery::RunQuery(const char* SQL) { int len = strlen(SQL) + 1; sql = new char[len]; strcpy_s(sql, len, SQL); cout << "쿼리실행 " << sql << endl; } void main() { DBQuery::DBConnect("Secret", "admin", "pass1234"); DBQuery Q1, Q2, Q3; Q1.RunQuery("select * from 게시판1"); Q2.RunQuery("select * from 자료실); Q3.RunQuery("select * from 게시판5");
DBQuery::DBDisConnect(); } |