C ++

[C++] 3. 변수 타입(type) 정리, 연산자(operator), 분기문(statement)

걍작 2022. 4. 5. 21:41

#정리 

#08 type - 변수 타입 

#구조체(struct)
 - struct Test{ ~

  >> struct Test t = {"kim"};
#클래스(class)
 - class Person{ ~

 >> Person* p = new Person; // ()가 없고, *가 들어가는게 다르다.

 - 클래스 변수는 기본적으로 private 범위를 부여받는다. 

 - Person(){//생성자
 - ~Person(){//소멸자
 - getter / setter 함수는 별도로 만들어줘야한다.

#int / unsigned int 
 - int su = 100;//4Byte
 - unsigned int su2 = -100;//4byte
#char / unsigned char 
 - char c = 'a';//1Byte -128~127 : 256

 - unsigned char c2 = 97;//1byte

#wchar_t (다국어 표현을 위한 문자열 변수)
 - wchar_t wc_t = L'김'; //2Byte

#short / unsigned short
 - short s1 = 100;//2Byte
 - unsigned short s2 = -100;//2byte
#long / unsigned long
 - long lng1 = 100;//4Byte
 - unsigned long lng2 = -100;//4byte
#long long / unsigned long long
 - long long llng1 = 100;//4Byte
 - unsigned long long llng2 = -100;//4byte
#float / double 

//기본적으로 6자리까지만 보여줌

 - float f = 3.141592653589793f;

 - cout.precision(15);//15자리까지 보여줘

 - cout << "min-max: " << numeric_limits::min() ; //#include<limits>  필요
//floa와 값이 다른 이유는 float가 6자리까지 유효하기 때문이다.
 - double d = 3.141592653589793;
 - cout << "min-max: " << numeric_limits<double>::min() ; //#include <limits> 필요

//** 주의 ** 실수는 1000번 돌아도 100이 되지 않는다.
// 실수를 다룰 때는 반올림, 올림, 내림등의 조치가 필요하다.
 - cout << "sum: " << sum << endl;
 - cout << "floor(sum): " << floor(sum) << endl;
 - cout << "ceil(sum): " << ceil(sum) << endl;
 - cout << "round(sum): " << round(sum) << endl;
#bool
 - bool b = true;


#09operator - 연산자

//1.(),[], ,,(연결 연산자), ., *, **, ***, -> 
//2. (cast)캐스팅, ++, --. + 
//3. (기본 연산자) *, /, +, - %  
//4. (쉬프트) <<, >> 
//5. == != > < >= <=
 - cout << (5==5) << endl; //<<연산자가 우선순위가 높아서 ( )묶어줘야한다.
 - cout << x.compare("x") <<endl; //같으면 0을 반환
//6. & | ^ ~ (비트연산 - 숫자가 온다)
 - bitset<4> x1{15};
 - cout << (x1 & x2) << endl;//비트연산 확인 가능
//7. && || ! (true/false)
 - printf("%d\n", 5==5 && 5==5); //비교 연산자가 = 연산자보다 우선한다.
//8. ? : (삼항연산자)
 - 0이 아니면 다 참이다.
 - printf("%d\n",num?1000:2000); //num = 100 >> num이 0이 아니므로 참.




#10statment - 분기문 

//1. if else if ... eles
//2. switch case break;
//3. while, do - while
//4. for - default
//5. foreach
 >> c에는 없던 foreach 문법이 가능함.
 - int arr[3] = {11,22,33};
 - for(int x:arr){
 - cout << x << endl;
 - }

 

 

 

 

#08 type - 변수 타입 

#구조체(struct)
 - struct Test{ ~

  >> struct Test t = {"kim"};
#클래스(class)
 - class Person{ ~

 >> Person* p = new Person; // ()가 없고, *가 들어가는게 다르다.

 - 클래스 변수는 기본적으로 private 범위를 부여받는다. 

 - Person(){//생성자
 - ~Person(){//소멸자
 - getter / setter 함수는 별도로 만들어줘야한다.

#int / unsigned int 
 - int su = 100;//4Byte
 - unsigned int su2 = -100;//4byte
#char / unsigned char 
 - char c = 'a';//1Byte -128~127 : 256

 - unsigned char c2 = 97;//1byte

#wchar_t (다국어 표현을 위한 문자열 변수)
 - wchar_t wc_t = L'김'; //2Byte

#short / unsigned short
 - short s1 = 100;//2Byte
 - unsigned short s2 = -100;//2byte
#long / unsigned long
 - long lng1 = 100;//4Byte
 - unsigned long lng2 = -100;//4byte
#long long / unsigned long long
 - long long llng1 = 100;//4Byte
 - unsigned long long llng2 = -100;//4byte
#float / double 

//기본적으로 6자리까지만 보여줌

 - float f = 3.141592653589793f;

 - cout.precision(15);//15자리까지 보여줘

 - cout << "min-max: " << numeric_limits::min() ; //#include<limits>  필요
//floa와 값이 다른 이유는 float가 6자리까지 유효하기 때문이다.
 - double d = 3.141592653589793;
 - cout << "min-max: " << numeric_limits<double>::min() ; //#include <limits> 필요

//** 주의 ** 실수는 1000번 돌아도 100이 되지 않는다.
// 실수를 다룰 때는 반올림, 올림, 내림등의 조치가 필요하다.
 - cout << "sum: " << sum << endl;
 - cout << "floor(sum): " << floor(sum) << endl;
 - cout << "ceil(sum): " << ceil(sum) << endl;
 - cout << "round(sum): " << round(sum) << endl;
#bool
 - bool b = true;

 

 

--예문 코드 보기--

더보기
#include <iostream>
#include <limits> //float double 의 min-max - 사용 방법이 조금 다름
#include <climits> //일반적인 타입들의 min-max
#include <cmath> 
#include <typeinfo>

using namespace std;



struct Test{
	string name;
};

class Person{
	//public : 
		string name; //default private
		int *pt_arr;
	public : 
		Person(){//생성자
			this->name = "kim";
		}
		~Person(){//소멸자
			delete pt_arr;
		}
		void setName(string name){//setter
			this->name = name;
		}
			string getName(){//getter
			return name;
		}
};



int main(int argc, char **argv)
{
	cout << "Hello" << endl;
	cout << "-----------------" << endl;
	int su = 100;//4Byte
	cout << "su: " << su << endl;
	cout << "min-max: " << INT_MIN;
	cout << "~" << INT_MAX << endl;
	cout << "typeid: " << typeid(su).name() << endl;
	
		
	unsigned int su2 = -100;//4byte
	cout << "su2: " << su2 << endl;
	cout << "max: " << 0;
	cout << "~" << UINT_MAX << endl;
	cout << "typeid: " << typeid(su2).name() << endl;
	
	
	
	cout << "-----------------" << endl;
	char c = 'a';//1Byte -128~127 : 256
	cout << "c: " << c << endl;
	cout << "CHAR_BIT: " << CHAR_BIT << endl; //8 - 8bit라는 의미
	cout << "min-max: " << CHAR_MIN;
	cout << "~" << CHAR_MAX << endl;
	cout << "typeid: " << typeid(c).name() << endl;
	
	
	cout << "-----------------" << endl;
	unsigned char c2 = 97;//1byte
	cout << "c2: " << c2 << endl;
	cout << "min-max: " << CHAR_MIN;
	cout << "~" << CHAR_MAX << endl;
	cout << "typeid: " << typeid(c2).name() << endl;
	
	cout << "-----------------" << endl;
	wchar_t wc_t = L'김';
	cout << "wc_t: " << wc_t << endl;


	cout << "-----------------" << endl;
	short s1 = 100;//2Byte
	cout << "s1: " << s1 << endl;
	cout << "min-max: " << SHRT_MIN;
	cout << "~" << SHRT_MAX << endl;
	cout << "typeid: " << typeid(s1).name() << endl;
	

	unsigned short s2 = -100;//2byte
	cout << "s2: " << s2 << endl;
	cout << "max: " << 0;
	cout << "~" << USHRT_MAX << endl;
	cout << "typeid: " << typeid(s2).name() << endl;
	
	
	
	cout << "-----------------" << endl;
	long lng1 = 100;//4Byte
	cout << "lng1: " << lng1 << endl;
	cout << "min-max: " << LONG_MIN;
	cout << "~" << LONG_MAX << endl;
	cout << "typeid: " << typeid(lng1).name() << endl;
	
		
	unsigned long lng2 = -100;//4byte
	cout << "lng2: " << lng2 << endl;
	cout << "max: " << 0;
	cout << "~" << ULONG_MAX << endl;
	cout << "typeid: " << typeid(lng2).name() << endl;
    
	
	
	cout << "-----------------" << endl;
	cout.precision(15);//15자리까지 보여줘
	float f = 3.141592653589793f;
	cout << "f: " << f << endl;//기본적으로 6자리까지만 보여줌
	cout << "min-max: " << numeric_limits<float>::min() ; //#include <limits> 필요
	cout << "~" << numeric_limits<float>::max() << endl;
	
	cout << "-----------------" << endl;
	//floa와 값이 다른 이유는 float가 6자리까지 유효하기 때문이다.
	double d = 3.141592653589793;
	cout << "d: " << d << endl;
		cout << "min-max: " << numeric_limits<double>::min() ; //#include <limits> 필요
	cout << "~" << numeric_limits<double>::max() << endl;
	
	
	cout << "-----------------" << endl;
	//** 주의 ** 실수는 1000번 돌아도 100이 되지 않는다.
	// 실수를 다룰 때는 반올림, 올림, 내림등의 조치가 필요하다.
	double sum = 0;
	for (int i=0;i<1000;i++){
		sum += 0.1;
		//if(i==9)break;
	}
	cout << "sum: " << sum << endl;
	cout << "floor(sum): " << floor(sum) << endl;
	cout << "ceil(sum): " << ceil(sum) << endl;
	cout << "round(sum): " << round(sum) << endl;
	
	
	
	cout << "-----------------" << endl;
	bool b = true;
	cout << "b: " << b << endl;
	b = false;
	cout << "b: " << b << endl;
	
	
	
	cout << "-----------------" << endl;
	struct Test t = {"kim"};
	cout << "t.name: " << t.name << endl;
	cout << "typeid(t): " << typeid(t).name() << endl;
	
	// ()가 없고, *가 들어가는게 다르다.
	Person* p = new Person;
	//p->name = "lee";//private
	//cout << "p.name: " << p->name << endl;
	cout << "p->getName(): " << p->getName() << endl;
	cout << "typeid(p): " << typeid(p).name() << endl;
	
	
	return 0;
}

 

 

 


 

#09operator - 연산자

//1.(),[], ,,(연결 연산자), ., *, **, ***, -> 
//2. (cast)캐스팅, ++, --. + 
//3. (기본 연산자) *, /, +, - %  
//4. (쉬프트) <<, >> 
//5. == != > < >= <=
 - cout << (5==5) << endl; //<<연산자가 우선순위가 높아서 ( )묶어줘야한다.
 - cout << x.compare("x") <<endl; //같으면 0을 반환
//6. & | ^ ~ (비트연산 - 숫자가 온다)
 - bitset<4> x1{15};
 - cout << (x1 & x2) << endl;//비트연산 확인 가능
//7. && || ! (true/false)
 - printf("%d\n", 5==5 && 5==5); //비교 연산자가 = 연산자보다 우선한다.
//8. ? : (삼항연산자)
 - 0이 아니면 다 참이다.
 - printf("%d\n",num?1000:2000); //num = 100 >> num이 0이 아니므로 참.

--예문 코드 보기--

더보기
#include <iostream>
#include <bitset>
using namespace std; 

int main(int argc, char **argv)
{
	//console out << contents << endline;
	//std::cout << "Hello" << std::endl;
	cout << "Hello" << endl;
	cout << "-----------------" << endl;
	
	//1.(),[], ,,(연결 연산자), ., *, **, ***, ->
	
	//2. (cast)캐스팅, ++, --. +
	
	//3. *, /, +, - %
	
	//4. <<, >>
	
	//5. == != > < >= <=
	cout << (5==5) << endl; //<<연산자가 우선순위가 높아서 ( )묶어줘야한다.
	string x = "x";
	cout << x.compare("x") <<endl; //같으면 0을 반환
	
	//6. & | ^ ~ (비트연산 - 숫자가 온다)
	bitset<4> x1{15};
	bitset<4> x2{10};
	cout << x1 << endl;
	cout << x2 << endl;
	cout << (x1 & x2) << endl;//비트연산 확인 가능
	
	//7. && || ! (true/false)
	printf("%d\n", 5==5 && 5==5); //비교 연산자가 = 연산자보다 우선한다.
	printf("%d\n", true && true); 
	
	//8. ? : (삼항연산자)
	int num =100; //0이 아니면 다 참이다.
	printf("%d\n",num?1000:2000);
	num=0;
	printf("%d\n",num?1000:2000);
	
	return 0;
}

 

 

#10statment - 분기문 

//1. if else if ... eles
//2. switch case break;
//3. while, do - while
//4. for - default
//5. foreach
 >> c에는 없던 foreach 문법이 가능함.
 - int arr[3] = {11,22,33};
 - for(int x:arr){
 - cout << x << endl;
 - }

 

--예문 코드 보기--

더보기

 

#include <iostream>
using namespace std;

int main(int argc, char **argv)
{
	//console out << contents << endline;
	//std::cout << "Hello" << std::endl;
	cout << "Hello" << endl;
	cout << "-----------------" << endl;
	
	//1. if else if ... eles
	//2. switch case break;
	//3. while, do - while
	//4. for - default
	//5. foreach
	
	//c에는 없던 foreach 문법이 가능함.
	int arr[3] = {11,22,33};
	for(int x:arr){
		cout << x << endl;
	}
	
	return 0;
}