- 클래스란? 클래스는 객체지향 프로그래밍에서 특정 개체를 생성하기 위해 변수와 메소드를 정의하는 일종의 틀이다. public class Animal{ } 다음과 같이 클래스 틀을 만들었다. Animal cat = new Animal(); new 를 이용해 객체를 생성하였다. cat은 Animal클래스의 인스턴스이자 하나의 객체이다. 이제 하나하나의 객체에 대한 정보를 객체 변수를 이용해 만들어보자. (멤버 변수, 속성이라고도 한다.) public class Animal{ String name; public static void main(String[] args){ Animal cat = new Animal(); System.out.println(cat.name); } 다음과 같이 객체이름.객체변수 처럼..
- if문 int money = 2000; if (money >= 3000) { System.out.println("택시를 타고 가라"); }else { System.out.println("걸어가라"); } if문을 이용해 다음과 같이 상황에 따라 출력을 달리할 수 있다. contains 메소드 ArrayList pocket = new ArrayList(); pocket.add("paper"); pocket.add("handphone"); pocket.add("money"); if(pocket.contains("money")){ System.out.println("택시를 타고 가라"); } else { System.out.println("걸어가라"); } contains 메소드는 문자열 안에 money가..
const 함수 오버로딩 const의 유무에 따라 같은 이름의 함수도 구분될 수 있다. #include using namespace std; class SoSimple { private: int num; public: SoSimple(int n) : num(n) { } SoSimple& AddNum(int n) { num += n; return *this; } void ShowData() const { cout
wikidocs.net/book/31 위키독스 온라인 책을 제작 공유하는 플랫폼 서비스 wikidocs.net 위키독스 점프 투 자바를 중요한 것, 내가 잘 몰랐던 것을 위주로 포스팅해보겠다. 03-4 문자열(String) String a = "hello"; String b = new String("hello"); System.out.println(a.equals(b)); System.out.println(a==b); //false 다음 코드에서 a 와 b는 값이 같고 서로 다른 객체이다. == 연산자는 두 개의 자료형이 '동일한' 객체인지 판별할 때 쓰는 연산자이기 때문에 false를 리턴한다. String a = "Hello Java"; System.out.println(a.indexOf("Java"..
복사 생성자란, 생성자의 한 종류로서 호출 시키는 객체의 선언과 동시에 초기화할 때 쓸 수 있다. 이미 생성되고 초기화 된 객체를 이용해 다른 객체를 초기화시킬 수 있는 것이다. 다음 예제를 보자. #include using namespace std; class SoSimple { private: int num1; int num2; public: SoSimple(int n1, int n2) :num1(n1), num2(n2) { } SoSimple(SoSimple& copy) : num1(copy.num1), num2(copy.num2) { cout
객체는 메모리 공간을 할당한 후 생성자 호출까지 마쳐야 객체라고 할 수 있다. 즉 생성자는 객체가 되기 위해 반드시 호출되어야하는 것인데, C++에서는 default 생성자를 두어 예외가 발생하지 않도록 한다. class aaa { private: int num; public: /* * aaa(){} */ int GetNum() { return num; } }; 다음 코드에서 주석을 처리한 부분은 default 생성자이다. new 연산자를 이용한 객체의 생성과정에서도 생성자가 호출된다. aaa *ptr = new aaa; class SoSimple { private: int num; public: SoSimple(int n) : num(n) { } }; 이니셜라이저로 num값에 n을 넣는 생성자를 만들었..
#include using namespace std; class Point { public : int x; int y; }; class Rectangle { public : Point upLeft; Point lowRight; public: void ShowRecInfo() { cout
struct안에 함수를 선언하면 .(점) 연산자로 해당 함수에 접근이 가능하다. #define LEN 10 #define FUEL 2 #define ACC 10 #define MAX 200 #define BREAK 10 struct Car { char id[LEN]; int fuel; int speed; void show() { cout
2-2 참조자 (Reference) : 자신이 참조하는 변수를 대신할 수 있는 또 하나의 이름인 것이다. - 새로 선언된 변수의 이름 앞에 &를 붙여 기존에 있던 변수 이름의 별칭을 만들어주는 것이다. int &num2 = num1; 여기서 num2는 num1과 기능과 성격이 같다. #include using namespace std; int main() { int num = 12; int* ptr = # //num의 주소 int** dptr = &ptr; //ptr포인터 변수의 주소 int& ref = num; //num의 참조자 ref int* (&pref) = ptr; // ptr포인터 변수의 참조자 pref int** (&dpref) = dptr;//dptr 포인터 변수의 참조자 dpref..