1 / 36

Object Oriented Programming Development - Week 3

By: Marc Conrad University of Luton Email: Marc.Conrad@luton.ac.uk Room: D104. Object Oriented Programming Development - Week 3. Introduction The non object oriented basics Classes Design Approaches Testing. Inheritance Aggregation Polymorphism

nancy
Download Presentation

Object Oriented Programming Development - Week 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Introduction The non object oriented basics Classes Design Approaches Testing Inheritance Aggregation Polymorphism Multifile Development Module Outline

  3. Today: • Control structures. • Pointers. • Classes • Objects

  4. A typical C++ program // FileID: hello.cpp // Title: The program doing something #include <iostream.h> 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. Control Structures - Decisions • The if statement: if ( x > 0 ) { cout << “positive”; } else { cout << “negative or zero”; }

  6. Control Structures - Decisions • The 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. Control Structures - Iteration • The for loop: for(k = 0; k < 10; k++ ) { cout << “The square of “ << k << “ is “ << k * k << endl; } Terminating condition Start condition Action taking place atthe end of each iteration

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

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

  10. Control Structures • And finally: • Yes, C++ has a goto. Don’t use it.

  11. Basics of C++ - pointer • A pointer points to a memory location which contains data of a particular type. The contents of a pointer is the address of some data • Declaration: • int *p; • double *aDoublePointer; 2.73817

  12. Again: Basics of C++ - arrays • Declaration: • int numbers[10]; • Declaration & Initialisation: • int primes[] = { 2, 3, 5, 7, 11, 13, 17, 19 }; • Access: • numbers[6] = 2483; • cout << “The fourth prime is “ << primes[4];

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

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

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

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

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

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

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

  20. class Creature { private: int yearOfBirth; public: void setYearOfBirth(year); int getYearOfBirth(); }; void Creature::setYearOfBirth { yearOfBirth = year; } int Creature::getYearOfBirth() { return yearOfBirth; } Example: The Creature class 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. class Creature { private: int yearOfBirth; public: void setYearOfBirth(year) { yearOfBirth = year; } int getYearOfBirth() { return yearOfBirth; } }; Example: The Creature class This method is an example for a ‘modifier’ method. It modifies the attribute. The method changes the state of the object.

  22. class Creature { private: int yearOfBirth; public: void setYearOfBirth(year) { yearOfBirth = year; } int getYearOfBirth() { return yearOfBirth; } }; Example: The Creature class 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. Classes & Objects • What may be different for all objects in a class, and what remains the same? • All 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. A class is defined by: A Unique Name Attributes Methods An object is defined by: Identity State Behaviour Objects & Classes

  25. Instantiating Objects • An 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. Multiple Objects • Of course we can create many objects of the same class: Creature myDog; Creature theMilkman; Creature myBestFriend; Creates three objects.

  27. Sending Messages / Calling Methods. • A 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. Back to the Instantiation... • An 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. class Creature { private: int yearOfBirth; public: // … Creature() { yearOfBirth = 1970; cout << “Hello.”; } }; The Creature class with a user defined default constructor. • The syntax for a constructoris similar as for a method, but: • It has the same name as the class. • It has no return value.

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

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

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

  33. A class is defined by: A Unique Name Attributes Methods An object is defined by: Identity State Behaviour Again: Objects & Classes

  34. A class is defined by: A Unique Name Attributes Methods An object is defined by: Identity State Behaviour Again: Objects & Classes But: We can give a class state and behaviour with the keyword static!

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

  36. Summary. • A class is a blueprint for an object. • Objects are created similar to other data types (int, char, …). • The construction of an object can be defined by the user. • Messages are sent to an object by calling a method. • static messes the concept of classes and objects (but is nevertheless useful).

More Related