Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) class

 

class is a keyword to start a class declaration. A class is a user-defined data type for a concept There are multiple class access levels. There are multiple class types. There are multiple class examples.

 

'A class is a user-defined data type which consists of data elements and functions which operate on that data. In C++, this may be declared as a class; it may also be declared as a struct or a union. Data defined in a class is called member data and functions defined in a class are called member functions.' [2]

 

Class elements are:

 

The class keyword also be used to create a template function.

 

 

 

 

 

C++98C++11 Example class

 

class MyClass
{
  public: int mValue;
};

int main()
{
  MyClass m;
  m.mValue = 10;
}

 

 

 

 

 

C++98C++11 Class elements

 

A class can have many types of members:

 

struct MyClass
{
  //public by default
  void SetX(const int x) { m_x = x; } //A member function
  int m_x;                            //A data member
};

 

All classes have a four special methods called the Big Four: default constructor, destructor, copy constructor and copy assignment operator:

 

struct NoClass {}; //Do all classes really have a constructor, destructor,
                   //copy constructor and copy-assignment operator?

 

This class called NoClass is silently converted by your compiler to the following (from [1]):

 

struct NoClass
{
  NoClass()                              //Default constructor                
  {
    //something
  }
  NoClass(const NoClass& rhs)            //copy constructor
  {
    //something
  }
  ~NoClass()                             //Default destructor
  {
    //something
  }
  NoClass& operator=(const NoClass& rhs) //copy-assignment operator
  {
    //something
  }
};

 

 

 

 

 

Advice

 

 

 

 

 

 

References

 

  1. Scott Meyers. Effective C++ (3rd edition). ISBN: 0-321-33487-6. Item 5: 'Know what functions C++ silently writes and calls'
  2. Joint Strike Fighter Air Vehicle C++ Coding Standards for the System Development and Demonstration Program. Document Number 2RDU00001 Rev C. December 2005. 4.3.5: 'A class is a user-defined data type which consists of data elements and functions which operate on that data. In C++, this may be declared as a class; it may also be declared as a struct or a union. Data defined in a class is called member data and functions defined in a class are called member functions.'
  3. Paul Deitel, Harvey Deitel. C++11 for programmers (2nd edition). 2014. ISBN: 978-0-13-343985-4. Chapter 3.1, Common Programming Error 3.1. page 39: 'Forgetting the semicolon at the end of a class definition is a syntax error.'

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict