A Paradigm (para-dime) is a framework containing the basic...

40

Transcript of A Paradigm (para-dime) is a framework containing the basic...

Page 1: A Paradigm (para-dime) is a framework containing the basic ...cs.uky.edu/~kwjoiner/cs215/notes/OOP.pdf · - Start with one procedure (function) that represents the entire program
Page 2: A Paradigm (para-dime) is a framework containing the basic ...cs.uky.edu/~kwjoiner/cs215/notes/OOP.pdf · - Start with one procedure (function) that represents the entire program

A Paradigm (para-dime) is a framework containing the basic assumptions, ways of thinking, and

methodology

that are commonly accepted by members of a scientific community.

Page 3: A Paradigm (para-dime) is a framework containing the basic ...cs.uky.edu/~kwjoiner/cs215/notes/OOP.pdf · - Start with one procedure (function) that represents the entire program

So far this semester, we've been concentrating on the POP Paradigm

Page 4: A Paradigm (para-dime) is a framework containing the basic ...cs.uky.edu/~kwjoiner/cs215/notes/OOP.pdf · - Start with one procedure (function) that represents the entire program

- Start with one procedure (function) that represents the entire

program

- Identify/specify a few sub-steps of the one procedure

- Repeat this refinement process for each procedure until each

procedure is simplistic (performs one specific task).

Each procedure something, and that is usually reflected in its name as a verb.

Page 5: A Paradigm (para-dime) is a framework containing the basic ...cs.uky.edu/~kwjoiner/cs215/notes/OOP.pdf · - Start with one procedure (function) that represents the entire program
Page 6: A Paradigm (para-dime) is a framework containing the basic ...cs.uky.edu/~kwjoiner/cs215/notes/OOP.pdf · - Start with one procedure (function) that represents the entire program

A software design paradigm that focuses on building models of

to solve a programming problem(instead of focusing on )

Page 7: A Paradigm (para-dime) is a framework containing the basic ...cs.uky.edu/~kwjoiner/cs215/notes/OOP.pdf · - Start with one procedure (function) that represents the entire program

1. Identify the "natural" Objects in the project description

2. For each Object, identify its:

- Name

- Data Elements

- Operations to/for/with/on ONE object

3. Design the interaction/communication between objects.

Page 8: A Paradigm (para-dime) is a framework containing the basic ...cs.uky.edu/~kwjoiner/cs215/notes/OOP.pdf · - Start with one procedure (function) that represents the entire program

An Object is composed of:

and

Page 9: A Paradigm (para-dime) is a framework containing the basic ...cs.uky.edu/~kwjoiner/cs215/notes/OOP.pdf · - Start with one procedure (function) that represents the entire program

May be simple pieces of data

(number, character, string, etc.)

OR other objects.

They are called:

- in C++

- in Java

Page 10: A Paradigm (para-dime) is a framework containing the basic ...cs.uky.edu/~kwjoiner/cs215/notes/OOP.pdf · - Start with one procedure (function) that represents the entire program

perform actions on the data elements

and/or

communicate with other objects

They are called:

- in C++

- in Java

Page 11: A Paradigm (para-dime) is a framework containing the basic ...cs.uky.edu/~kwjoiner/cs215/notes/OOP.pdf · - Start with one procedure (function) that represents the entire program

is a of an Object, including:

these define what "the outside world"

may directly access.

Page 12: A Paradigm (para-dime) is a framework containing the basic ...cs.uky.edu/~kwjoiner/cs215/notes/OOP.pdf · - Start with one procedure (function) that represents the entire program

A is to an

as a

is to a

as a

is to a

Page 13: A Paradigm (para-dime) is a framework containing the basic ...cs.uky.edu/~kwjoiner/cs215/notes/OOP.pdf · - Start with one procedure (function) that represents the entire program
Page 14: A Paradigm (para-dime) is a framework containing the basic ...cs.uky.edu/~kwjoiner/cs215/notes/OOP.pdf · - Start with one procedure (function) that represents the entire program

is divided into two parts:

declarative information

usually written in .h file

(called a Header File)

- Function Member declarations

usually written in a .cpp file

Page 15: A Paradigm (para-dime) is a framework containing the basic ...cs.uky.edu/~kwjoiner/cs215/notes/OOP.pdf · - Start with one procedure (function) that represents the entire program

class {

public:

private:

};

Page 16: A Paradigm (para-dime) is a framework containing the basic ...cs.uky.edu/~kwjoiner/cs215/notes/OOP.pdf · - Start with one procedure (function) that represents the entire program

is the same as a variable declaration

class customer {

public:

string firstName, lastname;

private:

int age;

float shoesize;

};

Page 17: A Paradigm (para-dime) is a framework containing the basic ...cs.uky.edu/~kwjoiner/cs215/notes/OOP.pdf · - Start with one procedure (function) that represents the entire program

same as the header line of a function declaration,with a semicolon on the end.

class {

public:

void print(int x)

private:

int getStuff()

float calcStuff(float x, float y)

};

Page 18: A Paradigm (para-dime) is a framework containing the basic ...cs.uky.edu/~kwjoiner/cs215/notes/OOP.pdf · - Start with one procedure (function) that represents the entire program

in a file called class fraction {

public:

void setNumer(int n);

void setDenom(int d);

int getNumer();

int getDenom();

float getDecimal();

private:

int numer, denom;

};

Page 19: A Paradigm (para-dime) is a framework containing the basic ...cs.uky.edu/~kwjoiner/cs215/notes/OOP.pdf · - Start with one procedure (function) that represents the entire program

Remember from functions that a is a to the compiler that we will write a function,

but it is declared elsewhere.

The Implementation "fulfills" the promises made in the Interface.

Page 20: A Paradigm (para-dime) is a framework containing the basic ...cs.uky.edu/~kwjoiner/cs215/notes/OOP.pdf · - Start with one procedure (function) that represents the entire program

For each function member prototyped in the interface:

:: ( )

{

}

Page 21: A Paradigm (para-dime) is a framework containing the basic ...cs.uky.edu/~kwjoiner/cs215/notes/OOP.pdf · - Start with one procedure (function) that represents the entire program

in a file called void fraction::set(int n, int d) {

}

void fraction::getNumer(){

}

void fraction::getDenom(){

}

float fraction::getDecimal(){

}

void fraction::reduce() {

}

Page 22: A Paradigm (para-dime) is a framework containing the basic ...cs.uky.edu/~kwjoiner/cs215/notes/OOP.pdf · - Start with one procedure (function) that represents the entire program

Within all Function Members of a class,

all Data Members have

(but also can be !)

Page 23: A Paradigm (para-dime) is a framework containing the basic ...cs.uky.edu/~kwjoiner/cs215/notes/OOP.pdf · - Start with one procedure (function) that represents the entire program

void fraction::set(int n, int d) {

numer = n; // data mem = arg

denom = d; // data mem = arg

}

float fraction::getDecimal(){

// localVar = expression w/ data mems

float dec = (1.0 * numer)/denom;

return dec;

}

Page 24: A Paradigm (para-dime) is a framework containing the basic ...cs.uky.edu/~kwjoiner/cs215/notes/OOP.pdf · - Start with one procedure (function) that represents the entire program

A function that is not a Method of a class.

It does not have in front of its name.

int largest(int a, int b) {

if (a > b)

return a;

else

return b;

}

Page 25: A Paradigm (para-dime) is a framework containing the basic ...cs.uky.edu/~kwjoiner/cs215/notes/OOP.pdf · - Start with one procedure (function) that represents the entire program

After all that work...what have we accomplished??

Essentially, we've invented a new ...except it is more complex than a Type, and so we call it a .

Now the class name can be used the exact same way as a Type (ex: any way is used).

Page 26: A Paradigm (para-dime) is a framework containing the basic ...cs.uky.edu/~kwjoiner/cs215/notes/OOP.pdf · - Start with one procedure (function) that represents the entire program

void funstuff() {

// allocate 3 of , nothing new

double a,b,c;

// allocate 3 of

fraction f1, f2, f3;

}

Page 27: A Paradigm (para-dime) is a framework containing the basic ...cs.uky.edu/~kwjoiner/cs215/notes/OOP.pdf · - Start with one procedure (function) that represents the entire program
Page 28: A Paradigm (para-dime) is a framework containing the basic ...cs.uky.edu/~kwjoiner/cs215/notes/OOP.pdf · - Start with one procedure (function) that represents the entire program
Page 29: A Paradigm (para-dime) is a framework containing the basic ...cs.uky.edu/~kwjoiner/cs215/notes/OOP.pdf · - Start with one procedure (function) that represents the entire program

void funstuff() {

fraction f1;

f1.numer=1; //error: numer is private

f1.denom=0; //error: denom is private

f1.set(2,3);

float d = f1.getDecimal();

cout << d; // prints 0.66666

}

Page 30: A Paradigm (para-dime) is a framework containing the basic ...cs.uky.edu/~kwjoiner/cs215/notes/OOP.pdf · - Start with one procedure (function) that represents the entire program

are (special) function members where:

- their name is exactly the same as the class name

- they do not have a return type

- they must always be public

- they are automatically invoked whenever an

object is allocated (declared).

to initialize data members at the time they are created.

Page 31: A Paradigm (para-dime) is a framework containing the basic ...cs.uky.edu/~kwjoiner/cs215/notes/OOP.pdf · - Start with one procedure (function) that represents the entire program

class fraction {

public: fraction(); // constructor prototype

private: int numer; int denom;

};

fraction::fraction() { // constructor declaration

numer = 0;

denom = 1;

}

Page 32: A Paradigm (para-dime) is a framework containing the basic ...cs.uky.edu/~kwjoiner/cs215/notes/OOP.pdf · - Start with one procedure (function) that represents the entire program

void main() {

fraction f; // constructor executed NOW!

// f.numer = 0, f.denom = 1

fraction fr[3]; // 5 constructors exec. NOW!

// fr[0].numer=0, fr[0].denom=1

// fr[1].numer=0, fr[1].denom=1

// fr[2].numer=0, fr[2].denom=1

}

Page 33: A Paradigm (para-dime) is a framework containing the basic ...cs.uky.edu/~kwjoiner/cs215/notes/OOP.pdf · - Start with one procedure (function) that represents the entire program

All Data Members should be (unless there is a very good reason to make them public)

Code and Function Members

for each Data Member

to control access from the "outside world"

Page 34: A Paradigm (para-dime) is a framework containing the basic ...cs.uky.edu/~kwjoiner/cs215/notes/OOP.pdf · - Start with one procedure (function) that represents the entire program

Return a COPY of the data member

Page 35: A Paradigm (para-dime) is a framework containing the basic ...cs.uky.edu/~kwjoiner/cs215/notes/OOP.pdf · - Start with one procedure (function) that represents the entire program

class fraction {

public: getNumer(); // return type is the same

private: numer; // as the data member

};

int fraction::getNumer() {

return numer;

}

Page 36: A Paradigm (para-dime) is a framework containing the basic ...cs.uky.edu/~kwjoiner/cs215/notes/OOP.pdf · - Start with one procedure (function) that represents the entire program

Pass the new value in as an argument

Assign the argument to the data member

if the argument's value is valid

Page 37: A Paradigm (para-dime) is a framework containing the basic ...cs.uky.edu/~kwjoiner/cs215/notes/OOP.pdf · - Start with one procedure (function) that represents the entire program

class fraction {public: void setDenom( newDen); // argument typeprivate: denom; // same as member type

};void fraction::setDenom(int newDen) {

if (newDen > 0) { // denom should be > 0denom = newDen;

}// else print error, do something else, or do nothing

}

Page 38: A Paradigm (para-dime) is a framework containing the basic ...cs.uky.edu/~kwjoiner/cs215/notes/OOP.pdf · - Start with one procedure (function) that represents the entire program

//in a file called class fraction {

public:

fraction();

void setNumer(int n);

void setDenom(int d);

int getNumer();

int getDenom();

private:

int numer, denom;

};

Page 39: A Paradigm (para-dime) is a framework containing the basic ...cs.uky.edu/~kwjoiner/cs215/notes/OOP.pdf · - Start with one procedure (function) that represents the entire program

//in a file called fraction::fraction() { numer=0; denom=1; }

int getNumer() { return numer; }

int getDenom() { return denom; }

void setNumer(int n){ numer = n; }

void setDenom(int d) {

if (d > 0) {

denom = d;

}

}

Page 40: A Paradigm (para-dime) is a framework containing the basic ...cs.uky.edu/~kwjoiner/cs215/notes/OOP.pdf · - Start with one procedure (function) that represents the entire program

Term Definition

Class a description of an object, including a name, list of data members, list of function members(methods), and the access levels of each.

Object allocated instantiation of a Class, containing the members and methods defined in the Class with the designated access levels.

Public designates data members and methods that CAN be directly accessed by the code using the object

Private designates data members and methods that can NOT be directly access by the code using the object.

Paradigm A framework containing the basic assumptions, ways of thinking, and methodology that are commonly accepted by members of a scientific community.

Members Data elements of a class/object.

Methods Functions/operations of a class/object.

Interface Declarative portion of a class: defines the name, members, list of methods, and their access levels.

Implementation Coding of the Methods of a class; where the methods are written.

Constructor Function member with the same name as the class, used to initialize objects of the class.

Free Function A function that is not part of a class.