U E D R , A S I H C RSS

스네이크바이트/C++

MFC공부를 시작하기 앞서 강희경이 C++과 객체지향에 대한 내용을 설명하기 위해 만든 페이지 입니다.


1. 클래스 연습

~cpp 
#include<iostream> 
using namespace std; 
class student 
{ 
private: 
        char Name[20];          //이름 
        int ID;                 //학번 
        int Math;                       //수학성적 
        int Korean;             //국어성적 
        int English;            //영어성적 
public: 
        student();                      //디폴트 생성자 
        student(char *name, int id, int math, int kor, int eng);//생성자 
        ~student();                     //소멸자 
        int getTotal();         //총점을 구하는 함수  
        void outputID();        //학번, 이름 출력 
        int getMath();          //수학점수에 접근 
        int getKor();           //국어점수에 접근 
        int getEng();           //영어점수에 접근 
}; 
 
student::student() 
{ 
} 
 
student::student(char *name, int id, int math, int kor, int eng) 
{ 
        strcpy(Name, name);     //이름초기화(카피) 
        ID = id;                        //학번초기화 
        Math = math;            //수학점수초기화 
        Korean= kor;            //국어점수초기화 
        English = eng;          //영어점수초기화 
} 
 
student::~student() 
{ 
} 
 
int student::getTotal() 
{ 
        return Math + Korean + English;//총점리턴 
} 
 
void student::outputID() 
{ 
        cout << ID << " " << Name << endl;//학번과 이름 출력 
} 
 
int student::getMath() 
{ 
        return Math;            //수학점수리턴 
} 
int student::getKor() 
{ 
        return Korean;          //국어점수리턴 
} 
int student::getEng() 
{ 
        return English;         //영어점수리턴 
} 
         
void main() 
{ 
        int Max; 
        int bestStu; 
        int i; 
        const int numberOfStudent = 10;//학생 수 
        student stu[numberOfStudent] =  
        {student("KangHeeKyoung", 953, 99, 99, 99), 
        student("KimSooJin", 954, 55, 100, 12), 
        student("ParkJinHa", 955, 66, 87, 11), 
        student("ParkJinYoung", 956, 11, 23, 54), 
        student("KimTaeHyuk", 957, 10, 9, 4), 
        student("LeeChunSoo", 958, 100, 40, 19), 
        student("AnJaeHyun", 959, 0, 0, 0), 
        student("ByunJoonWon", 960, 2, 3, 1), 
        student("SinJaeDong", 961, 1, 2, 100), 
        student("NoSooMin", 963, 0, 4, 1)};//객체 배열 생성 및 초기화 
        Max = 0; 
        for(i = 0; i < numberOfStudent; i++) 
        { 
                if(stu[i].getTotal() > Max) 
                { 
                        bestStu = i; 
                        Max = stu[i].getTotal(); 
                } 
        }                               //최대값과 최대값을 가진 객체를 찾는다. 
        cout << "전체수석: ";  
        stu[bestStu].outputID();                //학번, 이름 출력 
        Max = 0; 
        for(i = 0; i < numberOfStudent; i++) 
        { 
                if(stu[i].getMath() > Max) 
                { 
                        bestStu = i; 
                        Max = stu[i].getMath(); 
                } 
        } 
        cout << "수학수석: ";  
        stu[bestStu].outputID(); 
        Max = 0; 
        for(i = 0; i < numberOfStudent; i++) 
        { 
                if(stu[i].getKor() > Max) 
                { 
                        bestStu = i; 
                        Max = stu[i].getKor(); 
                } 
        }                                                                
        cout << "국어수석: ";  
        stu[bestStu].outputID(); 
        Max = 0; 
        for(i = 0; i < numberOfStudent; i++) 
        { 
                if(stu[i].getEng() > Max) 
                { 
                        bestStu = i; 
                        Max = stu[i].getEng(); 
                } 
        } 
        cout << "수학수석: ";  
        stu[bestStu].outputID(); 
} 

1.1. 콘스트와 스태틱

~cpp 
#include<iostream.h>
void output(int a);
void main()
{	
	const int b = 10;
	output(5);
	b--;
	cout << b <<endl;
}
void output(int a)
{
	for(int i = 0; i < a; i++)
	{
		cout << "천재" << endl;
	}
}

1.2. 클래스안에서의 스태틱


2. 포인터

~cpp 
#include<iostream.h>

void main()
{
	char c;
	char *pc = &c;
	c = 'y';
	cout << *pc << endl;
	cout << c << endl;
}

배열포인터
~cpp 
#include<iostream.h>

void main()
{
	char array[3];
	char *pa;
	pa = array; //&array[0]
	*pa = 'a';
	*(pa + 1) = 'b';
	*(pa + 2) = 'c';
	for(int i = 0; i < 3; i++)
		cout << array[i] << endl;
}

포인터++
~cpp 
#include<iostream.h>

void main()
{
	char array[3];
	char *pa;
	pa = array; //&array[0]
	for(int i = 65; i < 68; i++) 
		*(pa++) = i;
	for(i = 0; i < 3; i++)
		cout << *(--pa) << endl;
}

포인터 배열
~cpp 
#include<iostream.h>

void main()
{
	char array[3];
	char *pa[3];
	//pa = array; //&array[0]
	for(int i = 0; i < 3; i++) 
		pa[i] = &array[i];
	for(i = 0; i < 3; i++) 
        *pa[i] = i+65; 
	for(i = 0; i < 3; i++)
		cout << array[i] << endl;
}

구조체 포인터
~cpp 
#include<iostream.h>

struct node 
{
	char data;
	node *link;
};
void main()
{
	node type1;
	type1.data = 'a';
	node type2;
	type2.data = 'b';
	type1.link = &type2;
	type2.link = NULL;
	cout << (*(type1.link)).data << endl;
}

링크드 리스트 예제
~cpp 
#include<iostream>
using namespace std;

struct Node
{
	char data;
	Node* link;
};	

void main()
{
	Node array[100];
	Node* pNode;
	int index = 0;
	char input;
	do{
		cout << "숫자: ";
		cin >> input;
		if(input != 'q')
		{
			if(index == 0){
				array[0].data = input;
				array[0].link = NULL;
			}
			else
			{
				array[index].data = input;
				array[index].link = NULL;
				array[index-1].link = &array[index];
			}
			index++;
			pNode = array;
			while(pNode != NULL)
			{
				cout << pNode->data << " ";
				pNode = pNode->link;
			}
			cout << "NULL" << endl;
		}
	}while(input != 'q');
}


Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:30:16
Processing time 0.0243 sec