Object Oriented Programming Development

Download Report

Transcript Object Oriented Programming Development

Object Oriented Programming
Development - Week7
Rob Manton
Email:
[email protected]
Room D104
1
Module Outline
Introduction
Non object oriented
basics
Classes
Inheritance
Aggregation
Polymorphism
Multifile Development
2
Today:
Timing of the practical test in week 9
Advice for lab sessions
Classes & Objects recap
Inheritance
3
Practical Test in Week 9
Who already has a lecture/practical on
Tuesday 26th November 5-8pm
Wednesday 27th November 5-8pm
Thursday 28th November 5-8pm
4
Advice for lab sessions
Create a ‘hello world’ project instead of an
empty one
type a line or two of code then compile - this
should make it easier to identify and fix syntax
problems
When writing a class or method add the
opening and closing braces/semicolon and
compile before ‘fleshing out’.
5
Advice for lab sessions
#include "stdafx.h"
The skeleton program you
get from a ‘hello world’ project
int main(int argc, char* argv[])
{
printf("Hello World!\n");
return 0;
}
6
Advice for lab sessions
#include "stdafx.h"
int main(int argc, char* argv[])
{
printf("Hello World!\n");
It will compile, giving you
return 0;
}
a good starting point for your
code. Remember to use
Build/Rebuild All
7
Advice for lab sessions
#include "stdafx.h"
int main(int argc, char* argv[])
{
printf("Hello World!\n");
return 0;
}
This is specific to the Microsoft
compiler - just leave it there and
everything should work properly
8
Advice for lab sessions
#include "stdafx.h"
int main(int argc, char* argv[])
{
printf("Hello World!\n");
These are used
return 0;
if you need to access any command
}
line parameters passed to the
program. Having them here
won’t do any harm, but you can delete
them if you like
9
Advice for lab sessions
#include "stdafx.h"
int main( )
{
printf("Hello World!\n");
return 0;
}
You can get rid of the printf line
but you do need to keep the return 0; line
10
Advice for lab sessions
#include "stdafx.h"
class creature
{
};
int main()
{
return 0;
}
Add your class definition as
a ‘skeleton’: add the opening
and closing braces and semicolon
and compile before adding
any more detail
11
Advice for lab sessions
#include "stdafx.h"
class creature
Now you can start to
{
private:
‘flesh out’ the class definition
int yearOfBirth;
public:
int getYearOfBirth();
void setYearOfBirth(int YOB);
};
int main()
12
Classes and objects recap
The creature class - analysis of
the component parts (handout)
13
#include "stdafx.h"
#include <iostream.h>
class creature
{
private:
int yearOfBirth;
public:
creature();
virtual ~creature();
int getYearOfBirth();
void setYearOfBirth(int year);
};
int main()
{
creature myDog;
myDog.setYearOfBirth(1966);
cout << "my dog was born in" << myDog.getYearOfBirth() << endl;
return 0;
}
14
creature::creature()
{
cout << "constructor called for creature object." << endl;
}
creature::~creature()
{
cout << "destructor called for creature object." << endl;
}
void creature::setYearOfBirth(int year)
{
yearOfBirth = year;
}
int creature::getYearOfBirth()
{
return yearOfBirth;
}
15
Now for something new..
Inheritance
16
What is Inheritance?
Classes organised into a ‘classification
hierarchy’
Classes can inherit attributes and methods
from other classes
Inheriting class can add extra attributes
and or methods of its own
17
What is the purpose of Inheritance?
Specialisation
Extending the functionality of an existing class
Generalisation
sharing commonality between two or more
classes
Improved efficiency and greater robustness
Plays a major part in polymorphism
-more about this in two weeks time
18
Terminology
 Derived class or subclass or child class.
A class which inherits some of its attributes and
methods from another class
 Base class or superclass or parent class.
A class from which another class inherits
 ancestor.
A class’s ancestors are those from which its own
superclasses inherit
 descendant.
A class’s descendants are those which inherit from its
subclasses
19
Terminology - a classification
Hierarchy
Building
Commercial
Office
block
Public
Domestic
Office
block
Factory
Cathedral
Apartment
Block
Hospital
20
Terminology - a classification
Hierarchy
Generalisation
Building
Commercial
Office
block
Public
Domestic
Office
block
Factory
Cathedral
Hospital
Apartment
Block
Specialisation
21
Terminology - a classification
Hierarchy
Generalised
Building
Commercial
Office
block
Public
‘base class’
Domestic
Office
block
Factory
Cathedral
Apartment
Block
Hospital
22
Terminology - a classification
Hierarchy
Building
Commercial
Office
block
Factory
Public
Domestic
A ‘kind of’ BuildingOffice
block
Apartment
Block
(AKO)
Cathedral
Hospital
23
Terminology - a classification
Hierarchy
A ‘kind of’
Commercial building
Building
(AKO)
Commercial
Office
block
Public
Domestic
Office
block
Factory
Cathedral
Apartment
Block
Hospital
24
Terminology - a classification
Hierarchy
Arrow in diagram
means
Building
Commercial
Office
block
Public
’inherits from’
Domestic
Office
block
Factory
Cathedral
Apartment
Block
Hospital
25
Designing your classification hierarchy:
‘A kind of’ or ‘a part of’?
Vehicle
Car

A car is ‘a kind of’ vehicle
car class can inherit from
vehicle class
26
Designing your classification hierarchy:
‘A kind of’ or ‘a part of’?
Vehicle
Car

A car is ‘a kind of’ vehicle
car class can inherit from
vehicle class
A wheel isn’t ‘a kind of’ car.
Car
Wheel

A wheel is ‘a part of’ a car
- this is dealt with by aggregation
which is next week’s topic
27
Designing your classification hierarchy:
Different classes or different states?
Need to analyse whether differences
between objects are dependant on type (such
as a house being different to a factory) or
state. (different values of the class’s
attributes)
short building and tall building

Building
Short
Building
might vary only in the value of
the height attribute - don’t need
separate classes
Tall
Building
28
What do objects inherit?
Line
Attributes:
start position
end position
Methods:
draw
Coloured Line
Attributes:
colour
Methods:
set colour
A ‘coloured line’ is a kind of line
the coloured line class inherits all
the attributes and methods of
the line class and adds attributes
and methods of its own
An object of the ‘coloured line’
class has all the attributes and
methods of the ‘line’ base class as
well as the attributes and methods
added by the derived class
29
Specialisation
Extending the functionality of an existing class
eg a coloured line is a specialised kind of line
30
Specialisation
A class is both
closed in that it has an encapsulated,
private part which cannot be affected
by external manipulation
and open in that it allows itself to be
used as part of a larger software unit.
31
Generalisation
Sharing commonality between two or more
classes
If we were modelling animals in a zoo
would we create a separate class for each
animal type?
Cow
Whale
Elephant
Eagle
This would duplicate attributes such as
legs and methods such as getAge()
32
Generalisation
Helpful to place common elements (attributes and
methods) in a shared base class and organise
problem into an inheritance hierarchy.
Animal
Mammal
Cow
Whale
Elephant
Bird
Eagle
33
Generalisation
Sometimes this leads to the creation of abstract
classes which can’t be instantiated directly
Abstract classes
Animal
Mammal
Cow
Whale
Elephant
Bird
Eagle
34
Generalisation
concrete classes can be instantiated directly
Animal
Concrete classes
Cow
Whale
Mammal
Elephant
Bird
Eagle
35
C++ Syntax
The colon (:) operator to denote
inheritance
Public and private derivation of object
methods from a base class
The ‘protected’ keyword to allow derived
classes to access inherited attributes
36
C++ Syntax
class BaseClass
{
A simple base class
with one private attribute x
private:
int x;
and two public methods
public:
void setX(int x_in);
int getX();
}
37
C++ Syntax: public derivation
class DerivedClass: public BaseClass
{
A derived class.
private:
The colon operator means
int y;
public:
void setY(int y_in);
int getY();
the derived class
inherits from
the base class
}
38
C++ Syntax: public derivation
class DerivedClass: public BaseClass
{
the public derivation means
private:
int y;
public:
that objects of the derived class
can access the public methods
and attributes of the base class
void setY(int y_in); This is the most usual type
of derivation
int getY();
}
39
C++ Syntax: public derivation
 class BaseClass
 {
 private:
 int x;
 public:
 void setX(int x_in);
 int getX();
 }
Main()
{
BaseClass base_object;
DerivedClass derived_object;
base_object.setX(7);
derived_object.setX(12);
derived_object.setY(1);
 class DerivedClass: public BaseClass
return 0;
}
 {
 private:
Object of the derived
 int y;
class can access methods
 public:
of the derived class
 void setY(int y_in);
and also methods of the
 int getY();
base class
 }
40
C++ Syntax: private derivation
class DerivedClass: private BaseClass
{
Another derived class - the private
private:
derivation means that objects
of the derived class can’t
access the public methods and
public:
attributes of the base class - but
void setY(int y_in);the methods of the derived class
can!
int getY();
This is the least common type
int y;
}
41
C++ Syntax: the ‘protected’ keyword
An object of a publicly derived class can
access the public methods of the base
class, but not the private attributes
Changing the private keyword in the base
class to protected makes the attributes
available to derived classes
42
C++ Syntax: inheriting constructors
 A derived class always inherits the constructor of the
base class. The base class constructor is called first.
 If the base class constructor takes no parameters then
the inheritance is implicit - you don’t need to do
anything!
 If the base class constructor takes parameters then each
derived class needs to declare a constructor with the
same parameters. You can pass the arguments given to
the derived class constructor to the constructor for the
base class
43
C++ Syntax: inheriting constructors
class Customer
{
Customer (char * name_in);
…
}
Base class declares a
constructor that takes
a char pointer parameter
Derived class declares a
constructor that takes the
same char pointer parameter
Class AccountCustomer:public Customer
{
AccountCustomer(char * name_in);
..
44
}
C++ Syntax: inheriting constructors
AccountCustomer: AccountCustomer(char * name_in):
Customer (name_in)
In the implementation of the
{
//main body of constructor.. constructor for the derived class,
the parameter passed to the
}
derived class constructor is
passed down to the base class
constructor. Note use of the
colon (:) syntax once again
45
C++ Syntax: inheriting constructors
class creature
{
private:
int yearOfBirth;
public:
creature(int YOB);
int
getYearOfBirth();
};
This class has a constructor
that takes an integer argument.
When instantiating an object of
this class you pass a parameter
to the constructor.
int main()
{
creature myCreature(1985);
cout << "my creature was born in " << myCreature.getYearOfBirth() <<endl;
return 0;
}
46
C++ Syntax: inheriting constructors
class dog:public creature
{
public:
void bark();
};
int main()
{
creature myCreature(1985);
dog myDog(1985);
Dog class derived from
creature class
At the moment we can’t do this:
there is no constructor for the
dog class that takes an integer
argument
cout << "my creature was born in " << myCreature.getYearOfBirth() <<endl;
return 0;
}
47
C++ Syntax: inheriting constructors
class dog:public creature
{
public:
dog(int YOB);
void bark();
};
//implementation for dog constructor
dog::dog(int YOB):
creature(YOB)
{
//other constructor stuff goes here
}
Now we have defined a
constructor that does take
an integer argument
The argument sent to the
dog constructor gets sent
to the creature constructor
so the YearOfBirth attribute
of the base class gets set
properly
48
C++ Syntax: inheriting constructors
class dog:public creature
{
public:
dog(int YOB);
void bark();
};
int main()
{
creature myCreature(1985);
dog myDog(1985);
Now we do have an
appropriate constructor
for the dog class which
correctly initialises the
attributes defined in the
base class
cout << "my creature was born in " << myCreature.getYearOfBirth() <<endl;
cout << "my dog was born in " << myDog.getYearOfBirth() <<endl;
return 0;
}
49
C++ Syntax: inheriting destructors
 A derived class always inherits the destructor of the
base class. The derived class destructor is called first.
This is the reverse of the sequence for constructors
 Because destructors never take an argument there is no
issue with inheritance of destructor parameters.
50
Summary: Inheritance
 Inheritance allows classes to inherit attributes and
methods from other classes in a classification hierarchy
 Inheritance allows specialisation (extending the
functionality of an existing class) and generalisation
(sharing commonality between two or more classes)
 Inheritance is appropriate where a class can be said to
be ‘a kind of’ other class
51
Summary: Inheritance
 Inheriting from a class doesn’t affect the integrity of
that class - objects of the original base class can still be
created
 Generalisation allows removal of redundancy and
duplication among classes
 Some base classes are abstract - they are not specific
enough to be instantiated but act as holders for
common attributes and methods of derived classes
52
Summary: Inheritance
 In C++ the protected keyword allows methods of a
derived class access to its inherited attributes
 Base class constructor methods are automatically called
by the constructors of derived classes but argument lists
must be compatible
 Destructors are called in the reverse order of
constructors
53