Class Member Functions |
- define within the definition of the class
class Sample {
public:
void test(int i);
}
void Sample::test(int i) { // scope resolution operator
...
}
- define out of the definition of the class
class Sample {
public:
void test(int i) {
...
}
}
|
Class Access Modifiers |
Each section remains in effect untill either other section labels or the closing right curly brace of the class is seen.
public |
|
protected |
A protected member variable or function is very similar to a private member
but it provided one additional benefit that they can be accessed in child classes which are called derived classes.
|
private |
default |
|
Constructor & Destructor |
constructor |
class Sample {
public:
Sample(...) {
...
}
}
|
destructor |
class Sample {
public:
~Sample(...) {
...
}
}
|
|
Copy Constructor |
a constructor which create an object by initialising it with an object of the same class, which has been created previously.
is used to:
- Initialize one object from another of the same type.
- Copy an object to pass it as an argument to a function.
- Copy an object to return it from a function.
classname (const classname &obj) {
// body of constructor
}
|
Friend Functions |
A friend function of a class is defined outside that class‘ scope but it has the right to access all private and protected members of the class.
Even though the prototypes for friend functions appear in the class definition, friends are not member functions.
- friend functiones or classes must be declared within the declaration of the class
class Sample {
public:
friend void getSomeThing(Sample obj);
}
it can also be defined completely within the class
class Sample {
public:
frined void getSomeThing(...) {
...
}
}
|
Inline Functions |
C++ inline function is powerful concept that is commonly used with classes.
If a function is inline, the compiler places a copy of the code of that function at each point where the function is called at compile time.
|
This Pointer |
Every object in C++ has access to its own address through an important pointer called this pointer.
- The this pointer is an implicit parameter to all member functions. Therefore,
inside a member function, this may be used to refer to the invoking object.
- Friend functions do not have a this pointer, because friends are not members of a class.Only member functions have a this pointer.
|
Pointer to C++ Classes |
A pointer to a C++ class is done exactly the same way as a pointer to a structure.
- to access members of a pointer to a class you use the member access operator -> operator, just as you do with pointers to structures
- Also as with all pointers, you must initialize the pointer before using it
|
Static Members of a Class |
We can define class members static using static keyword.
Static Function Members
By declaring a function member as static, you make it independent of any particular object of the class
-
- A static member function can be called even if no objects of the class exist
- the static functions are accessed using only the class name and the scope resolution operator ::.
- A static member function can only access static data member, other static member functions and any other functions from outside the class.
- Static member functions have a class scope and they do not have access to the this pointer of the class.
|