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

Slides:



Advertisements
Similar presentations
Lesson 13 Introduction to Classes CS1 Lesson Introduction to Classes1.
Advertisements

Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Programming Languages and Paradigms
Written by: Dr. JJ Shepherd
1 Object Oriented Programming Development - Week 5 z By: Marc Conrad University of Luton z z Room: D104.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
Computer Science 1620 Loops.
CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Introduction to Data Structures, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Object Oriented Programming Concepts.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 10 Classes Continued
C++ fundamentals.
Distribution of Marks Internal Sessional Evaluation Assignments – 10 Quizzes – 10 Class Participation Attendence – 5 Mid – Term Test – 25 External Evaluation.
1 Object Oriented Programming Development z By: Marc Conrad University of Luton z z Room: D104.
11 Introduction to Object Oriented Programming (Continued) Cats II.
Object Oriented Software Development
Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Programming Languages and Paradigms Object-Oriented Programming.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
1 Object Oriented Programming Development - Polymorphism I z By: Marc Conrad & Rob Manton University of Luton z
Object Oriented Programming Development
1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z z Room: D104.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
1 Practical test tomorrow zWill involve writing a simple class zinstantiating objects zother C++ constructs as practised in the lab sheets to date zinheritance.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
1 Object Oriented Programming Development - Multi File Development z By: Marc Conrad & Rob Manton University of Luton z
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
1 Object Oriented Programming Development - Week7 z Rob Manton z z Room D104.
Object-Oriented Programming in C++
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Object-Oriented Programming in C++ More examples of Association.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
1 CSC241: Object Oriented Programming Lecture No 02.
Designing Classes CS239 – Jan 26, Key points from yesterday’s lab  Enumerated types are abstract data types that define a set of values.  They.
1 Object Oriented Programming Development z By: Marc Conrad University of Luton z z Room: D104.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
OOP Basics Classes & Methods (c) IDMS/SQL News
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
Object Oriented Programming Development
Programming Logic and Design Seventh Edition
Review: Two Programming Paradigms
Chapter 3: Using Methods, Classes, and Objects
This technique is Called “Divide and Conquer”.
Introduction to Classes
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Object-Oriented Programming Using C++
BCA-II Object Oriented Programming using C++
CIS 199 Final Review.
Classes and Objects Systems Programming.
Presentation transcript:

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

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

3 Today: zControl structures. zPointers. zClasses zObjects

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

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 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 Control Structures - Iteration zThe while loop: while ( condition ) { // do something } Equivalent to: for( ; condition ; ) { // do something }

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

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

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;

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

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

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

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.

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.

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.

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.

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 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 Objects & Classes zA class is defined by: yA Unique Name yAttributes yMethods z An object is defined by: yIdentity yState yBehaviour

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

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

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.

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.

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 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 Again: Objects & Classes zA class is defined by: yA Unique Name yAttributes yMethods z An object is defined by: yIdentity yState yBehaviour

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!

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