C++ 시작

아현·2021년 6월 28일
0

C++

목록 보기
1/5

삼성 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

참조


  • 네임스페이스 (Namespace)

    : 네임스페이스는 모든 식별자(변수, 함수, 형식 등의 이름)가 고유하도록 보장하는 코드 영역을 정의한다.



스코프 분석 연산자(::) (scope resolution operator (::))


  • 컴파일러가 특정 네임스페이스에서 식별자를 찾게 하는 첫 번째 방법은 스코프 분석 연산자(::)를 사용하는 것이다.

    • 이 연산자를 사용하려면 식별자 이름 앞에 사용할 네임스페이스를 붙이면 된다.

    • 다음은 :: 연산자를 사용해서 컴파일러에 명시적으로 "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; 

}





중첩된 네임스페이스 (Nested namespaces)


  • 네임스페이스는 다른 네임스페이스 안에 중첩될 수 있다.

    • 네임스페이스 "Goo"는 네임스페이스 "Foo"내부에 있으므로 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


using namespace std;

main(){
	cout << "구매금액을 입력하세요" << endl; // std 생략 가능
}
profile
For the sake of someone who studies computer science

0개의 댓글