Structured Programming Instructor: Prof. K. T. Tsang Lecture 13:Object 物件 Oriented 面向 Programming (OOP)

Slides:



Advertisements
Similar presentations
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Advertisements

Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Lecture 9 Concepts of Programming Languages
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
1 C++ Structures Starting to think about objects...
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Review of C++ Programming Part II Sheng-Fang Huang.
CS 1031 C++: Object-Oriented Programming Classes and Objects Template classes Operator Overloading Inheritance Polymorphism.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Object-Oriented Programming in C++
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
Learners Support Publications Classes and Objects.
Programming Languages by Ravi Sethi Chapter 6: Groupings of Data and Operations.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Object-Oriented Programming. Procedural Programming All algorithms in a program are performed with functions and data can be viewed and changed directly.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
Object-Oriented Programming in C++ More examples of Association.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Structures, Classes and Objects Handling data and objects Unit - 03.
1 CSC241: Object Oriented Programming Lecture No 02.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
1 Review: C++ class 2 kinds of class members: data members and function members Class members are private by default Data members are generally private.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
1 Classes classes and objects - from object-oriented programming point of view class declaration class class_name{ data members … methods (member functions)
1 Introduction to Object Oriented Programming Chapter 10.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Programming with ANSI C ++
By Muhammad Waris Zargar
Introduction to Classes
Chapter 5 Classes.
More about OOP and ADTs Classes
This technique is Called “Divide and Conquer”.
Lecture 9 Concepts of Programming Languages
Introduction to Classes
More about OOP and ADTs Classes
Dr. Bhargavi Dept of CS CHRIST
Classes and Objects.
UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS
COP 3330 Object-oriented Programming in C++
Recitation Course 0603 Speaker: Liu Yu-Jiun.
Types of Computer Languages
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Lecture 9 Concepts of Programming Languages
Presentation transcript:

Structured Programming Instructor: Prof. K. T. Tsang Lecture 13:Object 物件 Oriented 面向 Programming (OOP)

What is an Object [ 物件, 对象 ]? In plain English, “object” is a “thing”. In programming, “object” is the “thing” (data + functions) that models the real world object that we have to deal with. It is easier to group all related data to form a data “object” in the process of problem solving.

Classes[ 类 ] and Objects In our daily life, objects have general properties (attributes) that characterize the group each object belonged to. For example: “I” am a “teacher”. “You” are a “student”. or“Mr. Fu” is a “teacher”. “Mary” is a “student”. Both “I”, “You”, “Mr. Fu” & “Mary” are “objects”. “I” & “Mr. Fu” belong to the class of “teacher”. “You” & “Mary” belong to the class of “student”.

Object is an instance[ 例 ] of a class In programming language, “I” & “Mr. Fu” are instances of the “teacher” class. “You” & “Mary” are instances of the “student” class. In general, any object in programming is an instance of a class. The relationship between object and class is like that between variable and type.

“class” is a more general form of “struct” “struct” in C/C++ is a data structure contains data only. “class” in C++ is a structure contains both data and functions (methods) to process the data within the class.

Procedural programming vs. Object Oriented Programming In C, programming efforts focus on developing “functions”, the procedures to handle data. In C++, programming efforts focus on developing “classes”. For this reason, C++ is an Object Oriented Programming language. C is a procedural programming language.

Example of a class class student { private : int ID; char name[99]; char major[99]; char *course_taking[ ]; public : void print_info(); void changeMajor (char *ma); void enroll_class(char *class_name); }

Another example of class #define PI class circle{ private: double x, y;//coordinates of the center double r;//the radius public: double circumference() { return 2*PI*r; } double area() { return PI*r*r; } }

C++ File-processing classes #include using namespace std; void main(){ ifstream fin; int A[4], r; fin.open("file1.dat"); //open data file for(r=0; r<4; r++) fin >> A[r]; //read from file into array fin.close(); ofstream fout; fout.open("file2.dat"); //open output file for(r=3; r>=0; r--) //write to file fout << A[r] << ' '; fout.close(); } Name of a class Name of an object

Data & their operations In C, the relationship between data and their associated operations is not clear. Operations like % can operate on “int” but not “float” +, -, * or / on “int”, “float” & “double” but not on “char” Programmer has to be careful in using operations on data. Functions and data are not related.

Operators are functions “=” as a function with prototype: int operator= (int a); float operator= (float f); “+” as a function with prototype: int operator+ (int a, int b); float operator+ (float a, float b); double +(double a, double b); c operator= (operator+ (a, b));//c = a + b;

From “struct” to “class” A C/C++ struct is a collection of data. In C++ data and the functions (methods) operated on them are grouped together in a structure called class. A C++ class holds not only data, but also the functions that manipulate the data. C++ class also provides protection against improper access of its data.

Stack: a C structure to hold data Stack is an algorithm for storing data. Data is stored in last-in-first-out (LIFO) order #define STACK_SIZE 100 //definition of a “stack” stuct stack { int count; int data[ STACK_SIZE ]; } Functions to manipulate this stack is defined separately.

Stack functions void stack_init (struct stack &astack) {//initialize stack astack.count = 0;//set count=0 } void stack_push(struct stack &astack, int idat) {//add data in stack astack.data[astack.count] = idat; astack.count++;//increase count } int stack_pop(struct stack &astack) {//take data out astack.count--;//decrease count return astack.data[astack.count]; }

Use the stack main (){ struct stack mystack; stack_init(mystack); stack_push (mystack, 11); stack_push (mystack, 21); stack_push (mystack, 8); cout << stack_pop(mystack) << endl; } Result:

An improved stack class stack { private: //data are protected from outside use int count; int data[STACK_SIZE]; public: //no restriction in using methods void init(); //initialize the stack void push(int idata); //push data in int pop( ); //get data out }

Implement class methods void stack::init( ) { count = 0; } void stack::push ( int item ) { data[count] = item; count++; } int stack::pop( ) { count--; return data[count]; } Outside the class definition, a class method Is known by its name preceded by the class name and “::” (double colon).

Using the stack class main( ){ stack mystack; mystack.init( ); mystack.push (11); mystack.push (16); mystack.push (22); mystack.push (9); cout << mystack.pop () << endl; } Result:

Keyword: private Data in a class are also called attributes. They are usually protected from outside use by the keyword “private”. We cannot access any attribute labeled private. Private attributes can only be used in the implementation of class methods. int n = mystack.data;//error mystack.data = n;//error

Keyword: public Functions in a class are called methods. They are usually labeled by the keyword “public”. That means they can be used without restriction. mystack.init( ); mystack.push (11); cout << mystack.pop () << endl;

Constructors of object C++ allows you to define a class method to create an object of that class with all attributes initialized. This is called the “constructor” and has the same name as the class. For example, the constructor for the “stack” class is named “stack”. Outside the class body it is known as “stack ::stack”.

A “stack” class with constructor class stack { private: //data are protected from outside use int count; int data[STACK_SIZE]; public: //no restriction in using methods stack(void); //constructor void init(); //initialize the stack void push(int idata); //push data in int pop( ); //get data out }

A simple constructor stack::stack (void){ count = 0; } Constructor can never return anything, so even “void” is not needed as return type. Constructor is called automatically when an object is first declare.

How is constructor used? //Instead of main ( ){ stack mystack; mystack.init(); …//use mystack }//we can just write: main ( ){ stack mystack; //constructor called automatically //no need to initialize as this is done by the constructor …//use mystack }

Parameterized constructor Constructor may take parameters as arguments to initialize the newly created object.

Example: class friend{ private: char name[50]; char phone[50]; public: friend (char aname[ ], char phonen[ ]); …//rest of the class definition }

friend::friend (char nm[ ], char ph[ ]) { strcpy (name, nm); strcpy (phone, ph); }

Overloaded constructor class friend{ private: char name[50]; char phone[50]; public: friend (void); friend (char aname[ ]); friend (char aname[ ], char phonen[ ]); …//rest of the class definition }

friend::friend (void) { strcpy (name, “none”); strcpy (phone, “none”); } friend::friend (char nm[ ]) { strcpy (name, nm); strcpy (phone, “none”); } friend::friend (char nm[ ], char ph[ ]) { strcpy (name, nm); strcpy (phone, ph); }

main ( ){ friend friend1; //constructor without argument called friend friend2(“Li John”); //constructor with 1 argument called friend friend3 (“Li John”, “ ”); //constructor with 2 arguments called }

Attention: If you have not defined a constructor that takes no argument, you cannot declare an object without specifying an argument. //if friend (void) is not defined friend my_good_friend;//error

Overloaded functions In C++ we can define function with the same name but with different arguments. They are regarded as different functions by the compiler. When an overloaded function is called, the compiler will check how many arguments there are and choose the right one to use.

Copy constructor Copy constructor is a constructor used to make an exact copy of an object (of the same class). friend::friend (friend o_friend) { strcpy (name, o_friend.name); strcpy (phone, o_friend.phone); }

main ( ){ friend friend1 friend friend2(“Li John”); friend friend3 (“Li John”, “ ”); //create a copy of friend3 friend friend4 (friend3); //copy constructor called … }

Destructor Constructor is automatically called when an object is created. Likewise, destructor is called automatically when the object goes out of scope or when a pointer to the object is deleted. The name for a destructor is the class name with a ‘~’ in front of it, e.g. ~stack(void)

Every class should have a constructor and a destructor. If you do not write these member functions, C++ will automatically generate them.

Automatic generated member functions by C++ Default constructor class::class( ) Copy constructor class::class(const class &obj) Default destructor class::~class( ) Default assignment operator class class::operator= (const class &obj )

How constructor & destructor are called without you knowing it? void use_stack (stack local_stack) { local_stack.push(9); local_stack.push(11); …//more operations on local_stack } main { stack stack1; stack1.push(2); stack1.push(9); use_stack (stack1); cout << stack1.pop( ) << endl; } Default Constructor called Copy constructor called: local_stack.stack(stack1); Default destructor called after local_stack out of scope: local_stack.~stack();

Summary “class” is a data structure similar to “struct” in C. “object” is an instance of a “class”. “class” contains data (attributes) and member functions (methods). All classes have at least a constructor and a destructor for object creation and deletion.