Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CMT2050: Object Oriented Programming and Design www.cs.mdx.ac.uk/staffpages/xiaohong/cmt2050.html Data Polymorphism Dr. Xiaohong Gao Room 2C23,BG Ext.

Similar presentations


Presentation on theme: "1 CMT2050: Object Oriented Programming and Design www.cs.mdx.ac.uk/staffpages/xiaohong/cmt2050.html Data Polymorphism Dr. Xiaohong Gao Room 2C23,BG Ext."— Presentation transcript:

1 1 CMT2050: Object Oriented Programming and Design Data Polymorphism Dr. Xiaohong Gao Room 2C23,BG Ext (Week 5)

2 2 1. Review of Lectures 1-4 Program = class.h + class.cpp + classMain.cpp class = private: public: data members member functions

3 3 Note: C++ Builder only runs through local spaces C drive ---- e.g., desktop E drive ---- e.g., zip disk A drive ---- e.g., floppy disk But NOT through networked spaces (linking errors) such as H: drive --- your personal storage spaces L: drive --- lecturers’ spaces

4 4 Copy your programs from H: to C (desktop): Run C++ builder on the desktop Before you logout, remember to copy your programs back to your H: drive to save them Steps to run your program saved in H drive

5 5 1.1 Some key words related to a class pre-processor -- #ifndef - #define - #endif const default constructor destructor dynamic memory allocation (pointer)

6 6 class _Sent { private: char name [40];// the name of recipient char message [120]; // message to the recipient public: void set_all(char*, char*); char * get_name(); char * get_message(); }; 1.2 Example: an _sent class keeps records the s you have sent

7 7 1.3 Exercises : modify the _Sent class to make sure: 1). all the data are initialised 2). “message” can be any length 3). “message” can not be changed before the “get_message” 4) “message” is a read-only file. 5) _Sent class is only included once in the main program

8 8 void main() // A running problem { Student student1; char aName[40],aCampus[40]; int newAge, newID; cout << "Input student's name:" << endl; cin.getline(aName,40); cout << "input ID:” << endl; cin >> newID; cin.ignore(256,'\n'); cout << "input student's campus:" << endl; cin.getline(aCampus,40); student1.set_all(aName, aCampus,newID,18); student1.print_all(); getch(); return; }

9 9 The deadline for coursework 2 is extended to Week 7 The deadline for coursework 1 is extended to Week 6

10 10 2. The features that Object Oriented Programming Supports 2.1 Four data features are supported: Data encapsulation Data abstraction Data polymorphism Data inheritance

11 Polymorphism Some definitions Poly ---- many (from Greek) Morph ---- Form (from Greek) Polymorphism ---- having many forms. polymorphism refers to many functions sharing the same name. In C++, polymorphism is realised using overloading functions.

12 12 Example 1: function overloading (1) -- video.h // video.h #ifndef VIDEO_H//pre-processor #define VIDEO_H class Video { private: char name[40]; char producer[40]; short quantity;

13 13 Example 1: function overloading (2) -- video.h public: Video(); //Default constructor void set_all(); //no arguments void set_all(char *); //1 argument void set_all(char*, char*); //2 arguments void set_all(char*, char*, short); //3 arguments void print(); char * get_name(); char * get_producer(); short get_quantity(); }; #endif

14 Overloading Is used to achieve polymorphism. Function overloading ---- using the same name for several functions Signature ---- consists the function name and the argument list (but not function return type) Signature is used to identify which function is called.

15 15 Example 1: function overloading (3) -- videoMain.cpp #include #include “video.h” int main() { Video vid1; vid1.set_all(); // Uses 0 argument function vid1.print(); vid1.set_all("Titanic"); // Uses 1 argument function vid1.print(); vid1.set_all("Titanic", "Cameron Co."); vid1.print(); // Uses 2 argument function vid1.set_all("Titanic", "Cameron Co.", 56); vid1.print(); // Uses all 3 argument function return 0; }

16 16 Example 1: function overloading (4) -- output // The output of the programme: ____0 Titantic__0 TitanticCameronCo.0 Titantic.CameronCo.56

17 17 Example 1: function overloading (5) -- video.cpp (1) #include "video.h" #include // Default constructor, invoked with no arguments; Video::Video() // initializes all data members to 0 values. { strcpy(name,""); strcpy(producer, ""); quantity = 0; }

18 18 void Video::print()// Method - prints data members { cout << name << " " << producer << " " << quantity; cout << endl; } // Invoked with no arguments; assigns 0 values to all data members void Video::set_all() { strcpy(name,""); strcpy(producer,""); quantity = 0; } // video.cpp (2)

19 19 // data member and assigns 0 values to the other data members. void Video::set_all(char* name_in) { strcpy(name, name_in); strcpy(producer, "");//option quantity = 0;//option } //0 to the quantity data member. void Video::set_all(char* name_in, char* prod_in) { strcpy(name, name_in); strcpy(producer, prod_in); quantity = 0;//option } // video.cpp (3)

20 20 // Invoked with three arguments. void Video::set_all(char* name_in, char* prod_in, short qty) { strcpy(name, name_in); strcpy(producer, prod_in); quantity = qty; } // Return name data member const char* Video::get_name() { return name; } // video.cpp (4)

21 21 // Returns producer data member const char* Video::get_producer() { return producer; } // Return quantity data member short Video::get_quantity() { return quantity; } // video.cpp (5)

22 Default function arguments Instead of using 4 set_all methods in Example 4, a single method can be applied using default function arguments. void set_all (char * = “”, char* = “”, short =0); void set_all(); void set_all(char *); void set_all(char*, char*); void set_all(char*, char*, short); A single prototype and the implementation showing the values that can be defaulted. To cover the variations where parameters are defaulted.

23 23 Argument subset must be (disadvantages): in sequence continuous starting from left to right Advantage ---- less functions to program

24 24 //video.h void set_all (char* = “”, char* = “”, short =0); //video.cpp void Video::set_all(char* name_in, char* prod_in, short qty) { strcpy(name, name_in); strcpy(producer, prod_in); quantity = qty; } Implementation of default argument function space

25 25 // video.h #ifndef VIDEO_H #define VIDEO_H class Video { private: char name[40]; char producer[40]; short quantity; public: Video(); void set_all(char* = “”, char* =“”, short = 0); void print(); char * get_name(); char * get_producer(); short get_quantity(); }; #endif Example 5 (1)

26 26 //main.cpp #include #include “video.h” int main() { Video vid1; vid1.set_all(); // Uses 0 default argument values vid1.print(); vid1.set_all("Titanic"); // Uses 1 default argument value vid1.print(); vid1.set_all("Titanic", "Cameron Co."); vid1.print(); // Uses 2 default argument values vid1.set_all("Titanic", "Cameron Co.", 56); vid1.print(); // Uses all 3 default argument values return 0; } Example 5 (2)

27 The Advantages of Function Overloading Giving meaningful names to functions. making an application programmer interface (API) easy to remember. e.g., print in MsWord, MsPowerPoint, etc..

28 Overloading Constructors A constructor is a special member function. No return values. Share the same name with the class. A constructor is called whenever an object is created. Default constructor ---- no arguments. Overloaded constructors: Video(); Video(char*, char*, short);

29 29 Example 2: overloading constructors (1) class Classroom { private: charcampus[50]; charroom[50]; floatfloorarea; public: Classroom ( );// DEFAULT CONSTRUCTOR Classroom (char *, char *, float ); }; Classroom::Classroom ( ) { strcpy (campus, ""); strcpy (room, ""); floorarea = 0.0; } Classroom::Classroom (char *acampus, char *aroom, float anarea); { strcpy (campus, acampus); strcpy (room, aroom); if (anarea >= 0.0) floorarea = anarea; else floorarea = 0.0; } // classroom.h // classroom.cpp

30 30 Example 2: overloading constructors (2) int main() { Classroom clr1; Classroom clr2( “BG”, ”1C2”, 1); return 0; } // classroommain.cpp

31 31 3. Differences between overloaded constructors and overloaded functions No return types for constructors e.g., Video::Video(char * aname); void Video::set_all(char *aname); The object can be declared by constrctors. e.g., Video vid1(“ET”, “Unknown”, 30); Video vid2; vid2.set_all((“ET”, “Unknown”, 30);

32 32 Summary: 1. Polymorphism 2. Function overloading 3. Constructor overloading


Download ppt "1 CMT2050: Object Oriented Programming and Design www.cs.mdx.ac.uk/staffpages/xiaohong/cmt2050.html Data Polymorphism Dr. Xiaohong Gao Room 2C23,BG Ext."

Similar presentations


Ads by Google