amstars   5년 전

#if 0
#include
using namespace std;
struct st{
private://함부로 접근할 수 없다. private 멤버 변수는 구조체의 안의 정의된 함수만 접근이 가능하다.
int no;
char ch;
public:
void InitStruct(int n, char c
{
if (n%2 == 0)
n++;
no = n, ch=c;
}
void PrintStruct(
{
printf("%d %c\n", no, ch);
}
};

void main(void)
{
struct st aa;
aa.InitStruct(4, 'A')
aa.PrintStruct()
printf("%d" , sizeof(aa))
}
#endif

#if 0
#include
#include
using namespace std;

void main(void){
printf("seoul\n");
cout << "seoul" << endl ;
cout << 5 << "seoul" << 3.14 << endl
cout << hex << 10 << endl
cout << oct << 10 << endl
cout << dec << 10 << endl
}
#endif

#if 0
#include
#include
using namespace std;

void main(void){
char name[20],addr[20];
int age;

cout<<"이름 : ";

cin.getline(name,20)

cout<<"나이 : ";
cin>>age;

cout<<"주소 : ";

cin.ignore()
cin.getline(addr, 20);

cout<<"이름 : "< 하지만 지역변수가 없더라도 전역변수로 사용할 때 ::를 사용하면 가독성이 좋다.
※ 지역함수와 전역함수란? 구조체 안에 함수가 선언되었으면 지역함수
*/
#if 0
#include
using namespace std;

struct st{
private:

int no;
char ch;
public:
void InitStruct(int n, char c);
void PrintStruct() const
}

void st::InitStruct(int n, char c)
{
if (n%2==0) n++;
no = n, ch=c;
}
void st::PrintStruct() const
{
printf("%d %c\n", no, ch);
}
void main(void){
st aa;
aa.InitStruct(4, 'A');
aa.PrintStruct();
}
#endif

#if 0
#include
using namespace std;
int no = 10;
void main(void)
{
int no = 5;

cout<
using namespace std;

struct st{
int no;
void input(int n);
void print()
};
void print()

void st::input(int n)
{
no = n;
print();
::print();
}
void st::print(
{
cout<
using namespace std;
void main(void)
{
const int n=5


char ch[n]

int *p = (int *)&n

*p = 10;

cout << "int n의 값 : " << n << " int &n의 주소 : " << &n << endl

cout << "int* p의 *p값 : " << *p << " int* p의 주소 : " << p << endl
cout << "&(*p)의 주소 : " << &(*p) << endl;
}
#endif
#if 0
#include
using namespace std;
void sub(const int ** t);
void sub2(int * const *t);
void main(void)
{
int n=5, k=6;
const int *p
int * const q=&k
const int * const z=&k

p= &n;

cout<<*p<
using namespace std; \
const int* sub(void);

void main(void)
{



sub();
}

const int* sub(void
{
static int n=5;
cout<
using namespace std;

void main(void){
const char * str = "seoul"


}
#endif

#if 0
#include
#include
using namespace std;
void Memcpy(void * dst, const void * sou, size_t cnt);
void Swap(int *a, int *b);
void Swap(double *a, double *b);
void Swap(void *p1, void *p2, size_t size);

void main(void)
{
int n=5, k=6;
Swap(&n, &k)
cout << n << " " << k << endl;

double some1 = 3.14 , some2 = 2.54;
Swap(&some1 , &some2)
cout << some1 << " " << some2 << endl;

double some3 = 3.14, some4=2.54;
Swap(&some3, &some4, sizeof(some1))
cout<0; cnt--){
*d++=*s++;
}
}
void Swap(void *p1, void *p2, size_t size

void * temp = malloc(size);

cout << "Swap3" << endl;
Memcpy(temp, p1, size);
Memcpy(p1, p2, size);
Memcpy(p2, temp, size);
}
#endif

#if 0
#include
#include
using namespace std;

struct stream{
stream& printf(int n);
stream& printf(char c);
stream& printf(double d);
stream& printf(const char * s);
};

stream& stream::printf(int n)
{
::printf("%d\n", n)
return *this
}
stream& stream::printf(char c)
{
::printf("%c\n", c);
return *this;
}
stream& stream::printf(double d)
{
::printf("%lf\n", d);
return *this;
}
stream& stream::printf(const char *s)
{
::printf("%s\n", s);
return *this;
}
void main(void)
{
stream c;

c.printf(5).printf('B').printf(3.14).printf("seoul")
}
#endif

#if 0
#include
using namespace std;

struct st{
void sub()
{

}
void sub() const
{

}

};

void main(void){
st aa;
}
#endif

#if 0
#include
#include
using namespace std;
void sub(int a, int = 20 , int = 10)
void main(void)
{
sub(5, 3, 2);
sub(5);

}

void sub(int a, int b, int c)
{

}
#endif

#if 0
#include
#include
using namespace std;

void DateDisplay(int day, int month=5, int year=2018);
void DateDisplay(void)
void main(void)
{
DateDisplay(14, 5);
DateDisplay(14, 6);
DateDisplay(14, 6, 2019);
DateDisplay();
}

void DateDisplay(int day, int month, int year)
{
cout<
int MsgBox(LPCWSTR text, LPCWSTR caption, int style=MB_OK)
void main(void)
{
MessageBox(NULL, L"메세지 내용", L"타이틀", MB_OK)
MessageBox(NULL, L"정말 종료??", L"프로그램 종료", MB_YESNO)
MsgBox(L"수고", L"감사");
}

int MsgBox(LPCWSTR text, LPCWSTR caption, int style)
{
return MessageBox(NULL, text, caption, style)

#endif

#if 0

#include
using namespace std;

void sub();;

void main(void)
{
void func(int n=10)
sub();
func();
}

void sub()
{
void func(int n=5)
cout<<"sub"<
using namespace std;

void main(void)
{
int n = 5;
int &k=n
cout<
using namespace std;
void subv(int n);
void subp(int *p);
void subr(int &k);

void main(void)
{
int n = 5;
subv(n);
cout<
using namespace std;

int vsub();
int* psub();
int& rsub();
void main(void)
{
vsub();


*psub()=10


rsub()=10

cout<
using namespace std;

void main(void){
int n=5;
int &k=n;
const int &c = n;
k = 10;

cout<
using namespace std;

void main(void){
long data = 0x12345678
short (&s)[2] = (short (&)[2])data
coutin_avail());
cin>>score;
} while (cin.fail());에는>

cout<<"Grade : "> count;
}while(cin.fail());());>

*/

#if 0

#include
using namespace std;

void main(void){
int *p;
p = new int

*p=100;
cout<<*p<
using namespace std;

void main(void){
int size;
char *p;
cout<<"Size : ";
cin>>size;

p = new char[size];

p[0]='A';

cout<<*(int*)(p-16)>size;
cin.ignore(cin.rdbuf()->in_avail());
p=new char*[size];

for (int i=0; i>> delete
new[] ===>>> delete[]

④ 연산자 new 우측에는 항상 자료형이 오며, 그 자료형 크기만큼 메모리를 확보합니다.

⑤ 연산자 new는 정상적으로 메모리를 확보했다면 확보된 메모리의 시작번지를 리턴하고,
그렇지 않으면 NULL을 리턴합니다.

⑥ 연산자 delete는 포인터 변수가 가리키는 메모리번지부터 포인터 변수의 자료형 크기만큼 제거(반환)
만약 포인터 변수 p를 선언할 때,
char *형으로 선언했다면 p가 가리키는 메모리 번지부터 1바이트(char) 만큼만 반환
int *형으로 선언했다면 4B

⑦ new 연산자를 이용하여 '배열'로 메모리를 확보했을 때,
포인터 변수 ptr 앞에 배열기호([])를 사용하여 메모리를 반환해야 합니다.

※ malloc과 free의 밀애 : 내부적으로 size 약속이 존재
※ 동적 메모리의 필요성
※ 동적인 공간에 문자열 입력 받기

*/

#if 0

#include
using namespace std;

void main(void){
union{
int data;
short s[2];
char c[4];
};

#if 0

#include
using namespace std;

class A{
int no;
char ch;
public:
A()
A(int n);
A(int n, char c)
};

A::A(){
cout<<"Default Constructor"<
#include
using namespace std;

#define IsLeap(Y) ((!(Y % 4) && (Y % 100)) || !(Y % 400))

class Date{
private:
int year, month, day, week;
void CalcDate(void)
public:
//생성자로 바꿈
Date();
Date(int d, int m, int y=2018);
void DisplayDate(void);
};

static int days[13] ={ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
Date::Date(void)
{
time_t lt;
tm c;
::time(<)
::localtime_s(&c, <);
year = c.tm_year+1900;
month = c.tm_mon+1;
day = c.tm_mday;
week = c.tm_wday;
}

Date::Date(int d, int m, int y
{
if (!(y>=1&&y<=4000)) y = 1;
if (IsLeap(y)) days[2] = 29;
else days[2] = 28;
if (!(m>=1&&m<=12)) m = 1;
if (!(d>=1&&d<=days[m])) d = 1;
year = y, month = m, day = d;
CalcDate()
}

void Date::CalcDate(void)
{
int y, sum;
y = year-1;
sum = y*365+(y/4)-(y/100)+(y/400);
for (int n = 1; n < month; n++) sum += days[n];
sum += day;
week = sum%7;
}

void Date::DisplayDate(void)
{
char * strweek[] ={
"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday" };
cout<
#include
using namespace std;

#define ScoreRng(S) (((S >= 0) && (S <= 100)) ? (S) : (0))
class Score{
private:
char * name
int kor, eng, mat, tot;
double ave;
void CalcScore(void);
public:

Score();
Score(const char * irum, int k, int e, int m);
void PrintScore(void);

~Score();
};

Score::Score()
{
char temp[20];
size_t len;

do{
cout<<"Name:";
cin.clear();
cin.ignore(cin.rdbuf()->in_avail());
cin.getline(temp, 20);
} while (cin.fail());
len = ::strlen(temp);
name = new char[len+1];
::strcpy_s(name, len+1, temp);

do{
cout<<"Score(K, E, M):";
cin.clear();
cin.ignore(cin.rdbuf()->in_avail());
cin>>kor>>eng>>mat;
} while (cin.fail()||!ScoreRng(kor)||!ScoreRng(eng)||!ScoreRng(mat));

CalcScore();
}

Score::Score(const char * irum, int k, int e, int m
{
size_t len;

len = ::strlen(irum);
name = new char[len+1];
::strcpy_s(name, len+1, irum);
kor = ScoreRng(k), eng = ScoreRng(e), mat = ScoreRng(m);

CalcScore();
}

void Score::CalcScore(void)
{
tot = kor+eng+mat;
ave = tot/3.0;
}

void Score::PrintScore(void)
{
static char ch[] = "FFFFFFDCBAA";

cout<>num;

Score *bb;
bb=new Score[num]

for (int i=0; i return type X)

④ 클래스에 생성자함수를 만들지 않으면 컴파일러에 의해 빈문장(empty)의 생성자함수 자동 생성
=> Default 생성자함수(기본생성자함수)

⑤ 일반적으로 생성자함수는 오버로딩(Overloading)해서 다양하게 개체를 초기화
=> 다양하게 object 초기화 가능

⑥ 생성자함수 호출의 기본형식
클래스명 개체 = 생성자함수명(인수,...);
클래스명 개체(인수...);

⑦ friend, static, const, virtual(가상) 함수로 둘 수 없습니다.

※ 생성자함수의 중복 정의
※ 디폴트 생성자함수의 부재
※ 생성자함수 호출의 제 경우

*/

#if 0

#include
using namespace std;

class A{
int no;
public:
A();
A(int n);
~A();
};

A::A(){
cout<<"Default Constructor"<~A(); 를 수행

A *p = new A[2];
cout<<*(int *)((char *)p-20)<~A();

cout<<"end main..."<~A();aa[0]->~A(); 를 수행
}

#endif

#if 0
#include
#include
using namespace std;

class CFile{
FILE *fp;
char buf[133];
public:
CFile(){}
CFile(const char * fname, const char * mode="r");
~CFile();
char * Read();
char * GetBuf();
};

CFile::CFile(const char * fname, const char * mode){
fopen_s(&fp, fname, mode);
}

CFile::~CFile(){
fclose(fp);
}

char * CFile::Read(){
return ::fgets(buf, 132, fp);
}

char * CFile::GetBuf(){
return buf;
}

void main(void){
CFile file("chap2.cpp");
while (file.Read()){
printf("%s", file.GetBuf());
}
}

#endif

#if 0

#include
using namespace std;

class A{
int no; \
char ch;
public:
A(){}
A(int n, char c);
void sub(int dummy);
void func();
};

A::A(int n, char c)
this->no=n, this->ch=c;
couta == (*this).a )
상수처럼 쓰임 => 값을 대입할 수 없다.
C에서는();>

① 예약어 'this'는 멤버함수 내(內)에서만 사용할 수 있습니다.

② 예약어 'this'는 멤버함수를 호출한 개체의 번지(포인터 상수)입니다.

③ 전역함수를 비롯해서 friend 함수, static 멤버함수에서 사용할 수 없습니다.

④ 예약어 this의 사용이 가능한 것은 멤버함수가 __thiscall 호출 규약(-16번지)을 사용
_cdecl호출 규약(C방식, -4번지 가변인수 때문에)을 사용하는 멤버함수도 this를 사용 가능

첫번째 인수의 주소로부터 -16번지에 this가 존재한다.(곧 object의 주소가 존재한다.)

- static 멤버함수에서는 this 사용 불가(아예 stack에 안들어옴)

※ C언어에서 this의 경험은 없었나요?
※ 예약어 this의 리턴

*/

#if 0

#include
using namespace std;

class A{
int no;
public:
A()
A(int n);
};

A::A(){
cout<<"A def con...."< 묵시적 초기화
cout<<"B def con...."<
using namespace std;
int go=5;

class B{
char ch;
public:
B(){}
B(char c) :ch(c){}
};

class A{
int no;
int &ro;
const int co;

B bb=B('A');
public:
A();
A(int n);
void sub();
};

A::A() : ro(go), co(10){

}

A::A(int n) : ro(no), co(5){
no=n;
}

void A::sub(){
cout<<"sub"<
using namespace std;

class A{
double d;
};
A aa;

class B{
int no;
int & rn;
A &ra;
public:
B();
};

B::B() :rn(no), ra(aa){

}
void main(void){
B bb;
cout< 참조시 내부적으로 포인터 방식이라 포인터 4B 만큼 늘어남 클래스의 레퍼런스도 4B

}

#endif

#if 0

#include
using namespace std;

class A{
mutable int no
char ch;
public:
A(){}
A(int n, char c) :no(n), ch(c){ }
void sub() const
void sub();
void func();
const A* func2() const;
};

void A::sub() const{
no=10
cout const 함수 => 가독성을 위해!();>

void A::sub()
cout<<"non-const" 일반함수를 못부름
③ const 개체는 오로지 const 멤버함수만을 호출 할 수 있습니다.
(일반 멤버함수 호출 시 멤버데이터의 값이 변경될 수 있으므로)
();>

④ const 개체일지라도 mutable로 선언된 멤버는 예외적으로 변경 가능합니다.
*/

#if 0

#include
using namespace std;

class A{
int no;
public:
static int sum
A(){}
A(int n);
};

int A::sum=0

A::A(int n){
no = n;

sum++
}

void main(void){
A aa, bb;
cout<
using namespace std;

class A{
static int array[]
public:
A(){}
A(int n);
};

int A::array[]={ 0, 1, }

A::A(int n){

}

void main(void){

}

#endif

#if 0

#include
using namespace std;

class A{
int no;
static int sum;
public:
A(){}
A(int n) :no(n){}
void sub();
static void SetSum(int s);
static int GetSum();
};

int A::sum=0;

void A::SetSum(int s)
A::sum=s;
}

int A::GetSum()
return A::sum;
}

void A::sub(){
cout<
using namespace std;

class CMainFrame{
CMainFrame()
~CMainFrame();
public:
void ShowWindow();
void DestroyWindow();
static CMainFrame* CreateObject()
};

CMainFrame::CMainFrame(){
cout<<"Create window..."DestroyWindow();
}();>

#endif

#if 0

#include
using namespace std;

class B{
public:
void sub(const class A& ra);
};

class A{
int no;
public:
A(){}
A(int n) :no(n){}
void sub() const;
friend void B::sub(const class A& ra);
void func(A *p);
};

void B::sub(const A& ra)

(지역)

※ CPoint 클래스 모델링
※ 2항 연산자 함수의 중복정의(+, - , *, +=, -=, <<, >>)
※ 1항 연산자 함수의 중복정의(++, --)

*/

#if 0

#include
using namespace std;

class A{
int no;
public:
A(){ cout<<"cons.."< operator = 와 같은 문제 발생(동적할당의 경우)

A::~A()
{
cout<<"A:~A"< int cons 1개 -> A ra = imsi; -> default cons 1개 ->되돌아와서 return1개

}

#endif

#if 0

#include
using namespace std;
int & preplus(int &r);
int &&postplus(int &r);
void main(void){
int n = 5;
int &k = n;
const int c = 5;
int && rr = 5;

rr= 10;
cout<
using namespace std;

class A
{
const char * str;
public:
A(){ }
A(const char *s) :str(s){ cout<<"str cons..."< A imsi(bb)니까 copy cons... 1개로 총 2개

}

void main(void)
{

sub();
}

#endif

#if 0

#include
using namespace std;

class A{
int no;
protected://상속과 관련
char ch;
public:
int sum;
A(){
A(int n, char c):no(n),ch(c){}
void sub();
};

void A::sub(){
cout<자식클래스 위쪽에 부모(헤더파일)

② 물려주는 기존하는 클래스를 기반(base)클래스라 하고, 물려받는 새로운 클래스를 파생(Derived)클래스라 합니다.

③ 파생(Derived) 클래스 정의 형식
class 파생class명 : 상속권한명시자 기반class명{
멤버1;
멤버2;
...
};
④ 상속권한 명시자는 private, protected, public이 있고 각각의 경우 상속의 권한을 달리함(public 주로씀)

상속권한 명시자 기반의private 기반의protected 기반의public
------------------------------------------------------------------------------------
private 접근권한 X private private
protected 접근권한 X protected protected
prublic 접근권한 X protected prublic

⑤ 기반클래스의 멤버데이터는 모두 상속되지만 기반클래스의 private 권한의 멤버는 파생클래스에서 접근할 수 없습니다.

⑥ 기반 클래스의 protected 또는 public 권한의 멤버를 파생 클래스에서 다시 선언하여 접근 권한을 변경 가능

⑦ 상속은 부모클래스 보편적 코드에서 구체적인 코드로 전개되어 자식클래스가 만들어지게 되는데,
이를 IS-A 관계(IS-A Relationship)의 성립(사람->학생->대학생->....)

*/

#if 0

#include
using namespace std;

class A{
int no;
protected://자식은 접근가능
char ch;
public:
A();
A(int n);
A(int n, char c);
~A();
void sub();
}

A::A(){
cout<<"A def cons..."<
#include
#include
using namespace std;

typedef const char * LPCTSTR;

class mstring : public string{
public:
mstring();
mstring(const char *s);

operator LPCTSTR() const;
};

mstring::mstring()
{
}

mstring::mstring(const char *s) :string(s)
{
}

mstring::operator LPCTSTR() const
{
return c_str();
}

void sub(const char *s){
cout< 파생클래스의 생성자에 묵시적 콜론 초기화로 인해 부모 클래스의 생성자가 우선적으로 수행된다.

③ 기반클래스에서 물려받은 멤버데이터의 초기화를 위해 파생클래스의 생성자함수에서 명시적(콜론(:) 초기화)으로 기반클래스의 생성자함수를 선택 호출할 수 있습니다. 만약, 기반 클래스의 생성자함수를 선택하지 않으면 기반의 디폴트 생성자함수가 호출(묵시적 콜론 초기화)됩니다.

④ 파생클래스의 생성자함수에서 기반클래스의 생성자함수를 호출하는 형식
파생class생성자명() : 기반class생성자명() =>안써도 묵시적 콜론 초기화가 이루어짐
{
...
};

⑤ 상속관계에서 생성자함수의 수행 순서는 기반의 생성자함수부터 수행됩니다.

⑥ 소멸자함수는 개체가 메모리에서 제거되기 바로 직전 개체에 의해 마지막으로 호출되는 멤버함수로 생성자함수 수행의 역순으로 호출됩니다.
즉, 파생클래스의 소멸자함수, 기반클래스의 소멸자함수 순으로 수행됩니다.

⑦ 파생클래스의 소멸자함수에서는 파생클래스에서의 마무리 작업만을 처리하면 됩니다.(기반클래스의 마무리 작업은 기반클래스의 소멸자함수에서 처리 될 것이므로)

※ string 클래스로부터 파생클래스 만들기

*/

#if 0

#include
using namespace std;

class A{
int no;
public:
A(){}
A(int n) :no(n){}
void sub();
};

void A::sub()
{
coutBsub()

delete p;

();>

}

#endif

#if 0

#include
using namespace std;

class A{
int no;
public:
virtual void sub();

void func();
};

void A::sub()
{
cout<<"sub"virtual 함수
}
();>

class B :public A{
public:
void sub()
};

void B::sub()
{
cout<<"B"<호출의 우선순위가 자식에게 있다.
*/

#if 0

#include
using namespace std;

class CView
public:
void OnPaint();
virtual void OnDraw()=0

};

void CView::OnPaint()
{
OnDraw()
}

class CImgView :public CView
public:
void OnDraw();
};

void CImgView::OnDraw(){
cout<<"image view"<
#include
using namespace std;

class A{
public:
int no;
char ch;
double d;
A(){};
A(int n, char c, double d);
};

A::A(int n, char c, double d)
{
no=n, ch=c, this->d=d;
}

void main(void){
int A:: *np;
char A:: *cp = &A::ch;
double A:: *dp = &A::d;
np=&A::no;

printf("%d %d %d\n", np, cp, dp)

A aa(5, 'A', 3.14);
A *ap=&aa;

cout<*dp< 멤버함수의 포인터를 저장하기 위해 포인터 사용
}

#endif

#if 0

#include
using namespace std;

class A{
public:
void sub();
void func(int n);

};

void A::sub()
{
cout<<"A sub"<*fp)()
(*ap.*fp)()

(A().*fp)()
}

#endif

#if 0

#include
using namespace std;

class A;
typedef void(__thiscall A::*FP)();

class A{
FP m_vfptr
public:
A() :m_vfptr(&A::sub){}
A(FP vfptr) :m_vfptr(vfptr){}
void sub();
void func()

};

void A::sub()
{
cout<<"A::sub"*m_vfptr)();();>

}

class B :public A{
public:
B() :A((FP)&B::sub){}
void sub();
};
void B::sub()
{
cout<<"B::sub"<*)를 이용해서 사용할 수 있습니다.

⑤ 멤버 포인터 연산자 형식

개체 .* 멤버 포인터
개체포인터 ->* 멤버 포인터

⑥ 같은 클래스에서 같은 자료형의 다른 멤버도 가리킬 수 있습니다.

⑦ 클래스 멤버 포인터는 멤버함수를 접근하기 위한 용도로 주로 사용됩니다.(멤버함수 포인터)

⑧ 멤버함수 포인터 선언 형식

리턴형 (호출규약 클래스명::* 포인터변수)(인수타입, ...);

⑨ 클래스 멤버 포인터 사용 시 주의할 점

- static 멤버는 접근할 수 없습니다.(static 멤버는 전역)
- 참조형 멤버데이터는 접근할 수 없습니다.(전역변수의 별명일 수도 있으므로)
- 멤버 포인터는 일반적인 포인터처럼 증가 또는 감소시킬 수 없습니다.

※ 가상함수 없이 동적 바인딩 처리(__vfptr)

*/

댓글을 작성하려면 로그인해야 합니다.