1 / 18

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

計算機概論實習 2007-04-27. private: char name[80]; protected: int number; public: void setName(char *); void getName(char*); void setNumber(int); int getNumber(); void input(); void print();. Last Practice (P4). members. Student. Teacher. private:

makya
Download Presentation

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

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-27

  2. private: char name[80]; protected: int number; public: void setName(char *); void getName(char*); void setNumber(int); int getNumber(); void input(); void print(); Last Practice (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();

  3. Last Practice (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.

  4. private: char name[80]; protected: int number; public: void setName(char *); void getName(char*); void setNumber(int); int getNumber(); void input(); void print(); members private: int chinese; public: void setChinese(int); int getChinese(); void input(); void print(); Student If you write setName(char *) You have to override in class Student void member::setName(char* na){ cout "Input Member’s Name:"; cin >> name; }

  5. private: char name[80]; protected: int number; public: void setName(char *); void getName(char*); void setNumber(int); int getNumber(); void input(); void print(); members private: int chinese; public: void setChinese(int); int getChinese(); void input(); void print(); Student If you write Input() Need Override and setName() will be nothing void member::input(){ cout "Input Member’s Name:"; cin >> name; }

  6. Overloading Operator

  7. Overloading Operator • If we want to print an integer, the following programming can be used for this purpose. int a = 5; cout << a; • If we want to print the value of an object. class SQUARE{ int length; int weight; }; void main(){ SQUARE square; cout << "length:" << square.length; cout << "weight:" <<square.weight; }

  8. Overloading Operator • C++ provides another way called overloading operator. for example: class SQUARE{ friend ostream &operator<<( ostream&, const SQUARE& ); public: int length; int weight; SQUARE() {length = weight = 0;} }; ostream &operator<<( ostream &output, const SQUARE &squ ){ output << "length:" << squ.length << endl; output << "weight:" <<squ.weight << endl; return output; }

  9. Overloading Operator void main() { SQUARE square; cout << square; } length:0 weight:0

  10. Another Example class SQUARE{ friend ostream &operator<<( ostream&, const SQUARE& ); public: int length; int weight; SQUARE() {length = weight = 0;} void add(SQUARE&); SQUARE& add2(SQUARE&); };

  11. Another Example void SQUARE::add(SQUARE& squ){ length += squ.length; weight += squ.weight; } SQUARE& SQUARE::add2(SQUARE& squ){ length += (squ.length+1); weight += (squ.weight+1); return *this; }

  12. Another Example void main(){ SQUARE squ1, squ2; squ1.add(squ2); cout << squ1; cout << squ1.add(squ2);  error cout << squ1.add2(squ2); } friend ostream &operator<<( ostream&, const SQUARE& ); SQUARE add2(SQUARE);

  13. Another Example – Overloading Operator+ class SQUARE{ friend ostream &operator<<( ostream&, const SQUARE& ); public: int length; int weight; SQUARE() {length = weight = 0;} SQUARE& operator+ (SQUARE&); }; SQUARE& SQUARE::operator + (SQUARE& squ){ SQUARE *temp = new SQUARE; temp->length = this->length + squ.length; temp->weight = this->weight + squ.weight; return *temp; }

  14. class SQUARE{ friend ostream &operator<<( ostream&, const SQUARE& ); public: int length; int weight; SQUARE() {length = weight = 0;} SQUARE operator+ (SQUARE); }; SQUARE SQUARE::operator + (SQUARE squ){ SQUARE temp; temp.length = this->length + squ.length; temp.weight = this->weight + squ.weight; return temp; }

  15. Another Example – Overloading Operator+ void main(){ SQUARE squ1, squ2; cout << "squ1 + squ2:" << squ1 + squ2; } • In fact, its original statement is squ1.operator+(squ2);

  16. Others SQUARE& operator+ (int); void main() { SQUARE squ; cout << "squ+1:" << squ + 1; //squ.operator+(1) }

  17. Practice 5 (P5) #include<iostream> #define SIZE 4 using namespace std; class MATRIX{ friend ostream &operator<<( ostream&, const MATRIX& ); private: int ma[SIZE][SIZE]; public: MATIRX(); //constructor : initialize the matrix MATRIX& operator+ (MATRIX&);//Matrix add operation MATRIX& operator- (MATRIX&);//Matrix subtract operation MATRIX& operator* (MATRIX&);//Matrix multiply operation };

  18. Practice 5 (P5) void main(){ MATRIX aMatrix, bMatrix, cMatrix, dMatrix; cout << "aMatrix + bMatrix:" <<aMatrix + bMatrix << endl; cout << "cMatrix - bMatrix:" << cMatrix - bMatrix << endl; cout << “dMatrix * bMatrix:" << dMatrix * bMatrix << endl; }

More Related