Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Object Oriented Programming Development - Week 3 z By: Marc Conrad University of Luton z z Room: D104.

Similar presentations


Presentation on theme: "1 Object Oriented Programming Development - Week 3 z By: Marc Conrad University of Luton z z Room: D104."— Presentation transcript:

1 1 Object Oriented Programming Development - Week 3 z By: Marc Conrad University of Luton z Email: Marc.Conrad@luton.ac.uk z Room: D104

2 2 Module Outline zIntroduction zThe non object oriented basics zClasses zDesign Approaches zTesting z Inheritance z Aggregation z Polymorphism z Multifile Development

3 3 Today: zControl structures. zPointers. zClasses zObjects

4 A typical C++ program // FileID: hello.cpp // Title: The program doing something #include void doSomething(int p); int main() { int p = 7; doSomething(p); cout << “I have something done.” << endl; return 0; } void doSomething(int p) { for( int i = 0; i < p; i++ ) { cout << “*” << endl; }

5 5 Control Structures - Decisions zThe if statement: if ( x > 0 ) { cout << “positive”; } else { cout << “negative or zero”; }

6 6 Control Structures - Decisions zThe switch statement - example: int x; cout << "Enter choice (1, 2, or 3)"; cin >> x; switch(x) { case 1: doThis(); break; case 2: doThat(); break; case 3: doSomethingElse(); break; default: cout << "Sorry, invalid Input"; }

7 7 Control Structures - Iteration zThe for loop: for(k = 0; k < 10; k++ ) { cout << “The square of “ << k << “ is “ << k * k << endl; } Start condition Action taking place at the end of each iteration Terminating condition

8 8 Control Structures - Iteration zThe while loop: while ( condition ) { // do something } Equivalent to: for( ; condition ; ) { // do something }

9 9 Control structures - do … while zThe do … while loop: do { // something } while( condition); Equivalent to: // something while( condition) { // something }

10 10 Control Structures zAnd finally: zYes, C++ has a goto. Don’t use it.

11 11 Basics of C++ - pointer zA pointer points to a memory location which contains data of a particular type. The contents of a pointer is the address of some data zDeclaration: yint *p; ydouble *aDoublePointer; 2.73817

12 12 Again: Basics of C++ - arrays zDeclaration: yint numbers[10]; zDeclaration & Initialisation: yint primes[] = { 2, 3, 5, 7, 11, 13, 17, 19 }; zAccess: ynumbers[6] = 2483; ycout << “The fourth prime is “ << primes[4];

13 13 Basics of C++ - pointer zIn C++ pointer and arrays are strongly related (“array = pointer + memory”). yint primes[] = {2, 3, 5, 7, 11 }; yint *aPr = primes; ycout << “The third prime is “ << *(aPr + 3); The same as primes[3] The * operator accesses the data on the memory address

14 14 What is Object Oriented Programming? An object is like a black box. The internal details are hidden. z Identifying objects and assigning responsibilities to these objects. z Objects communicate to other objects by sending messages. z Messages are received by the methods of an object

15 15 The two steps of Object Oriented Programming zMaking Classes: Creating, extending or reusing abstract data types. zMaking Objects interact: Creating objects from abstract data types and defining their relationships.

16 Example: The Creature class class Creature { private: int yearOfBirth; public: void setYearOfBirth(year) { yearOfBirth = year; } int getYearOfBirth() { return yearOfBirth; } }; born1997

17 Example: The Creature class class Creature { private: int yearOfBirth; public: void setYearOfBirth(year) { yearOfBirth = year; } int getYearOfBirth() { return yearOfBirth; } }; The definition of a class: The class keyword, followed by the class name. private attributes. public methods. the ; at the end

18 Example: The Creature class class Creature { private: int yearOfBirth; public: void setYearOfBirth(year) { yearOfBirth = year; } int getYearOfBirth() { return yearOfBirth; } }; This class has an attribute of type int. Note that each C++ data type and also abstract data types can be used as attribute types.

19 Example: The Creature class class Creature { private: int yearOfBirth; public: void setYearOfBirth(year) { yearOfBirth = year; } int getYearOfBirth() { return yearOfBirth; } }; This class has two (public) methods. One to set the attribute value and the other to retrieve the attribute value.

20 Example: The Creature class class Creature { private: int yearOfBirth; public: void setYearOfBirth(year); int getYearOfBirth(); }; void Creature::setYearOfBirth { yearOfBirth = year; } int Creature::getYearOfBirth() { return yearOfBirth; } Note that unless the methods are very short, declaration and implementation is usually separated. The declaration goes into a header file (.h), the implementation in a.cpp file.

21 Example: The Creature class class Creature { private: int yearOfBirth; public: void setYearOfBirth(year) { yearOfBirth = year; } int getYearOfBirth() { return yearOfBirth; } }; This method is an example for a ‘modifier’ method. It modifies the attribute. The method changes the state of the object.

22 Example: The Creature class class Creature { private: int yearOfBirth; public: void setYearOfBirth(year) { yearOfBirth = year; } int getYearOfBirth() { return yearOfBirth; } }; This method is an example for a ‘selector’ method. It returns information about the attribute but does not change the state of the object.

23 23 Classes & Objects zWhat may be different for all objects in a class, and what remains the same? zAll the objects in a class may have different attribute values (state data), but their allowed behaviours are all the same. So a class is a blueprint for objects

24 24 Objects & Classes zA class is defined by: yA Unique Name yAttributes yMethods z An object is defined by: yIdentity yState yBehaviour

25 25 Instantiating Objects zAn object is instantiated just like any other data type: int x; char y; Creature z; Declaring z of type ‘creature’ means we have generated an object with the attributes and methods of the class.

26 26 Multiple Objects zOf course we can create many objects of the same class: Creature myDog; Creature theMilkman; Creature myBestFriend; Creates three objects.

27 27 Sending Messages / Calling Methods. zA message is send to an object by calling a method of this object. Use the. (dot) for calling a method of an object. int k; k = theMilkman.getYearOfBirth(); myDog.setYearOfBirth(1998); Messages are sent to my dog and the milkman.

28 28 Back to the Instantiation... zAn object is instantiated just like any other data type: int x; char y; Creature z; Here the “default constructor” of the Creature class is automatically called. If we don’t like this we can specify constructors explicitly!

29 The Creature class with a user defined default constructor. class Creature { private: int yearOfBirth; public: // … Creature() { yearOfBirth = 1970; cout << “Hello.”; } }; The syntax for a constructor is similar as for a method, but: It has the same name as the class. It has no return value.

30 The Creature with a parametrized constructor. class Creature { private: int yearOfBirth; public: // … Creature(int year) { yearOfBirth = year; } }; This constructor can be used as follows: Creature theMilkman(1953); instantiates a 49 years old milkman.

31 The Creature with a copy constructor. class Creature { private: int yearOfBirth; public: // … Creature(Creature & otherCreature) { yearOfBirth = otherCreature.getYearOfBirth(); } }; Example: Creature myDog(1995); Creature myCat(myDog); creates a cat of the same age as the dog.

32 32 Constructors - summary zA constructor is always called when an object is created. zWe can define our own constructors (Note: a class can have more than one constructor). zIf an object is copied from another object then the copy constructor is called.

33 33 Again: Objects & Classes zA class is defined by: yA Unique Name yAttributes yMethods z An object is defined by: yIdentity yState yBehaviour

34 34 Again: Objects & Classes zA class is defined by: yA Unique Name yAttributes yMethods z An object is defined by: yIdentity yState yBehaviour But: We can give a class state and behaviour with the keyword static!

35 Example: The Creature class class Creature { private: int yearOfBirth; static int numberOfAllCreatures = 0; public: Creature() { // Constructor - counts the creatures. numberOfAllCreatures++; } static int getNumberOfAllCreatures() { return numberOfAllCreatures; } }; Note that all objects share the same value of the “class attribute” numberOfAllCreatures.

36 36 Summary. zA class is a blueprint for an object. zObjects are created similar to other data types (int, char, …). zThe construction of an object can be defined by the user. zMessages are sent to an object by calling a method. zstatic messes the concept of classes and objects (but is nevertheless useful).


Download ppt "1 Object Oriented Programming Development - Week 3 z By: Marc Conrad University of Luton z z Room: D104."

Similar presentations


Ads by Google