C++ class file organization
#include the header file
C++ class declaration
class className {
public:
// member function prototypes
private:
// data member declarations
};
typical layout of header file
#ifndef CLASSNAME_H
#define CLASSNAME_H
class className {
public:
type fcn1( ); // prototype
type fcn2( ); // prototype
private:
type fcnX( ); // prototype
type field1; // data member
};
#endif
typical layout of source file
#include "className.h"
using namespace std;
type className::fcn1( ) { // definition
}
type className::fcn2( ) { // definition
}
constructors
new)
constructors
Declaration
className( );
Definition
className::className( ) {
}
If more than 1 constructor,
they must have different signatures
IntList example:
Intlist(); // prototype Intlist(int size); // prototype
When the constructor is used
to create and define a new object:
className X; className X(param); className * pX = new className;
the copy constructor
a specific form (signature) of
a constructor,
in order to instantiate a copy of
an existing object
the copy constructor's prototype:
className(const className & one);
When the copy constructor is used
1. If passing a call-by-value parameter,
the compiler needs a copy of the object:
X = fcn(.., obj, ..); // call
int my_class::fcn(. . , ClassName one, . . ) {
// definition
}
When the copy constructor is used
2. If directly invoked:
className two(one); // declaration and call
two is a newly constructed instance,
and it is a copy of the data members
in one
When the copy constructor is used
3. (the same as 1., actually)
when the compiler needs a temporary object
variable, it creates a copy
Code example to motivate
functionality of copy constructor
className * ptr1; ptr1 = new className; // invokes constructor
className * ptr1; ptr1 = new className; // invokes constructor . . className * ptr2; ptr2 = ptr1;
className * ptr1;
ptr1 = new className; // invokes constructor
.
.
className * ptr2;
ptr2 = new className(*ptr1);
// invokes copy constructor
Example from IntList
(from the declaration)
private: int * items;
items is a pointer to an int.
Not really! items is a pointer that gives
access to a dynamically-allocated
array of integers.
So, consider the code needed
in the
IntList copy constructor for
IntList L1; // constructor IntList L2(L1); // copy constructor
A (simple) shallow copy does
This would be wrong!
Why? Consider what happens if the code does
delete L1;
The copy constructor needs a deep copy.
A deep copy allocates a new array,
and copies the original array's values into it.
destructors
delete)
Destructor example:
code that implicitly calls the destructor
void foo( ) {
Object my_object;
.
.
}
When foo() is invoked, it invokes the constructor
to create an instance of the object.
The destructor is invoked
to delete this local variable to foo()
as the function completes.
IntList destructor example
declaration
~IntList();
default definition would have been
IntList::~IntList() { }
IntList definition
IntList::~IntList() {
delete [] items;
}
Not covered
Member Initializer List for the constructor
const or references
IntList example of Member Initializer List
IntList::IntList(const IntList &L) :
items(new int[L.arraySize]), numItems(L.numItems),
arraySize(L.arraySize) {
The list is between the argument list and the {.
It is a comma-separated list.
Each thing in the list uses the syntax
datamembername(value)
Note: member initializer list is not necessary
for the IntList constructor.
Order of events for Member Initializer List
| Copyright © Karen Miller, 2009 |