Course Literature
Course Literature
C++
• C++ Programming Language, 3rd edition,
Bjarne Stroustrup, Addison-Wesley
• C++ Primer, 3rd edition,
Stanley B. Lippman, Josee Lajoie, Addison-Wesley
• The Waite’s Group’s Object-Oriented Programming in C++,
3rd edition, Robert Lafore, SAMS
functions
data
Object C Object B
data
functions functions
data data
Abstraction
• An abstraction is a named collection of attributes and
behavior relevant to model a given entity for some
particular purpose
real-world abstraction software
{data, data,…}
attributes
entity
visible interface
hidden Imple-
mentation
Structure of an Object
Interface Implementation
Object
method code
method code
data
method code
method code
Examples of Objects
• physcial objects
• vehicles in a traffic-flow simulation
• electrical components in a circuit-design program
• elements of a computer user environment
• menus
• graphic objects
• data-storage constructs
• arrays
• linked lists
• human entities
• employees
• students
• collections of data
• an inventory
• an address book
• user defined data types
• time
• complex numbers
Example of a Class in C++
person
data: Lena Brat, 761203-7111, Stureplan 4, female
person
data: Erik Olsson, 780605-4789, Hamngatan 3, male
person
data: Lars Backe, 671110-A562, Mälartorget 19, male
Relationships among Objects
• Attribute:
One object uses as another object as an attribute,
namely as member data, for example a Person
contains an attribute Name. This type of relation-
ship is also called a weak association or has-a
relationship. Example: A Person has a Name
• Association:
One object uses another to help it carry out a task.
Classes that collaborate are usually related through
associations. This type of relationship is also
called a uses relationship.
Example: The object Driver invokes
the method Brake of the object BrakingSystem.
Relationships among Objects
• Aggregation:
Aggregation means that one object contains other
objects. Aggregation is also called part-of relationship.
Example: The class Adressbook contains many People
Objects.
• Composition:
Composition is building objects from parts. It is a stronger
type of aggregation in which the parts are necessary to
the whole, namely they are permanently bound to the
object and do not exist outside the object.
A class Processor contains a CPU, Memory and I/O-Ports.
Relationships among Objects
• Generalization
Generalization is a relationship defined at the class level
not the object level, which means that all objects of
this class must obey the relationship. This is type of
relationship is also called a is-a-kind-of relationship.
Generalization in object oriented languages is realized
by means of inheritance.
Example: A car is a kind of vehicle.
Inheritance
• In our daily lives we use the concept of classes divided
into subclasses, for example vehicles are divided into cars,
trucks, buses and motor cycles.
• The principle in this sort of division is that each sub-class shares
some common features with the base class from which it
is derived, but also has its own particular features.
base class
Vehicle
wheels
engine
Car Truck
wheels wheels
engine sub-classes or engine
trunk derived classes trailer
Inheritance
• A sub-class also shares common methods with its
super-class but can add its own methods or overwrite
the methods of its super-class.
base class
Vehicle
brake()
start_engine()
Car Truck
brake() brake()
start_engine() sub-classes or start_engine()
open_door() derived classes open_door()
pull_trailer()
Inheritance
Terminology:
• Car is a sub-class (or derived class) of Vehicle
• Car inherits from Vehicle
• Car is a specialization of Vehicle
• Vehicle is a super-class (or base class) of Car
• Vehicle is a generalization of Car