Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Object Oriented Programming Development - Week7 z Rob Manton z z Room D104.

Similar presentations


Presentation on theme: "1 Object Oriented Programming Development - Week7 z Rob Manton z z Room D104."— Presentation transcript:

1 1 Object Oriented Programming Development - Week7 z Rob Manton z Email: Rob.Manton@luton.ac.uk z Room D104

2 2 Module Outline zIntroduction zNon object oriented basics zClasses z Inheritance z Aggregation z Polymorphism z Multifile Development

3 3 Today: zTiming of the practical test in week 9 zAdvice for lab sessions zClasses & Objects recap zInheritance

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

5 5 Advice for lab sessions zCreate a ‘hello world’ project instead of an empty one ztype a line or two of code then compile - this should make it easier to identify and fix syntax problems zWhen writing a class or method add the opening and closing braces/semicolon and compile before ‘fleshing out’.

6 6 Advice for lab sessions #include "stdafx.h" int main(int argc, char* argv[]) { printf("Hello World!\n"); return 0; } The skeleton program you get from a ‘hello world’ project

7 7 Advice for lab sessions #include "stdafx.h" int main(int argc, char* argv[]) { printf("Hello World!\n"); return 0; } It will compile, giving you a good starting point for your code. Remember to use Build/Rebuild All

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

9 9 Advice for lab sessions #include "stdafx.h" int main(int argc, char* argv[]) { printf("Hello World!\n"); return 0; } These are used 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

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

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

12 12 Advice for lab sessions #include "stdafx.h" class creature { private: int yearOfBirth; public: int getYearOfBirth(); void setYearOfBirth(int YOB); }; int main() { printf("Hello World!\n"); return 0; } Now you can start to ‘flesh out’ the class definition

13 13 Classes and objects recap zThe creature class - analysis of the component parts (handout)

14 14 #include "stdafx.h" #include 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; }

15 15 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; }

16 16 Now for something new.. zInheritance

17 17 What is Inheritance? zClasses organised into a ‘classification hierarchy’ zClasses can inherit attributes and methods from other classes zInheriting class can add extra attributes and or methods of its own

18 18 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing class zGeneralisation sharing commonality between two or more classes zImproved efficiency and greater robustness zPlays a major part in polymorphism -more about this in two weeks time

19 19 Terminology zDerived class or subclass or child class. A class which inherits some of its attributes and methods from another class zBase class or superclass or parent class. A class from which another class inherits zancestor. A class’s ancestors are those from which its own superclasses inherit zdescendant. A class’s descendants are those which inherit from its subclasses

20 20 Terminology - a classification Hierarchy Building CommercialPublicDomestic Office block Factory CathedralHospital Office block Apartment Block

21 21 Terminology - a classification Hierarchy Generalisation Specialisation Building CommercialPublicDomestic Office block Factory CathedralHospital Office block Apartment Block

22 22 Terminology - a classification Hierarchy Generalised ‘base class’ Building CommercialPublicDomestic Office block Factory CathedralHospital Office block Apartment Block

23 23 Terminology - a classification Hierarchy Building CommercialPublicDomestic Office block Factory CathedralHospital Office block Apartment Block A ‘kind of’ Building (AKO)

24 24 Terminology - a classification Hierarchy Building CommercialPublicDomestic Office block Factory CathedralHospital Office block Apartment Block A ‘kind of’ Commercial building (AKO)

25 25 Terminology - a classification Hierarchy Building CommercialPublicDomestic Office block Factory CathedralHospital Office block Apartment Block Arrow in diagram means ’inherits from’

26 26 Designing your classification hierarchy: ‘A kind of’ or ‘a part of’? Car Vehicle A car is ‘a kind of’ vehicle car class can inherit from vehicle class

27 27 Car Wheel Vehicle  A car is ‘a kind of’ vehicle car class can inherit from vehicle class A wheel isn’t ‘a kind of’ car. A wheel is ‘a part of’ a car - this is dealt with by aggregation which is next week’s topic Designing your classification hierarchy: ‘A kind of’ or ‘a part of’?

28 28 yNeed 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 Tall Building  short building and tall building might vary only in the value of the height attribute - don’t need separate classes Designing your classification hierarchy: Different classes or different states?

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

30 30 Specialisation Extending the functionality of an existing class eg a coloured line is a specialised kind of line

31 31 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.

32 32 Generalisation Sharing commonality between two or more classes xIf we were modelling animals in a zoo would we create a separate class for each animal type? xThis would duplicate attributes such as legs and methods such as getAge() CowWhaleEagleElephant

33 33 Generalisation xHelpful to place common elements (attributes and methods) in a shared base class and organise problem into an inheritance hierarchy. CowWhaleEagleElephant Animal MammalBird

34 34 Generalisation xSometimes this leads to the creation of abstract classes which can’t be instantiated directly CowWhaleEagleElephant Animal MammalBird Abstract classes

35 35 Generalisation xconcrete classes can be instantiated directly CowWhaleEagleElephant Animal MammalBird Concrete classes

36 36 C++ Syntax zThe colon (:) operator to denote inheritance zPublic and private derivation of object methods from a base class zThe ‘protected’ keyword to allow derived classes to access inherited attributes

37 37 C++ Syntax zclass BaseClass z{ zprivate: yint x; zpublic: yvoid setX(int x_in); yint getX(); z} A simple base class with one private attribute x and two public methods

38 38 C++ Syntax: public derivation zclass DerivedClass: public BaseClass z{ zprivate: yint y; zpublic: yvoid setY(int y_in); yint getY(); z} A derived class. The colon operator means the derived class inherits from the base class

39 39 C++ Syntax: public derivation zclass DerivedClass: public BaseClass z{ zprivate: yint y; zpublic: yvoid setY(int y_in); yint getY(); z} the public derivation means that objects of the derived class can access the public methods and attributes of the base class This is the most usual type of derivation

40 40 C++ Syntax: public derivation zclass BaseClass z{ zprivate: yint x; zpublic: yvoid setX(int x_in); yint getX(); z} zclass DerivedClass: public BaseClass z{ zprivate: yint y; zpublic: yvoid setY(int y_in); yint getY(); z} Main() { BaseClass base_object; DerivedClass derived_object; base_object.setX(7); derived_object.setX(12); derived_object.setY(1); return 0; } Object of the derived class can access methods of the derived class and also methods of the base class

41 41 C++ Syntax: private derivation zclass DerivedClass: private BaseClass z{ zprivate: yint y; zpublic: yvoid setY(int y_in); yint getY(); z} Another derived class - the private derivation means that objects of the derived class can’t access the public methods and attributes of the base class - but the methods of the derived class can! This is the least common type

42 42 C++ Syntax: the ‘protected’ keyword zAn object of a publicly derived class can access the public methods of the base class, but not the private attributes zChanging the private keyword in the base class to protected makes the attributes available to derived classes

43 43 C++ Syntax: inheriting constructors zA derived class always inherits the constructor of the base class. The base class constructor is called first. zIf the base class constructor takes no parameters then the inheritance is implicit - you don’t need to do anything! zIf 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

44 44 C++ Syntax: inheriting constructors class Customer { Customer (char * name_in); … } Class AccountCustomer:public Customer { AccountCustomer(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

45 45 C++ Syntax: inheriting constructors AccountCustomer: AccountCustomer(char * name_in): Customer (name_in) { //main body of constructor.. } In the implementation of the 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

46 46 C++ Syntax: inheriting constructors class creature { private: int yearOfBirth; public: creature(int YOB); intgetYearOfBirth(); }; int main() { creature myCreature(1985); cout << "my creature was born in " << myCreature.getYearOfBirth() <<endl; return 0; } This class has a constructor that takes an integer argument. When instantiating an object of this class you pass a parameter to the constructor.

47 47 C++ Syntax: inheriting constructors class dog:public creature { public: void bark(); }; int main() { creature myCreature(1985); dog myDog(1985); cout << "my creature was born in " << myCreature.getYearOfBirth() <<endl; return 0; } At the moment we can’t do this: there is no constructor for the dog class that takes an integer argument Dog class derived from creature class

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

49 49 C++ Syntax: inheriting constructors class dog:public creature { public: dog(int YOB); void bark(); }; int main() { creature myCreature(1985); dog myDog(1985); cout << "my creature was born in " << myCreature.getYearOfBirth() <<endl; cout << "my dog was born in " << myDog.getYearOfBirth() <<endl; return 0; } Now we do have an appropriate constructor for the dog class which correctly initialises the attributes defined in the base class

50 50 C++ Syntax: inheriting destructors zA 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 zBecause destructors never take an argument there is no issue with inheritance of destructor parameters.

51 51 Summary: Inheritance zInheritance allows classes to inherit attributes and methods from other classes in a classification hierarchy zInheritance allows specialisation (extending the functionality of an existing class) and generalisation (sharing commonality between two or more classes) zInheritance is appropriate where a class can be said to be ‘a kind of’ other class

52 52 Summary: Inheritance zInheriting from a class doesn’t affect the integrity of that class - objects of the original base class can still be created zGeneralisation allows removal of redundancy and duplication among classes zSome base classes are abstract - they are not specific enough to be instantiated but act as holders for common attributes and methods of derived classes

53 53 Summary: Inheritance zIn C++ the protected keyword allows methods of a derived class access to its inherited attributes zBase class constructor methods are automatically called by the constructors of derived classes but argument lists must be compatible zDestructors are called in the reverse order of constructors


Download ppt "1 Object Oriented Programming Development - Week7 z Rob Manton z z Room D104."

Similar presentations


Ads by Google