1 / 17

計算機概論實習 2007-04-13

計算機概論實習 2007-04-13. Class Sample: RECT Class. class RECT{ private : int weight, length; public : RECT(); int surface(); int perimeter(); void setWeight( int ); int getWeight(); };. weight. length. RECT(){ weight = 10; length = 10; }. int perimeter(){

Download Presentation

計算機概論實習 2007-04-13

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 計算機概論實習2007-04-13

  2. Class Sample: RECT Class class RECT{ private: int weight, length; public: RECT(); int surface(); int perimeter(); void setWeight(int); int getWeight(); }; weight length RECT(){ weight = 10; length = 10; } int perimeter(){ return 2(weight+length); } int surface(){ return weightlength; }

  3. Function Overloading class RECT{ private: int weight, length; public: RECT(); RECT(int, int); int surface(); int perimeter(); void setWeight(int); int getWeight(); }; weight length Constructor Overloading RECT(int w, int l){ weight = 10; length = 10; } RECT(){ RECT(10, 10); }

  4. Function Overloading • Function overloading • Functions with same name and different parameters • Should perform similar tasks • I.e., function to square ints and function to square floats int square(int x, int y){return x * y;} int square(int x) {return x * x;} float square(float x) { return x * x; } • Overloaded functions distinguished by signature • Based on name and parameter types (order matters) • Name mangling • Encodes function identifier with parameters • Type-safe linkage • Ensures proper overloaded function called

  5. Class Sample: CUBOID Class class CUBOID{ private: int weight, length, height; public: int surface(); int perimeter(); int volume(); void setWeight(int); int getWeight(); }; weight length height int perimeter(){ return 4(weight+length+height); } int surface(){ return 2(weightlength + weightheight + heightlength); } int volume(){ return weightlengthheight; }

  6. Overview of Two Classes class CUBOID{ private: int weight, length, height; public: CUBOID(); int surface(); int perimeter(); int volume(); void setWeight(int); int getWeight(); void setLength(int); int getLength(); void setHeight(int); int getHeight(); }; class RECT{ private: int weight, length; public: RECT(); int surface(); int perimeter(); void setWeight(int); int getWeight(); void setLength(int); int getLength(); };

  7. Function Override class CUBOID : public RECT{ private: int height; public: CUBOID(); int volume(); void setHeight(int); int getHeight(); }; class RECT{ private: int weight, length; public: RECT() int surface(); int perimeter(); void setWeight(int); int getWeight(); void setLength(int); int getLength(); }; Inheritance int surface(); int perimeter(); void setWeight(int); int getWeight(); void setLength(int); int getLength(); override

  8. Function Override • When derived class inherits from base class • Rewrite the implementation of functions • Return type, function name, and parameters are the same. • Called Override

  9. Introduction to Inheritance • Inheritance • Software reusability • Create new class from existing class • Absorb existing class’s data and behaviors • Enhance with new capabilities • Derived class inherits from base class • Derived class • More specialized group of objects • Behaviors inherited from base class • Can customize • Additional behaviors

  10. Introduction to Inheritance • When class A inherits from class B, we show that Inheritance symbol class A : public B Derived class Base class

  11. Member Access Specifiers • Public • Can be accessible publicly • Protected • Can be accessible by • Base class • Derived class • Private • Only can be accessible by base class

  12. void main() { ExA ex; ex.a; // Error: can’t access private ex.b; // Error: can’t access protected } class ExA{ private: int a; protected: int b; public: void set(int); int get(); } NOT inheritance  Can not access non-public members

  13. class ExA{ private: int a; protected: int b; public: void set(int); int get(); } • ExB inherits from ExA • Can access public and protected members class ExB : public ExA { public: void fa1(){cout <<get();} void fb(){cout<<b;} //Error: can’t access private void fa2(){cout <<a;} }

  14. Member Member private: name; private: name; by public function ex: getName(); protected: number; protected: number; directly accessible by Student. public: getName(); public: getName(); Public, Protected, and Private Student

  15. Summary Protected Data Member • Intermediate level of protection between public and private • protected members can be accessed by • Derived class member functions directly • Derived class friends functions directly • Advantages • Derived classes can modify values directly • Slight increase in performance • Avoid set/get function call overhead • Disadvantages • No validity checking • Derived class can assign illegal value

  16. private: char name[80]; protected: int number; public: void setName(string); void getName(); void setNumber(int); int getNumber(); void input(); void print(); Practice 4 (P4) members Student Teacher private: int chinese; public: void setChinese(int); int getChinese(); void input(); void print(); private: int bounds; public: void setBounds(int); int getBounds(); void input(); void print();

  17. Practice 4 (P4) • PLEASE write a program that a user can input information of Student, Teacher, or Member. • If the user inputs information of Student/Teacher/Member, please output all the information about the Student/Teacher/Member.

More Related