Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "計算機概論實習 2007-04-13. 2 Class Sample: RECT Class weight length class RECT{ private: int weight, length; public: RECT(); int surface(); int perimeter();"— Presentation transcript:

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

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

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

4 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 5 Class Sample: CUBOID Class weight length class CUBOID{ private: int weight, length, height; public: int surface(); int perimeter(); int volume(); void setWeight(int); int getWeight(); }; int surface(){ return 2  (weight  length + weight  height + height  length); } int perimeter(){ return 4  (weight+length+height); } int volume(){ return weight  length  height; } height

6 6 Overview of Two Classes class RECT{ private: int weight, length; public: RECT(); int surface(); int perimeter(); void setWeight(int); int getWeight(); void setLength(int); int getLength(); }; 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(); };

7 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(); }; override int surface(); int perimeter(); void setWeight(int); int getWeight(); void setLength(int); int getLength(); Inheritance

8 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 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 10 Introduction to Inheritance When class A inherits from class B, we show that Inheritance symbol class A : public B Derived classBase class

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

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

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

15 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 16 Practice 4 (P4) members StudentTeacher private: char name[80]; protected: int number; public: void setName(string); void getName(); void setNumber(int); int getNumber(); void input(); void print(); 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 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.


Download ppt "計算機概論實習 2007-04-13. 2 Class Sample: RECT Class weight length class RECT{ private: int weight, length; public: RECT(); int surface(); int perimeter();"

Similar presentations


Ads by Google