이원자 탄소 2020. 10. 10. 11:40
728x90

C++은 객체지향 프로그래밍 언어이다

Object - Oriented Programming

객체(Object) 라는 작은 단위로서 모든 처리를 기술하는 프로그래밍 방법

 

<iostream> - 입출력을 담당하는 함수

 

씨쁠쁠에서 출력

#include <iostream>

int main() {

	printf("탄소 \n");
	printf("19\n");
	printf("B\n");
	printf("156\n\n");

	std::cout << "탄소" << std::endl;
	std::cout << "19" << std::endl;
	std::cout << "B" << std::endl;
	std::cout << "156" << std::endl;

	return 0;
}

결과는 같다

cmd 창으로 디렉터리 파일 하나 만들어보자

 

cmd 창 > dir> cd 파일 이름> dir>>dirlist.txt (dir에 dirlist라는 파일 생성)> 생성된거 확인

 

printf 대신에 c++에선 std::cout << "~" << std::endl; 쓰면 됨

 

#include <iostream>

int main() {

	//변수를 이용한 출력
	char name[10] = "홍길동";
	int age = 20;
	char bloodtype = 'A';
	double height = 175.5;

	std::cout << "이름은" << name << "입니다" << std::endl;
	std::cout << "나이는" << age << "입니다" << std::endl;
	std::cout << "혈액형은" << bloodtype << "입니다" << std::endl;
	std::cout << "키는" << height << "입니다" << std::endl;

	return 0;
}

이렇게 변수를 이용한 출력도 ㄱㄴ

 

형식:

std(공간 이름)::(스코프 연산자)cout(출력함수)<<출력대상;

std(공간 이름)::(스코프 연산자)cout(출력함수)<<출력대상<<출력대상<<출력대상;

std(공간 이름)::(스코프 연산자)cout(출력함수)<<출력대상<<std::endline(닫는거)

 

데이터 입력받기 

- 헤더파일 선언

- std::cin과 >>연산자를 이용한 입력 (%d 대용)

 

형식:

std::cin>>번수;

std::cin>>번수1>>변수2;

 

직접 입력받고 출력

#include <iostream>

int main() {

	int age;
	char name[10];
	char bloodtype;
	double height;
	int result = 0;

	std::cout << "나이 입력: ";
	std::cin >> age;
	std::cout << "이름 입력: ";
	std::cin >> name;
	std::cout << "혈액형 입력: ";
	std::cin >> bloodtype;
	std::cout << "키 입력: ";
	std::cin >> height;
	
	std::cout << "\n나이: " << age << std::endl;
	std::cout << "이름: " << name << std::endl;
	std::cout << "혈액형: " << bloodtype << std::endl;
	std::cout << "키: " << height << std::endl;

	return 0;
}

728x90