2016년 11월 3일 목요일

[C++] Hello World

main.cpp 예제
#include 
#include     /* getpid() 를 사용하기 위해 필요 */
#include     /* rand() 사용하기 위해 필요 */
#include 

#include "libs/testClass.h"  /* 사용자 정의 클래스 */

using namespace std;

/* prototype */
void sayHello(string msg);  /* 테스트용 메서드 */
string sayHello2(string msg); /* 테스트용 메서드2 */
int getRandInt(int maxNum);  /* 랜덤한 숫자를 얻어내기 위한 메서드 */

int main() {
 cout << "PID : " << getpid() << " ==> Hello World!!!" << endl; // prints !!!Hello World!!!

 sayHello("홍길동");
 cout << sayHello2("홍길순") << endl;
 cout << "RAND NUM : " << getRandInt(64) << endl;

 cout << "=====================================" << endl;

 testClass tc = testClass();
 cout << tc.iSay("ho~") << endl;
 cout << tc.youSay("hu~~") << endl;

 return 0;
}

/* 테스트용 메서드 */
void sayHello(string msg){
 cout << msg + "님, 안녕하세요?" << endl;
}

/* 테스트용 메서드2 */
string sayHello2(string msg){
 return msg + "님, 안녕하세요?";
}

/* 랜덤한 숫자를 얻어내기 위한 메서드 */
int getRandInt(int maxNum){
 srand(time(NULL));
 return rand() % maxNum +1;
}
testClass.h 예제
#ifndef TESTCLASS_H_
#define TESTCLASS_H_

#include 
using namespace std;

class testClass {
public:
 testClass();    /* 생성자 */
 virtual ~testClass();  /* 소멸자 */

 string iSay(string msg); /* 내가 말하는 메서드 */
 string youSay(string msg); /* 네가 말하기 메서드 */
};

#endif
testClass.cpp 예제
#include "testClass.h"

/* 생성자 */
testClass::testClass() {
 // TODO Auto-generated constructor stub
 cout << "생성자 호출 됨" << endl;
}

/* 소멸자 */
testClass::~testClass() {
 // TODO Auto-generated destructor stub
 cout << "소멸자 호출 됨" << endl;
}

/* 내가 말하는 메서드 */
string testClass::iSay(string msg){
 return "i say " + msg;
}

/* 네가 말하는 메서드 */
string testClass::youSay(string msg){
 return "you say " + msg;
}

댓글 없음 :

댓글 쓰기