object-based design/programming
DESCRIPTION
Object-Based Design/Programming. An Informal Introduction and Overview CS340100, NTHU Yoshi. 5. 4,5,6. Let’s Start with an Example. Let x be an Integer int x = 1; How do you “ get the next number ”?. Solution. Write a function next int next( int number) { return number+1; } … - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Object-Based Design/Programming](https://reader036.vdocument.in/reader036/viewer/2022062321/568135ca550346895d9d2ccc/html5/thumbnails/1.jpg)
Object-Based Design/Programming
An Informal Introduction and Overview
CS340100, NTHUYoshi
![Page 2: Object-Based Design/Programming](https://reader036.vdocument.in/reader036/viewer/2022062321/568135ca550346895d9d2ccc/html5/thumbnails/2.jpg)
5
![Page 3: Object-Based Design/Programming](https://reader036.vdocument.in/reader036/viewer/2022062321/568135ca550346895d9d2ccc/html5/thumbnails/3.jpg)
4,5,6
![Page 4: Object-Based Design/Programming](https://reader036.vdocument.in/reader036/viewer/2022062321/568135ca550346895d9d2ccc/html5/thumbnails/4.jpg)
Let’s Start with an Example
• Let x be an Integer– int x = 1;
• How do you “get the next number”?
![Page 5: Object-Based Design/Programming](https://reader036.vdocument.in/reader036/viewer/2022062321/568135ca550346895d9d2ccc/html5/thumbnails/5.jpg)
Solution
• Write a function nextint next(int number) {
return number+1;}…int nextNum = next(x);
Any other way?
![Page 6: Object-Based Design/Programming](https://reader036.vdocument.in/reader036/viewer/2022062321/568135ca550346895d9d2ccc/html5/thumbnails/6.jpg)
Integer as an Object
• Actually, integer can be treated as an “Object”• Why not– int nextNumber = x.next();
What do you get in this example?
![Page 7: Object-Based Design/Programming](https://reader036.vdocument.in/reader036/viewer/2022062321/568135ca550346895d9d2ccc/html5/thumbnails/7.jpg)
Another Example
• Let’s think about “Linked List”• How to get B’s “next node”? i.e., node C
A B C D
![Page 8: Object-Based Design/Programming](https://reader036.vdocument.in/reader036/viewer/2022062321/568135ca550346895d9d2ccc/html5/thumbnails/8.jpg)
Solutions – function next
//Data structure Nodetypedef struct { Node* next; int value;} Node;
//Function nextNode next(Node* node) { return node->next;}
![Page 9: Object-Based Design/Programming](https://reader036.vdocument.in/reader036/viewer/2022062321/568135ca550346895d9d2ccc/html5/thumbnails/9.jpg)
Solution – Object-based
//Blueprint of a nodeclass Node { private int value; private Node nextNode; public Node next() {
return nextNode; }
}…//Method nextb.next();
![Page 10: Object-Based Design/Programming](https://reader036.vdocument.in/reader036/viewer/2022062321/568135ca550346895d9d2ccc/html5/thumbnails/10.jpg)
Object-Based Design
• All the things are individual objects• Three main properties in OOP– Encapsulation– Inheritance– Polymorphism
![Page 11: Object-Based Design/Programming](https://reader036.vdocument.in/reader036/viewer/2022062321/568135ca550346895d9d2ccc/html5/thumbnails/11.jpg)
Give Me Examples
• (You give me)
![Page 12: Object-Based Design/Programming](https://reader036.vdocument.in/reader036/viewer/2022062321/568135ca550346895d9d2ccc/html5/thumbnails/12.jpg)
Let’s See the Descriptions on Wikipedia
• A class is a noun…– A blueprint to create objects, aka object factory– Describes the state and behavior that the objects of the
class all share.– Encapsulates state through data placeholders
called attributes• Attributes = member variables = instance variables
– Encapsulates behavior through reusable sections of code called methods• Behavior = operation = member method = instance method• Usually represents a verb (why?)
![Page 13: Object-Based Design/Programming](https://reader036.vdocument.in/reader036/viewer/2022062321/568135ca550346895d9d2ccc/html5/thumbnails/13.jpg)
Review the class Nodeclass Node { private int value; private Node nextNode; public Node next() {
return nextNode; }
}
See the hierarchy now?
![Page 14: Object-Based Design/Programming](https://reader036.vdocument.in/reader036/viewer/2022062321/568135ca550346895d9d2ccc/html5/thumbnails/14.jpg)
Can you write the class Car now?
![Page 15: Object-Based Design/Programming](https://reader036.vdocument.in/reader036/viewer/2022062321/568135ca550346895d9d2ccc/html5/thumbnails/15.jpg)
Summary
• We have explained Object-Based Design, but we are still far from Object-Oriented Programming (why?)
• We will study OOP and modeling tools (UML) in the following courses
• Also, we will study Design Patterns (subset)
![Page 16: Object-Based Design/Programming](https://reader036.vdocument.in/reader036/viewer/2022062321/568135ca550346895d9d2ccc/html5/thumbnails/16.jpg)
Q & A
![Page 17: Object-Based Design/Programming](https://reader036.vdocument.in/reader036/viewer/2022062321/568135ca550346895d9d2ccc/html5/thumbnails/17.jpg)
Homework
• Use your pencil and paper to write down an example of blueprint and instances, and indicate which parts are attributes and which are operations
![Page 18: Object-Based Design/Programming](https://reader036.vdocument.in/reader036/viewer/2022062321/568135ca550346895d9d2ccc/html5/thumbnails/18.jpg)
References
• http://en.wikipedia.org/wiki/Class_(computer_science)