180 likes | 321 Views
計算機概論實習 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:
E N D
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();
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.
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; }
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; }
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; }
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; }
Overloading Operator void main() { SQUARE square; cout << square; } length:0 weight:0
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&); };
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; }
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);
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; }
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; }
Another Example – Overloading Operator+ void main(){ SQUARE squ1, squ2; cout << "squ1 + squ2:" << squ1 + squ2; } • In fact, its original statement is squ1.operator+(squ2);
Others SQUARE& operator+ (int); void main() { SQUARE squ; cout << "squ+1:" << squ + 1; //squ.operator+(1) }
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 };
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; }