Classes N Object 2
Classes N Object 2
Contents 1. Program Design Using Objects 2. Classes in C++ 3. Constructors and Destructors 4. Operator Overloading 5. Data Types 6. Class Derivation
Copies of these notes plus additional materials relating to this course can be found at: http://mi.eng.cam.ac.uk/~er258/teaching
(I)
Data
Data
Data
Data
Functions
Functions
Functions
Functions
Programs = Objects
Objects provide abstractions. An object can be used without any knowledge of how it works. This allows software to be built from components - just like other forms of engineering system.
This species the private data contained in the Date class and the public interface that it provides. The latter consists of a set of class member functions (aka operations or methods ) that may be called to operate on instantiated class objects. Member functions are dened like any other function except that their names are prepended with the class name and within the function, all of the classs data elements are directly accessible.
(I)
int Date::get_day(){ return day; } int Date::get_month(){ return month; } void Date::set_date(int d, int m, int y){ // check month is 1..12 if(m < 1 || m > 12) Raise_Error(); // check day is 1..31 if(d < 1 || d > 31) Raise_Error(); // if April, May, September, November if(m==4 || m==6 || m==9 || m==11){ if(d > 30) Raise_Error(); } // if February use leap year rules if(m==2){ if((y%4==0)^(y%100==0)^(y%400==0)){ if(d > 29) Raise_Error(); } else { if(d > 28) Raise_Error(); } } // if we got here d,m,y are OK day = d; month = m; year = y; }
(I)
Programs which use this class would never know that the implementation had been changed. Also, additional interface functions can be added to enhance functionality without aecting existing programs.
The programmer has used the object img before its values have been initialised. Constructors provide a mechanism to make sure that objects have appropriate values that meet the invariants from the moment they are created. A constructor is executed when the object is created and it can be given parameters if required. For example
Image img(200,200); // img is 200 x 200 img.set_pixel(100,100,255); // now safe
Similarly, when we are nished with an image, we have to make sure that the allocated memory is released. This is achieved using a destructor.
(I)
class Image { public: Image(int w, int h); ~Image(); // functions to access the data int get_width(); int get_height(); char get_pixel(int x, int y); void set_pixel(int x, int y, char value); void save(char * filename); etc private: int width; int height; char *pixels; };
Image::Image(int w, int h) { width = w; height = h; pixels = new char[w*h]; } Image::~Image() { delete[] pixels; }
(I)
This provides all the basic operations and we can use this to write code like: Complex Complex Complex Complex c1(1.5,0); c2(1.7,1.9); c3(0,0); c4(0,0); // c3=c1*c2 // c4=c1+c3
c3.set_value(c1.multiply(c2)); c4.set_value(c1.add(c3));
cout << "answer is" << c4.real << "+" << c4.imag << "i" << endl;
10
Operator Overloading
It would be much more convenient if we could write the code using standard arithmetic symbols with complex numbers so that the code looks just the same as if we had been using floats. This can be done in C++ using a feature called operator overloading. class Complex { public: // Constructor Complex(float r, float i); //arithmetic Complex operator+(Complex Complex operator-(Complex Complex operator*(Complex Complex operator/(Complex
// to set the values Complex operator=(Complex rhs); // data members float real; float imag; };
(I)
11
This new version of the Complex class means we can now write c3 = c1*c2; c4 = c1+c3; instead of c3.set_values(c1.multiply(c2)); c4.set_values(c1.add(c3)); It is also possible to write code to enable the use of cout << c4 << endl; This would require code to dene the operator << in the context ostream << complex such as ostream & operator << (ostream& os, Complex& c) { os << c.real << "+" << c.imag << "i"; return os; }
12
(I)
13
Concrete Type
Concrete Type
Concrete Type
instantiate objects
instantiate objects
instantiate objects
14
Class Derivation
Suppose we are programming a video processing application such as an MPEG4 codec for a video camera or an industrial inspection system. Problem: For each image captured from the video camera by the system, we need to know the time when each image was captured. Unfortunately, there is no place to store this information in our existing Image class. Solution 1: Add a timestamp data member to class Image. BUT our image class would quickly become cluttered with data members which were only used in certain circumstances. Solution 2: Create a new class VideoFrame with a copy of all the Image data members and functions and with a timestamp as well. BUT now we cant use existing code that works with Image. Solution 3: Derive class VideoFrame from class Image.
(I)
15
class Image { public: Image(int w, int h); ~Image(); int get_width(); int get_height(); char get_pixel(int x, int y); void set_pixel(int x, int y, char val); void save(char * filename); etc private: int width; int height; char *pixels; }; class VideoFrame : public Image { public: VideoFrame(int w, int h, int t); int get_timestamp(); private: int timestamp; };
16
(I)
17
Class Derivation
By deriving VideoFrame from Image, the programmer is saying that: 1. A VideoFrame does everything an Image does 2. It can be used wherever an Image is expected 3. It can do some new things as well Items 1 and 2 mean that VideoFrame is-an Image. This is the strong kind of is-a relationship because code designed to work with Images can be reused for VideoFrames without changing anything (or even recompiling). We say that Image is the base class and VideoFrame is the derived class. These are also referred to as the superclass and the subclass respectively. But note that you can not get a timestamp from an Image class.
VideoFrame *vfp = new VideoFrame(200,200,10); Image *ip = vfp; // safe since a VideoFrame // can do anything that an image can do ip->set_pixel(10,20,0); // ok t = ip->get_timestamp(); // illegal even though ip // really points to a VideoFrame
18
Video Frame
Image