C++ 8. 상수화 멤버
- 카테고리 없음
- 2016. 4. 19. 21:12
-상수화 멤버
//Student.h
#pragma once
#include <iostream>
#include <string>
using namespace std;
class Student
{
const int num; //비 정적 상수화 멤버 필드
string name;
int hp;
static const int max_hp; //정적 장수화 멤버 필드
public:
Student(int _num, string _name);
void View()const; //상수화 멤버 메소드
};
//Student.cpp
#include "Student.h"
const int Student::max_hp = 200; //정적 상수화 멤버 필드 초기값 지정
Student::Student(int _num, string _name):num(_num) //비 정적 상수화 멤버 필드 초기화 = 이니셜라이저
{
name = _name;
hp = 0;
}
void Student::View()const //상수화 멤버 메소드
{
cout<<"번호 :"<<num<<endl<<"이름 :"<<name<<endl<<"HP :"<<hp<<endl;
}
//Program.cpp
#include "Student.h"
int main()
{
Student *stu = new Student(3,"홍길동");
stu->View();
delete stu;
return 0;
}
이 글을 공유하기