Title: ???????????????????????? Object-Oriented Programming(OOP)
1????????????????????????Object-Oriented
Programming(OOP)
2???????????????????????? (Object Oriented
Programming OOP)
- ???????????????????????????????????????
(Structure Programming) ???????????
??????????????????? (Sequence) ,
????????????????????????(Decision) ???
????????(Loop) - ???????????????????????? (object oriented
programming) ?????????????????????????????
??????????????? ??? Dr. Kristin Nygaard
??????????????????? ?????.?.1969 - ?????? OOP ???????????????????????????????????????
??????????????????????????????????????????????????
??? - ???????????????????????????????? ???? C, C,
java
3Structured Programming
- ??????????????????????????????????????
- ??????????? ??????????????????????
- ???????????????????? (global variable)
4Structured Programming
- ??????????????????????????????????????
- ?????????????????? ??????????????????????????????
- ???????????????????????????
- ??????????????????????????????????????????????????
?
5OOP
- ??????????????????????????????
- ???????????????????????????? (function)
??????????????????????????????????????????????????
????? ??? ????????????? (object) - ????????????????????????? ????????????????
???????????????????????????????????????????? - ??????????????????????????????? ?????????????
????????? - ??????????????????????????????
6OOP
- ??????????????????????????????
7????????????????? Object Oriented
- ????????? 4 ????????? Object Oriented
- abstraction
- encapsulation
- modularity
- hierarchy
8???????(Abstraction)
- Object oriented ????????????????????????????????
Abstraction ???????? - Abstraction ????????????????????????????
????????????????????????????????????????????
??????????????????????????????????????????????????
??????????????????? ??????????????????????????????
??? - ???????????? ????????????????? Abstraction ???
??????????????????????????????????????????????????
????????????????? Toyota ???? ?? 2003
???????????????? Abstraction
9?????????????????(Encapsulation)
- Property ??? Operation ?????????????????????Object
???????????????????????????Object
??????????????? Interface - Interface ??? Operation???????????????????????????
?????????????????????????????????Object - Encapsulation ?????????????????????? Debug ???
Maintenance
10?????(Modularity)
- ??????????????????????????????????????????????????
????????????????????????????????????
????????????????????? - ????????????????????????????????????????????????
Reuse - ????????????????????????? ????????????????????????
???????? ????????????????????? 3 ?????????
?????????????????????????? ???????????????????????
???????????????? ?????????????????????????????????
?????? ???????????????????????????????????????
??????????????????????????????????????
11Hierarchy
- Hierarchy ??????????????????????????????????
Abstraction ??????????????????????????? (Tree
structure) - Hierarchy ????????????????????????????????????????
????????????????????????????? - Hierarchy ?????????????????(perspective) ????
????????????????????????????????? ???????? ?????
??????????? ??????????????????????????????????????
?????(????????????????? ????? ???? ????????)
?????????? Hierarchy ?????????????
12????(Class)
- Class ??? Abstraction ????????????????????????????
??????Object - Class ????????????????????????????????????????????
?????????? C - Class ?????????????????????????? C
??????????????? ???????????????????????????????
C ?????(Primitive type)???? int char ????
float ??????? ????????????????? Class
??????????????????????????????????????????????????
???????? ?????????????????????????? type
???????????????????????????????????? - Class ????????????? Data member ??? Function
member ???? class telephone ?? phone number???
type (tone or rotary) ???? Data member
???????????????????????? ????? dial answer
hang-up ???? Function member ?????????????????????
13????(Class)
- ???????????? Class ??????????
- Class name
- Class data members(attributes)
- Class functions (methods/operations)
- Class definition ???????????? Template
?????????????? Object ????????????
?????????????????????????? int char - Class ????????????????????????????????????????????
???????????????????????????
14????(Class)
15Attribute ???? Data member
- Attribute ??????????????????????Class
???Attribute??????????????????????????????????????
??Object(instance of class) - Attribute ????????(type) ??????????????????????
Attribute ???????? ???? Attribute ?????? integer,
boolean,???? real
16Operation ???? function member
- Operation ????????????Class?????????????Object
???? Class ???????? - Operation ?? Class ????????????????? Class
????????????????????????????????????????
17Class ??? Object
- Class member function ??C ?????????? Method
- ???????????????? (define) Class
- class class_name
- int data_member//data member/attributes
- void show_member(int)//function member/methods
-
- ??????? define ??????????????????????(Object)???
- Class_name object_one, object_two
18Class ??? Object
- ????????????????????????? Employee class
- Class Employee
- public
- char name64
- long employee_id
- float salary
- void show_employ(void)
- coutltltname ltltnameltltendl
- coutltltId ltltemployee_idltltendl
- coutltltSalary ltltsalaryltltendl
-
19Class ??? Object
- ?????????
- Employee worker, boss, secretary
- class name class variables ( objects )
- ??? . (dot operator) ???????????(assign) Class
data members - ??? . (dot operator) ?????????? Class member
functions - ????????
20Class ??? Object
- include ltiostream,hgt
- include ltstring.hgt
- class Employee
- public
- char name64
- long employee_id
- float salary
- void show_employ(void)
- coutltltname ltltnameltltendl
- coutltltId ltltemployee_idltltendl
- coutltltSalary ltltsalaryltltendl
-
21Class ??? Object
- void main(void)
- Employee worker, boss
- strcpy(worker.name, John Doe)
- worker.employee_id1234
- worker.salary25000
- strcpy(boss.name, Happy Boss)
- boss.employee_id23456
- boss.salary100000
- worker.show_employee()
- boss.show_employee()
22Class ??? Object
- ??? Employee class ????????? function
???????????????? Class definition ???????
function ????????????????????? function
?????????????????????????????????? Class ???????
?????????????? Function definition ?????? Class
??? - class Employee
- public
- char name64
- long employee_id
- float salary
- void show_employ(void)
Function Prototype
23Class ??? Object
Function Name or Method
- void Employeeshow_employ(void)
- coutltltname ltltnameltltendl
- coutltltId ltltemployee_idltltendl
- coutltltSalary ltltsalaryltltendl
-
- ??????????? ?????????? Function definition
????????? Class ????????? Class name ??? Global
resolution operator() ??????? Function
definition ??????????????????????? - return _type class_namefunction_name(paratmete
rs) - //statement
Class name
24??????????????
- ????????????????????????????????(data members)
???????????????????????(method)?? C ?? 3 ???
??? - private Data members ??? Methods
?????????????????? private ???????????????????????
?????Class ?????????????????????? Class
??????????????????????????????????????????????????
????????? private ??? - public????????????? Class ?????????(?????????????
??????????????????????????????) - protected ?????????????????????? Class ???
Sub-class ????????????????????????????????????????
??????????????????? protected ???
25??????????????
- Information Hiding ???????????????????????????????
??????????????????????????????????????????????????
? ????????????????????????????????? Global
variable. - ??????????????(Access specifier)???
private,public ???? protected ??????? ???????????
- ???????????????? Access specifier
-
- class some_class
- public
- int some_variable
-
- void initialize_private(int, float)
-
- void show_data
-
- private
- int key_value
-
- float key_number
-
Public Members
Private Members
26??????????????
- ??????????????????? main function
- some_class object//create an object
- object.some_variable10//public data member
- object.key_value10//wrong private data member
- //syntax errors
- object.initialize_private(20, 12.345)//call
public method - object.show_data() )//call public method
- ???????????????????????????????????????????????
data member ????????? ??????????????????? Data
member ???? Public method(interface)
27class
main()
object1
object2
OK
private
private
OK
OK
public
public
28??????????????
- ????????
- includeltiostream.hgt
- includeltstring.hgt
- class employee
- public
- int assign_values(char arg_name,
- long arg_employee_id,
- float arg_salary)
- void show_employee(void)
- int change_salary(float)
- long get_id(void)
- private
- char name64
- long employee_id
- float salary
29??????????????
- int employeeassign_values(char emp_name,
- long emp_id,
- float emp_salary)
- strcpy(name, emp_name)
- employee_idemp_id
- if( emp_salarylt50000.0)
- salaryemp_salary
- return (0)//sucessful
- else
- return (-1)//invalid salary
-
30??????????????
- void employeeshow_employee(void)
- coutltlt Employee ltltnameltltendl
- coutltlt Id ltltemployee_idltltendl
- coutltlt Salary ltltsalaryltltendl
-
- int employeechange_salary(float new_salary)
- if( new_salarylt50000.0)
- salarynew_salary
- return (0)
- else
- return (-1)
-
31- long employeeget_id(void)
- return (employee_id)
-
- int main(void)
- employee worker
- if( worker.assign_values(Jim Fish,101,10101.0)
0) - coutltlt Employee values assigned ltltendl
- worker.show_employee()
- if( worker.change_salary(35000.0)0)
- coutltlt New salary assigned ltltendl
- worker.show_employee()
- else
- coutltlt Invalid salary specified ltltendl
-
- return 0
32??????????????
- ?????? (global resolution operator)
?????????????????????????????????????? - Method ??????????????????????? public access
?????? - C ????????(default) data member ??? method ????
private
33Constructor ??? Destructor
- Constructor ???? Class method ??????????????????
Class????? ????????????????????????????????
Object - ??????????? Constructor ??????????????????????????
? Data members - ???????????????????????Constructor
Compiler?????Constructor ??????????? Default
constructor - ???????????????? Constructor ?????? Class
employee
34Constructor ??? Destructor
- class employee
- public
- employee(char, long, float)//constructor (no
return type) - void show_employee(void)
- int change_salary(float)
- long get_id(void)
- private
- char name64
- long employee_id
- float salary
35Constructor ??? Destructor
- employeeemployee(char name,long
employee_id,float salary) -
- strcpy(employeename, name)
- employeeemployee_id employee_id
- if( salarylt50000)
- employeesalarysalary
- else
- employeesalary0.0//invalid salary specified
-
36Constructor ??? Destructor
- void employeeshow_employee(void)
- coutltlt Employee ltltnameltltendl
- coutltlt Id ltltemployee_idltltendl
- coutltlt Salary ltltsalaryltltendl
-
- int main(void)
- employee worker(Jim Jones, 101,10000.0)//const
ructor //???????? - worker.show_employee()
37Constructor ??? Destructor
- ???????????? Constructor ???????? return
???????????????? void ??????? Constructor - ???????? Object ??? Class employee ???? worker
????? initial ????????? Data members name,
employee_id ??? salary - Constructor ??? Class employee ?????????????????
38Constructor ??? Destructor
- ??? Overloading constructor ?????????????????
Constructor ?????????????????? Constuctor????????
?? Parameter ?????????????? - 1.employee(char, long, float)
- 2.employee(char, long)
- ?????????????????? Object
- employee worker(Jim Jones, 101,10000.0)
- Constructor ??????? 1 ??????????
- employee worker(Jim Jones, 101)
- Constructor ??????? 2 ??????????
39Constructor ??? Destructor
- Destructor function ?????????????????????(free
up/clean) Object ?????????????????????????????????
??????? Object - Desctructor function ?????????????? Class
???????????????? (tilde) ?????? - ???????? Destructor
- class_name(void)
- //function statement
-
40Constructor ??? Destructor
- employeeemployee(void)
- coutltlt Destroying the object for ltltnameltltendl
-
- ????? run ????????????(?????? main function)
Destructor ?????????? - ???????????????????? Dynamic allocate memory ??
Object ??????????? Code ?????????????(release)
Memory
41Static function ??? data members
- ?????????????????Object Object???????????????????
???????????????????????????? Data member(s)
????????????? ????????????????????????????????????
????????(share)????????? Object
?????????????????????????????? static ???????
type ??? Data member ???????????? - private
- static int shared_valued
- ?????????????????????? ??????????????
- int class_nameshared_valued
42Static function ??? data members
- ????????
- class book_series
- public
- book_series(char, char, float)
- void show_book(void)
- void set_page(int)
- private
- static int page_count
- char title64
- char author64
- float price
43Static function ??? data members
- int book_seriespage_count
- void book_seriesset_pages(int pages)
- page_countpages
-
- book_seriesbook_series(char title,char
author, float price) - strcpy(book_seriestitle, title)
- strcpy(book_seriesauthor, title)
- book_seriespriceprice
44Static function ??? data members
- void book_seriesshow_book(void)
- coutltlt Title ltlttitleltltendl
- coutltlt Author ltltauthorltltendl
- coutltlt Price ltltpriceltltendl
- coutltlt Pages ltltpage_countltltendl
-
45Static function ??? data members
- int main(void)
- book_series programming(C How
to,DD,20.0) - book_series word(Word for window,Ant,10.0)
- word.set_pages(256)
- programming.show_book()
- word.show_book()
- coutltltendlltlt Change page count ltltendl
- programming.set_pages(500)
- programming.show_book()
- word.show_book()
- return 0
46Static function ??? data members
- ?????????????????????????? page_count ????
static int ?????????????? page_count member
?????????? Global variable ???????????????????????
?? page_count object????????????????????????? - ????????? Data member ???? Method ???? static
?????????????????????? Method ???? initial
?????????????????? Object ??????????????????
Member ??? static ????????????????????? Member
?????? Class ???????????? Object
47Object ??? ????????
- ???????Object?????????????????????????????????????
??????????????? ??????????????????????????Object(d
ata member)???? ????Object??????????????? - ?????????????????Object????Object
????????????????????????????????????
????????????????????? ????????????? return???
????????????????????Object????????????????????????
????????????????
48Object ??? ????????
- ??????????????? object ???????????
- class airtime
- private
- int minutes
- int hours
- public
- void set()
- char dummy
- coutltlt Enter time(format 2359)
- cingtgthoursgtgtdummygtgtminutes
-
- void display()
- coutltlthoursltltltltsetfill(0)ltltsetw(2)ltltminute
s -
49Object ??? ????????
- void add(airtime at1, airtime at2)
- minutesat1.minutesat2.minutes//add minutes
- hoursat1.hoursat2.hours//add hours
- if( minutesgt59 )
- minutesminutes-60
- hourshours1
-
- if( hoursgt23 )
- hourshours-24
-
-
- //end class definition
50Object ??? ????????
- int main(void)
- airtime t1,t2,t3 //create 3 airtime objects
- char choice
- do
- coutltlt For t1,
- t1.set()
- coutltlt For t2,
- t2.set()
- t3.add(t1,t2)
- coutltlt t3
- t3.display()
- coutltlt Do another(y/n)?
- cingtgtchoice
- while(choice!n)
51Object ??? ????????
- ??????????????????????? Object ??????????????????
Object????????????????? ?????????????? - ?????????????????????? ??? Object
- ???????Object ??????????????????????????
- Employee employee_list50
- ????????????????????????? employee_list ???? 50
?????????????????????????? Employee
52Object ??? ????????
- ?????????????????????????object
- int main(void)//???????????airtime
- airtime at20 // array of 20 airtime objects
- char choice
- int n0 //index
- do
- coutltlt Airtime ltltn ltlt. //get time from
user - atn.set() //insert in array
- coutltlt Do another(y/n)?
- cingtgtchoice
- while(choice!n)
- for(int j0jltnj) //display every time
in the - coutltlt \nAirtime ltltjltlt //array
- atj.display()
-
53Object ??? ??????????
- ?????????????????????????????? Object
????????????????????? Function member ??? Object
???????????? - ???????????????????????? ???????? Object
- class Distance
- private
- int feet
- int inches
- public
- void getdist()
- coutltlt \nEnter feet cingtgt feet
- coutltlt Enter incehs cingtgtinches
-
- void showdist()coutltlt feet lt \- ltlt inches
ltlt \
54Object ??? ??????????
- int main(void)
- Distance a_distance
- Distance d_ptr //??????pointer
- d_ptr a_distance //??? d_ptr ????????
object(a_distance) - a_distance.getdist()
- a_distance.showdist()
- d_ptr-gtgetdist()//??? pointer
(d_ptr).getdist() - d_ptr-gtshowdist() //??? pointer
(d_ptr).showdist()
55Object ??? ??????????
- ?????? new ??? delete ??? Object
- ?????????????? new ??????????Object ?????????
delete - int main(void)
- Distance d_ptr //??????pointer
- d_ptr new Distance
- d_ptr-gtgetdist() //??? pointer
(d_ptr).getdist() - d_ptr-gtshowdist() //??? pointer
(d_ptr).showdist() - delete d_ptr
56Generalization ??? Inheritance
- Generalization ???????????????????????Class???Clas
s ??????Class ?????????? Attribute ???/????
Operation ????????? - Generalization ???????????????? Hierarchy???
Abstraction ??????????????? Generalization
??????? hierarchy of abstraction ???? Subclass
????????????????? superclass. - Generalization ?????????????(transitive)
??????????????????????????? ??is a kind of
57Generalization ??? Inheritance
- Subclass/derived class ??????????????????????????
?? Superclass/parent class/base class - attributes
- operations
- relationships
- ??????????????????????? Superclass
Subclass??????Superclass??????????????????????????
????????????Superclass????
58???????????? Single and Multiple Inheritance
59???????????? Single inheritance
- ???????????? Single inheritance ??????????
Subclass ?? Parent ???? Superclass ????? Class
????? ??????????????????? Subclass ?????????????
Class(superclass) ????? - ??????????????? Single inheritance ??? public
- class subclass public superclass
- //member of subclass
-
- ???????????????????? Single inheritance
???????????????????????????? public ????????
60???????????? Single inheritance
- class employee //superclass/base class
- public
- employee(char , char , float)
- void show_employee(void)
- private
- char name64
- char position64
- float salary
-
- ???????????????????????? Class ???? manage ????
Class manager ????????????????????????????????????
?
61???????????? Single inheritance
- float annual_bonus
- char company_car
- int stock_options
- class manager public employee//code?????????inh
eritances - public
- manager(char, char,char ,float, float,int)
- void show_manger(void)
- private
- float annual_bonus
- char company_car
- int stock_options
62???????????? Single inheritance
- ???????????????
- includeltiostream.hgt
- includeltstring.hgt
- class employee //superclass/base class
- public
- employee(char , char , float)
- void show_employee(void)
- private
- char name64
- char position64
- float salary
63???????????? Single inheritance
- employeeemployee(char name,char
position,float salary) - strcpy(employeename,name)
- strcpy(employeeposition,position)
- employeesalarysalary
-
- void employeeshow_employee(void)
- coutltlt Name ltlt name ltltendl
- coutltlt Position ltlt position ltltendl
- coutltlt Salary ltlt salary ltltendl
64???????????? Single inheritance
- class manager public employee
- public
- manager(char, char,char ,float, float,int)
- void show_manger(void)
- private
- float annual_bonus
- char company_car
- int stock_options
-
65???????????? Single inheritance
- managermanager(char name,char position,
- char company_car,float salary,float bonus,int
stock_options) employee(name,position,salary) - strcpy(managercompany_car,company_car)
- managerannual_bonusbonus
- managerstock_optionsstock_options
-
- void managershow_manager(void)
- show_employee()
- coutltlt Company car ltlt company_car ltltendl
- coutltlt Annual bonus ltlt bonus ltltendl
- coutltlt Stock options ltlt stock_options ltltendl
superclass constructor
superclass method
66???????????? Single inheritance
- int main(void)
- employee worker(John Dump, Programmer,
35000) - manager boss(Jim Dump, VP, Toyota,50000.0,
5000,1000) - worker.show_employee()
- boss.show_manager()
67Protected Member
- ???????????????????? Subclass ????????????????????
?????????? Public member ??? Superclass???????
Subclass ????????????????????????????? Private
member ??? Superclass ????????? - ?????????????? Member ??? Superclass ???????
protected ???????????? private ??????? Subclass
???????????????????????? Protected member
???Class ?????????????????Subclass
?????????????????????
68Protected Member
- ??????????????????????????? protected
- class manager public employee
- public
- manager(char, char,char ,float, float,int)
- void show_manger(void)
- protected
- float annual_bonus
- char company_car
- int stock_options
69???????????? Multiple inheritance
- ???????????? Single inheritance
???????????????????????Class????(subclass/derived
class)?????????????????Superclass????????
?????????????????????Class?????????????????????Sup
erclass ?????????? Multiple inheritance - ??????????????? Multiple inheritance ??? public
- class subclass public superclass1, public
superclas2 - //member of subclass
-
???????????????????????????? public ????????
70???????????? Multiple inheritance
- ???????????????????? Multiple inheritance
-
mother_board
computer_screen
Computer
71???????????? Multiple inheritance
- class computer_screen
- public
- computer_screen(char ,long, int, int)
- void show_screen(void)
- private
- char type32
- long colors
- int x_resolution
- int y_resolution
72???????????? Multiple inheritance
- class mother_board
- public
- mother_board(int, int, int)
- void show_ mother_board(void)
- private
- int processor
- int speed
- int RAM
73???????????? Multiple inheritance
- class computer public computer_screen,public
mother_board - public
- computer(char, int, float, char, long, int,
int, int, int,int) - void show_ computer(void)
- private
- char name64
- int hard_disk
- float floppy
Super classes/base classes
74???????????? Multiple inheritance
- includeltiostream.hgt
- inculdeltstring.hgt
- class computer_screen
- public
- computer_screen(char, long, int, int)
- void show_screen(void)
- private
- char type32
- long colors
- int x_resolution
- int y_resolution
75???????????? Multiple inheritance
- computer_screencomputer_screen(char type, long
colors, - int x_res, int y_res)
- strcpy(computer_screentype, type)
- computer_screencolorscolors
- computer_screenx_resx_res
- computer_screeny_resy_res
-
- void computer_screenshow_screen(void)
- coutltlt Screen type ltlt type ltltendl
- coutltlt Colors ltlt colors ltltendl
- coutltlt Resolution ltlt x_res ltlt by
- ltlt y_res ltltendl
76???????????? Multiple inheritance
- class mother_board
- public
- mother_board(int, int, int)
- void show_ mother_board(void)
- private
- int processor
- int speed
- int RAM
-
77???????????? Multiple inheritance
- mother_board mother_board(int processor,int
speed,int RAM - mother_boardprocessorprocessor
- mother_boardspeedspeed
- mother_boardRAMRAM
-
- void mother_boardshow_mother_board(void)
- coutltlt Processor ltlt processor ltltendl
- coutltlt Speed ltlt speed ltltendl
- coutltlt RAM ltlt RAM ltltendl
78???????????? Multiple inheritance
- class computer public computer_screen,public
mother_board - public
- computer(char, int, float, char, long, int,
int, int, int,int) - void show_ computer(void)
- private
- char name64
- int hard_disk
- float floppy
79???????????? Multiple inheritance
- computercomputer(char name, int hard_disk,
float floppy, char screen, long
colors, int x_res, - int y_res, int processor, int
speed,int RAM) - computer_screen(screen,colors,x_res,y_res),
- mother_board(processor,speed,RAM)
- strcpy(computername,name)
- computerhard_diskhard_disk
- computerfloppyfloppy
80???????????? Multiple inheritance
- computershow_computer(void)
- coutltlt Type ltlt type ltltendl
- coutltlt Hard Disk ltlt hard_disk ltltendl
- coutltlt Floppy Disk ltlt floppy ltltendl
- show_mother_board()
- show_screen()
-
- int main(void)
- computer my_pc(Compaq, 212, 1.44, SVGA,
16000000, - 640, 480, 486, 66, 8)
- my_pc.show_computer()