형변환

    [C++] atoi (char > int 형변환) / stoi (string > int 형변환) 함수 구현

    음수 표기하고자 하면 첫번째 원소가 - 와야됨. int Atoi(char* str) { int sign = 1, data = 0; char cur = *str; if (cur == '\n') return 0; if (cur == '-') sign = -1; while (cur != '\0') { cur = *str++; if (cur >= '0' && cur = '0' && cur

    [JS] 자바스크립트의 형변환 (Type Casting)

    자바스크립트는 타입이 매우 유연한 언어이다. 때문에 자바스크립트 엔진이 필요에 따라 암시적변환을 혹은 개발자의 의도에 따라 명시적변환을 실행한다. 암시적 형 변환(Implicit type conversion) 암시적 변환이란 자바스크립트 엔진이 필요에 따라 자동으로 데이터 타입을 변환시키는 것이다. 1) 산술연산자 더하기(+) 연산자는 숫자보다 문자열이 우선시 되기 때문에, 숫자형이 문자형을 만나면 문자형으로 변환하여 연산된다. number + number // number number + string // string string + string // string string + boolean // string number + boolean // number 50 + 50; //100 100 + “점”..

    C# 클래스 타입 업/다운 캐스팅 (Up-DownCasting)

    업캐스팅 부모 클래스 객체를 자식 클래스 객체로 변환. 인스턴스화 또는 자식 클래스 객체 할당하는 방법이 존재. 다운캐스팅 자식 클래스 객체를 부모 클래스로 객체로 변환. 오직 부모 클래스 객체로부터 할당만 가능. using System; using System.Collections.Generic; using System.Runtime.InteropServices; public class Parent { public virtual void Print() { Console.WriteLine("Parent 클래스"); } } public class Child : Parent { public override void Print() { Console.WriteLine("Child 클래스"); } } public..

    C++ 클래스 타입 업/다운 캐스팅 (Up-DownCasting)

    업캐스팅 클래스 객체를 기반 클래스로 변환하는것. 부모형으로 자식 클래스의 메소드에 접근 가능한 경우 추상메소드를 자식클래스에서 정의한 경우 부모클래스에 정의된 메소드를 자식에서 오버라이딩한 경우 class Parent { public: virtual void Print() { cout

    C++ 명시적 형변환/캐스팅 (explicit)

    명시적 형변환 int intVal = 2; float floatVal = (float)intVal; float floatVal = float(intVal); 아래와 같은 차이가 존재한다. int first = 5; int second = 2; float floatVal = first / second; float floatVal2 = (float) first / second; std::cout

    C# 형변환 is as 키워드

    is는 객체가 해당 형식에 해당하는 지를 검사하여 bool 값을 결과로 반환. as는 형 변환 연산자와 같은 역할을 하지만, 형변환 연산자가 변환에 실패하는 경우에는 예외를 던지는 반면, as 연산자는 객체 참조를 null로 만든다. using System; class Mammal{} class Dog : Mammal{} class Cat : Mammal{} Mammal m1 = new Dog(); Dog dog; Cat cat; if(m1 is Dog) { dog = (Dog)m1; Console.WriteLine("m1 is dog"); } else Console.WriteLine("m1 is not dog"); if (m1 is Cat) { cat = (Cat)m1; Console.WriteLine..