Download presentation
Presentation is loading. Please wait.
1
計算機概論實習 2007-04-27
2
2 Last Practice (P4) members StudentTeacher private: char name[80]; protected: int number; public: void setName(char *); void getName(char*); 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();
3
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
4 members private: char name[80]; protected: int number; public: void setName(char *); void getName(char*); void setNumber(int); int getNumber(); void input(); void print(); private: int chinese; public: void setChinese(int); int getChinese(); void input(); void print(); Student void member::setName(char* na){ cout "Input Member’s Name:"; cin >> name; } If you write setName(char *)You have to override in class Student
5
5 members private: char name[80]; protected: int number; public: void setName(char *); void getName(char*); void setNumber(int); int getNumber(); void input(); void print(); private: int chinese; public: void setChinese(int); int getChinese(); void input(); void print(); Student void member::input(){ cout "Input Member’s Name:"; cin >> name; } If you write Input()Need Override and setName() will be nothing
6
Overloading Operator
7
7 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
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
9 Overloading Operator void main() { SQUARE square; cout << square; } length:0 weight:0
10
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
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
12 Another Example void main(){ SQUARE squ1, squ2; squ1.add(squ2); cout << squ1; cout << squ1.add(squ2); error cout << squ1.add2(squ2); } SQUARE add2(SQUARE); friend ostream &operator<<( ostream&, const SQUARE& );
13
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
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
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
16 Others SQUARE& operator+ (int); void main() { SQUARE squ; cout << "squ+1:" << squ + 1; //squ.operator+(1) }
17
17 Practice 5 (P5) #include #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
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; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.