Share via


class (C++) 

The class keyword declares a class type or defines an object of a class type.

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

Remarks

For information on managed classes and structs, see Classes and Structs.

The elements of a class definition are as follows:

  • If the class is a template class, the applicable template specification. See Template Specifications.

  • The class keyword.

  • Microsoft-specific declaration specifications (See __declspec).

  • The class name, referred to as the tag. The tag becomes a reserved word within the scope of the class. The tag is optional. If omitted, an anonymous class is defined. For more information, see Anonymous Class Types.

  • Optionally, a colon and the base list, which is a comma-separated list that specifies the class or classes from which the class is derived (its base classes). Each base class's name can be preceded by an access specifier (public, private, protected) and the virtual keyword. See the member-access table in Controlling Access to Class Members for more information. In C++, a class need not have a base class.

  • Braces {} containing the member list (see Class Members). The member list declares members or friends of the class. Members can include data, functions, nested classes, enums, bit fields, and type names. Friends can include functions or classes. Explicit data initialization is not allowed. A class type cannot contain itself as a nonstatic member. It can contain a pointer or a reference to itself. See the virtual keyword and the member-access table in Controlling Access to Class Members for more information.

  • A comma-separated list of declarators, which declare one or more objects of the class type. The declarators may include initializers if all data members of the class are public. This is more common in structures, whose data members are public by default, than in classes. See struct.

For more information, see the following topics:

Example

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

#include <iostream>
#include <string>
#define TRUE = 1
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;
}

See Also

Reference

C++ Keywords
Classes, Structures, and Unions