Lecture 1
Lecture 1
Course Literature
C++ C++ Programming Language, 3rd edition, Bjarne Stroustrup, Addison-Wesley C++ Primer, 3rd edition, Stanley B. Lippman, Josee Lajoie, Addison-Wesley The Waites Groups Object-Oriented Programming in C++, 3rd edition, Robert Lafore, SAMS Object Oriented Design and UML Design Patterns, Erich Gamma, Addison-Wesley The Unified Modeling Language User Guide, Grady Booch, Addison-Wesley UML Distilled, Martin Fowler, Addison-Wesley
Course Requirements
regular attendance of lectures active participation in the exercises 7 lab exams lab1 : Matrices (optional but highly recommended) lab2 : Menu system (mandatory) : due 06.04.01 lab3 : Drawing program (mandatory) : due 11.05.01 lab4 : Fia Game (mandatory) : due 25.05.01 lab5 : Handling options ( 4 points ) : due 01.06.01 lab6 : Window library ( 4 points ) : due 01.06.01 lab7 : Adventure Game ( 5 points ) : due 01.06.01 if you fail a lab you can redo it within one week (only if you took the lab exam before the deadline) if you miss a lab exam or fail twice you can not take it earlier then the next omtentamenperiod in August 01
Procedural Languages
Examples of procedural languages: C, Pascal, Fortran A program in a procedural language is basically a list of instructions As programs become larger they are usually broken down into smaller units, such as functions, procedures, subroutines Functions can be grouped together into modules according to their functionality, objectives and tasks. Structured programming is a programming paradigm that to a large extent relies on the idea of dividing a program into functions and modules.
global data X
global data Y
global data Z
Large number of potential connections between functions and data (everything is related to everything, no clear boundaries) makes it difficult to conceptualize program structure makes it difficult to modify and maintain the program e.g. : it is difficult to tell which functions access the data
behaviours
data
Encapsulation: integrate data and functions into one object Data hiding : data is hidden to the outside world and can only be accessed via the functions In C++ functions are called membership functions in other languages such as Smalltalk they are called methods Data items are called attributes or instance variables
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,}
entity
attributes
behavior
{ method, method}
Separation
independent specification of a visible interface and a hidden implementation interface is some kind of contract between the object and the user of this object or module separation is not restricted to object-oriented programming for example header files in standard C can be regarded as interfaces
visible
interface
hidden
Implementation
Structure of an Object
Interface
Implementation
Object
method
method method method
code
code data code 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
Instantiation of Objects
person data: name, p_nummer, address, date of birth methods: getage(), changeaddress(newaddress) person data: Lena Brat, 761203-7111, Stureplan 4, female
Class
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
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
In C++ an object of a sub-class is substitutable for an object of the super-class, in other words an object of class Car can be used whenever an object of class Vehicle is required.
Reusability
Reusability means that a class that has been designed, created and debugged once can be distributed to other programmers for use in their own programs. Similar to the idea of a library of functions in a procedural language. The concept of inheritance provides an important extension to the idea of reusability, as it allows a programmer to take an existing class without modifying it and adding additional features and functionality. This is done by inheriting a new sub-class from the exisiting base class.
Polymorphism
polymorphism means having many shapes in C++ it refers to a situation in which an object could have any of several types a polymorphic variable can refer to objects of different classes, for example a graphic object can be either a circle or a triangle a polymorphic function or operator can take arguments of different types example: int max(int a, int b); double max(double a, double b);
C++ and C
C++ is derived from the language C C++ is a superset of C, that means almost every correct statement in C is also correct in C++ The most important elements added to C are concerned with classes, objects and object-oriented programming New features of C++ improved approach to input/output standard template library (STL) container classes for vectors, lists, maps, etc. reference type replaces pointers const variables replaces #define statements string data type replaces C-style strings char[] new comment style augments C-style comments /* */