This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
n (almost) A superset of C: (almost) every valid C program is a valid C++ program (with the same effect and efficiency).
n An OO language with classes, objects, methods, (multiple!) inheritance.
n A modern language with exceptions, templates, references.
Among the OO languages C++ is certainly: The one that can create the fastest running
applications. Not the one that allows the fastest development of
applications.
A Clock class interface
// file clock.h#ifndef CLOCK_H#define CLOCK_H
class Clock {public: // Public operations. void set (int hour, int min, int sec); int readHour (void) { return h; } int readMin (void) { return m; } int readSec (void) { return s; } void write (bool writeSec = true); void tick (void); private: // Internal representation of the time. int h, m, s;};
Specification in .h file, implementation in .cpp file.
A Clock class specification
// file clock.h
#ifndef CLOCK_H#define CLOCK_H
class Clock {public: // Public operations. void set (int hour, int min, int sec); int readHour (void) { return h; } int readMin (void) { return m; } int readSec (void) { return s; } void write (bool writeSec = true); void tick (void); private: // Internal representation of the time. int h, m, s;};
#endif
There is no user-defined constructor: hence the compiler will define one without parameters, that does essentially nothing.
So we need an init function to set the initial values.
This is bad style, we will do better lateron.
A Clock class specification
// file clock.h#ifndef CLOCK_H#define CLOCK_H
class Clock {public: // Public operations. void set (int hour, int min, int sec); int readHour (void) { return h; } int readMin (void) { return m; } int readSec (void) { return s; } void write (bool writeSec = true); void tick (void); private: // Internal representation of the time. int h, m, s;};
#endif
Simple methods, that are unlikely ever to change, can be defined in the class declaration.
Parameters to C++ function (methods are functions) can have a default parameter.
This is called Compostition, somtimes Composition-aggregation
A copy constructor
A constructor with its class type as argument, but const &, is called a copy-constructor.
This copy-constructor is called:
When an explicit copy is constructed
When a parameter is passed by value
When a class object is returned
Flight( const Flight & f );
Flight k(f);Flight k = f;
Flight (char * nr, Clock dep, Clock arr )
Clock f( void ){ Clock c; . . . return c;}
Why this ‘&’ ?
This is not an assignment!
(note: NOT for an assignment!)
The default copy constructor
When you don’t define a copy constructor, the compiler will define one.
This default copy constructor will: Call the copy constructor for each attribute (with the
value of the current attribute). Build-in types like char, int, float, struct, and pointers
have copy constructors that behave like their assignment operators: they do a bit-wise copying from the source to the destination.
Flight class definition : destructor
A destructor is called automatically:
When an object leaves scope.
When an object is disposed.
class Flight {public: // Constructors. Flight(){ no = new char [1]; no[0] = '\0'; } Flight( char flightNo[], int depH, int depM, int arrH, int arrM ); Flight( char flightNo[], Clock depT, Clock arrT ); // Copy constructor Flight( const Flight & f ); // Destructor ~Flight () { delete [] no; } // Public operations. void init( char flightNo[], int depH, int depM, int arrH, int arrM ); void delay( int min ); void writeInfo( void ); private: // Internal representation of a flight. char * no; Clock dep, arr;};
Flight class - constructors
Array new
Why +1 ??
Flight::Flight(){ no = new char [1]; no[0] = '\0'; }
Flight::Flight( char flightNo[], int depH, int depM, int arrH, int arrM): dep( Clock( depH, depM, 0 ) ), arr( Clock( arrH, arrM ) ){ no = new char [ strlen( flightNo ) + 1 ]; strcpy( no, flightNo );}
Flight::Flight( char flightNo[], Clock depT, Clock arrT ): dep( depT ), arr( arrT ){ no = new char[ strlen( flightNo ) + 1 ]; strcpy( no, flightNo );}
Construct clocks, pass them
Pass the clocks we got
Error?
Flight class – init, delay
void Flight::init( char flightNo[], int depH, int depM, int arrH, int arrM ){ no = new char[ strlen( flightNo ) + 1 ]; strcpy ( no, flightNo ); dep.set( depH, depM, 0 ); arr.set( arrH, arrM );} void Flight::delay( int min ){ for( int i = 1; i <= min * 60; i++ ){ dep.tick (); } for( int j = 1; j <= min * 60; j++ ){ arr.tick (); }}
Why not simplyvoid Flight::init( char flightNo[], int depH, int depM, int arrH, int arrM ){ no = flightNo; dep.set( depH, depM, 0 ); arr.set( arrH, arrM );}