클래스 (C++)

class 키워드(keyword) 클래스 형식을 선언하거나 클래스 형식의 개체를 정의합니다.

구문

[template-spec]
class [ms-decl-spec] [tag [: base-list ]]
{
   member-list
} [declarators];
[ class ] tag declarators;

매개 변수

template-spec
선택적 템플릿 지정입니다. 자세한 내용은 템플릿을 참조하세요.

class
class 키워드.

ms-decl-spec
선택적 스토리지 클래스 지정입니다. 자세한 내용은 __declspec 키워드(keyword) 참조하세요.

tag
클래스에 지정된 형식 이름입니다. 태그는 클래스 범위 내에서 예약된 단어가 됩니다. 태그는 선택 사항입니다. 생략하면 익명 클래스가 정의됩니다. 자세한 내용은 익명 클래스 형식을 참조 하세요.

base-list
이 클래스가 해당 멤버를 파생하는 클래스 또는 구조의 선택적 목록입니다. 자세한 내용은 기본 클래스를 참조하세요. 각 기본 클래스 또는 구조체 이름 앞에 액세스 지정자(퍼블릭, 프라이빗, 보호됨) 및 가상 키워드(keyword) 지정할 수 있습니다. 자세한 내용은 클래스 멤버에 대한 액세스 제어의 멤버 액세스 테이블을 참조하세요.

member-list
클래스 멤버 목록입니다. 자세한 내용은 클래스 멤버 개요를 참조하세요.

declarators
클래스 형식의 하나 이상의 인스턴스 이름을 지정하는 선언자 목록입니다. 클래스의 모든 데이터 멤버가 있는 경우 선언자에 이니셜라이저 목록이 포함될 수 있습니다 public. 이는 클래스보다 기본적으로 데이터 멤버가 있는 구조체에서 더 일반적입니다 public . 자세한 내용은 선언자 개요를 참조하세요.

설명

일반적인 클래스에 대한 자세한 내용은 다음 항목 중 하나를 참조하세요.

C++/CLI 및 C++/CX의 관리되는 클래스 및 구조체에 대한 자세한 내용은 클래스 및 구조체를 참조 하세요.

예시

// class.cpp
// compile with: /EHsc
// Example of the class keyword
// Exhibits polymorphism/virtual functions.

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

class dog
{
public:
   dog()
   {
      _legs = 4;
      _bark = true;
   }

   void setDogSize(string dogSize)
   {
      _dogSize = dogSize;
   }
   virtual void setEars(string type)      // virtual function
   {
      _earType = type;
   }

private:
   string _dogSize, _earType;
   int _legs;
   bool _bark;

};

class breed : public dog
{
public:
   breed( string color, string size)
   {
      _color = color;
      setDogSize(size);
   }

   string getColor()
   {
      return _color;
   }

   // virtual function redefined
   void setEars(string length, string type)
   {
      _earLength = length;
      _earType = type;
   }

protected:
   string _color, _earLength, _earType;
};

int main()
{
   dog mongrel;
   breed labrador("yellow", "large");
   mongrel.setEars("pointy");
   labrador.setEars("long", "floppy");
   cout << "Cody is a " << labrador.getColor() << " labrador" << endl;
}

참고 항목

키워드
클래스 및 구조체