Revision Lecture

2,005 views

Published on

1 Comment
3 Likes
Statistics
Notes
No Downloads
Views
Total views
2,005
On SlideShare
0
From Embeds
0
Number of Embeds
18
Actions
Shares
0
Downloads
118
Comments
1
Likes
3
Embeds 0
No embeds

No notes for slide

Revision Lecture

  1. 1. 1<br />Object Oriented ProgrammingDevelopment <br />
  2. 2. 2<br />What are we doing today?<br />Introduction of:<br />the lecturer<br />Objects<br />Basic Terminology<br />C++<br />the module<br />
  3. 3. 3<br />What is Object Oriented Programming?<br />Identifying objects and assigning responsibilities to these objects.<br />Objects communicate to other objects by sending messages.<br />Messages are received by the methods of an object<br />An object is like a black box.<br /> The internal details are hidden.<br />
  4. 4. 4<br />What is an object?<br />Tangible Things as a car, printer, ...<br />Roles as employee, boss, ...<br />Incidents as flight, overflow, ...<br />Interactions as contract, sale, ...<br />Specifications as colour, shape, …<br />
  5. 5. 5<br />So, what are objects?<br />an object represents an individual, identifiable item, unit, or entity, either real or abstract, with a well-defined role in the problem domain.<br />Or<br />An "object" is anything to which a concept applies.<br />Etc.<br />
  6. 6. 6<br />Why do we care about objects?<br />Modularity - large software projects can be split up in smaller pieces.<br />Reuseability - Programs can be assembled from pre-written software components.<br />Extensibility - New software components can be written or developed from existing ones.<br />
  7. 7. Example: The Person class<br />#include<string><br />#include<iostream><br />class Person{ <br /> char name[20]; <br /> int yearOfBirth;<br />public: <br /> void displayDetails() { <br /> cout << name << " born in " <br /> << yearOfBirth << endl; <br /> }<br /> //...<br />};<br />private <br />data<br />public <br />processes<br />
  8. 8. 8<br />The two parts of an object<br /> Object = Data + Methods<br />or to say the same differently:<br />An object has the responsibility to know and the responsibility to do.<br />=<br />+<br />
  9. 9. 9<br />Basic Terminology<br />Abstraction is the representation of the essential features of an object. These are ‘encapsulated’ into an abstract data type.<br />Encapsulation is the practice of including in an object everything it needs hidden from other objects. The internal state is usually not accessible by other objects.<br />
  10. 10. 10<br />Basic Terminology:Inheritance<br />Inheritance means that one class inherits the characteristics of another class.This is also called a “is a” relationship:<br />A car is a vehicle<br />A dog is an animal<br />A teacher is a person<br />
  11. 11. 11<br />Basic Terminology:Polymorphism<br />Polymorphism means “having many forms”. It allows different objects to respond to the same message in different ways, the response specific to the type of the object.<br />E.g. the message displayDetails() of the Person class should give different results when send to a Student object (e.g. the enrolment number).<br />
  12. 12. 12<br />Basic Terminology:Aggregation<br />Aggregation describes a “has a” relationship. One object is a part of another object. <br />We distinguish between composite aggregation (the composite “owns” the part) and shared aggregation (the part is shared by more then one composite).<br />A car has wheels.<br />
  13. 13. 13<br />Basic Terminology:Behaviour and Messages<br />The most important aspect of an object is its behaviour (the things it can do). A behaviour is initiated by sending a message to the object (usually by calling a method). <br />
  14. 14. 14<br />The two steps of Object Oriented Programming<br />Making Classes: Creating, extending or reusing abstract data types.<br />Making Objects interact: Creating objects from abstract data types and defining their relationships.<br />
  15. 15. 15<br />Module Outline<br />Introduction<br />The non object oriented basics<br />Classes<br />Design Approaches<br />Testing<br />Inheritance<br />Aggregation<br />Polymorphism<br />Multifile Development<br />
  16. 16. 16<br />Today:<br />Extensive analysis of an example program.<br />Data types, operators, functions and I/O.<br />Arrays, strings, pointers<br />Control structures.<br />
  17. 17. A typical C++ program<br />// FileID: hello.cpp<br />// Title: The program doing something<br />#include <iostream.h><br />void doSomething(int p);<br />int main() <br />{<br /> int p = 7;<br /> doSomething(p);<br /> cout << “I have something done.” << endl;<br /> return 0;<br />}<br />void doSomething(int p) { <br /> for( int i = 0; i < p; i++ ) { <br /> cout << “*” << endl; <br /> }<br />}<br />
  18. 18. A typical C++ program<br />// FileID: hello.cpp<br />// Title: The program doing something<br />#include <iostream.h><br />void doSomething(int p);<br />int main() <br />{<br /> int p = 7;<br /> doSomething(p);<br /> cout << “I have something done.” << endl;<br /> return 0;<br />}<br />void doSomething(int p) { <br /> for( int i = 0; i < p; i++ ) { <br /> cout << “*” << endl; <br /> }<br />}<br />The header of the file. Should contain general information as file name, author, description, etc.<br />The compiler ignores these lines (see next slide).<br />
  19. 19. A typical C++ program<br />// FileID: hello.cpp<br />// Title: The program doing something<br />#include <iostream.h><br />void doSomething(int p);<br />int main() <br />{<br /> int p = 7;<br /> doSomething(p);<br /> cout << “I have something done.” << endl;<br /> return 0;<br />}<br />void doSomething(int p) { <br /> for( int i = 0; i < p; i++ ) { <br /> cout << “*” << endl; <br /> }<br />}<br />This is a C++ comment.<br />Lines starting with // are ignored by the compiler.<br />Also ignored is text enclosed by /* and */<br />
  20. 20. A typical C++ program<br />// FileID: hello.cpp<br />// Title: The program doing something<br />#include <iostream.h><br />void doSomething(int p);<br />int main() <br />{<br /> int p = 7;<br /> doSomething(p);<br /> cout << “I have something done.” << endl;<br /> return 0;<br />}<br />void doSomething(int p) { <br /> for( int i = 0; i < p; i++ ) { <br /> cout << “*” << endl; <br /> }<br />}<br />A pre-processor directive. Lines starting with a # are interpreted by a pre-processor before the compiler processes the file.<br />Other important directives are #define, #ifdef, #endif, #pragma, ...<br />
  21. 21. A typical C++ program<br />// FileID: hello.cpp<br />// Title: The program doing something<br />#include <iostream.h><br />void doSomething(int p);<br />int main() <br />{<br /> int p = 7;<br /> doSomething(p);<br />cout << “I have something done.” << endl;<br /> return 0;<br />}<br />void doSomething(int p) { <br /> for( int i = 0; i < p; i++ ) { <br />cout << “*” << endl; <br /> }<br />}<br />In this example we include the file iostream.h into the program. iostream.h defines classes and objects related to input and output.<br />We need it here because cout is declared there.<br />
  22. 22. A typical C++ program<br />// FileID: hello.cpp<br />// Title: The program doing something<br />#include <iostream.h><br />void doSomething(int p);<br />int main() <br />{<br /> int p = 7;<br /> doSomething(p);<br />cout << “I have something done.” << endl;<br /> return 0;<br />}<br />void doSomething(int p) { <br /> for( int i = 0; i < p; i++ ) { <br />cout << “*” << endl; <br /> }<br />}<br />Observation: <br />cout is not a keyword of C++ but an identifier defined in the iostream library.<br />
  23. 23. A typical C++ program<br />// FileID: hello.cpp<br />// Title: The program doing something<br />#include <iostream.h><br />void doSomething(int p);<br />int main() <br />{<br /> int p = 7;<br /> doSomething(p);<br /> cout << “I have something done.” << endl;<br /> return 0;<br />}<br />void doSomething(int p) { <br /> for( int i = 0; i < p; i++ ) { <br /> cout << “*” << endl; <br /> }<br />}<br />A function prototype. <br />In this line the compiler is told about a function with the name doSomething and its signature. <br />The function here has one parameter (int) and no return value (void). <br />
  24. 24. A typical C++ program<br />// FileID: hello.cpp<br />// Title: The program doing something<br />#include <iostream.h><br />void doSomething(int p);<br />int main() <br />{<br /> int p = 7;<br /> doSomething(p);<br /> cout << “I have something done.” << endl;<br /> return 0;<br />}<br />void doSomething(int p) { <br /> for( int i = 0; i < p; i++ ) { <br /> cout << “*” << endl; <br /> }<br />}<br />Also global variables and class declarations usually come here.<br />
  25. 25. A typical C++ program<br />// FileID: hello.cpp<br />// Title: The program doing something<br />#include <iostream.h><br />void doSomething(int p);<br />int main()<br />{<br /> int p = 7;<br /> doSomething(p);<br /> cout << “I have something done.” << endl;<br /> return 0;<br />}<br />void doSomething(int p) { <br /> for( int i = 0; i < p; i++ ) { <br /> cout << “*” << endl; <br /> }<br />}<br />The main function is the entry point of a C++ program. Each C++ program must have exactly one main() method. <br />The return value of main() is an int. This value is passed to the system which invoked the C++ program.<br />
  26. 26. A typical C++ program<br />// FileID: hello.cpp<br />// Title: The program doing something<br />#include <iostream.h><br />void doSomething(int p);<br />int main()<br />{<br /> int p = 7;<br /> doSomething(p);<br /> cout << “I have something done.” << endl;<br /> return 0;<br />}<br />void doSomething(int p) { <br /> for( int i = 0; i < p; i++ ) { <br /> cout << “*” << endl; <br /> }<br />}<br />The code which implements the main function is enclosed in { and }. <br />Statements enclosed in { and } are called a block.<br />
  27. 27. A typical C++ program<br />// FileID: hello.cpp<br />// Title: The program doing something<br />#include <iostream.h><br />void doSomething(int p);<br />int main() <br />{<br />int p = 7;<br /> doSomething(p);<br /> cout << “I have something done.” << endl;<br /> return 0;<br />}<br />void doSomething(int p) { <br /> for( int i = 0; i < p; i++ ) { <br /> cout << “*” << endl; <br /> }<br />}<br />We declare a variable p of type int.<br />Other important data types of C++ are char, unsigned int, long, unsigned long, float, double, bool.<br />The variable p is immediately initialised with the value 7.<br />
  28. 28. A typical C++ program<br />// FileID: hello.cpp<br />// Title: The program doing something<br />#include <iostream.h><br />void doSomething(int p);<br />int main() <br />{<br /> int p = 7;<br />doSomething(p);<br /> cout << “I have something done.” << endl;<br /> return 0;<br />}<br />void doSomething(int p) { <br /> for( int i = 0; i < p; i++ ) { <br /> cout << “*” << endl; <br /> }<br />}<br />The function doSomething() is called with the parameter p. <br />
  29. 29. A typical C++ program<br />This statement prints the string “I have something done.” to the screen. <br />The “stream manipulator” endl outputs a newline and then “flushes the output buffer”.<br />// FileID: hello.cpp<br />// Title: The program doing something<br />#include <iostream.h><br />void doSomething(int p);<br />int main() <br />{<br /> int p = 7;<br /> doSomething(p);<br />cout << “I have something done.” << endl;<br /> return 0;<br />}<br />void doSomething(int p) { <br /> for( int i = 0; i < p; i++ ) { <br /> cout << “*” << endl; <br /> }<br />}<br />
  30. 30. A typical C++ program<br />Functions with a return value which is not void use the return keyword in order to return their value to the calling function.<br />In the special situation here, the main() method has no calling function. The value 0 is passed back to system when the program is finished. Usually 0 means that the program worked correctly.<br />// FileID: hello.cpp<br />// Title: The program doing something<br />#include <iostream.h><br />void doSomething(int p);<br />int main() <br />{<br /> int p = 7;<br /> doSomething(p);<br /> cout << “I have something done.” << endl;<br />return 0;<br />}<br />void doSomething(int p) { <br /> for( int i = 0; i < p; i++ ) { <br /> cout << “*” << endl; <br /> }<br />}<br />
  31. 31. A typical C++ program<br />// FileID: hello.cpp<br />// Title: The program doing something<br />#include <iostream.h><br />void doSomething(int p);<br />int main() <br />{<br /> int p = 7;<br /> doSomething(p);<br /> cout << “I have something done.” << endl;<br /> return 0;<br />}<br />void doSomething(int p) { <br /> for( int i = 0; i < p; i++ ) { <br /> cout << “*” << endl; <br /> }<br />}<br />The implementation of the previously defined function doSomething.<br />
  32. 32. A typical C++ program<br />// FileID: hello.cpp<br />// Title: The program doing something<br />#include <iostream.h><br />void doSomething(int p);<br />int main() <br />{<br /> int p = 7;<br /> doSomething(p);<br /> cout << “I have something done.” << endl;<br /> return 0;<br />}<br />void doSomething(int p) { <br /> for( int i = 0; i < p; i++ ) { <br /> cout << “*” << endl; <br /> }<br />}<br />
  33. 33. 33<br />Basics of C++ - data types<br />Some Fundamental data types:<br />char characters: ’a’, ’b’, ’ ’, ’’, ’7’<br />int integers: 3, 6883, -5, 0<br />double floating point numbers: 3.14, 7e9<br />bool true or false. <br />Also: float, long, unsigned long, short, unsigned char, wchar_t<br />
  34. 34. 34<br />Basics of C++ - variables<br />Declaring variables in a program:<br />char a;<br />int b; <br />double c;<br />Assignment:<br />b = 4; a = 'w’; c = -3.777;<br />int x = 78;<br />
  35. 35. 35<br />Basics of C++ - variables<br />Constants:<br />const double PI=3.1415926;<br />const int MAXBUFFER=20;<br />Note: the const keyword is also used for method parameters, methods and return values (later)<br />
  36. 36. 36<br />Basics of C++ - operators<br />Arithmetic operators:<br />+, -, *, /, %<br />Comparison:<br />==, !=, <, >, >=, <=<br />Logical:<br />&&, ||, !<br />Assignment:<br />=<br />Bitwise:<br />&, |, ~,^<br />Shortcuts:<br />+=, *=, ^=, (etc.)<br />Other:<br /><<, >>, ? :, ->, ., ,<br />
  37. 37. 37<br />Basics of C++ - operators<br />The unary operators ++ and --:<br />++ increment by 1<br />-- decrement by 1 <br />The language C++ got its name by this operator!<br />Note, that i++ and ++i have different behaviour […]<br />
  38. 38. 38<br />Basics of C++ - functions<br />int someFunction(double f, char c) { // …}<br />Name<br />Body<br />ParameterList<br />Return Type<br />
  39. 39. 39<br />Basics of C++ - functions<br />Please note, that a function is specified by the name and the parameter types. The following functions are all different:<br />int exampleFunction(int i, char c);<br />int exampleFunction(double f);<br />int exampleFunction();<br />int exampleFunction(char c, int i);<br />
  40. 40. 40<br />Basics of C++ - functions<br />Pass by reference, example:<br />void square(int &v) { v = v * v; }<br />In contrast, pass by value:<br />int square(int v) { return v * v; }<br />The parameter v is not copied.<br />
  41. 41. 41<br />Basics of C++: I/O<br />For output use cout, e.g.<br />cout << “The result is: “ << result << endl;<br />For input use cin, e.g.<br />cin >> x;<br />Note that iostream.h must be included via <br />#include <iostream.h><br />
  42. 42. 42<br />Basics of C++ - arrays<br />Declaration:<br />int numbers[10];<br />Declaration & Initialisation: <br />int primes[] = { 2, 3, 5, 7, 11, 13, 17, 19 };<br />Access: <br />numbers[6] = 2483;<br />cout << “The fourth prime is “ << primes[4];<br />
  43. 43. 43<br />Basics of C++ - Strings<br />There aren’t any strings in C++.<br />
  44. 44. 44<br />Basics of C++ - Strings<br />There aren’t any strings in C++.<br />O.k., that’s only half of the truth. In fact, by convention, strings are represented in C++ as ’’ terminated arrays of characters. In addition, the file string.h declares useful string manipulating functions, e.g. strcpy, strlen which deal with ’’ terminated character arrays.<br />
  45. 45. 45<br />Basics of C++ - pointer<br />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<br />Declaration: <br />int *p; <br />double *aDoublePointer;<br />
  46. 46. 46<br />Basics of C++ - pointer<br />In C++ pointer and arrays are strongly related (“array = pointer + memory”).<br />int primes[] = {2, 3, 5, 7, 11 };<br />int *aPr = primes;<br />aPr++;<br />cout << “The third prime is “ << *(aPr + 2); <br />pointer arithmetic<br />The * operator accesses the data on the memory address<br />
  47. 47. 47<br />Control Structures - Decisions<br />The if statement:<br />if ( x > 0 ) {<br /> cout << “positive”;<br />} else {<br /> cout << “negative or zero”;<br />}<br />
  48. 48. 48<br />Control Structures - Decisions<br />The switch statement - example:<br />int x;<br />cout << "Enter choice (1, 2, or 3)";<br />cin >> x;<br />switch(x) {<br /> case 1: doThis(); break;<br /> case 2: doThat(); break;<br /> case 3: doSomethingElse(); break;<br /> default: cout << "Sorry, invalid Input";<br />}<br />
  49. 49. 49<br />Control Structures - Iteration<br />The for loop:<br />for(k = 0; k < 10; k++ ) { <br /> cout << “The square of “ << k << “ is “<br /> << k * k << endl;<br /> }<br />Terminating condition<br />Start condition<br />Action taking place atthe end of each iteration<br />
  50. 50. 50<br />Control Structures - Iteration<br />The while loop:<br />while ( condition ) { <br /> // do something<br /> }<br />Equivalent to: <br />for( ; condition ; ) { <br /> // do something<br /> }<br />
  51. 51. 51<br />Control structures - do … while<br />The do … while loop:<br />do { <br /> // something<br />} while( condition);<br />Equivalent to:<br />// something<br />while( condition) { <br /> // something<br /> }<br />
  52. 52. 52<br />Control Structures<br />And finally: <br />Yes, C++ has a goto. Don’t use it.<br />
  53. 53. 53<br />Module Outline<br />Introduction<br />The non object oriented basics<br />Classes<br />Design Approaches<br />Testing<br />Inheritance<br />Aggregation<br />Polymorphism<br />Multifile Development<br />
  54. 54. 54<br />Today:<br />Control structures.<br />Pointers.<br />Classes<br />Objects<br />
  55. 55. 55<br />Basics of C++ - pointer<br />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<br />Declaration: <br />int *p; <br />double *aDoublePointer;<br />2.73817<br />
  56. 56. 56<br />Again: Basics of C++ - arrays<br />Declaration:<br />int numbers[10];<br />Declaration & Initialisation: <br />int primes[] = { 2, 3, 5, 7, 11, 13, 17, 19 };<br />Access: <br />numbers[6] = 2483;<br />cout << “The fourth prime is “ << primes[4];<br />
  57. 57. 57<br />Basics of C++ - pointer<br />In C++ pointer and arrays are strongly related (“array = pointer + memory”).<br />int primes[] = {2, 3, 5, 7, 11 };<br />int *aPr = primes;<br />cout << “The third prime is “ << *(aPr + 3); <br />The same as primes[3]<br />The * operator accesses the data on the memory address<br />
  58. 58. 58<br />What is Object Oriented Programming?<br />Identifying objects and assigning responsibilities to these objects.<br />Objects communicate to other objects by sending messages.<br />Messages are received by the methods of an object<br />An object is like a black box.<br /> The internal details are hidden.<br />
  59. 59. 59<br />The two steps of Object Oriented Programming<br />Making Classes: Creating, extending or reusing abstract data types.<br />Making Objects interact: Creating objects from abstract data types and defining their relationships.<br />
  60. 60. Example: The Creature class<br />class Creature { <br />private:<br /> int yearOfBirth;<br />public: <br /> void setYearOfBirth(year) {<br /> yearOfBirth = year;<br /> }<br /> int getYearOfBirth() { <br /> return yearOfBirth;<br /> } <br />};<br />born1997<br />
  61. 61. Example: The Creature class<br />class Creature{ <br />private:<br />int yearOfBirth;<br />public: <br />void setYearOfBirth(year) {<br /> yearOfBirth = year;<br /> }<br /> int getYearOfBirth() { <br /> return yearOfBirth;<br /> } <br />};<br />The definition of a <br />class:<br /><ul><li>The class keyword, followed by the class name.
  62. 62. private attributes.
  63. 63. public methods.
  64. 64. the ; at the end</li></li></ul><li>Example: The Creature class<br />class Creature {<br />private:<br /> int yearOfBirth;<br />public:<br />void setYearOfBirth(year) {<br /> yearOfBirth = year;<br /> }<br /> int getYearOfBirth() { <br /> return yearOfBirth;<br /> } <br />};<br />This class has anattribute of typeint. <br />Note that each C++<br />data type and also abstract data types can be used as attribute types. <br />
  65. 65. Example: The Creature class<br />class Creature {<br />private:<br /> int yearOfBirth;<br />public: <br /> void setYearOfBirth(year) {<br /> yearOfBirth = year;<br /> }<br /> int getYearOfBirth() { <br /> return yearOfBirth;<br /> } <br />};<br />This class has two (public) methods. One to set the attribute value and the other to retrieve the attribute value. <br />
  66. 66. Example: The Creature class<br />class Creature { <br />private:<br /> int yearOfBirth;<br />public: <br /> void setYearOfBirth(year);<br /> int getYearOfBirth();<br />};<br />void Creature::setYearOfBirth {<br /> yearOfBirth = year;<br /> }<br />int Creature::getYearOfBirth() { <br /> return yearOfBirth;<br /> } <br />Note that unless the methods are very short, declaration and implementation is usually separated.<br />The declaration goes into a header file (.h), the implementation in a .cpp file.<br />
  67. 67. Example: The Creature class<br />class Creature {<br />private:<br /> int yearOfBirth;<br />public: <br /> void setYearOfBirth(year) {<br /> yearOfBirth = year;<br /> }<br />int getYearOfBirth() { <br /> return yearOfBirth;<br /> } <br />};<br />This method is an example for a ‘modifier’ method. It modifies the attribute. The method changes the state of the object.<br />
  68. 68. Example: The Creature class<br />class Creature {<br />private:<br /> int yearOfBirth;<br />public: <br />void setYearOfBirth(year) {<br /> yearOfBirth = year;<br /> }<br /> int getYearOfBirth() { <br /> return yearOfBirth;<br /> } <br />};<br />This method is an example for a ‘selector’ method. It returns information about the attribute but does not change the state of the object.<br />
  69. 69. 67<br />Classes & Objects<br />What may be different for all objects in a class, and what remains the same?<br />All the objects in a class may have different attribute values (state data), but their allowed behaviours are all the same.<br />So a class is a blueprint for objects<br />
  70. 70. 68<br />Objects & Classes<br />A class is defined by:<br />A Unique Name<br />Attributes<br />Methods<br />An object is defined by:<br />Identity<br />State<br />Behaviour<br />
  71. 71. 69<br />Instantiating Objects<br />An object is instantiated just like any other data type:<br />int x;<br />char y;<br />Creature z; <br />Declaring z of type ‘creature’ means we have generated an object with the attributes and methods of the class.<br />
  72. 72. 70<br />Multiple Objects<br />Of course we can create many objects of the same class:<br />Creature myDog;<br />Creature theMilkman;<br />Creature myBestFriend;<br />Creates three objects.<br />
  73. 73. 71<br />Sending Messages / Calling Methods.<br />A message is send to an object by calling a method of this object. Use the . (dot) for calling a method of an object.<br />int k; <br />k = theMilkman.getYearOfBirth();<br />myDog.setYearOfBirth(1998);<br />Messages are sent to my dog and the milkman.<br />
  74. 74. 72<br />Back to the Instantiation...<br />An object is instantiated just like any other data type:<br />int x;<br />char y;<br />Creature z; <br />Here the “default constructor” of the Creature class is automatically called.<br />If we don’t like this we can specify constructors explicitly!<br />
  75. 75. The Creature class with a user defined default constructor.<br />class Creature { <br />private:<br /> int yearOfBirth;<br />public:<br /> // …<br /> Creature() { <br /> yearOfBirth = 1970;<br /> cout << “Hello.”;<br /> } <br />};<br />The syntax for a constructoris similar as for a method, but:<br /><ul><li>It has the same name as the class.
  76. 76. It has no return value.</li></li></ul><li>The Creature with a parametrized constructor.<br />class Creature { <br />private:<br /> int yearOfBirth;<br />public:<br /> // …<br /> Creature(int year) { <br /> yearOfBirth = year;<br /> } <br />};<br />This constructor can be used as follows:<br />Creature theMilkman(1953);<br />instantiates a 49 years old milkman.<br />
  77. 77. The Creature with a copy constructor.<br />Example:<br />Creature myDog(1995);<br />Creature myCat(myDog);<br />creates a cat of the same age as the dog.<br />class Creature { <br />private:<br /> int yearOfBirth;<br />public:<br /> // …<br /> Creature(Creature & otherCreature) { <br /> yearOfBirth =<br /> otherCreature.getYearOfBirth();<br /> } <br />};<br />
  78. 78. 76<br />Constructors - summary<br />A constructor is always called when an object is created.<br />We can define our own constructors (Note: a class can have more than one constructor).<br />If an object is copied from another object then the copy constructor is called.<br />
  79. 79. 77<br />Again: Objects & Classes<br />A class is defined by:<br />A Unique Name<br />Attributes<br />Methods<br />An object is defined by:<br />Identity<br />State<br />Behaviour<br />
  80. 80. 78<br />Again: Objects & Classes<br />A class is defined by:<br />A Unique Name<br />Attributes<br />Methods<br />An object is defined by:<br />Identity<br />State<br />Behaviour<br />But: We can give a class state and behaviour with the keyword static!<br />
  81. 81. Example: The Creature class<br />class Creature { <br />private:<br /> int yearOfBirth;<br /> static int numberOfAllCreatures = 0;<br />public: <br /> Creature() { // Constructor - counts the creatures.<br /> numberOfAllCreatures++;<br /> }<br /> static int getNumberOfAllCreatures() { <br /> return numberOfAllCreatures;<br /> }<br />};<br />Note that all objects share the same value of the “class attribute” numberOfAllCreatures.<br />
  82. 82. 80<br />Summary.<br />A class is a blueprint for an object.<br />Objects are created similar to other data types (int, char, …).<br />The construction of an object can be defined by the user.<br />Messages are sent to an object by calling a method.<br />static messes the concept of classes and objects (but is nevertheless useful).<br />

×