C++ Classes
Object-based
programming
A Java class
public class IntCell
{
public IntCell(
)
{ storedValue
=0; }
public IntCell(int intialV )
{ storedValue=
initialV; }
public int getValue( )
{
return stored Value; }
public
void setValue( int val )
{ storedValue
= val };
private int storedValue;
};
A C++ class
class IntCell
{
public:
IntCell( int initialV = 0 )
{ storedValue
= initialV; }
int getValue( )
{
return storedValue; }
void setValue( int val
)
{ storedValue
= val; }
private:
int storedValue;
};
The differences between the
Java class & C++ class
The placement of public and private
In a C++ class, visibility is private until a public
modifier is seen.
The constructors are different
Java has two constructors, C++ only one
The C++ class uses a default argument
In C++ the class declaration ends with a semicolon
Using the IntCell
class
The class
declaration of IntCell must be placed before main( )
Usually, it would
be put in a .h file, and the main program would use an include directive
Note:
the three ways the
constructor can be called
The objects are
kept on the runtime stack, not on the heap
One object can be
copied into another object with the assignment statement.
int main( )
{
IntCell m1;
IntCell m2 = 37;
IntCell m3(55);
cout <<m1.getValue( )
<< m2.getValue( )
<< m3.getValue( ) ;
m1 = m2;
m2.setValue( ) ;
cout << m1.getValue( )
<< m2.getValue( );
return 0;
}
Constructors
The
constructor is called when a object is declared
Three calls to
the constructor
IntCell m1; IntCell m2 = 37;
IntCell m3(55);
The purpose of a
constructor is twofold
It
defines a variable
It
also initializes it (gives it an initial value)
This is true
whether or not new is used.
In C++, objects
are stored in static RAM unless new is called
Default constructors
In C++, if you do
not write a constructor for a class, the compiler will proved on for you.
Types of constructors
The number of arguments or parameters can vary in
constructors
Zero parameter constructors are called the default
constructor
The compiler will
automatically supply this if no other constructors are written
Some constructors are defined as explicit.
Some constructors
give optional default values
Some constructors
are defined in an initializer list
Using default values with constructors
Example: IntCell (int someValue=0)
{ storedValue
= someValue; }
Default values
in parameters
The user of the
class can either accept the default, or provide their own
value.
So this
constructor can be used by a client either with zero or one argument.
IF the user wants the zero parameter
constructor, parenthesis are not used
IntCell myCell;
If the one argument constructor is wanted, it is
called this way
IntCell
myCell(82); or myCell =
82;
It is important to note that the above constructor is
two constructors in one
Initializer list
This type of constructor uses an initializer
list prior to the body of the constructor
It initializes the data members directly.
Example: IntCell (int someValue=0)
: storedValue(someValue){ }
This is faster than assignment statements in the body
of the constructor
The body of the constructor may or may not be left
blank.
If the data member is const, an initializer
list must be used.
Explicit constructors
Explicit is used
with constructors with only one argument
The keyword explicit informs the compiler not to provide implicit type
conversions
Suppose the client of class IntCell makes these statements:
IntCell myCell; //this calls the default
constructor
myCell = 128; //this is
the wrong type
this will compile, since the compiler makes the type
conversion from an int to an IntCell
But in order to assign storedValue a value, setValue(x) should be called
Constant member functions
You can think of
all member functions as either mutators or accessors
That is, they
either change the data members or they dont
By default, all
member functions are mutators
If a member
function is not designed to change data member(s) it should be declared const
This is just
another way to let the compiler find errors rather than have them become
runtime errors you must find yourself
A function is specified as const both in its prototype
and in its definition
The keyword const is inserted after the functions parameter list
Example: int read( )
const
{ whatever }
Marking member functions const
Suppose you have a vector of IntCells,
which you want
to check to see if any are zero.
bool containsZero (const vector<IntCell> & arr)
for (int I = 0; I < arr.size( ); i++)
if (arr[i].getValue( ) = = 0)
return true;
else return false; }
This will not compile, since the vector is marked
constant
You cannot call a
mutator function on data that is marked const
We must indicate
that getValue( ) is an accessor, not a mutator by marking it const
Using const
Many less experienced C++ do not like to use const, since the program will not compile if not used
correctly
Most of the time these programs will run correctly
But in large complex programs, using const correctly all the time may mean that the compiler
will catch an error that otherwise will become a runtime error difficult to
find.
Memory management in C++ and Java
On the overhead are three versions of class Student
One is in Java, the other two in C++
Class Student has three data members:
name and birthdate are objects,
and gpa is a privative
In Java, objects are always accessed by reference
variables
In C++ objects can either be accessed directly, or
with pointers
Copying an object with pointer data members
Suppose we want to copy an object of type Student that
has pointer data members.
If we just use the assignment statement, we will get a
copy of the address pointing to the data (a shallow copy)
This is an alias, not a copy
We have two pointers pointing to the same data
We want a deep copy, a separate object
Copy constructors
A copy constructor is used to construct a new object that is
initialized to a copy of the same type of object
It copies data
member by data member.
In C++, if you do not write a copy constructor, the
compiler will provide one for you.
As long as your data members are not pointers to other
data, you can accept the default copy constructor
That means you do
not need to write one for your class
But if any of the data members of your class are pointers,
the default copy constructor will make a shallow copy.
The big three: destructor, copy constructor,
assignment operator
C++ furnishes defaults for all three of these for
every class
There are times when using these default will cause
problems
In general, this is when one of the data members is a
pointer, since then the defaults do not do what is expected
You must be careful when you write your classes, that
these three operations are working correctly i.e. write them if needed.