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

Course Literature

draw() method. - Overloading means defining functions or operators with the same name but different parameters. This allows functions to operate in different ways depending on the types of arguments passed to them. - Polymorphism and overloading together allow objects to respond to the same function call differently depending on their type or class. This enhances flexibility and reusability of code.

Uploaded by

Aftab Naseem
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Course Literature

draw() method. - Overloading means defining functions or operators with the same name but different parameters. This allows functions to operate in different ways depending on the types of arguments passed to them. - Polymorphism and overloading together allow objects to respond to the same function call differently depending on their type or class. This enhances flexibility and reusability of code.

Uploaded by

Aftab Naseem
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

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

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
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.
Problems with Structured
Programming
• Functions have unrestricted access to global data
Function A: Function B: Function C:
local data local data local data

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
Problems with Structured
Programming
• data and function are considered as two separate
entities
• makes it difficult to model things in the real world
• complex real world objects have both attributes and
behaviours
• attributes
• people: name, date of birth, eye color, job title
• cars: horse power, number of doors, color
• behaviours
• people: ask a person to bring you a beer
• cars: apply the brakes, turn on the engine
• attributes and behaviors alone are sufficient to realistically
model real world objects but a unified view is needed
Object Oriented Approach
Object

functions

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 member functions in other
languages such as Smalltalk they are called methods
• Data items are called attributes or instance variables
Object Oriented Approach
• separation: objects interact with each other only via
the their member functions
• separation helps to maintain the integrity of the entire
program
Object A
functions

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

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 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++

class someobject //declares a class


{
private:
int somedata; //class data
public:
void setdata(int d) //membership function to set data
{ somedata=d; }
int getdata() //membership function to get data
{ return somedata; }
}
Classes versus Objects
• A class is a prototype specification from which
one can generate a number of similar objects
• A class can be considered as an object factory.
• An object is said to be a member or instance of a class
• A class can be considered as a more complex
data structure than an ordinary built-in data type
• Standard C already knows the struct command for
user defined data types:
struct complex
{
double re;
double im;
};
complex x;
Instantiation of Objects
person Class
data: name, p_nummer, address, date of birth
methods: getage(), changeaddress(newaddress)

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

• 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 & Overloading
• Polymorphism : using functions and operators in different
ways, depending on what they are operating on.
• Polymorphism allows it to manipulate objects without
knowing their exact type but only their common property.
for example, the classes Triangle and Circle both
have their own (polymorphic) version of the method Draw,
but a graphic routine that draws graphical elements does
not have to know which object it manipulates.
• Overloading: an existing operator, such as + or = is given
the capability to operate on a new data type, for example
define the operator + for the class Complex such that
it realizes the addition of two complex numbers.
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);
Polymorphism and Dynamic Binding
• suppose we have a class Shape for graphical
objects with subclasses Circle and Triangle
• Circle and Triangle both provide their own polymorphic
method void Draw() to draw the shape on the screen
• The term dynamic binding refers to the process
of identifying at run time what code should be executed
as a result of a message
Circle c;
Triangle t;
Shape &s;
s=c;
s.Draw(); // bounded to Circle Draw()
s=t;
s.Draw(); // bounded to Triangle Draw()
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 /* */
#include <iostream.h>
class smallobj //declare a class
{
private:
int somedata; //class data
public:
void setdata(int d) //member function to set data
{
somedata = d;
}
void showdata() //member function to display data
{ cout << “Data is ” << somedata << endl; }
};
int main()
{
smallobj s1, s2; //define two objects of class smallobj
s1.setdata(1066); //call member function to set data
s2.setdata(1776);
s1.showdata(); //call member function to display data
s2.showdata();
return 0;
}

You might also like