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

Slides:



Advertisements
Similar presentations
Engineering Problem Solving With C++ An Object Based Approach Additional Topics Chapter 10 Programming with Classes.
Advertisements

Operator overloading redefine the operations of operators
Operator Overloading. Introduction Operator overloading –Enabling C++’s operators to work with class objects –Using traditional operators with user-defined.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 8 - Operator Overloading Outline 8.1 Introduction 8.2 Fundamentals of Operator Overloading 8.3.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 102 Computer Programming II (Lab:
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions.
More on Functions Programming. COMP104 Lecture 19 / Slide 2 Passing Parameters by Reference l To have a function with multiple outputs, we have to use.
1 CS 105 Lecture 8 Strings; Input Failure Mon, Mar 7, 2011, 3:39 pm.
計算機概論實習 Files and Streams C++ views file as sequence of bytes Ends with end-of-file marker n-1 end-of-file marker 67 This is.
計算機概論實習 Review of Practice 8 (P8) PLEASE write a program that when you fail to open a file, you have to throw a exception. Directly using.
計算機概論實習 What is Class In C++, class is the keyword which means encapsulation. Property and method are used to define what is class. Materialize.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
CS150 Introduction to Computer Science 1
計算機概論實習 Claim a Parameter int a; a = 5; typenameaddress inta0x0001 addressvalue 0x0001 0x0002 0x0003 0x0004 0x0005 0x0006 0x0007 0x0008.
Chapter 4 Summation.
計算機概論實習 Stream Stream: sequence of bytes Input: from device (keyboard, disk drive) to memory Output: from memory to device (monitor, printer,
計算機概論實習 Class Sample: RECT Class weight length class RECT{ private: int weight, length; public: RECT(); int surface(); int perimeter();
CMSC 2021 Stream I/O Operators and Friend Functions.
1 Overloading functions C++ allows us to define functions that have the same name but different sets of parameters This capability can be used to define.
計算機概論實習 A Simple Way to Handle Error if(b != 0) { cout
CMSC 202 Lesson 12 Operator Overloading II. Warmup  Overload the + operator to add a Passenger to a Car: class Car { public: // some methods private:
CSC241 Object-Oriented Programming (OOP) Lecture No. 10.
CS1201: Programming Language 2 Recursion By: Nouf Almunyif.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
Inheritance One of the most powerful features of C++
C ++ Basics by Bindra Shrestha sce.uhcl.edu/shresthab CSCI 3333 Data Structures.
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
Input a number #include using namespace std; int main() { int num; cout num; return 0; }
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Introduction to Programming Lecture 40. Class Class is a user defined data type.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Review of Function Overloading Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int.
11 Introduction to Object Oriented Programming (Continued) Cats.
Reference Parameters There are two ways to pass arguments to functions: pass- by-value and pass-by-reference. pass-by-value –A copy of the arguments’svalue.
Operator Overloading Chapter Objectives You will be able to Add overloaded operators, such as +,-, *, and / to your classes. Understand and use.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
CS 240 Computer Programming 1
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Introduction to Programming
C++ Templates.
Overloading Operator MySting Example
Operator Overloading Part 2
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
לולאות קרן כליף.
Function Basics.
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
Null-Terminated Character Arrays
Function Overloading C++ allows us to define functions that have the same name but different sets of parameters This capability can be used to define similar.
Introduction to Programming
Starting Out with C++: From Control Structures through Objects
Pointers & Functions.
CSC 102 Chabli Boler.
الوحدة الرابعة البرمجة وصياغة حل المسائل البرمجة وأهميتها أهداف الدرس الأول مفهوم البرمجة. الفرق بين المبرمج ومستخدم البرنامج. الحاجة إلى البرامج.
Introduction to Programming
Statements and flow control
Eighth step for Learning C++ Programming
Pointers & Functions.
(Dreaded) Quiz 2 Next Monday.
Overloading the << operator
Presentation transcript:

計算機概論實習

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

Overloading Operator

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 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); } SQUARE add2(SQUARE); friend ostream &operator<<( ostream&, const 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 #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; }