Академический Документы
Профессиональный Документы
Культура Документы
If
Introduction to
Object-Oriented
Programming
Lecture 1
TCP1201 OOPDS
Learning Objectives
To
What is an object?
An object is a computer representation
of real-world person, place, event or
anything in the problem that we are
solving.
What is an object?
An
attributes/data/states/fields/variables (typically
noun), e.g. name, date, balance, size, mark, etc.
behaviors/procedures/methods/operations/function
s (typically verb), e.g. eat, drive, set, get, push, etc.
We
The
Object-Oriented
Programming
idea is to design a program to solve a problem by combining both the data and
procedures (member functions) that operate on that data into a singe unit called object.
Object2
Popular
Object1
Member
Function
Object3
Data
Data
Member
Function
Member
Function
Messages
Object1
Member
Function
Object3
Data
Data
Member
Function
Member
Function
Messages
10
4 Principles of OOP
1.
2.
3.
4.
OOP Principle
Abstraction
Assume
OOP in Practice
Abstraction
Student
Attributes
id
subjects
Methods
show_subjects
register_subject
withdraw_subject
13
Based
The
OOP in Practice
Abstraction
on what we collected and consider appropriate data type, the following class definition can be declared:
int id;
vector<string> subjects;
void show_subjects();
void register_subject();
void withdraw_subject();
};
14
OOP Principle
Encapsulation
Encapsulation
15
16
public members
Accessible from
anywhere including
outside the class.
We generally declare
methods as public.
By default, all
members of a C++
struct are declared
as public if
unspecified.
17
OOP Principle
Encapsulation
To
Since
The
OOP in Practice
Encapsulation
all our methods are meant to be used from outside the class, we should declare them as public.
class
Student {
next step is to consider where to place the implementation.
// private by default
int id;
vector<string> subjects;
public: // public from now on
int getId(); // get method for id
void setId (int id); // set method for id
void show_subjects();
void register_subject();
void withdraw_subject();
};
19
Placing Implementation
There
class Student {
int id;
...
int getId(); //prototype
void setId (int id); //prototype
...
}; // End of class
int Student::getId() {
return id;
}
void Student::setId (int id) {
this->id = id;
20
}
Placing Implementation
Outside of Class Declaration
"::"
21
We
int id;
...
void setId (int id); //prototype
...
}; // End of class
...
void Student::setId (int id) {
this->id = id;
}
id here is attribute
We
class Student {
int id;
...
void setId (int id); //prototype
...
}; // End of class
...
void Student::setId (int id) {
id = id; // Wrong, "parameter = parameter".
// Attribute id is not updated.
this->id = id; // Correct, "attribute = parameter".
Student::id = id; // Correct, "attribute = parameter".
}
23
By
...
// Ensure id entered is within range.
void Student::setId (int id) {
if (id < 1000000000 || id > 9999999999) {
cout << "Error: Id out of range.\n";
this->id = 0;
}
else
this->id = id;
}
24
The
26
27
Why Encapsulation?
1.
2.
3.
28
Categorizing Objects
There
29
Categorizing by Composition
Sales Dept.
Personnel
Staff
Sales Data
Sales
Staff
Personnel Dept.
Finance Dept.
Finance Data
Messages
Finance
Staff
30
Categorizing by Composition
Sample
class.
Composition, attribute is
a class by itself.
// Also composition.
// Also composition.
// Also composition.
31
Class Diagram
Student
-id:int
-subjects:string[*]
+getId():int
+setId(id:int):void
+show_subjects():void
+register_subject():void
+withdraw_subject():void
33
Class Diagram
Faculty
Student
-name:string
-students:Student[*]
+intake()
Association
1..n
'n' means
many
-id: int
-subjects:string[*]
+getId():int
+setId(id:int):void
+show_subjects():void
+register_subject():void
+withdraw_subject():void
Revisiting Procedural
Programming
Main Program
Data
Function1
Function2
Function3
35
struct Student {
int id;
vector<string> subjects;
};
void register_subject
(Student& a)
{ ... }
void withdraw_subject
(Student& s)
{ ... }
int main() {
Student s;
register_subject (s);
withdraw_subject (s);
}
36
Why Object-Oriented
Programming?
Converting Procedural
Program to OOP
1.
2.
3.
// Procedural version
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
struct Point {
int x, y; // public by default
};
Point readPoint();
void printPoint (Point);
double distPoint (Point, Point);
int main() {
Point p1 = readPoint();
cout << "p1 = ";
printPoint (p1);
cout << endl;
Point p2 = readPoint();
cout << "p2 = ";
printPoint (p2);
cout << endl << endl;
double d = distPoint (p1, p2);
cout << "Distance p1 to p2 = "
<< d << endl;
}
Point readPoint() {
Point p;
cout << "Enter point x y : ";
cin >> p.x >> p.y;
return p;
}
void printPoint (Point p) {
cout << "(x = " << p.x
<< ", y = "<< p.y
<< ")" << endl;
}
double distPoint (Point p, Point q)
{
double dx = p.x - q.x;
double dy = p.y - q.y;
double dsquared = dx*dx + dy*dy;
return sqrt (dsquared);
}
39
// OOP version
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
class Point {
int x, y; // private by default
public:
void readPoint();
void printPoint();
double distPoint (Point q);
};
void Point::readPoint() {
cout << "Enter point x y : ";
cin >> x >> y;
}
void Point::printPoint() {
cout << "(x = " << x
<< ", y = " << y
<< ")" << endl;
}