개발자/C++
C++ 클래스와 객체 사용법
김진리
2015. 2. 12. 21:14
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
class Bmi { Bmi 클래스의 선언 마지막에는 세미콜론으로 닫아준다.
public : public 형의 변수선언
int weight;
int tall;
double getBMI();
};
Bmi 클래스의 함수 구현
double Bmi::getBMI(){
return weight/((tall*tall)/10000); Bmi 클래스 안의 변수 사용
}
int main() {
Bmi bmi; 자바와 다르게 변수 선언으로 객체가 생성!!
bmi.weight = 71;
bmi.tall = 180;
cout << "비만도는 " << bmi.getBMI() << endl;
return 0;
}
결과값
자바와 다르게 변수가 선언되면 객체가 생성되어 메모리에 할당된다.