Thursday 25 April 2013

The following shows a concatenation operator for the string class:
#include <iostream.h>
#include <string.h>
class string {
private:
char *str;
public:
constructors and destructors as before
string operator+ (const string& rhs) const
{
char *s;
// create a new C style string holding
// the concatenated text
s = new char [strlen(str)+strlen(rhs.str)+1];
strcpy(s,str);
strcat(s,rhs.str);
// create a new `string', initialising it with the C
// style string
string newstring(s);
// delete the C style string, no longer needed
delete [] s;
// return a copy of the newly created string
return newstring;
}
void print() const
{
cout << str;
}

Operators as Member Functions

It is possible to define operators for classes. These operators can be infix binary operators
or unary operators. It is only possible to define operators that use an existing C++ operator
symbol, and the precedence of the symbol cannot be changed. There are some operators
that cannot be defined, these are:
. .* :: ?: sizeof # ##
Unary operators are defined as a member function taking no parameters, binary operators
are defined as member functions that take one parameter—the object on the right hand side
of the operator.
When defining an assignment operator (operator=) always declare it to take a reference
to the object on the right hand side of the assignment operator and return a reference to the
object on the left of the operator; this will permit assignments to be chained (a = b = c;).
Also declare the parameter (the object on the right of the operator) to be const to prevent
accidental modification and also to permit const objects to be used on the right hand side
of the assignment operator.
class string {
char *str;
public:
string& operator= (const string& rhs)
{
// delete the current string
delete [] str;
// allocate enough room for the new one
str = new char [ strlen(rhs.str) + 1 ];
// copy in the string
strcpy(str, rhs.str);
return *this; // see 2.2.12
}
};

Member Functions

Member functions are functions that are defined or declared within the body of a class
definition. They form part of the class description. Member functions are normally used to
provide operations on the class data. As they form part of the class definition, each instance
(variable) of the class will have associated with it the operations defined within the class.
By making the functions that modify or read the data contained in the class member
functions we know that any code that sets or reads the data incorrectly must be within the
class itself. This helps debugging and makes it easier to produce abstract data types.
Member functions are also referred to as class methods.
If the code for a member function is defined within the class, then that member function will
be treated as an inline function (see 1.12); more normally the member function is declared
within the class and defined elsewhere. The scope resolution operator, ::, is used to specify
the class to which the function belongs.
An alternative way of defining the print method from the string class shown above
would be:
class string {
private:
char *str;
public:
constructors and destructors
// declare the print method
void print() const;
};
// define the print method
void string::print() const
{
cout << str;
}

Constructors and Destructors

Constructor methods are called when an object is created, destructor methods are called
when the object is destroyed. For automatic objects this will be on entry and exit from the
block in which the object is defined. Constructors are frequently used for memory allocation
and initialisation, destructors could be used for deallocation of storage in a data structure.
For a class called string, the constructor is called string and the destructor is called
~string.
Constructors cannot return a value, but it is also invalid to declare them as returning void.
Several constructors can be specified, as long as they have different parameter lists.
Aconstructor that takes no parameters is called the default constructor, and a constructor
that takes an object of the same class as its parameter is called the copy constructor.
The copy constructor takes a reference to an object of the same class. So that const
objects can be used as an initialiser, the parameter should be declared to be const.
class string {
char *str;
public:
string() {} // default constructor
string(const string& s) {}
// copy constructor
~string() {} // destructor
If a constructor can take parameters, then the parameters can be specified using the
following syntax:
string s(parameters);
There are also two methods of calling the copy constructor after having first constructed a
new temporary object:
string s = string(parameters);
or the abbreviated form:
string s = parameter;
For example, the following are all valid ways of initialising a string using a char *
initialiser; the first uses a constructor that takes a char * parameter, the others use the
char * constructor to create a new object, and then use the copy constructor to initialise
the object:
string s1("one");
string s2 = string("two");
string s3 = "three";
The copy constructor is used for passing parameters by value; constructors are also needed
when returning values from functions.
string search(const string s, char c)
{
return string(strchr(s.str,c));
}

When the search function is called, the argument is copied and a new string created
using the copy constructor to initialise it. When the string value is returned from
search, again a new string is created, but this time the constructor used is the one that
takes a char * parameter.

Objects

An object is an instance of a class. For object read variable!
Every time we define a variable we get a new object which has all the properties of the
associated class. This is the same as with the built-in types of C (float, int etc.)—they
have certain properties (range of values, operations etc), the bonus is that we can make up
our own as well!

Structures

Structures are similar to classes, but the default is for members to be public rather than
private. Therefore structures can contain both data and functions.

Wednesday 24 April 2013

Classes

The Class Construct
Aclass definition gives the data and the operations that apply to that data. The data can be
private, public or protected enabling data hiding and construction of Abstract
Data Types (ADTs).
An example of a simple class is given below:
#include <iostream.h>
#include <string.h>
class string {
char *str;
public:
string() // default constructor, takes
// no parameters
{
str = NULL;
}
string(const char *s)
// constructor with `char *' argument
{
str = new char [strlen(s) + 1];
strcpy(str,s);
}
string(const string& t)
// copy constructor, takes a reference
// to another string as the parameter
{
str = new char [strlen(t.str) + 1];
strcpy(str,t.str);
}
~string() // destructor
{
delete [] str;
}
void print() const
// print a string
{
cout << str;
}
};
int main()
{
string s1; // use default constructor
string s2("hello world");
// use constructor with `char *' argument
string s3(s2);
// use copy constructor
// print the strings
s1.print();
s2.print();
s3.print();
return 0;
}