a paradigm (para-dime) is a framework containing the basic...
TRANSCRIPT
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.
So far this semester, we've been concentrating on the POP Paradigm
- 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.
A software design paradigm that focuses on building models of
to solve a programming problem(instead of focusing on )
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.
An Object is composed of:
and
May be simple pieces of data
(number, character, string, etc.)
OR other objects.
They are called:
- in C++
- in Java
perform actions on the data elements
and/or
communicate with other objects
They are called:
- in C++
- in Java
is a of an Object, including:
these define what "the outside world"
may directly access.
A is to an
as a
is to a
as a
is to a
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
class {
public:
private:
};
is the same as a variable declaration
class customer {
public:
string firstName, lastname;
private:
int age;
float shoesize;
};
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)
};
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;
};
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.
For each function member prototyped in the interface:
:: ( )
{
}
in a file called void fraction::set(int n, int d) {
}
void fraction::getNumer(){
}
void fraction::getDenom(){
}
float fraction::getDecimal(){
}
void fraction::reduce() {
}
Within all Function Members of a class,
all Data Members have
(but also can be !)
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;
}
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;
}
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).
void funstuff() {
// allocate 3 of , nothing new
double a,b,c;
// allocate 3 of
fraction f1, f2, f3;
}
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
}
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.
class fraction {
public: fraction(); // constructor prototype
private: int numer; int denom;
};
fraction::fraction() { // constructor declaration
numer = 0;
denom = 1;
}
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
}
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"
Return a COPY of the data member
class fraction {
public: getNumer(); // return type is the same
private: numer; // as the data member
};
int fraction::getNumer() {
return numer;
}
Pass the new value in as an argument
Assign the argument to the data member
if the argument's value is valid
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
}
//in a file called class fraction {
public:
fraction();
void setNumer(int n);
void setDenom(int d);
int getNumer();
int getDenom();
private:
int numer, denom;
};
//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;
}
}
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.