Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS415 C++ Programming Takamitsu Kawai 304-293-0405 x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.

Similar presentations


Presentation on theme: "CS415 C++ Programming Takamitsu Kawai 304-293-0405 x4212 G11 CERC building WV Virtual Environments Lab West Virginia University."— Presentation transcript:

1 CS415 C++ Programming Takamitsu Kawai kawai@csee.wvu.edu 304-293-0405 x4212 G11 CERC building WV Virtual Environments Lab West Virginia University

2 Inheritance - “To make a new class by adding some member variables and/or member functions to an existing class” “Getting a butt-load of money from a dead relative” :-) - “The ability for a class to acquire members from another class and then be able to add more members” What is Inheritance... ? Seriously... - Also known as “Derivation”

3 Benefits of Inheritance - Reusability of existing code - Extensibility of existing code Inheritance brings... But... OOP is not a magic. Even if we use OOP language, we can not always write a good OO program... To provide good reusability and extensibility, we need to analyze the target problem and design the class hierarchy very well in the early stage of system development.

4 Example of Inheritance class Telephone hangupStatus pickUp() dial() hangUp() class SpeakerTelephone hangupStatus pickUp() dial() hangUp() speakerVolume volumeUp() volumeDown() class WirelessTelephone hangupStatus pickUp() dial() hangUp() antennaStatus extendAntenna() shrinkAntenna() class VoiceMailTelephone hangupStatus pickUp() dial() hangUp() preservedMassages listenMessage() deleteMessage() Superclass (Base Class) Subclasses (Derived Classes) attributes methods

5 General or Specific Base classes - general - less members - smaller object size Derived Classes - specific - more members - larger object size class Telephone class SpeakerTelephone class WirelessTelephone class VoiceMailTelephone class SpeakerRedialTelephone Inheritance is called “is-a” relationship. (cf. “has-a” relationship) Ex. “ WirelessTelephone is a kind of Telephone.”

6 Lab topics: A Simple Graphics System Canvas CharScreenBitmapScreenPrinter (Character Terminal)(High-resolution Display)(Page Printer etc.) (General drawing area. It’s just a memory that contains pixel values of a picture) Extensible...

7 Class Canvas A “general drawing area” that consists of a memory that stores the pixel values of a picture whose size is sizeX * sizeY. sizeX sizeY buf Actually, the memory is allocated on the heap as a one- dimensional array : 0 th row1st row buf[0] buf[1] buf[2]... 2nd row buf[sizeX]buf[sizeX*2] buf[0]buf[1]... buf[sizeX]

8 Members in Canvas class Canvas { private: char* buf; // buffer body int sizeX; // size of X direction int sizeY; // size of Y direction public: Canvas(int sx, int sy); ~Canvas(void); // buffer body should be deallocated in this. int getSizeX(void) const; int getSizeY(void) const; void putPixel(int x, int y, char pixel) const; char getPixel(int x, int y) const; void clear(void) const; };

9 Class CharScreen CharScreen class imitates a screen of a “character terminal”. It displays the contents in the canvas to the character screen. Canvas CharScreen Public member functions of CharScreen are also available in Canvas.

10 Ex. BitmapScreen class “Bitmap screen” can be an output device as well. You can “reuse” Canvas class to create BitmapScreen class and “extend” the system. Canvas BitmapScreen

11 Ex. Printer class We can “draw” a picture even onto a printer... Canvas Printer

12 Members in CharScreen class CharScreen : public Canvas { private: public: CharScreen(int sx, int sy); void show(void) const; }; Public member functions in Canvas int getSizeX(void) const; int getSizeY(void) const; void putPixel(int x, int y, char pixel) const; char getPixel(int x, int y) const; void clear(void) const; are also available in CharScreen (inherited).

13 Class Rectangle This is a rectangular graphic object which can be written on Canvas (In this weeks’ lab, it’s CharScreen ). Note that sizeX, sizeY, centerX, centerY are all defined as double in Rectangle. sizeX sizeY origin x y (centerX, centerY)

14 Members in Rectangle class Rectangle { private: double centerX, centerY; double sizeX, sizeY; public: Rectangle(void); void setSize(double sizeX, double sizeY); void moveTo(double x, double y); void draw(const CharScreen* s) const; };


Download ppt "CS415 C++ Programming Takamitsu Kawai 304-293-0405 x4212 G11 CERC building WV Virtual Environments Lab West Virginia University."

Similar presentations


Ads by Google