본문 바로가기

C++

C++ 언어의 특징

반응형

1. 소멸자

: 소멸자의 경우 객체의 수명이 끝났을 때 자동으로 컴파일러가 소멸자 함수를 호출

delete -> ~소멸함수() 실행

 

2. 상속

#include <iostream>
#include <string>
using namespace std;
class Person { 
private:
	string name;
public:
	Person(string name): name(name) { } string getName() {
    return name;
} 
void showName() {
	cout << "이름: " << getName() << '\n’;
} };


class Student: Person { 
private:
	int studentID;
public:
	Student(int studentID, string name) : Person(name) { 
    this->studentID = studentID;
} 
void show() { cout << "학생 번호: " << studentID << '\n';
cout << "학생 이름: " << getName() << '\n';
} };

 이 경우 Student의 생성자 중 Person(name)은 Person을 상속받은 변수 name에 값을 넣어준다.

 

  2.1 생성자와 소멸자

  : 자식의 인스턴스를 생성할 경우 부모의 생성자 호출 -> 자식 생성자 호출 순으로 진행되며,

소멸자의 경우 자식 소멸자 호출 -> 부모 소멸자 호출 순으로 진행이 된다.

반응형

'C++' 카테고리의 다른 글

가상 함수  (0) 2019.12.11
인터페이스 vs 추상화  (0) 2019.10.30
탬플릿  (0) 2019.10.30
참조형 변수  (0) 2019.10.28