[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppClass
The class keyword can be used to
Don't forget the semicolon at the end of the class definition!
There is no semantic difference between class and typename in a template-parameter [26].
There is no semantic difference between class and typename in a template-parameter.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
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
- Create a class
- Create a template function
- Create a template class
Page overview
- Creating a simple class
- Creating a template function
- Creating a template class
- More complete example of a class
- Class access
- Other class-specific keywords
- Specific class types
- Design patterns
- Design guidelines
- Code links
- 'class' links
- References
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.
class MyClass { MyClass() { std::cout << "Hello world" << std::endl; } }; int main() { MyClass myClass; //ERROR! As MyClass' constructor is private! }
- include <iostream>
Other class specific keywords
- mutable, use together with const for const-correctness.
- virtual, for using derived class, enabling polymorphism.
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
- ::, scope operator
- <<, stream out operator
- #include
- cout
- endl
- #include
- iostream
- main
- private
- protected
- public
- return
- scope operator, ::
- std
- std::cout
- std::endl
- stream out operator, <<
'class' links
References
- 1) Scott Meyers. Effective C++ (3rd edition).ISBN: 0-321-33487-6. Item 18: Make interfaces easy to use correctly and hard to use incorrectly.
- 2) Scott Meyers. Effective C++ (3rd edition).ISBN: 0-321-33487-6. Item 19: Treat class design as type design.
- 3) Scott Meyers. Effective C++ (3rd edition).ISBN: 0-321-33487-6. Item 20: Prefer pass-by-reference-to-const to pass-by-value.
- 4) Scott Meyers. Effective C++ (3rd edition).ISBN: 0-321-33487-6. Item 21: Don't try to return a reference when you must return an object.
- 5) Scott Meyers. Effective C++ (3rd edition).ISBN: 0-321-33487-6. Item 22: Declare data members private.
- 6) Scott Meyers. Effective C++ (3rd edition).ISBN: 0-321-33487-6. Item 23: Prefer non-member non-friend functions to member functions.
- 7) Scott Meyers. Effective C++ (3rd edition).ISBN: 0-321-33487-6. Item 24: Declare non-member functions when type conversion should apply to all parameters
- 8) Scott Meyers. Effective C++ (3rd edition).ISBN: 0-321-33487-6. Item 25: Consider support for a non-throwing swap.
- 9) Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Chapter 11: 'Hide information'.
- 10) Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Chapter 32: 'Be clear what kind of class you're writing'.
- 11) Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Chapter 33: 'Prefer minimal classes to monolithic classes'.
- 12) Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Chapter 34: 'Prefer composition to inheritance'.
- 13) Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Chapter 35: 'Avoid inheriting from classes that were not designed to be base classes'.
- 14) Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Chapter 36: 'Prefer providing abstract interfaces'.
- 15) Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Chapter 37: 'Public inheritance is substitutability. Inherit, not te reuse, but to be reused'.
- 16) Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Chapter 38: 'Practive safe overriding'.
- 17) Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Chapter 39: 'Consider making virtual functions nonpublic, and public functions nonvirtual'.
- 18) Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Chapter 40: 'Avoid providing implicit conversions'.
- 19) Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Chapter 41: 'Make data members private, except in behaviourless aggregates (C-style structs).
- 20) Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Chapter 42: 'Don't give away your internals'.
- 21) Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Chapter 43: 'Pimpl judiciously'.
- 22) Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Chapter 44: 'Prefer writing nonmember nonfriend functions'
- 23) Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Chapter 45: 'Always provide new and delete together'.
- 24) Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Chapter 46: 'If you provide any class-specific new, provide all of the standard forms (plain, in-place, and nothrow)'.
- 25) Bjarne Stroustrup's C++ glossary: http://www.research.att.com/~bs/glossary.html#Gclass
- 26) C++. International Standard. ISO/IEC 14882. Second edition. Paragraph 14.1.2.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
