170 likes | 351 Views
計算機概論實習 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(){
E N D
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 weightlength; }
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); }
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
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(weightlength + weightheight + heightlength); } int volume(){ return weightlengthheight; }
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(); };
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
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
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
Introduction to Inheritance • When class A inherits from class B, we show that Inheritance symbol class A : public B Derived class Base class
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
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
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;} }
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
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
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();
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.