반응형
//****************************************************
//	const멤버의 이해 !! 
//****************************************************
// const 키워드에 대해 알아보자 !! 
//****************************************************
#include 
using namespace std; 
//const :  상수화 ---> 값을 변경하지 못하도록 한다 !! 
#define SIZE 100  

// const 멤버 변수 : 멤버 변수중에 절대 값을 바꾸면 안되는 경우 사용 !! 
class Student 
{
private: 
     const   int m_StudentNo; // 값을 변경하지 못한다!! 
	 const bool m_sex;
			  int  m_age; 
public :	// 초기화 리스트 :  이니셜라이저 !! 
	Student() :  m_StudentNo(1000) , m_sex(true), m_age(20)    //생성자 
	{//  const멤버 변수는 생성자에서 초기화 불가능하다 !! 
	 // 반드시 초기화 리스트를 통해서만 초기화 가능하다 !! 
	//	m_StudentNo = 1000; 
	//	m_sex  = true; 
	}
	Student( int no ,  bool sex , int age ) : 
	m_StudentNo(no), m_sex(sex), m_age(age) //초기화 리스트 !! 
	{
		
	}
	void Show() { cout << m_StudentNo << m_sex << m_age << endl; }
	
};
void main()
{	
	Student s; 
	s.Show();
	// 변수의 값으 변경하지 못하게 할때 !! 
//	const int MAX = 100; // 선언과 동시에 초기화 !!	
//	cout << MAX++ << endl;

}


//****************************************************
// const 멤버 함수의 선언과 사용 !! 
//****************************************************
// const 멤버함수 :  함수안에서 멤버 변수의 값을 변경 불가능 !! 
#include 
using namespace std;

class  Car 
{
private: 
const int m_CarNo; 
	   int m_Year; //연식 
public :
	Car() : m_CarNo(1000) { m_Year = 2000; } 
	
	void CarInfo() const // ==> 이함수에서는 절대 멤버 변수의값을 변경불가능 !! 
	{	
		// const함수에서는 절대 일반 멤버 함수를 호출 할수 없도록 한다 !! 
		// 문법적인 오류를 발생 시킨다 !! 
		cout << m_CarNo << setYear(1000)  << endl; 
	}
	void setYear( int NewYear) const  { m_Year  = NewYear; }

};


void main() 
{
	

}



//*****************************************************
//	const 멤버 함수와 함수의 활용 !! 
//*****************************************************
#include 
using namespace std;

// 마린 !! 
class Marin 
{
private: 
                 int m_hp;    //체력 
    const static  int m_price; //가격
			static int m_att;	   // 공격력 
			static int m_def;    // 방어력 

public : 
	// setter/getter 
	int getHp()	 const { return m_hp; }
	int getPrice() const  { return m_price; }
	int getAtt()   const  { return m_att; } 
	int getDef()   const  { return m_def;}

	Marin() : m_price(50) //const멤버 초기화 
	{
		m_hp = 40; 
		m_att = 1; 
		m_def = 1; 
	}
	void ShowMarinInfo() const 
	{
		cout << "가격 : "    << m_price << endl;
		cout << "체력 : "    << m_hp << endl; 
		cout << "공격력  : " << m_att << endl; 


	}

};



void main()
{

}


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

[C++] 캡슐화  (0) 2014.11.18
[C++] static 키워드, 멤버 함수와 활용  (0) 2014.11.18
[C++] 추상화  (0) 2014.11.18
[C++] 생성자  (0) 2014.11.18
[C++] struct , 구조체!!  (0) 2014.11.18

+ Recent posts