Object Oriented Programming Development Week 7 z Rob
Object Oriented Programming Development - Week 7 z Rob Manton z Email: Rob. Manton@luton. ac. uk z Room D 104 1
Module Outline z Introduction z Non object oriented basics z Classes z Inheritance z Aggregation z Polymorphism z Multifile Development 2
Today: z. Timing of the practical test in week 9 z. Advice for lab sessions z. Classes & Objects recap z. Inheritance 3
Practical Test in Week 9 Who already has a lecture/practical on Tuesday 26 th November 5 -8 pm Wednesday 27 th November 5 -8 pm Thursday 28 th November 5 -8 pm 4
Advice for lab sessions z Create a ‘hello world’ project instead of an empty one z type a line or two of code then compile - this should make it easier to identify and fix syntax problems z 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 year. Of. Birth; public: int get. Year. Of. Birth(); void set. Year. Of. Birth(int YOB); }; int main() 12
Classes and objects recap z. The creature class - analysis of the component parts (handout) 13
#include "stdafx. h" #include <iostream. h> class creature { private: int year. Of. Birth; public: creature(); virtual ~creature(); int get. Year. Of. Birth(); void set. Year. Of. Birth(int year); }; int main() { creature my. Dog; my. Dog. set. Year. Of. Birth(1966); cout << "my dog was born in" << my. Dog. get. Year. Of. Birth() << endl; return 0; } 14
creature: : creature() { cout << "constructor called for creature object. " << endl; } creature: : ~creature() { cout << "destructor called for creature object. " << endl; } void creature: : set. Year. Of. Birth(int year) { year. Of. Birth = year; } int creature: : get. Year. Of. Birth() { return year. Of. Birth; } 15
Now for something new. . z. Inheritance 16
What is Inheritance? z. Classes organised into a ‘classification hierarchy’ z. Classes can inherit attributes and methods from other classes z. Inheriting class can add extra attributes and or methods of its own 17
What is the purpose of Inheritance? z Specialisation Extending the functionality of an existing class z Generalisation sharing commonality between two or more classes z Improved efficiency and greater robustness z Plays a major part in polymorphism -more about this in two weeks time 18
Terminology z Derived class or subclass or child class. A class which inherits some of its attributes and methods from another class z Base class or superclass or parent class. A class from which another class inherits z ancestor. A class’s ancestors are those from which its own superclasses inherit z 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’ Building. Office 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? y. 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) Building Short Building short building and tall 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 yclosed in that it has an encapsulated, private part which cannot be affected by external manipulation yand 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 x. If we were modelling animals in a zoo would we create a separate class for each animal type? Cow Whale Elephant Eagle x. This would duplicate attributes such as legs and methods such as get. Age() 32
Generalisation x. 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 x. 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 xconcrete classes can be instantiated directly Animal Concrete classes Cow Whale Mammal Elephant Bird Eagle 35
C++ Syntax z. The colon (: ) operator to denote inheritance z. Public and private derivation of object methods from a base class z. The ‘protected’ keyword to allow derived classes to access inherited attributes 36
C++ Syntax zclass Base. Class z{ A simple base class with one private attribute x zprivate: yint x; and two public methods zpublic: yvoid set. X(int x_in); yint get. X(); z} 37
C++ Syntax: public derivation zclass Derived. Class: public Base. Class z{ A derived class. zprivate: The colon operator means yint y; zpublic: yvoid set. Y(int y_in); yint get. Y(); the derived class inherits from the base class z} 38
C++ Syntax: public derivation zclass Derived. Class: public Base. Class z{ the public derivation means zprivate: yint y; zpublic: that objects of the derived class can access the public methods and attributes of the base class yvoid set. Y(int y_in); This is the most usual type of derivation yint get. Y(); z} 39
C++ Syntax: public derivation z class Base. Class z { z private: y int x; z public: y void set. X(int x_in); y int get. X(); z } Main() { Base. Class base_object; Derived. Class derived_object; base_object. set. X(7); derived_object. set. X(12); derived_object. set. Y(1); z class Derived. Class: public Base. Class return 0; } z { z private: Object of the derived y int y; class can access methods z public: of the derived class y void set. Y(int y_in); and also methods of the y int get. Y(); base class z } 40
C++ Syntax: private derivation zclass Derived. Class: private Base. Class z{ Another derived class - the private zprivate: derivation means that objects of the derived class can’t access the public methods and zpublic: attributes of the base class - but yvoid set. Y(int y_in); the methods of the derived class can! yint get. Y(); This is the least common type yint y; z} 41
C++ Syntax: the ‘protected’ keyword z. An object of a publicly derived class can access the public methods of the base class, but not the private attributes z. Changing the private keyword in the base class to protected makes the attributes available to derived classes 42
C++ Syntax: inheriting constructors z A derived class always inherits the constructor of the base class. The base class constructor is called first. z If the base class constructor takes no parameters then the inheritance is implicit - you don’t need to do anything! z 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 Account. Customer: public Customer { Account. Customer(char * name_in); . . 44 }
C++ Syntax: inheriting constructors Account. Customer: Account. Customer(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 year. Of. Birth; public: creature(int YOB); int get. Year. Of. Birth(); }; 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 my. Creature(1985); cout << "my creature was born in " << my. Creature. get. Year. Of. Birth() <<endl; return 0; } 46
C++ Syntax: inheriting constructors class dog: public creature { public: void bark(); }; int main() { creature my. Creature(1985); dog my. Dog(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 " << my. Creature. get. Year. Of. Birth() <<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 Year. Of. Birth 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 my. Creature(1985); dog my. Dog(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 " << my. Creature. get. Year. Of. Birth() <<endl; cout << "my dog was born in " << my. Dog. get. Year. Of. Birth() <<endl; return 0; } 49
C++ Syntax: inheriting destructors z 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 z Because destructors never take an argument there is no issue with inheritance of destructor parameters. 50
Summary: Inheritance z Inheritance allows classes to inherit attributes and methods from other classes in a classification hierarchy z Inheritance allows specialisation (extending the functionality of an existing class) and generalisation (sharing commonality between two or more classes) z Inheritance is appropriate where a class can be said to be ‘a kind of’ other class 51
Summary: Inheritance z Inheriting from a class doesn’t affect the integrity of that class - objects of the original base class can still be created z Generalisation allows removal of redundancy and duplication among classes z 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 z In C++ the protected keyword allows methods of a derived class access to its inherited attributes z Base class constructor methods are automatically called by the constructors of derived classes but argument lists must be compatible z Destructors are called in the reverse order of constructors 53
- Slides: 53