0% found this document useful (0 votes)
202 views

1.background On OOP

This document discusses the evolution of programming languages from machine language to modern object-oriented languages like C++ and Java. It notes that Simula, designed for simulating systems like factories, introduced the concept of classes and objects. This inspired the development of more general-purpose object-oriented languages. The document outlines some key concepts of object-oriented programming like classes, objects, inheritance, polymorphism, and memory management. It also compares C++ and Java's approaches to memory and object lifetimes.

Uploaded by

4sainadh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
202 views

1.background On OOP

This document discusses the evolution of programming languages from machine language to modern object-oriented languages like C++ and Java. It notes that Simula, designed for simulating systems like factories, introduced the concept of classes and objects. This inspired the development of more general-purpose object-oriented languages. The document outlines some key concepts of object-oriented programming like classes, objects, inheritance, polymorphism, and memory management. It also compares C++ and Java's approaches to memory and object lifetimes.

Uploaded by

4sainadh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 31

Object-Oriented Programming

46-691
MSCF
Carnegie Mellon University
Background on O-O Programming
Patch Panel Programming
Programming Language
Evolution
Machine language
Assembler
3rd generation (COBOL, FORTRAN, C)
Specialized (Lisp, Prolog, APL)
4th generation (SQL, GAMS,
spreadsheets, Mathematica)
5th generation (example, anyone?)
Why So Many Languages?
Bring the language closer to the
problem.
But 4GLs are typically focused on
specialized domains (e.g., relational
databases).
We want a language that is general
purpose, yet can easily be tailored to
any domain.
An inspiration from an odd place
Simula
Designed to simulate job shops, banks, etc.
To code, you create classes (like milling machines)
and instances (machine #1, etc.).
All milling machines have properties (how much time
to make Part #1).
All milling machines have abilities (mill to depth n,
move part to position x).
Job shop operations simulated by making the
machines, and moving material through them.
Whats Cool Here?
Very close to the problem; domain
experts can pitch in easily.
Its possible to make new types of
machines (e.g., drill press).
Description of the shop floor is
transparent:
millingMachine#1. millPart (part#3, depth
2mm)
drillPress#2. idleUntil (Poisson(0.2))
Programming = Simulation?
For a stock market application, we
might write:
value = Stock1.determineValue()
risk = Stock1.determineRisk()
if (Stock1.f(value, risk) > 0.12) then
Stock1.sell()
So Simula inspired more general-
purpose object-oriented languages.
Object-Oriented Languages
Smalltalk, C++, Java, etc
You can make any kind of objects you
want
How different from procedural
languages?
No different at all: Every (reasonable)
language is Turing complete
Very different: Make expression easier, less
error-prone
O-O Languages (Alan Kay)
Everything is an object.
A program is a bunch of objects telling
each other what to do, by sending
messages.
Each object has its own memory, and is
made up of other objects.
Every object has a type (class).
All objects of the same type can receive
the same messages.
Making C++ Work
It's easy as pie to write procedural
code in C++.
It takes some discipline (an attitude) to
think O-O.
It's worthwhile to do it.
Objects
An object has an interface (its powers
and abilities), determined by its class.
A class is an abstract data type, or user-
defined type.
Designing a class means defining its
interface.
Built-In Types
Think of an int
What is its interface?
How do you send it messages?
How do you make one?
Where does it go when youre done with it?
In solving a computational problem, the goal
is to
Dream up useful classes,
Endow them with appropriate characteristics, and
Make some and set them to work.
Example
Suppose Ive defined this class in C++:




To make one, I type
BottleOfBeer myBeer; // compare to int i
This allocates memory on the stack;
myBeer refers to its location.
BottleOfBeer
+open():void
+lif t(height:int):void
+tilt(angle:int):void
Example (cont.)
If I want many beers, I might say
BottleOfBeer ninetyNineBottlesOfBeer[99];
This creates an array of bottles, contiguously
located in memory on the stack
ninetyNineBottlesOfBeer is a pointer to the
beginning of this memory
If I want a beer opened, I say
myBeer.open();
or
ninetyNineBottlesOfBeer[3].open();

Designers Design, Users Use
The interface is the critical part, but the
details (implementation) are important too.





Users use the interface (the public part);
the implementation is hidden by access
control.
BottleOfBeer
-topOn:Boolean
+open():void
+lif t(height:int):void
+tilt(angle:int):void
Objects vs. Procedural
Libraries
C libraries are like this, sort of:
The library designer invents a useful struct.
Then she provides some useful functions for it.
The user creates an instance of the struct, then
applies library functions to it.
One big difference is that anyone can change any
part of the struct. Booo, hsss!
Another difference is in initialization (and well attend
to this soon enough).
Two Ways of Reusing Classes
Composition: One class has another as a part
(indicated by the diamond aggregation
symbol).

BottleOfBeer CaseOfBeer
1 *
Tavern
1 *
Patron
1
*
Two Ways of Reusing Classes
Inheritance: One class is a specialized
version of another (indicated by the
triangle inheritance symbol).
BottleOfBeer
+open():void
+lif t(height:int):void
+tilt(angle:int):void
BottleOfRollingRock
-lowCalorie:Boolean
+isLowCalorie():Boolean
Polymorphism
Different subclasses respond to the
same message, possibly with different
actions. Patron
+beerPlease():Polite
BritishPatron AmericanPatron
+beerPlease():Rude
GermanPatron
+beerPlease():InGerman
Polymorphism Using Pointers
Patron * p1 = new Patron;
Patron * p2 = new AmericanPatron;
Patron * p3 = new BritishPatron;
Patron * p4 = new GermanPatron;
p1->BeerPlease() ; // polite request
p2-> BeerPlease() ; // rude request
p3->BeerPlease() ; // polite request
p4->BeerPlease() ; // request in German (but polite)

This is a bit of a trick: it requires late binding
of the function call.

Creating Objects
We usually assume this is free; with
built-in types like int or char, we just say
int i;
char c;
With user-defined types (the ones we
make), we need to be explicit about
what we want:
one or more constructor functions
This is a very important issue!
Destroying Objects
If an object goes out of scope, it can
no longer be used (its name is no
longer known).
In C++, we might need to write an
explicit function to free memory
allocated to an object.
(Compare Java, which uses references
and garbage collection.)
Example of Object Scope




What happens to lect (and s)?
The LectureNotes object is created and
initialized, but it disappears after return (its
out of scope).
The class designer needs to say what cleanup
is needed (in something called a destructor).

public string getTitle(int lectureNumber) {
LectureNotes lect = syllabus.getLecture(lectureNumber);
string s = lect.getLine(1);
return s;
}
A Simple Model of Memory
(An Ordinary C++ Variable)
The variable lect is the
memory where the
LectureNotes object resides.
22 C++ Versus Java\n
The LectureNotes object
may in turn refer (through a
pointer) to other memory
When you speak of the variable lect, you are speaking of
the actual LectureNotes object. When lect goes out of scope, it is
automatically destroyed. If destruction is complicated, an
explicit destructor must be written, or else we have a memory leak.
memory
Javas Model of Memory
The variable lect refers
to a memory location
0x1234 22 C++ Versus Java\n
The LectureNotes object
may in turn refer (through a
pointer) to other memory
When you speak of the variable lect, you are speaking of
the actual LectureNotes object. When lect goes out of scope, it is
automatically destroyed, but what it refers to isnt.
memory
C++s Use of Memory
Registers
Stack
Heap
Static variables (both global and
members)
Constants
Function code
Creating New Types
class MyNewType {
// definition here
};
Now its legal to say
MyNewType m;
or
MyNewType * mPointer = new MyNewType;
Class Members
Fields (a.k.a. member variables, data
members)
Methods (a.k.a. member functions)
class MyClass {
int a;
YourClass b;
double memberFunction(int x, double d) {
return 0;
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy