티스토리 뷰
#입출력 기본형
*소스파일 추가- .cpp
#include<iostream> //입출력 라이브러리 전처리
int main() //int 타입 리턴
{
//std 네임스페이스 지정
std::cout << "Hellow World!!\n"; //,cout-표준 출력메서드, cout<<(cout에 Hellow를 전달)
std::cout << "Hwanguk lee";
return 0; //return 으로 종료
}
*std:: ->네임스페이스를 지정한다
*cout이라는 표준 입출력 메서드사용 (cout<<"Hellow World";) (cout에 "hellow World"; 문자열을 대입)
*return으로 메서드 종료
*기본입출력문제
https://www.acmicpc.net/problem/2557
2557번: Hello World
Hello World!를 출력하시오.
www.acmicpc.net
#include<iostream>
int main() {
std::cout << "Hello World!";
return 0;
}
*입력받은 정수를 연산(stdio, iostream)
https://www.acmicpc.net/problem/1000
1000번: A+B
두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오.
www.acmicpc.net
*1. #includs<stdio.h> , scanf
#include<stdio.h>
int main(int arg,char const *argv[]) {
int A;
int B;
scanf("%d", &A);
scanf("%d", &B);
printf("%d", A + B);
return 0;
}
*2. #include<iostream> , cin
#include<iostream>
using namespace std;
int main(int arg,char const *argv[]) {
int A;
int B;
cin >> A;
cin >> B;
cout << A + B;
return 0;
}
#Class
*생성자와 소멸자가 존재함
https://learn.microsoft.com/ko-kr/cpp/cpp/destructors-cpp?view=msvc-170
소멸자 (C++)
자세히 알아보기: 소멸자(C++)
learn.microsoft.com
*C#과 같은 한정자, private, public, protect
*인라인 함수(매크로와 유사) -함수 호출에 대한 오버헤드X 실행속도 개선
*객체 포인터-객체의 주소값을 가지는 변수
*동적메모리 할당(new연산자/delete연산자)
int* pInt = new int();
char* pChar = new char();
delete pInt;
delete pChar;
*C#의 this. 사용가능 "this->" 으로 표기
#include<iostream> //전처리
using namespace std; //std 네임스페이스
//클래스 선언
class Circle
{
//맴버
public:
int radius; //변수
double getArea(); //메서드
};
double Circle::getArea() {
return 3.14 * radius * radius;
}
int main() {
Circle donut;
donut.radius = 1;
double area = donut.getArea();
cout << "dount 면적은" << area << endl;
}
#메서드의 인자 전달 방식
*값에 의한 호출 ex)swap(n,m);
*주소에 의한 호출 ex)swap(&n,&m);
*포인터 변수 " * "사용 / 참조 변수 선언 " & "사용
#include <iostream>
using namespace std;
class Circle {
private:
int radius;
public:
Circle() { radius = 1; }
Circle(int radius) { this->radius = radius; }
double getArea() { return 3.14*radius*radius; }
};
void swap(Circle &a, Circle &b) {
Circle tmp;
tmp = a;
a = b;
b = tmp;
}
int main() {
Circle a(1), b(2);
cout << "a의 면적 = " << a.getArea() << ", b의 면적 = " << b.getArea() << endl;
swap(a, b);
cout << "a의 면적 = " << a.getArea() << ", b의 면적 = " << b.getArea() << endl;
}
#프랜드,연산자
#include <iostream>
using namespace std;
class Power {
int kick;
int punch;
public:
Power(int kick=0, int punch=0) {
this->kick = kick; this->punch = punch; //C# this.kick this.punch
}
void show();
Power operator+ (Power op2); // + 연산자 함수 선언
};
void Power::show() {
cout << "kick=" << kick << ',' << "punch=" << punch << endl;
}
Power Power::operator+(Power op2) {
Power tmp; // 임시 객체 생성
tmp.kick = this->kick + op2.kick; // kick 더하기
tmp.punch = this->punch + op2.punch; // punch 더하기
return tmp; // 더한 결과 리턴
}
int main() {
Power a(3,5), b(4,6), c;
c = a + b; // 파워 객체 + 연산
a.show();
b.show();
c.show();
}
'C, C++' 카테고리의 다른 글
C++ 로 레스토랑 툴 만들기 (0) | 2023.06.11 |
---|---|
C언어를 이용한 학생 관리 프로그램 (0) | 2023.06.05 |