Procedure v.s. Object

22
03/14/22 ITK 279 @ Chung-Chih Li 1 Procedure v.s. Object write a program is to: find a way to manipulate data -- We design procedures Procedure-Oriented Programming Statements + functions programs find data to manipulate – We choose data (object) Object-Oriented Programming Classes + Objects programs (interfaces, methods, attributes…)

description

To write a program is to:. Procedure v.s. Object. find a way to manipulate data -- We design procedures Procedure-Oriented Programming Statements + functions  programs. find data to manipulate – We choose data (object) Object-Oriented Programming Classes + Objects  programs - PowerPoint PPT Presentation

Transcript of Procedure v.s. Object

Page 1: Procedure  v.s. Object

04/19/23ITK 279 @ Chung-Chih Li 1

Procedure v.s. ObjectTo write a program is to:

find a way to manipulate data --

• We design procedures • Procedure-Oriented Programming

Statements + functions programs

find data to manipulate –

• We choose data (object) • Object-Oriented Programming

Classes + Objects programs(interfaces, methods, attributes…)

Page 2: Procedure  v.s. Object

04/19/23ITK 279 @ Chung-Chih Li 2

Class ObjectsVehicle

SUV

011-JAV

instantiation

Honda Pilot

instantiation

000-AAA

Object

Number

IntegerDouble

I J

Integer I,J;I = new Integer(3);J = new Integer(9);

int i,j;i = Integer.parseInt(“1”);j = I.parseInt(“1”);

Drivethis

not theseChevrolet Tahoe

Page 3: Procedure  v.s. Object

04/19/23ITK 279 @ Chung-Chih Li 3

Method & Attribute Class:

Method (or, member function): behaviors, operation, functionalities.

Like a verb, its definition is the behavior.E.g., accelerate, start, stop, turn, lock

Attribute (or, Field): the properties the class

Like a noun, its value is an adjective. E.g., wheels, seats, engine, windows, color

Page 4: Procedure  v.s. Object

04/19/23ITK 279 @ Chung-Chih Li 4

Point Class:

A Point needs to have: x, y.

Or we may want to know:distance from the origin

distance to another point, or,

shift, get the value of x, get the value of y,

change the value of x and y.

Page 5: Procedure  v.s. Object

04/19/23ITK 279 @ Chung-Chih Li 5

Old fashion: struct

struct point{ double x; double y;};

void main(){ point p,q; double a,b,distance; p.x = 3.0; p.y = 4.0; q.x = q.y = 5.4; a = p.x – q.x; b = p.y – q.y;

distance = sqrt(a*a+ b*b);

cout << “(“ << p.x << “,” << p.y << “):”; cout << “(“ << q.x << “,” << q.y << “) == ” << dis; p.x = p.x+2; p.y = p.y+3; // move (2,3)....}

Page 6: Procedure  v.s. Object

04/19/23ITK 279 @ Chung-Chih Li 6

C++ : classclass point{public: // public means every one can use the following. double x; double y; double distance(point p); double from_org(); void shift(point v);};....

void main(){ point p,q;

p.x = 3.0; p.y = 4.0; q.x = q.y = 5.4; cout << ........ << p.distance(q); p.shift(q); }

member variables

member functions

Page 7: Procedure  v.s. Object

04/19/23ITK 279 @ Chung-Chih Li 7

A better designed class

class point{public: void set_x(double x); void set_y(double y); double get_x(); double get_y(); double distance(point p); double from_org(); void shift(point v);private: double my_x; double my_y; double diff(double x, double y); // this is just a helper};....

public section

private section

// private means no one but the class’s function can use

Page 8: Procedure  v.s. Object

04/19/23ITK 279 @ Chung-Chih Li 8

Using member functions

point p,q,v;

p.set_x(5); p.set_y(5); q.set_x(2); q.set_y(3); v.set_x(1); v.set_y(-1.2);

cout << p.get_x(); cout << q.get_x();

q.move(v); cout << p.from_org() << “:” << q.from_org();

double distance = p.distance(v);............. distance = q.distnace(p);

Mutator: a member function that can change the values of

member variables

Accessor: a member function that provide an access to

member variables

Page 9: Procedure  v.s. Object

04/19/23ITK 279 @ Chung-Chih Li 9

Private variables and functions can’t be used from outside

class point{public:....................private: double my_x; double my_y; double diff(double x, double y); // this is just a helper};....

void main(){ point p,q,v; double a

cout << p.my_x; cout << q.my_y; a = p.diff(3.2, 4);}

// diff, my_x, and my_y are private

Page 10: Procedure  v.s. Object

04/19/23ITK 279 @ Chung-Chih Li 10

2 Objects of a class

P:

set_x(.)...set_y(.)...from_org(.)..........

point p,q;

p.set_x(4); p.set_y(5); q.set_x(2); q.set_y(3); cout << p.from_org() << “:” << q.from_org();

4

5

my_x

my_y

q:

set_x(.)...set_y(.)...from_org(.)..........

2

3

my_x

my_y

constructconstruct

p’s member function call q’s member

function call

Page 11: Procedure  v.s. Object

04/19/23ITK 279 @ Chung-Chih Li 11

Implementation of class

class point{public: void set_x(double x); void set_y(double y); double get_x(); double get_y(); double distance(point p); double from_org(); void shift(point v);private: double my_x; double my_y; double diff(double x, double y); // this is just a helper};

void point::set_x(double a){

my_x = a;}

double point::get_y() { return my_y; }

name of the member

name of the class

Page 12: Procedure  v.s. Object

04/19/23ITK 279 @ Chung-Chih Li 12

Implementation of pointvoid point::set_x(double a){

my_x = a;}

void point::set_y(double a){

my_y = a;}

double point::get_x() {

return my_x; }

double point::get_y() {

return my_y; }

Mutator: a member function that can change the values of

member variables

Accessor: a member function that provide an access to

member variables

Page 13: Procedure  v.s. Object

04/19/23ITK 279 @ Chung-Chih Li 13

Private member function

double point::distance(point p){

double a,b,c;

a = diff(my_x, p.my_x);b = diff(my_y, p.my_y);c = sqrt(a*a+b*b);return c;

}

double point::from_org(){

point o;o.set_x(0);o.set_y(0);return distance(o);

}

Using its own private member function

Using its own member function

Using an o’s member function

Page 14: Procedure  v.s. Object

04/19/23ITK 279 @ Chung-Chih Li 14

Other’s private variables and functions: They are in the same class, so can be used.

double point::from_org(){

point p;p.set_x(0);p.set_y(0);

double a,b,c; a = p.diff(my_x, p.my_x); a = a*a; b = diff(my_y, p.my_y); b = b*b; c =sqrt(a+b);

return c;} Using own private

member function

Using p’s private member function

Using p’s private variable

Page 15: Procedure  v.s. Object

04/19/23ITK 279 @ Chung-Chih Li 15

Using the class

void printp(point p){ cout << "(" << p.get_x() << "," << p.get_y() << ")"; }

int main(){ point p,q,v; p.set_x(5); p.set_y(6); q.set_x(1); q.set_y(1); printp(p); cout << " to (0,0) :: " << p.from_org() << endl; printp(q); cout << " to (0,0) :: " << q.from_org() << endl << endl; for (int i = 0; i<4; i++) {

v.set_x(i); v.set_y(i);q.shift(v);printp(q);cout << " Distance to p = " << q.distance(p)

<< " = " << p.distance(q) << endl; } return 0;}

(5,6) to (0,0) :: 7.81025(1,1) to (0,0) :: 1.41421

(1,1) Distance to p = 6.40312 = 6.40312(2,2) Distance to p = 5 = 5(4,4) Distance to p = 2.23607 = 2.23607(7,7) Distance to p = 2.23607 = 2.23607

Page 16: Procedure  v.s. Object

04/19/23ITK 279 @ Chung-Chih Li 16

Constructor

int main(){ point p,q;

..... ......} P:

set_x(.)...set_y(.)...from_org(.)..........

#$?

#$?

my_x

my_y

q:

set_x(.)...set_y(.)...from_org(.)..........

*&?

$#?

my_x

my_y

Compiler will create a default constructor; but can’t take care of too many details like...

Page 17: Procedure  v.s. Object

04/19/23ITK 279 @ Chung-Chih Li 17

Explicitly ask the Constructor to do somethingint main(){ point p,q;

..... ......} P:

set_x(.)...set_y(.)...from_org(.)..........

0

0

my_x

my_y

q:

set_x(.)...set_y(.)...from_org(.)..........

0

0

my_x

my_y

A better constructor should

Page 18: Procedure  v.s. Object

04/19/23ITK 279 @ Chung-Chih Li 18

Explicitly define a default Constructor

class point{public: point (); ......private: ......};

point::point(){ my_x = my_y = 0;}

point a;point b();

0

0

my_x

my_y

a:

.....

.....

0

0

my_x

my_y

b:

.....

.....

Page 19: Procedure  v.s. Object

04/19/23ITK 279 @ Chung-Chih Li 19

Define two Constructorsclass point{public: point (); point (double x, double y); ......private: ......};point::point(){ my_x = my_y = 0;}point::point(double x, double y){ my_x = x; my_y = y;}

point a;point b(7,4);

0

0

my_x

my_y

a:

.....

.....

7

4

my_x

my_y

b:

.....

.....

point::point(double x, double y): my_x(x), my_y(y) {} alternative initialization

default constructor

another constructor

Page 20: Procedure  v.s. Object

04/19/23ITK 279 @ Chung-Chih Li 20

Separate Compiling

class point{public: point(); point(double x, double y); void set_x(double x); void set_y(double y); double get_x(); double get_y(); double distance(point p); double from_org(); void shift(point v); private: double my_x; double my_y; double diff(double x, doubley);};

Header file, (class interface)

points.h

#include <iostream>#include <cmath>#include "points.h"using namespace std;

point::point():my_x(0), my_y(0) {}

point::point(double x, double y):my_x(x), my_y(y) {}

void point::set_x(double a){

my_x = a;}

void point::set_y(double a){

my_y = a;}

double point::get_x() { return my_x; }double point::get_y() { return my_y; }

Class implementation filepoints.cpp

We need this line

Page 21: Procedure  v.s. Object

04/19/23ITK 279 @ Chung-Chih Li 21

Separate Compiling

class point{public: point(); ......private: double my_x; .......};

points.h

$g++ -c points.cpp

Under Unix

Compiling only,generate object code

points.o

.....

#include “points.h”......int main(){ point p; ........}

prog.cpp

$g++ points.o prog.cpp

Compile progr.cpp and link its object code with points.o to create an executable program (a.out).

generates

1

2

...#include "points.h"..........................point::point().......................................

points.cpp

Page 22: Procedure  v.s. Object

04/19/23ITK 279 @ Chung-Chih Li 22

What we won’t cover in this class: • The heart of OOP

1. Inheritance2. Virtual functions3. Polymorphisms

But we will discuss more on:1. Copy constructor2. Destructors3. Assignment operators

Whom to blame: Pointers and dynamic arrays