[C++] Tutorial

두꺼비·2022년 1월 11일
2
post-custom-banner

vscode에 컴파일 설정하느라 넘 힘들었다 ㅠㅜㅠㅜ 후..
C++ 빠르게 끝내자!!


#include <iostream>
using namespace std;

int main() {
  cout << "Hello World!";
  return 0;
}

Hello World!

#include <iostream> -> header file library
using namespace std; -> we can use names for object and variables from the standard library.


insert new line

#include <iostream>
using namespace std;

int main() {
  cout << "Hello World!" << endl;
  cout << "I am learning C++";
  return 0;
}

Hello World!
I am learning C++


Display Variables

int myAge = 35;
cout << "I am " << myAge << " years old.";

I am 35 years old.


Constants
Unchangeable and read-only

const int myNum = 15;
myNum = 10;
cout << myNum;

error


Input

#include <iostream>
using namespace std;

int main() {
  int x;
  cout << "Type a number : "
  cin >> x;
  cout << "Your number is : " << x;
  return 0;
}

입력값 : 3
Type a number : 3
Your number is : 3


Character Data Types

char a = 65, b = 66, c = 67, d = 48;
cout << a;
cout << b;
cout << c;
cout << d;

ABC0


String

#include <iostream>
#include <string>
using namespace std;

int main() {
  string greeting = "Hello";
  cout << greeting;
  return 0;
}

Hello
include<string> 해주기


String Concatenation and append

string firstName = "John ";
string lastName = "Doe";
string fullName1 = firstName + lastName;
string fullName2 = firstName.append(lastName);

String input with white space

  string fullName;
  cout << "Type your full name: ";
  getline (cin, fullName);
  cout << "Your name is: " << fullName;

input : Lee Babo
Type your full name:Lee Babo
Your name is:Lee Babo


Omitting Namespace

#include <iostream>
#include <string>

int main() {
  std::string greeting = "Hello";
  std::cout << greeting;
  return 0;
}

Hello


C++ Math
C++<cmath> Header
#include <cmath>

max(x, y)
min(x, y)
abs(x)
cos(x)
acos(x)
sin(x)
asin(x)
tan(x)
atan(x)
.
.


The if statement of C++ is the same as that of C.
That's the same with the else and else if statement.
삼항연산자도~
switch문도~
while, do while문도~
for문도~


References

string food = "Pizza";  // food variable
string &meal = food;    // reference to food

cout << food << endl;
cout << meal << endl;
cout << $food;

Pizza
Pizza
0x61fef0
<- memory address


Pointer

string food = "Pizza"; 
string* ptr = &food;    

cout << food << "\n";

cout << &food << "\n";

cout << ptr << "\n";

//Dereference
cout << *ptr << "\n";

//Modify the Pointer Value
*ptr = "Hamburger";
cout << *ptr << "\n";
cout << food;

Pizza
0x6dfed4
0x6dfed4
Pizza
Hamburger
Hamburger






참고 : W3schools

profile
두꺼비는 두껍다
post-custom-banner

0개의 댓글