IUST
Fall 2018
But the developer must still think of computer structures (even with a high level language)
It is better for the programmer to think about the problem structure than the structure of the computer
Programming = Creating a software solution for a real issue
● Example: Software for managing books and members in a library
Problem space
Solution Space
Programming = An attempt to map the problem space and solution space
Objects in Library Problem:
A book,
A member
A shelf and ...
Object Oriented Approach
Object:
entities that are in the space of the problem and in the solution space (programs) are also seen
Object-Oriented Programming Elements: Objects + Message Exchange between Objects
Procedural programming elements: functions + variables + function call
The Difference Between Thinking at Programming Time:
Thinking about objects and the relationship between objects: the object-oriented method
Thinking about memory, computer structure and ...: procedural approach
Object-oriented programming summarizes in five main features: "Allan Key"
4. Each object has one type
5. All objects of a particular type can receive similar messages
Description of the booch from the object
An object consisting of status, behavior, and identity
Booch has added a description of "identity".
Each object has its own characteristics That state determines the object
For example: A library member has features such as name, age, etc.
Each object has methods It shows behaviors
For example: behavior "borrowing the book" for the object "Member"
And each object is unique: it has a distinct identity
even if there are two objects have the same "status"
have unique address in memory
Existing objects in the problem space
Library:
Member Mr."Ali Alavi", "Shahnameh" Book.
Football game:
Ali Karimi, Farhad Majidi, Azadi Stadium, Ball
Banking Program:
Maryam Alavi (customer), Azadi Branch
Type, category, or batch of objects that have similar "Behavior" and "Attributes"
Each class has several instances
Each class defines behavior and attributes (Property)
Any object in this class has these features and behaviors
Features: Name, age, occupation
Behaviors: borrowing books
Light lt = new Light();
lt.On();UML class diagram for light bulb
Person personl = new Person();
personl.Name = "Taghi Taghavi";
personl.PhoneNumber = 66166601;
personl.ShowInformation();
| Person |
|---|
| -name : String -PhoneNumber : int |
| + ShowInformation() : void + Name.set() : void + Name.get() : String + PhoneNumber.set() : void + PhoneNumber.get() : String |
In class design: we can limit access to parts of the class
● Public and private in class
● Private sections are hidden from the viewers (implementation hiding)
Encapsulation: defining features and behaviors + hiding implementation
// Class Declaration
public class Dog
{
private String name;
public void SetName(String n)
{
name = n;
}
public void Bark()
{
Console.WriteLine("Hop! Hop!");
}
}
// Object Creation, Instantiation
Dog d = new Dog();
// Change state of object
d.SetName("Fido");
// Message passing
d.Bark();What is variable "d"?
A reference to the object.
Person p = new Person();
p.SetName("Ali Hosseini");
Book b1 = new Book();
b1.SetTitle("Shahname");
p.Borrow(b1);
String s = p.name;
p.Age = 22;Dot
Dog d1 = new Dog();
d1.Name = "Belle";
d1.Age = 3;Dog d2 = new Dog();
d2.Name = "Fido";
d2.Age = 4;| Belle |
|---|
| 3 |
| Fido |
| 4 |
Reminder:
Every object has a state, behavior, and identity
String s = new String();
Dog d = new Dog();
Rectangle r = new Rectangle();
StreamReader sr = new StreamReader();new int(); //ERROR
int a = 5;
a.value = 6; //ERROR
String str;str= new String(); //Refer to new object
str= name; //Refer objectString name = new String("Ali");Ali
name
name
Ali