삼성 SDS 아카데미에 합격을 했으나 테스트 시, 혹은 강의를 수강할 때 C, C++, Java를 이용하는 듯 하여 강의를 듣기 전에, C는 수업을 들어서 알고 있지만, C++은 경험해본 적이 없기에 공부를 조금 해보려고 한다.
먼저 유튜브로 개념 강의를 조금 보아야겠다.
#include <iostream>
int main()
{
std::cout << "Hello World!\n"; // std::endl
std::cout << "wonderful c++" << std::endl;
}
#include <iostream>
int main() {
std::cout << "구매금액을 입력하세요" << std::endl;
int caltot, calpoint; //변수 위치 상관 x (c는 처음에)
std::cin >> caltot; //공백없는 입력
calpoint = caltot * 0.01;
std::cout << "이번 회 발생 포인트: " << calpoint << "점 입니다. \n";
}
#include <iostream>
int main() {
char irum[30];
std::cout << "성함과 전화번호를 입력하세요" << std::endl;
std::cin.getline(irum, sizeof(irum));
std::cout << "성함과 전화번호를 구매금액을 입력하세요" << std::endl;
int caltot, calpoint; //변수 위치 상관 x (c는 처음에)
std::cin >> caltot;
calpoint = caltot * 0.01;
std::cout<< irum << "이번 회 발생 포인트: " << calpoint << "점 입니다. \n";
}
네임스페이스 (Namespace)
: 네임스페이스는 모든 식별자(변수, 함수, 형식 등의 이름)가 고유하도록 보장하는 코드 영역을 정의한다.
컴파일러가 특정 네임스페이스에서 식별자를 찾게 하는 첫 번째 방법은 스코프 분석 연산자(::)를 사용하는 것이다.
이 연산자를 사용하려면 식별자 이름 앞에 사용할 네임스페이스를 붙이면 된다.
다음은 :: 연산자를 사용해서 컴파일러에 명시적으로 "Foo 네임스페이스"에 있는 doSomthing() 함수를 사용할것 이라고 알려주는 예제다.
int main(void) {
std::cout << Foo::doSomething(4, 3); return 0;
}
int main(void) {
std::cout << Foo::doSomething(4, 3) << '\n';
std::cout << Goo::doSomething(4, 3) << '\n';
return 0;
}
네임스페이스는 다른 네임스페이스 안에 중첩될 수 있다.
Foo:Goo:g_x
로 접근한다.
#include <iostream> namespace Foo {
namespace Goo // Goo is a namespace inside the Foo
namespace {
const int g_x = 5;
}
}
int main() {
std::cout << Foo::Goo::g_x;
return 0;
}
//이렇게도 가능
#include <iostream>
namespace Foo::Goo // Goo is a namespace inside the Foo namespace (C++17 style)
{
const int g_x = 5;
}
int main() {
std::cout << Foo::Goo::g_x;
return 0;
}
using namespace std;
main(){
cout << "구매금액을 입력하세요" << endl; // std 생략 가능
}