Presentation is loading. Please wait.

Presentation is loading. Please wait.

Evolution of Programming Languages Object-Oriented Paradigm II.

Similar presentations


Presentation on theme: "Evolution of Programming Languages Object-Oriented Paradigm II."— Presentation transcript:

1 Evolution of Programming Languages Object-Oriented Paradigm II

2 Object-Oriented Programming zIn the game outlined in the previous presentation, we defined a ‘Goodie ’as class goodie{ int personality; int agility; int inteligence; int health; void move( ); void fight( ); void getTreasure( ); };

3 Object-Oriented Programming zWe may define a ‘Baddie’ as class baddie{ int agility; int health; int aggression; int badType; void move( ); void fight( ); void makeNoise( ); };

4 Object-Oriented Programming zThe baddie class is very similar to the goodie class. zThe attribute ‘intelligence’ has been removed, and ‘aggression’ and ‘badType’ added. zA ‘makeNoise’ method has also been added.

5 Object-Oriented Programming zWhy can’t we combine these two classes into one, and create a superclass like ‘character’? zThis class could contain all of the common attributes and methods of the two classes.

6 Inheritance zSub-classes can inherit all of the attributes and methods from their superclass or parent class. zThis process is called inheritance. zInheritance encourages modularity and robust code.

7 Example Character GoodieBaddie Mr NiceMiss LovelyMr Wild GrizzlyCrazy

8 Example zThe C++ declaration of a ‘character’ could be: class character{ int agility; int health; void move( ); void fight( ); };

9 Example zThe declaration of a goodie now becomes: class goodie: character{ int personality; int intelligence; void getTreasure( ); };

10 Example zThe declaration of a baddie becomes: class baddie: character{ int aggression; int badType; void makeNoise( ); };

11 Abstraction zThe process of designing objects by breaking them down into component classes or picking out the common features of objects and procedures is called abstraction. zEach feature can then be dealt with in isolation.

12 Initialising Attributes zTo initialise the value of an attribute, you need to use a method within the declaration of that class called a constructor method.

13 Constructor Methods zConstructor methods always have the same name as the class in which they reside. zThis is what tells the compiler that the method is a constructor rather than a normal method.

14 Constructor Method Examples class character{ int agility; int health; character( ) { agility=health=5; } void move( ); void fight( ); }; class baddie: character{ int aggression; int badType; baddie(int a, int b) { aggression=a; badType=b; } void makeNoise( ); };

15 Creating the object zThe statement baddie Crazy(3,2) would create a baddie object called Crazy with agility and health values of 5, an aggression value of 3 and a badType value of 2

16 What next? zWhat if we want to create goodies with a different initial agility and health to a baddie?

17 What next? zWe have to modify the character class definition to allow this. zWe will also add a constructor to the goodie class that receives all our inputs and passes on the health and agility inputs to the character class.

18 Modified character class class character{ int agility; int health; character( ) { agility=health=5; } character(int a, int h){ agility = a; health = h; } void move( ); void fight( ); };

19 Modified goodie class class goodie: character{ int personality; int intelligence; goodie(int a, int h, int p, int I):character(a,h){ personality=p; intelligence=i; } void getTreasure( ); };

20 Creating the object zThe statement goodie MrNice(4,7,3,8) would create a goodie object called MrNice where agility=4, health=7, personality=3 and intelligence=8.


Download ppt "Evolution of Programming Languages Object-Oriented Paradigm II."

Similar presentations


Ads by Google