[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
CppClass

(C++) class

A class is "a user-defined type. A class can have member functions, member data, member constants, and member types. A class is thee primary mechanism for representing concepts in C++" [25].

The class keyword can be used to The keyword class is not supported in C.

Page overview



Creating a simple class

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


Don't forget the semicolon at the end of the class definition!

Creating a template function

//Template function
template <class T> 
T add(const T&a, const T& b) { return a + b; }
int main()
{
  const int x = add(3,4);
  const double y = add(3.141592654, 2.718281828);
}


There is no semantic difference between class and typename in a template-parameter [26].

Creating a template class

template <class T>
struct MyClass
{
  T mValue;
};
int main()
{
  MyClass<int> m;
  m.mX = 10;
}


There is no semantic difference between class and typename in a template-parameter.

More complete example of a class

class MyClass
{
  public:
  //your public functions 
  protected:
  //your protected functions 
  private:
  //your private functions 
};
Don't forget the semicolon at the end of the class definition!

Class access

A class has three different member variables and functions:
  • public : public functions can be called from outside the class. Public variables can be changed directly.
  • protected : these functions and variables become public in a derived class.
  • private : these functions and variables can only be called and changed by the class itself.
A class' default for functions and variables is private. Declare data members private [5].

  1. include <iostream>
class MyClass { MyClass() { std::cout << "Hello world" << std::endl; } }; int main() { MyClass myClass; //ERROR! As MyClass' constructor is private! }


Other class specific keywords

Specific class types

Design patterns

Classes can be programmed in specific design patterns enabling the programmer to e.g. be sure there is only one instance of a class.

Class design guidelines

Although entire books are written on this subject, here some guidelines from some experts:
  • Make interfaces easy to use correctly and hard to use incorrectly [1].
  • Treat class design as type design [2].
  • Prefer pass-by-reference-to-const to pass-by-value [3].
  • Don't try to return a reference when you must return an object [4].
  • Declare data members pprivate [5,9,19], except in behaviourless aggregates (C-style structs) [19].
  • Prefer non-member non-friend functions to member functions [6,22].
  • Declare non-member functions when type conversion should apply to all parameters [7].
  • Consider support for a non-throwing swap [8].
  • Be clear what kind of class you're writing [10].
  • Prefer minimal classes to monolithic classes [11].
  • Prefer composition to inheritance [12].
  • Avoid inheriting from classes that were not designed to be base classes [13].
  • Prefer providing abstract interfaces [14].
  • Public inheritance is substitutability. Inherit, not te reuse, but to be reused [15].
  • Practive safe overriding [16].
  • Consider making virtual functions nonpublic, and public functions nonvirtual [17].
  • Avoid providing implicit conversions [18]
  • Don't give away your internals [20].
  • Pimpl judiciously [21].
  • Always provide new and delete together [23].
  • If you provide any class-specific new, provide all of the standard forms (plain, in-place, and nothrow) [24].

Code links

'class' links

References



last edited (November 14, 2006) by bilderbikkel, Number of views: 11840, Current Rev: 28 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.