반응형


//***********************************************************
// 템플릿 !! 
//***********************************************************
// 일반화 프로그래밍 !!
//  -->  반복적으로 작성해야할 코드를 하나의 코드로 모두 해결하는것 !! 

//  월평동 ---->              

//  용전동 ---->   대전ic -->  대캠 --> 계룡--> 연산           논산 

//  송촌동 ---->   대전ic --> 

//  노은동 ---->   유성IC --> 

//  


/*
기름값 * 4 
피로도 * 4
시간   * 4 
템플릿 template  - ppt, 홈페이지 !! ㅅ
*/
#include 
using namespace std; 
// 템플릿 함수 !! 
// ---> 로직 --> 코딩이 같아야한다 !! 
// ---> 타입의 차이만 나는경우 !! 
/*
int sum( int x ,  int y)
{
	return x + y; 
}
double sum( double x , double y) 
{
	return x + y; 
}*/ // typename 

//1) 로직이 동일 
//2) 타입 때문에 여러가지를 만들어야하는경우 !! 
template  
void  Swap( T * x ,  T * y )
{
	T temp = *x; 
	*x = *y; 
	*y = temp;
}
// 템플릿 특수화 !!! 
template  
void  Swap( T  x ,  T y )
{
	T temp = x; 
	x = y ; 
	y = temp;
}
// malloc : 할당하는 함수 --> 일반화가 되어있는 함수 !! 
// C언어에서는 일반화 void * 일반화 !! 
// 신탄진 -->!! 
template< typename T> 
T * Malloc( T  x ,   int size) 
{
	return (T*)malloc( sizeof(T) * size); 
}


class Point
{
public : 
	int x; 
	int y;
	Point & operator+( Point & p) 
	{	
		Point temp; 
		temp.x = this->x + p.x;
		temp.y = this->y + p.y;
		return temp;
	}
};


template < typename T1  , typename T2>
T1  sum( T1 x , T2 y)  // 기존에 자료형 때문에 여러개를 만들어야하는 자리에 T
{
	return x + y; 
}


void main()
{
		
	Point p1 ={1,2};
	Point p2 ={3,4};

	Point p3 = sum( p1, p2); 

	cout << p3.x << p3.y << endl;

//	char x = 97; 
//	char y = 98; 
//	cout << x << y << endl;
//	Swap(&x , &y);
//	cout << x << y << endl;

}



//********************************************
//  템플릿 ( template) 클래스 !!  
//********************************************	
#include 
#include  
#define DEFAULT_SIZE 16
using namespace std; 

template < typename T> 
class Array
{
private: 
	T * m_buf;		// 동적배열 포인터 !! 
	int   m_size;	    // 현재 데이타의 갯수 !! 
	int   m_capacity;  // 전체 용량 
public : 
	Array() //기본 생성자 !! 
	{	
		m_buf  = new T[DEFAULT_SIZE];
		memset( m_buf, 0, DEFAULT_SIZE );
		m_size = 0;  
		m_capacity = DEFAULT_SIZE; 
	}
	Array( int capa) 
	{
		m_buf =  new T[capa];
		memset( m_buf, 0, capa );
		m_size = 0;  
		m_capacity = capa;
	}
	// 1) 인덱스 기능 !!! 
	T  operator[] (int index) 
	{	
		return m_buf[index]; 
	}
	//1) 리사이즈 :  배열의 크기를 변경 !! 
	void Resize( int newCapa)
	{
		m_capacity =  newCapa; 
		T * temp =  new T[m_capacity]; 
		memcpy( temp, m_buf, sizeof( T ) * m_size );	
		delete [] m_buf; 
		m_buf  = temp; 
	}
	// 2) 자동 증가 !!  --> 배열에 값이 인서트가 되면 !! 
	void push_back( T data ) //배열에 마지막에 값을 하나 삽입 !! 
	{   
		if( m_size == m_capacity) 
			Resize( m_capacity *2 ); // _msize: heap에 할당된 메모리의 크기 !! 

		m_buf[m_size] = data; 
		m_size++; 
	}
	void pop_back() //배열의 맨뒤에서 1개 삭제 !! -->0으로 초기화 !!  
	{
		m_buf[m_size] = 0; 
		m_size--; 
	}
	//*************************************************************************
	// 1) 삽입 
	//    - insert (  int index , int value )  // 1개 
	//   -  insert ( int index,  int  value[] );   // 여러개의 값을 넣어라 !! 
	// 2) 삭제 
	//    erase (int index): index한개 지워라 !! 
	//    erase ( int value) // 배열안에서 value값을 처음에 있는 한개를 지워라 !! 
	//    clear() : 초기화 !! 
	// 3) 유틸리티  
	//  - reverse : 뒤집어라 -->  10 , 20, 30, --> 30, 20, 10  
	//*************************************************************************
	int getSize(){ return m_size; }
	int getCapacity(){return this->m_capacity; }
	~Array()
	{	//메모리 해제 !! 
		delete [] m_buf; 
	}
	// 객체 출력자 !! 
	void show()
	{
		for(int i = 0; i< m_size; i++) 
			cout << "[ " << m_buf[i] << " ]"; 
			cout  << endl; 
	}
	
};
// 클래스 템플릿 특수화 !! 
template  
ostream & operator<<( ostream & os ,  Array & ar)
{
		ar.show();
		return os;
}

template <> class Array
{
private: 
	int** m_buf;		// 동적배열 포인터 !! 
	int   m_size;	    // 현재 데이타의 갯수 !! 
	int   m_capacity;  // 전체 용량 
public : 
	Array() //기본 생성자 !! 
	{	
		m_buf  = new int*[DEFAULT_SIZE];
		memset( m_buf, 0, DEFAULT_SIZE );
		m_size = 0;  
		m_capacity = DEFAULT_SIZE; 
	}
	Array( int capa) 
	{
		m_buf =  new int*[capa];
		memset( m_buf, 0, capa );
		m_size = 0;  
		m_capacity = capa;
	}
	// 1) 인덱스 기능 !!! 
	int*  operator[] (int index) 
	{	
		return m_buf[index]; 
	}
	//1) 리사이즈 :  배열의 크기를 변경 !! 
	void Resize( int newCapa)
	{
		m_capacity =  newCapa; 
		int** temp =  new int*[m_capacity]; 
		memcpy( temp, m_buf, sizeof( int* ) * m_size );	
		delete [] m_buf; 
		m_buf  = temp; 
	}
	// 2) 자동 증가 !!  --> 배열에 값이 인서트가 되면 !! 
	void push_back( int* data ) //배열에 마지막에 값을 하나 삽입 !! 
	{   
		if( m_size == m_capacity) 
			Resize( m_capacity *2 ); // _msize: heap에 할당된 메모리의 크기 !! 

		m_buf[m_size] = data; 
		m_size++; 
	}
	void pop_back() //배열의 맨뒤에서 1개 삭제 !! -->0으로 초기화 !!  
	{
		m_buf[m_size] = 0; 
		m_size--; 
	}
	//*************************************************************************
	// 1) 삽입 
	//    - insert (  int index , int value )  // 1개 
	//   -  insert ( int index,  int  value[] );   // 여러개의 값을 넣어라 !! 
	// 2) 삭제 
	//    erase (int index): index한개 지워라 !! 
	//    erase ( int value) // 배열안에서 value값을 처음에 있는 한개를 지워라 !! 
	//    clear() : 초기화 !! 
	// 3) 유틸리티  
	//  - reverse : 뒤집어라 -->  10 , 20, 30, --> 30, 20, 10  
	//*************************************************************************
	int getSize(){ return m_size; }
	int getCapacity(){return this->m_capacity; }
	~Array()
	{	//메모리 해제 !! 
		delete [] m_buf; 
	}
	// 객체 출력자 !! 
	void show()
	{
		for(int i = 0; i< m_size; i++) 
			cout << "[ " << *m_buf[i] << " ]"; 
			cout  << endl; 
	}
	
};


class Market
{
	Array arr;
		
};


void main()
{
	Array arr[10]; // 2차원 
	Array< Array > arr; // 2차원 !! 
	Array< Market > arr; // 2차원	


}


'C, C++' 카테고리의 다른 글

[C++] String class  (0) 2014.11.18
[C++] 동적 배열 클래스  (0) 2014.11.18
[C++] () 연산자 오버로딩 !!  (0) 2014.11.18
[C++] 전역 함수를 이용한 연산자 오버로딩  (0) 2014.11.18
[C++] 연산자 오버로딩  (0) 2014.11.18
반응형
//*****************************************************************************
//   String  class 
//*****************************************************************************
// -  문자열을 다루는 타입 
// - char [] .  char * 
//*****************************************************************************
#include 
using namespace std;
// 문자열을 추상화한 클래스 !! 
class String 
{
private : 
	char * m_buf; // 문자열 데이타 저장소 
	  int  m_len;  //문자열 길이  
public : 
	String() 
	{
		m_buf = NULL; 
		m_len = 0; 
	}
	String( const char * str )  // "홍길동" 
	{
		m_len = strlen( str); 
		// 문자열 길이 만큼의 공간확보 !! 
		m_buf =  new char[m_len+1];  
		strcpy( m_buf, str); 
	}
	String ( const String & str) 
	{
		this->m_len = str.m_len; 
		this->m_buf = new char[m_len+1]; 
		strcpy( m_buf , str.m_buf); 
	}
	// 문자열 연산들... !!
	//	strlen()
	int getLength(){ return m_len; }
//	strcmp()
	bool compare( const char * str )
	{		
		if( m_len != strlen(str)) 
			return false; 

		for(int i = 0; i< m_len; i++) 
		{
			if( m_buf[i] != str[i]) 
				return false;
		}
		return true; 
	}
	bool compare( const String & str  )
	{
		return compare( str.m_buf);
	}
//	strcat() 
	String &  Concat( const char * str ) 
	{
		char * temp =  new char[m_len + strlen(str)+ 1]; 
		memset(temp,  0,  m_len + strlen(str)+ 1); 
		for(int i = 0; i

'C, C++' 카테고리의 다른 글

[C++] 템플릿  (0) 2014.11.18
[C++] 동적 배열 클래스  (0) 2014.11.18
[C++] () 연산자 오버로딩 !!  (0) 2014.11.18
[C++] 전역 함수를 이용한 연산자 오버로딩  (0) 2014.11.18
[C++] 연산자 오버로딩  (0) 2014.11.18
반응형
//****************************************************************
// array class 2단계 !! 
//****************************************************************
// 동적 배열 클래스 !! 
//****************************************************************
#include 
using namespace std; 
#define DEFAULT_SIZE 16
class Array
{
private: 
	int * m_buf;		// 동적배열 포인터 !! 
	int   m_size;	    // 현재 데이타의 갯수 !! 
	int   m_capacity;  // 전체 용량 
public : 
	Array() //기본 생성자 !! 
	{	
		m_buf  = new int[DEFAULT_SIZE];
		memset( m_buf, 0, DEFAULT_SIZE );
		m_size = 0;  
		m_capacity = DEFAULT_SIZE; 
	}
	Array( int capa) 
	{
		m_buf =  new int[capa];
		memset( m_buf, 0, capa );
		m_size = 0;  
		m_capacity = capa;
	}
	// 1) 인덱스 기능 !!! 
	int  operator[] (int index) 
	{	
		return m_buf[index]; 
	}
	//1) 리사이즈 :  배열의 크기를 변경 !! 
	void Resize( int newCapa)
	{
		m_capacity =  newCapa; 
		int * temp =  new int[m_capacity]; 
		memcpy( temp, m_buf, sizeof(int) * m_size );	
		delete [] m_buf; 
		m_buf  = temp; 
	}
	// 2) 자동 증가 !!  --> 배열에 값이 인서트가 되면 !! 
	void push_back( int data ) //배열에 마지막에 값을 하나 삽입 !! 
	{   
		if( m_size == m_capacity) 
			Resize( m_capacity *2 ); // _msize: heap에 할당된 메모리의 크기 !! 

		m_buf[m_size] = data; 
		m_size++; 
	}
	void pop_back() //배열의 맨뒤에서 1개 삭제 !! -->0으로 초기화 !!  
	{
		m_buf[m_size] = 0; 
		m_size--; 
	}
	//*************************************************************************
	// 1) 삽입 
	//    - insert (  int index , int value )  // 1개 
	//   -  insert ( int index,  int  value[] );   // 여러개의 값을 넣어라 !! 
	// 2) 삭제 
	//    erase (int index): index한개 지워라 !! 
	//    erase ( int value) // 배열안에서 value값을 처음에 있는 한개를 지워라 !! 
	//    clear() : 초기화 !! 
	// 3) 유틸리티  
	//  - reverse : 뒤집어라 -->  10 , 20, 30, --> 30, 20, 10  
	//*************************************************************************
	int getSize(){ return m_size; }
	int getCapacity(){return this->m_capacity; }
	~Array()
	{	//메모리 해제 !! 
		delete [] m_buf; 
	}
	// 객체 출력자 !! 
	
	
};

ostream & operator<<( ostream & os ,  Array & ar)
{
	for(int i = 0; i< ar.getSize(); i++) 
		os << "[ " <<  ar[i] << " ]"; 
	   
		os << endl; 
		return os;
}


void main()
{
	Array arr(2); 
		
	arr.push_back(10);
	arr.push_back(20);
	cout <<  arr.getCapacity() << endl; 
	arr.push_back(30);
	cout <<  arr.getCapacity() << endl; 
	

	
	cout << arr; 
	
	arr.pop_back();

	cout << arr; 

	arr.pop_back();
	
	cout << arr;
	
}

'C, C++' 카테고리의 다른 글

[C++] 템플릿  (0) 2014.11.18
[C++] String class  (0) 2014.11.18
[C++] () 연산자 오버로딩 !!  (0) 2014.11.18
[C++] 전역 함수를 이용한 연산자 오버로딩  (0) 2014.11.18
[C++] 연산자 오버로딩  (0) 2014.11.18
반응형
//************************************************************
// () 연산자 오버로딩 !! 
//************************************************************
#include 
using namespace std;

class Printf
{	
public : 
	int operator()(char * str )
	{	//printf?? 출력한 문자열 길이를 리턴 !! 
		return printf(str);
	}
};
Printf printF; 

void main()
{
	// 함수?  객체?  ==> 함수객체 !! 객체를 함수처럼
	printF("건양비트");	
}


'C, C++' 카테고리의 다른 글

[C++] String class  (0) 2014.11.18
[C++] 동적 배열 클래스  (0) 2014.11.18
[C++] 전역 함수를 이용한 연산자 오버로딩  (0) 2014.11.18
[C++] 연산자 오버로딩  (0) 2014.11.18
[C++] 다형성  (0) 2014.11.18
반응형
//************************************************************
// 전역 함수를 이용한 연산자 오버로딩 !!!
//************************************************************ 

#include 
using namespace std;

class Time
{
private:
	int m_h;
	int m_m;
	int m_s; 
public : 
	Time( int h = 12 , int m = 0 , int s = 0) : m_h(h), m_m(m), m_s(s){} 
	void Show() { cout << m_h <<":" << m_m << ":" << m_s << endl; }
	// C++에서는 시간을 나타내는 클래슬 제공하지 않는다 !! 
	// --> 시간 + 시간 : 시간  
	// --> 시간 + 정수 : 초 단위 
	// --> 시간 - 시간 :  시간 
	// --> 시간 - 정수 : 초 단위
	// --> 시간 == 시간 
	// --> 시간 != 시간 
	
	
	
	// 과연 좋은 방법일까?? 
	//friend ostream & operator<<( ostream & os ,  Time & time); 
};
//cout객체와 함께 time객체를 사용하기 위해서 전역 함수를 이용한 연산자 오바로딩 !! 
ostream & operator<<( ostream & os ,  Time & time) 
{
	time.Show();
	//os << time.m_h << ":" <<  time.m_m << ":" << time.m_s << endl; 
	return os; 
}

void main()
{
	Time time(8,19,0); 
	//time.Show();

	cout << time;

}

'C, C++' 카테고리의 다른 글

[C++] 동적 배열 클래스  (0) 2014.11.18
[C++] () 연산자 오버로딩 !!  (0) 2014.11.18
[C++] 연산자 오버로딩  (0) 2014.11.18
[C++] 다형성  (0) 2014.11.18
[C++] 상속 이란? 이해, 생성, 소멸  (0) 2014.11.18
반응형
//************************************************
// 연산자 오버 로딩 !! !!
//   -  하나의 연산자가 여려가지 용도로 사용되게 하는것 !! 
//************************************************
// ex) 
// << :  비트 시프트 연산자 !! 
#include 
using namespace std;

class MyCout
{	
	// << :연산자 오버로딩 함수를 추가 !! 
	// 1) 멤버 함수 연산자 오버로딩 !! 
public:
	MyCout()
	{	static int cnt = 0;
		cout << ++cnt << endl; 
	}
	MyCout( const  MyCout &  c  )
	{
		cout << "xxxxx" << endl;
	}

	MyCout & operator<<( int x )
	{
		printf("%d", x);
		return (*this); 
	}
	MyCout &   operator<<(char * str)
	{
		printf(str);
		return (*this); 
	}
	MyCout &  operator<<( double d) 
	{
		printf("%g", d);
		return (*this); 
	}
	MyCout &  operator<<( void * p) 
	{
		printf("\n");
		return (*this); 
	}
};
// Cout객체 전역 변수 선언 !! 
MyCout  Cout; 

void main()
{	
	Cout <<  100  << "도선아" << 3.14 ; 




//	 cout << 100  << endl; 
//	cout.operator<<("xxxx");
	//연산자 오버로딩은 연산자를 이름으로 갖는 함수의 호출이다 !! 
}

'C, C++' 카테고리의 다른 글

[C++] () 연산자 오버로딩 !!  (0) 2014.11.18
[C++] 전역 함수를 이용한 연산자 오버로딩  (0) 2014.11.18
[C++] 다형성  (0) 2014.11.18
[C++] 상속 이란? 이해, 생성, 소멸  (0) 2014.11.18
[C++] this 이해  (0) 2014.11.18
반응형


//*************************************************
// 다형성 !! 
//*************************************************
//*************************************************
	class Person 
	{
	}

	class Student : public Person 
	{
	
	}
	
	void main()
	{
		// 
		int a = 10; 
		int b = 10;
		double d; 
		a = b; 
		// 
		a = d; 
		// 부모의 포인터       자식의 객체  
		Person * p= new Student(); 

	}





//*************************************************
//  - 다형성 
//*************************************************
//  --> 부모는 자식을 가르킬 수 있다 !! 
//*************************************************
#include 
using namespace std;

class Player 
{	
private:
	int m_no; //넘버 
	int m_hp; //체력 
public :
	Player(int no = 0, int hp = 100 )
	{
		m_no = no;
		m_hp = hp;
	}
   virtual void training(){ cout << "키티타카 ,,," << endl; } 
	virtual	void Vacaiton(){}
};
// 공격수 
class  Striker : public Player
{	
private : 
	int m_goal;  // 
public : //오버라이딩 :  재정의 : 
	// 부모의 행동을 자식이 새롭게 정의 하는것 !! 
	void training(){ cout << " 슛~ "<< endl; } 	
	void Shot(){} 
	void Vacaiton(){}
};

class Defender :  public Player
{
private : 
	int m_tackle; //태클 
public : // 오버라이딩 : 재정의 !! 
	void training(){ cout <<  " 공 돌리기 !! "  << endl; }
	void Vacaiton(){}
};
void main()
{
	Player * p1 =  new Striker();
	Player * p2 =  new Defender(); 

	// 축구팀 !! 
	Player * PlayerList[3]; 
	
	PlayerList[0] = new Striker(); 
	PlayerList[1] =  new Defender(); 
	PlayerList[2] =  new Striker(); 
	
	for(int  i = 0; i<3; i++) 
	{	// 신기한 코드 !! --> 알아서 찾아간다 !! 
		// --> 
		//함수호출 
		PlayerList[i]->training();  
		// --> 동적 바인딩 : 실행 시간에 
		// 어떤 함수 호출될지가 결정되는것 !! 
	// 하나의 코드로 다양한 형태로 실행이 되는것 !! 
		// --> 다형성 !!  
		//함수 호출 
		printf("");  // 코딩 !!! --> 정적 바인딩 !! 
		// 정적 바인딩 --> 컴파일타임: 코딩할때 어떤함수
		//를 호출 할지가 결정된다 !! 
	}


}


'C, C++' 카테고리의 다른 글

[C++] 전역 함수를 이용한 연산자 오버로딩  (0) 2014.11.18
[C++] 연산자 오버로딩  (0) 2014.11.18
[C++] 상속 이란? 이해, 생성, 소멸  (0) 2014.11.18
[C++] this 이해  (0) 2014.11.18
[C++] 캡슐화  (0) 2014.11.18
반응형


//******************************************************
// 상속 
//******************************************************
// --> 부모로 부터 물려 받는것 !! 
// --> 이미 구현되어있는 클래스로 부터 물려 받아서 새로운 기능을 확장 !! 
// --> extends --> 확장 !! 
// --> 재사용성의 개념을 가장 중요한 개념으로 여긴다 !! 	
//         폭스바겐 
// 					엔진 
//                     |
//    뉴비플 - 폴로 -골프 - 제타 - 파사트 - 티구안 
// 
// 일반화 : 모든 자동차에 적용될수 있는  기능들을 담고 있다. 
//******************************************************
// 상속 (구현상속) :  부모되는 클래스에 작성한 내용을 자식에서 그대로 물려 받는것 !! 
//  코드를 상속시켜주는것 을 의미한다 !! 
#include 
using namespace std; 


class  HumanBase // 사람의 기본적인 속성과 행동을 정의 한다.. 
{	
public : 
	int m_No; //식별 번호  
	int m_age;  // 나이 
	char m_name[20];  // 이름 !! 

public : 
	HumanBase(){} 
	~HumanBase(){} 
	
	void setNo(int no) {m_No = no; }
	void setAge(int age){m_age = age; }
		
	int getNo(){return m_No;}
	int getAge(){return m_age;}
	const char * getName(){return m_name;}
	// 추가적인 행동을 !! 
};
// 상속을 이용한 자식 클래스의 구현 !! 
				//상속지정자  부모 클래스 !! 
class Student : public HumanBase  
{
	public : 
	int m_No; //식별 번호  
	int m_age;  // 나이 
	char m_name[20];  // 이름 !! 

public : 
	HumanBase(){} 
	~HumanBase(){} 
	
	void setNo(int no) {m_No = no; }
	void setAge(int age){m_age = age; }
		
	int getNo(){return m_No;}
	int getAge(){return m_age;}
	const char * getName(){return m_name;}
private: 
	int m_grade ;//학년 
	char m_major[100]; //전공 
public : 
	void Study(){}  // 공부 
	void Circle(){} //동아리활동
}; 

class Teacher : public HumanBase
{
private: 
	int m_sal; //연봉 
public: 
	void Work(){} 
};


void main()
{
	Student s; 
	s.Study(); 
	// 행동도 물려받는다 !! 
	s.getAge(); 
	// 속성값도 물려받는다 !!
	s.m_age = 20; 
	
	Teacher t;
	t.getName(); 
	t.m_sal = 200; 
}


//************************************************* // 상속을 구현하는 상황에 대한 이해 !! //************************************************* // is - a 관계 !! // --> ex) 사람은 동물이다 !! ?? OK !! // --> ex) 스마트폰은 전화이다 !!?? OK !! // --> ex) 스마트폰은 게임기이다!!?? // 일반적으로 상속 --> is - a 관계가 성립하는 관계를 말한다 !! //************************************************* // has-a 관계 !! //************************************************* // ex) 스마트폰은 mp3를 갖고 있다 !! --> OK !! // ex) 자동차는 엔진을 갖고 있다 !! --> OK !! // 포함관계를 통해 구현하는 방법을 바람직하다 !! // 인터페이스 상속을 구현하는방법 !! // ex) 학교는 학생을 갖고 있다 !! // ex) 학교는 선생님을 갖고있다 !! // 상속은 --> is -a 관계일 경우에만 하자 !! // has - a 관계는 --> 포함관계, 인터페이스 상속


//**********************************************
// 상속상속 지정자와 상속의 은폐엄폐 
//**********************************************
// 95%이상은 public상속이다 !! 
// --> 부모의 특성을 그대로 물려 받겠다 !! 

// 상속 !! 
// --> is-a관계가 성립하는 public상속을 상속 !! 

#include  
using namespace std; 

class Base 
{
private:	  // 클래스 내에서만 사용이 가능 
	int a; 
protected : // 자기 클래스내와  자식클래스에서만 접근 !!    
	int b; 
public :
	int c; 

};
class BabyBase : private Base 
{	
public :									        // private  protected  public
	void foo()					   //public      접근 불가  protected  public
	{   cout <<  c << endl;     } // protected 접근 불가  protected  protected
};									   // private    접근 불가  private	    private
class  BabyBaby : public BabyBase 
{
	void goo(){  cout << c << endl;  }
		
};
void main()
{
	BabyBase bb;
	cout <<  bb.c << endl;
	
}





//*************************************************** // 상속과 객체의 생성과 소멸 //*************************************************** #include using namespace std; class Car { private: int m_No; public : Car(){ cout << "Car()" << endl; } Car(int no ){ m_No = no; cout << "Car(int no)" << endl; } ~Car(){ cout << "~Car()" << endl; } }; class Bus : public Car { private: int m_LineNo; public: Bus() { cout << "Bus()" << endl; } // 자식의 멤버는 자식의 생성자에서 부모의 멤버 부모의 생성자 !! Bus(int Carno , int LineNo) : Car( Carno) { cout << "Bus(int Carno , int LineNo)" << endl; m_LineNo = LineNo; } ~Bus(){ cout << "~Bus()" << endl; } }; void main() { Bus ns802( 1004, 801); }




'C, C++' 카테고리의 다른 글

[C++] 연산자 오버로딩  (0) 2014.11.18
[C++] 다형성  (0) 2014.11.18
[C++] this 이해  (0) 2014.11.18
[C++] 캡슐화  (0) 2014.11.18
[C++] static 키워드, 멤버 함수와 활용  (0) 2014.11.18
반응형
//*******************************************************
//  this의 이해 !! 
//*******************************************************
//  this 포인터 --> 자기 자신을 나타내는 포인터 !!  
// --> 자신의 개념 --> 자기객체 --> 지금 이 함수를 실행하고 있는 객체  !! 

#include 
using namespace std;

class Person
{
private: 
	int m_age; 
public: 
	Person(): m_age(0) {}
	// static 멤버 함수 안에서는 static멤버만 접근 가능 !! 
	 
	 void Show( Person * this  )  
	{
		//cout << pthis->m_age++  << endl;
	}	
} 


void main()
{
	Person::Show(); 


	Person p1; 
	Person p2; 
	Person p3; 
			 //함수를 호출한 객체의 주소값이 넘어간다 !!
	p1.Show( &p1 ); 

	cout << &p1 << endl;
//	p2.Show();
//	p3.Show(); 
}

'C, C++' 카테고리의 다른 글

[C++] 다형성  (0) 2014.11.18
[C++] 상속 이란? 이해, 생성, 소멸  (0) 2014.11.18
[C++] 캡슐화  (0) 2014.11.18
[C++] static 키워드, 멤버 함수와 활용  (0) 2014.11.18
[C++] const 멤버의 이해, 활용, 사용  (0) 2014.11.18
반응형
//*****************************************************
// 캡슐화 !! --> 캡슐 == 알약 !! 
// -->  1) 가루형태로 분리되어있는것을 하나로 모아 관리 !!
//      2)  약 좋은 속성과 나쁜속성을 제어 하기 위해 !! 
// --> 치료는 그대로 두면서 쓰다라는 속성을 감춘다 !! 
// -->  캡슐화 !! \
// private:  클래스 내부에서만 접근이 가능한 멤버 !! 
//*****************************************************
// 1) 속성은 private멤버로하자 !! 
// 2) 초기화 : 생성자를 통해서 구현하자 !! 
class Person 
{
private: 
	int   m_age;		  // 나이 --> 사람의 나이를 외부에서 임의로 수정 가능? 
	char m_name[20]; // 이름 --> 접근 할수 없도록 막는다 !! 
	int   m_Score;  
public : 
	//get/set 함수 ( getter/setter)함수를 작성한다!! 
	//  -  프로그래머가 직접 멤버 변수의 접근 권한을 시스템의 따라 설정 !!
	//  -  데이타의 신뢰성을 보장 할수 있다 !! 
	int getAge(){ return m_age; } // 나이를 가져와본다 ! 
	void setAge(int NewAge)
	{
		if( NewAge >= 1) 
			m_age = NewAge; 
	}
	char * getName(){ return m_name;} // 이름을 가져와 본다 !!
	int getScore(){ return m_Score;} 
	void setScore(int score) 
	{  // 예외처리 구문을 포함 할수 있다 !! 
		if( score >= 0  &&  score <= 100) 
    		m_Score =  score; 
	}

	Person() //생성자 !! 
	{
		m_age = 20; 
		strcpy(m_name, "홍길동");
	}
};
void main()
{
	Person p;
	p.setAge(-10); // 논리적 모순 !! 


}

'C, C++' 카테고리의 다른 글

[C++] 상속 이란? 이해, 생성, 소멸  (0) 2014.11.18
[C++] this 이해  (0) 2014.11.18
[C++] static 키워드, 멤버 함수와 활용  (0) 2014.11.18
[C++] const 멤버의 이해, 활용, 사용  (0) 2014.11.18
[C++] 추상화  (0) 2014.11.18

+ Recent posts