G52PGP. Lecture OO2 Object Oriented programming in C

Size: px
Start display at page:

Download "G52PGP. Lecture OO2 Object Oriented programming in C"

Transcription

1 G52PGP Lecture OO2 Object Oriented programming in C

2 Last OO lecture Brief comments about the Object Oriented Paradigm Reminder of basic C code Including structs and pointers A simple one-function C program Functional decomposition Collecting data together into a struct Passing a pointer to the structto functions to use 2

3 Questions from the decomposition Where is the data going to be stored? It was in local variables for that function only Do we move it into globals? Or pass pointers to the local variables into each function call? Do we need to have multiple copies? (e.g. single program runs two games simultaneously?) Maintenance How hard is it to change something? What if I want a random number of zombies? What if I want to add new types of map items e.g. teleporters, treasure to pick up, etc What if I want multiple types of zombies? Maybe with some random behaviour? 3

4 This Lecture An object oriented approach in C We will try this from a C point of view, to see what is happening, and what features actually do We need to cheat slightly since C doesn t support overloaded functions (i.e. compiling as C++) We will use structfor our objects Group data together into coherent objects Create functions which work on those objects Let ONLY those functions access the data in the objects Pass a pointer to the object to work on into the function

5 Benefits and penalties of this approach Benefits: More control over the access to the data The internal data format can be changed Modifications/maintenance much easier As long as the functions don t change then nobody should even realise it Can add checks, logging, etcon the data access Penalties More code to write Partly because C doesn t have the object oriented support natively would be less code in C++ or Java Java and C++ have better support to reduce the amount of code that we have to write so don t worry

6 Data in Zombies struct Data int zombie_x[max_zombies ]; int zombie_y[max_zombies ]; char zombie_old[max_zombies]; int player_x = 0; int player_y = 0; char player_old = 0; int pit_x, pit_y; char* display[max_rows]; ;

7 Data in Zombies struct Data int zombie_x[max_zombies ]; int zombie_y[max_zombies ]; char zombie_old[max_zombies]; int player_x = 0; int player_y = 0; char player_old = 0; int pit_x, pit_y; char* display[max_rows]; ; x, y, content x, y, content x, y

8 Zombies Game as interacting objects Display (Board/Map) 2D array of contents Position X Y struct Data Mover zombie[max_zombies]; Mover player; Position pit; Display d; ; Mover X Y Old content

9 Decomposition and warning Decomposition basically means splitting up the problem into smaller parts Here we have decomposed the data which is needed into smaller clumps of data And we will define some functions to work on that data There are other ways to split this up There are probably better ways to handle the display data (the strings) Remember this when you come back to revise this is not the ideal object model for this problem 9

10 Zombies Game as interacting objects Display (Board/Map) 2D array of contents Position X Y struct Data Mover zombie[max_zombies]; Mover player; Position pit; Display d; ; Mover X Y Old content

11 Example : simulating position objects struct Position int x; int y; ; Create the structure to group the data Create distinct set of functions to use it Simplest functions are: Getters - Accessors Setters Mutators Use these functions NEVER access the data directly from another function // Getters get values int getpositionx( Position* p ) // Getter return p->x; int getpositiony( Position* p ) // Getter return p->y; // Setter sets values void setposition( Position* p, int newx, int newy ) // Setter p->x = newx; p->y = newy; 11

12 Other Position Functions struct Position ; int x; int y; int getpositionx( Position* p ) return p->x; int getpositiony( Position* p ) return p->y; void setposition( Position* p, int newx, int newy ) p->x = newx; p->y = newy; void modifyposition( Position* p, int xoffset, int yoffset ) p->x += xoffset; p->y += yoffset; void randomizepositiondifferentto( Position* pnew, Position* pcheck ) pnew->x = rand() % MAX_COLS; pnew->y = rand() % MAX_ROWS; if ( (pnew->x == pcheck->x) && (pnew->y == pcheck->y) ) randomizepositiondifferentto( pnew, pcheck );

13 The mover object could contain a position Position X Y Mover X Y Old content Mover X Y Old content Position struct Mover // Figure which can move // Embed Position object inside, which has x & y Position p; // Old value at location char cvalue; ;

14 Simple mover functions struct Mover // Figure which can move Position p; // Embed Position object inside, which has x & y char cvalue; // Old value at location ; // Simplest wrapper - just avoids ever getting the value directly int getstoredcontent( Mover* pm ) return pm->cvalue; // Simplest wrapper - just avoids ever setting the value directly void setstoredcontent( Mover* pm, char value ) pm->cvalue = value;

15 Other functions on Mover // Get the position from the embedded position object int getpositionx( Mover* pm ) int getpositiony( Mover* pm ) Position* getposition( Mover* pm ) // Questionable // Set the position: give x and y positions or offset void setposition( Mover* pm, int newx, int newy ) void modifyposition( Mover* pm, int xoffset, int yoffset ) // Randomly determine the original position, in various ways void randomizeposition( Mover* pm ) // Totally random void randomizezombieposition( Mover* pm ) // Random in outside edges // Different to a specific position or another mover void randomizepositiondifferentto( Mover* pset, Position* pcheck ) void randomizepositiondifferentto( Mover* pset, Mover* pcheck ) int isdead( Mover* pm ) // Is the mover dead (x and y are -1 if so) void setdead( Mover* pm ) // Record that mover is dead // This function uses the existing get and modify position functions void movetowardsothermover( Mover* pthis, Mover* target ) 15

16 Finally, Display struct Display char* data[max_rows]; // renamed to data ; // Basic functions create/draw/destroy the strings void createdisplaydata( Display* pd ) // Initialise it void freedisplaydata( Display* pd ) // Free it void drawdisplay( Display* pd ) // Draw the display // Retrieve the value at a location int getdisplayvalue( Display* pd, int x, int y ) // Set value given x and y coordinates or a position void setdisplayvalue( Display* pd, int x, int y, int value ) void setdisplayvalue( Display* pd, Position* p, int value ) 16

17 And some functions to change the map struct Display char* data[max_rows]; // renamed to data ; // Set value given a position object void setdisplayvalueforposition( Display* pd, Position* pos, int value ) // Set value for Mover s position to stored or fixed value void setdisplayvaluetomoverstored( Display* pd, Mover* m ) void setdisplayvalueatmoverposition( Display* pd, Mover* m, char cvalue ) // Retrieve the value from map, and optionally store in a mover void storedisplayvalueinmover( Display* pd, Mover* m ) char retrievevalueatmoverposition( Display* pd, Mover* m ) 17

18 The main function is conceptually easier Old code: for ( i = 0 ; i < MAX_ZOMBIES ; i++ ) if ( pdata->zombie_y[i] >= 0 ) New code: pdata->display[pdata->zombie_y[i]] [pdata->zombie_x[i]] = ZOMBIE; for ( i = 0 ; i < MAX_ZOMBIES ; i++ ) if ( isdead( zombie[i] ) == 0 ) setdisplayvalueatmoverposition( display, zombie[i], ZOMBIE ); We can see more easily what it is actually doing 18

19 The calling function is conceptually easier Old code: for ( i = 0 ; i < MAX_ZOMBIES ; i++ ) if ( pdata->zombie_y[i] >= 0 ) pdata->display[pdata->zombie_y[i]] [pdata->zombie_x[i]] = ZOMBIE; Note: I cheated actually these are New code: pointers to the for ( i = 0 ; i < MAX_ZOMBIES ; i++ ) data but I simplified it for if ( isdead( zombie[i] ) == 0 ) this slide setdisplayvalueatmoverposition( display, zombie[i], ZOMBIE ); We can see more easily what it is actually doing 19

20 Decomposition Here we used the data to identify possible objects because we already had the data This is usually a BAD way to create an object oriented program it will lead to a poor design Later we will see other ways to identify objects Similar to ER diagrams in database theory too In software engineering module you will see some more detail about OO design We have now divided the work but not functional decomposition by object-based decomposition The objects collaborate together to solve tasks Each of the individual functions is quite simple The functions on the objects work together to solve tasks As a developer, you can think of one object at a time

21 Simplifying matters This got us to the fake functions sample Now we will go further 21

22 Example was a lot of work struct Position int x; int y; ; Each of these functions has the same first parameter We had to type Position* p each time And we had to specify p-> to use the data // Getters get values int getpositionx( Position* p ) return p->x; int getpositiony( Position* p ) return p->y; // Setter sets values void setposition( Position* p, int newx, int newy ) p->x = newx; p->y = newy; 22

23 We could name the parameter consistently struct Position int x; int y; ; Since the parameter is the same, and always refers to the current object, we could give it a consistent name, e.g. this // Getters get values int getpositionx( Position* this ) return this->x; int getpositiony( Position* this ) return this->y; // Setter sets values void setposition( Position* this, int newx, int newy ) this->x = newx; this->y = newy; 23

24 First parameter can be implicit struct Position int x; int y; <insert functions here> Declare functions within the class compiler knows that they need to have an object to act on and makes a hiddenfirst parameter, called this // Getters get values int getpositionx(position* this) return this->x; int getpositiony(position* this) return this->y; // Setter sets values void setposition(position* this, int newx, int newy ) this->x = newx; this->y = newy; 24

25 The this-> can be implicit too struct Position int x; int y; <insert functions here> Compiler also knows that you will often access member data or member functions The compiler will check whether adding a this-> works before checking for globals with that name // Getters get values int getpositionx() return this->x; int getpositiony() return this-> y; // Setter sets values void setposition( int newx, int newy ) this->x = newx; this->y = newy; 25

26 struct Position int x; int y; Final code for a Position int getpositionx() return x; int getpositiony() return y; void setposition( int newx, int newy ) x = newx; y = newy; Note: in C++ we would actually do something slightly different to this, but we are heading for Java so I did it this way Attend the C++ Programming module to learn more Final version has a lot less code but does the same thing as the original version 26

27 Calling a function struct Position int x; // etc for y int getpositionx() return x; ; int main( int argc, char* argv[] ) struct Position pos; pos.setposition(3, 4); printf("position is %d, %d\n", pos.getpositionx(), pos.getpositiony()); printf("position is %d, %d\n", pos.x, pos.y ); When we call the function we no longer specify an object pointer as a first parameter. Instead we specify the object to act on before a. The same way as we would access member data jaa@shadowfax2 ~ $ g++ struct_test.cpp jaa@shadowfax2 ~ $./a.exe Position is 3, 4 Position is 3, 4 27

28 Class, object and struct In OO design and programming, you define a set of classes A class is a type of object A class specifies the data that each object of that class has Defining a class does not create any objects of that class An object is an instance of the class The lump of data of the format specified by the class i.e. each object actually resides somewhere in memory It will have a set of functions which act upon it 28

29 Public and private data (1) Nothing so far has stopped things outside of our class from accessing its data We can do so by making the data private struct Position private: int x; int y; public: int getpositionx() return x; int getpositiony() return y; ; void setposition(int newx, int newy) x = newx; y = newy; 29

30 Public and private data (2) If we do so, then the compiler will not let us access the private data struct Position private: int x; int y; public: int getpositionx() int getpositiony() int main( int argc, char* argv[] ) struct Position pos; pos.setposition(3, 4); printf("position is %d, %d\n", pos.getpositionx(), pos.getpositiony()); ; void setposition( int newx, int newy) printf("position is %d, %d\n", pos.x, pos.y ); return 0; 30

31 Public and private data (2) If we do so, then the compiler will not let us access the private data struct Position private: pos.x, pos.y); int^ x; int y; public: int getpositionx() int getpositiony() ; struct_test_private.cpp:10:6: error: int Position::x is private intx; ^ struct_test_private.cpp:38:7: error: within this context int main( int argc, char* argv[] ) struct Position pos; pos.setposition(3, 4); printf("position is %d, %d\n", pos.getpositionx(), pos.getpositiony()); struct_test_private.cpp:11:6: error: int Position::y is private inty; ^ void setposition( struct_test_private.cpp:38:14: int newx, int newy) error: within this context pos.x, pos.y); printf("position is %d, %d\n", pos.x, pos.y ); return 0; 31

32 Static members If we add data (variables) or methods (functions) to a class (or struct), it adds an implicit parameter (this), to say which object the function to acting on We may want to make a function a member (so that it can access private functions or data) but not give it the hidden this pointer This is basically a global function with access to the private data and functions Just make this a static member E.g. static int compare(position* pos1, Position* pos2) // Compare somehow? return (pos1.x - pos2.x) + (pos1.y - pos2.y); 32

33 Classes, instances and static Classes are outlines for objects Specify the data stored Specify the functions which can act on the data Objects are lumps of data You usually associate functions and data with instances (instance methods, instance data) You can associate them with the class Rather than each instance Make them static Static methods have no this pointer no instance Static data has one copy only, not one per instance I.e. shared by all instances (like global data) 33

34 Why all of this detail? When students get to the C++ programming module in year 2, I have seen a lot of confusion over what classes and objects are Also a lot of confusion over what static means A lot of erroneous use of static in Java by students Eclipse will often suggest making functions static to fix compilation errors A lot of procedural programming paradigm code that looks object oriented but isn t I am determined that you will understand You need to understand what static means You need to understand what a member function really is Question: If I add a member function to a class/struct, do the objects grow? What if I add member data? 34

35 Summary Member functions/methods are just functions with an extra (hidden) pointer to denote which object they are currently working on obj.foo() means apply function foo() to object obj (i.e. the hidden this pointer points at obj) You can protect data (or functions) so that ONLY members can access them You can make something a member without implicitly adding the hidden thispointed (e.g. if it does not act on a specific object) Make it static It can still access private data if you give it a pointer to an object somehow You can make data static too we will see this later 35

36 Next Lecture An introduction to Java Creating our first Hello World Java program Text output Some Java Classes Simple Graphical User Interfaces Our second, graphical, Hello World program 36

G52CPP C++ Programming Lecture 9

G52CPP C++ Programming Lecture 9 G52CPP C++ Programming Lecture 9 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Last lecture const Constants, including pointers The C pre-processor And macros Compiling and linking And

More information

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

G52CPP C++ Programming Lecture 7. Dr Jason Atkin G52CPP C++ Programming Lecture 7 Dr Jason Atkin 1 This lecture classes (and C++ structs) Member functions inline functions 2 Last lecture: predict the sizes 3 #pragma pack(1) #include struct A

More information

Variables and literals

Variables and literals Demo lecture slides Although I will not usually give slides for demo lectures, the first two demo lectures involve practice with things which you should really know from G51PRG Since I covered much of

More information

CSE 374 Programming Concepts & Tools

CSE 374 Programming Concepts & Tools CSE 374 Programming Concepts & Tools Hal Perkins Fall 2017 Lecture 8 C: Miscellanea Control, Declarations, Preprocessor, printf/scanf 1 The story so far The low-level execution model of a process (one

More information

QUIZ. What is wrong with this code that uses default arguments?

QUIZ. What is wrong with this code that uses default arguments? QUIZ What is wrong with this code that uses default arguments? Solution The value of the default argument should be placed in either declaration or definition, not both! QUIZ What is wrong with this code

More information

These are notes for the third lecture; if statements and loops.

These are notes for the third lecture; if statements and loops. These are notes for the third lecture; if statements and loops. 1 Yeah, this is going to be the second slide in a lot of lectures. 2 - Dominant language for desktop application development - Most modern

More information

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides 1B1b Inheritance Agenda Introduction to inheritance. How Java supports inheritance. Inheritance is a key feature of object-oriented oriented programming. 1 2 Inheritance Models the kind-of or specialisation-of

More information

C++ for Java Programmers

C++ for Java Programmers Basics all Finished! Everything we have covered so far: Lecture 5 Operators Variables Arrays Null Terminated Strings Structs Functions 1 2 45 mins of pure fun Introduction Today: Pointers Pointers Even

More information

Lecture 2, September 4

Lecture 2, September 4 Lecture 2, September 4 Intro to C/C++ Instructor: Prashant Shenoy, TA: Shashi Singh 1 Introduction C++ is an object-oriented language and is one of the most frequently used languages for development due

More information

G52CPP C++ Programming Lecture 13

G52CPP C++ Programming Lecture 13 G52CPP C++ Programming Lecture 13 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Last lecture Function pointers Arrays of function pointers Virtual and non-virtual functions vtable and

More information

CS 251 Intermediate Programming Inheritance

CS 251 Intermediate Programming Inheritance CS 251 Intermediate Programming Inheritance Brooke Chenoweth University of New Mexico Spring 2018 Inheritance We don t inherit the earth from our parents, We only borrow it from our children. What is inheritance?

More information

Kurt Schmidt. October 30, 2018

Kurt Schmidt. October 30, 2018 to Structs Dept. of Computer Science, Drexel University October 30, 2018 Array Objectives to Structs Intended audience: Student who has working knowledge of Python To gain some experience with a statically-typed

More information

Procedures, Parameters, Values and Variables. Steven R. Bagley

Procedures, Parameters, Values and Variables. Steven R. Bagley Procedures, Parameters, Values and Variables Steven R. Bagley Recap A Program is a sequence of statements (instructions) Statements executed one-by-one in order Unless it is changed by the programmer e.g.

More information

The University of Nottingham

The University of Nottingham The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 2 MODULE, SPRING SEMESTER 2011-2012 G52CPP C++ Programming Examination Time allowed TWO hours Candidates may complete the front cover of

More information

G52PGP. Lecture oo3 Java (A real object oriented language)

G52PGP. Lecture oo3 Java (A real object oriented language) G52PGP Lecture oo3 Java (A real object oriented language) 1 Last lecture Associating functions with data into objects is an alternative way to decompose a program Can then consider each object on its own

More information

Final CSE 131B Spring 2004

Final CSE 131B Spring 2004 Login name Signature Name Student ID Final CSE 131B Spring 2004 Page 1 Page 2 Page 3 Page 4 Page 5 Page 6 Page 7 Page 8 (25 points) (24 points) (32 points) (24 points) (28 points) (26 points) (22 points)

More information

377 Student Guide to C++

377 Student Guide to C++ 377 Student Guide to C++ c Mark Corner January 21, 2004 1 Introduction In this course you will be using the C++ language to complete several programming assignments. Up to this point we have only provided

More information

5) Attacker causes damage Different to gaining control. For example, the attacker might quit after gaining control.

5) Attacker causes damage Different to gaining control. For example, the attacker might quit after gaining control. Feb 23, 2009 CSE, 409/509 Mitigation of Bugs, Life of an exploit 1) Bug inserted into code 2) Bug passes testing 3) Attacker triggers bug 4) The Attacker gains control of the program 5) Attacker causes

More information

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control. C Flow Control David Chisnall February 1, 2011 Outline What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope Disclaimer! These slides contain a lot of

More information

Software Design and Analysis for Engineers

Software Design and Analysis for Engineers Software Design and Analysis for Engineers by Dr. Lesley Shannon Email: lshannon@ensc.sfu.ca Course Website: http://www.ensc.sfu.ca/~lshannon/courses/ensc251 Simon Fraser University Slide Set: 1 Date:

More information

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011 Print Your Name: Page 1 of 8 Signature: Aludra Loginname: CSCI 102L - Data Structures Midterm Exam #1 Fall 2011 (10:00am - 11:12am, Wednesday, October 5) Instructor: Bill Cheng Problems Problem #1 (24

More information

G52CPP C++ Programming Lecture 12

G52CPP C++ Programming Lecture 12 G52CPP C++ Programming Lecture 12 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Last lecture this and static members References Act like pointers Look like values More const And mutable

More information

What is it? CMSC 433 Programming Language Technologies and Paradigms Spring Approach 1. Disadvantage of Approach 1

What is it? CMSC 433 Programming Language Technologies and Paradigms Spring Approach 1. Disadvantage of Approach 1 CMSC 433 Programming Language Technologies and Paradigms Spring 2007 Singleton Pattern Mar. 13, 2007 What is it? If you need to make sure that there can be one and only one instance of a class. For example,

More information

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

G52CPP C++ Programming Lecture 3. Dr Jason Atkin G52CPP C++ Programming Lecture 3 Dr Jason Atkin E-Mail: jaa@cs.nott.ac.uk 1 Revision so far C/C++ designed for speed, Java for catching errors Java hides a lot of the details (so can C++) Much of C, C++

More information

D Programming Language

D Programming Language Group 14 Muazam Ali Anil Ozdemir D Programming Language Introduction and Why D? It doesn t come with a religion this is written somewhere along the overview of D programming language. If you actually take

More information

COSC 2P95 Lab 5 Object Orientation

COSC 2P95 Lab 5 Object Orientation COSC 2P95 Lab 5 Object Orientation For this lab, we'll be looking at Object Orientation in C++. This is just a cursory introduction; it's assumed that you both understand the lecture examples, and will

More information

COMP 250 Winter 2011 Reading: Java background January 5, 2011

COMP 250 Winter 2011 Reading: Java background January 5, 2011 Almost all of you have taken COMP 202 or equivalent, so I am assuming that you are familiar with the basic techniques and definitions of Java covered in that course. Those of you who have not taken a COMP

More information

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University Lecture 3 COMP1006/1406 (the Java course) Summer 2014 M. Jason Hinek Carleton University today s agenda assignments 1 (graded) & 2 3 (available now) & 4 (tomorrow) a quick look back primitive data types

More information

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java CS121/IS223 Object Reference Variables Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors Agenda

More information

CS103 Handout 29 Winter 2018 February 9, 2018 Inductive Proofwriting Checklist

CS103 Handout 29 Winter 2018 February 9, 2018 Inductive Proofwriting Checklist CS103 Handout 29 Winter 2018 February 9, 2018 Inductive Proofwriting Checklist In Handout 28, the Guide to Inductive Proofs, we outlined a number of specifc issues and concepts to be mindful about when

More information

Week 5, continued. This is CS50. Harvard University. Fall Cheng Gong

Week 5, continued. This is CS50. Harvard University. Fall Cheng Gong This is CS50. Harvard University. Fall 2014. Cheng Gong Table of Contents News... 1 Buffer Overflow... 1 Malloc... 6 Linked Lists... 7 Searching... 13 Inserting... 16 Removing... 19 News Good news everyone!

More information

Object-Oriented Programming, Iouliia Skliarova

Object-Oriented Programming, Iouliia Skliarova Object-Oriented Programming, Iouliia Skliarova CBook a = CBook("C++", 2014); CBook b = CBook("Physics", 1960); a.display(); b.display(); void CBook::Display() cout

More information

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet. Mr G s Java Jive #2: Yo! Our First Program With this handout you ll write your first program, which we ll call Yo. Programs, Classes, and Objects, Oh My! People regularly refer to Java as a language that

More information

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program: Welcome Back CSCI 262 Data Structures 2 - Review What you learned in CSCI 261 (or equivalent): Variables Types Arrays Expressions Conditionals Branches & Loops Functions Recursion Classes & Objects Streams

More information

Classes. C++ Object Oriented Programming Pei-yih Ting NTOU CS

Classes. C++ Object Oriented Programming Pei-yih Ting NTOU CS Classes C++ Object Oriented Programming Pei-yih Ting NTOU CS 1 Encapsulation Access Specifiers Default Access Private Data Public vs. Private Functions Object State Scope Inline Member Functions Constant

More information

A brief introduction to C++

A brief introduction to C++ A brief introduction to C++ Rupert Nash r.nash@epcc.ed.ac.uk 13 June 2018 1 References Bjarne Stroustrup, Programming: Principles and Practice Using C++ (2nd Ed.). Assumes very little but it s long Bjarne

More information

CS121/IS223. Object Reference Variables. Dr Olly Gotel

CS121/IS223. Object Reference Variables. Dr Olly Gotel CS121/IS223 Object Reference Variables Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors CS121/IS223

More information

CS18000: Programming I

CS18000: Programming I CS18000: Programming I Data Abstraction January 25, 2010 Prof. Chris Clifton Announcements Book is available (Draft 2.0) Syllabus updated with readings corresponding to new edition Lab consulting hours

More information

such a manner that we are able to understand, grasp and grapple with the problem at hand in a more organized fashion.

such a manner that we are able to understand, grasp and grapple with the problem at hand in a more organized fashion. Programming and Data Structure Dr.P.P.Chakraborty Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture 32 Conclusions Hello everybody. Today, we come to the

More information

And Even More and More C++ Fundamentals of Computer Science

And Even More and More C++ Fundamentals of Computer Science And Even More and More C++ Fundamentals of Computer Science Outline C++ Classes Special Members Friendship Classes are an expanded version of data structures (structs) Like structs, the hold data members

More information

Pointers and scanf() Steven R. Bagley

Pointers and scanf() Steven R. Bagley Pointers and scanf() Steven R. Bagley Recap Programs are a series of statements Defined in functions Can call functions to alter program flow if statement can determine whether code gets run Loops can

More information

CMPSCI 187 / Spring 2015 Hangman

CMPSCI 187 / Spring 2015 Hangman CMPSCI 187 / Spring 2015 Hangman Due on February 12, 2015, 8:30 a.m. Marc Liberatore and John Ridgway Morrill I N375 Section 01 @ 10:00 Section 02 @ 08:30 1 CMPSCI 187 / Spring 2015 Hangman Contents Overview

More information

Making Inheritance Work: C++ Issues

Making Inheritance Work: C++ Issues Steven Zeil September 19, 2013 Contents 1 Base Class Function Members 2 2 Assignment and Subtyping 3 3 Virtual Destructors 5 4 Virtual Assignment 9 5 Virtual constructors 11 51 Cloning 13 6 Downcasting

More information

Making Inheritance Work: C++ Issues

Making Inheritance Work: C++ Issues Steven Zeil September 19, 2013 Contents 1 Base Class Function Members 2 2 Assignment and Subtyping 3 3 Virtual Destructors 4 4 Virtual Assignment 7 5 Virtual constructors 9 51 Cloning 11 6 Downcasting

More information

Operator overloading

Operator overloading 1 Introduction 2 The copy constructor 3 Operator Overloading 4 Eg 1: Adding two vectors 5 The -> operator 6 The this pointer 7 Overloading = 8 Unary operators 9 Overloading for the matrix class 10 The

More information

My malloc: mylloc and mhysa. Johan Montelius HT2016

My malloc: mylloc and mhysa. Johan Montelius HT2016 1 Introduction My malloc: mylloc and mhysa Johan Montelius HT2016 So this is an experiment where we will implement our own malloc. We will not implement the world s fastest allocator, but it will work

More information

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture - 31 Static Members Welcome to Module 16 of Programming in C++.

More information

Heap Arrays. Steven R. Bagley

Heap Arrays. Steven R. Bagley Heap Arrays Steven R. Bagley Recap Data is stored in variables Can be accessed by the variable name Or in an array, accessed by name and index a[42] = 35; Variables and arrays have a type int, char, double,

More information

Supporting Class / C++ Lecture Notes

Supporting Class / C++ Lecture Notes Goal Supporting Class / C++ Lecture Notes You started with an understanding of how to write Java programs. This course is about explaining the path from Java to executing programs. We proceeded in a mostly

More information

Object-Oriented Programming. Lecture 2 Dr Piotr Cybula

Object-Oriented Programming. Lecture 2 Dr Piotr Cybula Object-Oriented Programming Lecture 2 Dr Piotr Cybula Encapsulation : data protection code safety and independence better team support with the code separation without «giving

More information

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial A Walkthrough with Examples CMSC 212 - Spring 2009 Last modified March 22, 2009 What is gdb? GNU Debugger A debugger for several languages, including C and C++ It allows you to inspect what the program

More information

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University Day 3 COMP 1006/1406A Summer 2016 M. Jason Hinek Carleton University today s agenda assignments 1 was due before class 2 is posted (be sure to read early!) a quick look back testing test cases for arrays

More information

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program: Welcome Back CSCI 262 Data Structures 2 - Review What you learned in CSCI 261 (or equivalent): Variables Types Arrays Expressions Conditionals Branches & Loops Functions Recursion Classes & Objects Streams

More information

10. Object-oriented Programming. 7. Juli 2011

10. Object-oriented Programming. 7. Juli 2011 7. Juli 2011 Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 1 of 47 Outline Object Case Study Brain Teaser Copy Constructor & Operators Object-oriented Programming, i.e.

More information

G52CPP C++ Programming Lecture 6. Dr Jason Atkin

G52CPP C++ Programming Lecture 6. Dr Jason Atkin G52CPP C++ Programming Lecture 6 Dr Jason Atkin 1 Last lecture The Stack Lifetime of local variables Global variables Static local variables const (briefly) 2 Visibility is different from lifetime Just

More information

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3 Hello, World! in C Johann Myrkraverk Oskarsson October 23, 2018 Contents 1 The Quintessential Example Program 1 I Printing Text 2 II The Main Function 3 III The Header Files 4 IV Compiling and Running

More information

Construction: High quality code for programming in the large

Construction: High quality code for programming in the large Construction: High quality code for programming in the large Paul Jackson School of Informatics University of Edinburgh What is high quality code? High quality code does what it is supposed to do......

More information

Coding Workshop. Learning to Program with an Arduino. Lecture Notes. Programming Introduction Values Assignment Arithmetic.

Coding Workshop. Learning to Program with an Arduino. Lecture Notes. Programming Introduction Values Assignment Arithmetic. Coding Workshop Learning to Program with an Arduino Lecture Notes Table of Contents Programming ntroduction Values Assignment Arithmetic Control Tests f Blocks For Blocks Functions Arduino Main Functions

More information

CS113: Lecture 4. Topics: Functions. Function Activation Records

CS113: Lecture 4. Topics: Functions. Function Activation Records CS113: Lecture 4 Topics: Functions Function Activation Records 1 Why functions? Functions add no expressive power to the C language in a formal sense. Why have them? Breaking tasks into smaller ones make

More information

CMPT 115. C tutorial for students who took 111 in Java. University of Saskatchewan. Mark G. Eramian, Ian McQuillan CMPT 115 1/32

CMPT 115. C tutorial for students who took 111 in Java. University of Saskatchewan. Mark G. Eramian, Ian McQuillan CMPT 115 1/32 CMPT 115 C tutorial for students who took 111 in Java Mark G. Eramian Ian McQuillan University of Saskatchewan Mark G. Eramian, Ian McQuillan CMPT 115 1/32 Part I Starting out Mark G. Eramian, Ian McQuillan

More information

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and Classes CS 251 Intermediate Programming Methods and Classes Brooke Chenoweth University of New Mexico Fall 2018 Methods An operation that can be performed on an object Has return type and parameters Method with

More information

CS 251 Intermediate Programming Methods and More

CS 251 Intermediate Programming Methods and More CS 251 Intermediate Programming Methods and More Brooke Chenoweth University of New Mexico Spring 2018 Methods An operation that can be performed on an object Has return type and parameters Method with

More information

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy CSCI 334: Principles of Programming Languages Lecture 18: C/C++ Homework help session will be tomorrow from 7-9pm in Schow 030A instead of on Thursday. Instructor: Dan Barowy HW6 and HW7 solutions We only

More information

Welcome to Lab! Feel free to get started until we start talking! The lab document is located on the course website:

Welcome to Lab! Feel free to get started until we start talking! The lab document is located on the course website: Welcome to Lab! Feel free to get started until we start talking! The lab document is located on the course website: https://users.wpi.edu/~sjarvis/ece2049_smj/ We will come around checking your pre-labs

More information

CS/ENGRD 2110 SPRING Lecture 3: Fields, getters and setters, constructors, testing

CS/ENGRD 2110 SPRING Lecture 3: Fields, getters and setters, constructors, testing 1 CS/ENGRD 2110 SPRING 2019 Lecture 3: Fields, getters and setters, constructors, testing http://courses.cs.cornell.edu/cs2110 CS2110 Announcements 2 Take course S/U? OK with us. Check with your advisor/major.

More information

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class CS112 Lecture: Defining Classes Last revised 2/3/06 Objectives: 1. To describe the process of defining an instantiable class Materials: 1. BlueJ SavingsAccount example project 2. Handout of code for SavingsAccount

More information

CS-202 Introduction to Object Oriented Programming

CS-202 Introduction to Object Oriented Programming CS-202 Introduction to Object Oriented Programming California State University, Los Angeles Computer Science Department Lecture III Inheritance and Polymorphism Introduction to Inheritance Introduction

More information

Lab 9: Pointers and arrays

Lab 9: Pointers and arrays CMSC160 Intro to Algorithmic Design Blaheta Lab 9: Pointers and arrays 3 Nov 2011 As promised, we ll spend today working on using pointers and arrays, leading up to a game you ll write that heavily involves

More information

Programming in C. Pointers and Arrays

Programming in C. Pointers and Arrays Programming in C Pointers and Arrays NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313 http://www.csee.umbc.edu/courses/undergraduate/313/fall11/" Pointers and Arrays In C, there is a strong relationship

More information

Arrays. Fundamentals of Computer Science

Arrays. Fundamentals of Computer Science Arrays Fundamentals of Computer Science Outline Array Basics Creating and Accessing Arrays Array Details The Instance Variable length Zombie Apocalypse How do I keep track of location of the person and

More information

CE221 Programming in C++ Part 1 Introduction

CE221 Programming in C++ Part 1 Introduction CE221 Programming in C++ Part 1 Introduction 06/10/2017 CE221 Part 1 1 Module Schedule There are two lectures (Monday 13.00-13.50 and Tuesday 11.00-11.50) each week in the autumn term, and a 2-hour lab

More information

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture - 08 Constants and Inline Functions Welcome to module 6 of Programming

More information

Java Review. Fundamentals of Computer Science

Java Review. Fundamentals of Computer Science Java Review Fundamentals of Computer Science Link to Head First pdf File https://zimslifeintcs.files.wordpress.com/2011/12/h ead-first-java-2nd-edition.pdf Outline Data Types Arrays Boolean Expressions

More information

G51PGP Programming Paradigms. Lecture OO-4 Aggregation

G51PGP Programming Paradigms. Lecture OO-4 Aggregation G51PGP Programming Paradigms Lecture OO-4 Aggregation 1 The story so far We saw that C code can be converted into Java code Note real object oriented code though Hopefully shows you how much you already

More information

CS 15 Design Section. Design

CS 15 Design Section. Design Design Introduction to the Section: This may be for advanced students who already know about Procedural Programming. Could be a nice lecture for them as to why we teach OO. This is from a lecture at Georgia

More information

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003 Outline Object Oriented Programming Autumn 2003 2 Course goals Software design vs hacking Abstractions vs language (syntax) Java used to illustrate concepts NOT a course about Java Prerequisites knowledge

More information

CS112 Lecture: Defining Instantiable Classes

CS112 Lecture: Defining Instantiable Classes CS112 Lecture: Defining Instantiable Classes Last revised 2/3/05 Objectives: 1. To describe the process of defining an instantiable class 2. To discuss public and private visibility modifiers. Materials:

More information

Equality for Abstract Data Types

Equality for Abstract Data Types Object-Oriented Design Lecture 4 CSU 370 Fall 2008 (Pucella) Tuesday, Sep 23, 2008 Equality for Abstract Data Types Every language has mechanisms for comparing values for equality, but it is often not

More information

COMP19612 exam performance feedback 2014

COMP19612 exam performance feedback 2014 COMP19612 exam performance feedback 2014 (excluding section A which is multiple choice) Questions in plain font, original marking scheme in bold, additional comments in bold italic. Question 1 The students

More information

Patterns: Working with Arrays

Patterns: Working with Arrays Steven Zeil October 14, 2013 Outline 1 Static & Dynamic Allocation Static Allocation Dynamic Allocation 2 Partially Filled Arrays Adding Elements Searching for Elements Removing Elements 3 Arrays and Templates

More information

COMP s1 Lecture 1

COMP s1 Lecture 1 COMP1511 18s1 Lecture 1 1 Numbers In, Numbers Out Andrew Bennett more printf variables scanf 2 Before we begin introduce yourself to the person sitting next to you why did

More information

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng Slide Set 2 for ENCM 335 in Fall 2018 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2018 ENCM 335 Fall 2018 Slide Set 2 slide

More information

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch Problem Solving Creating a class from scratch 1 Recipe for Writing a Class 1. Write the class boilerplate stuff 2. Declare Fields 3. Write Creator(s) 4. Write accessor methods 5. Write mutator methods

More information

CSE 431S Type Checking. Washington University Spring 2013

CSE 431S Type Checking. Washington University Spring 2013 CSE 431S Type Checking Washington University Spring 2013 Type Checking When are types checked? Statically at compile time Compiler does type checking during compilation Ideally eliminate runtime checks

More information

AIMS Embedded Systems Programming MT 2017

AIMS Embedded Systems Programming MT 2017 AIMS Embedded Systems Programming MT 2017 Object-Oriented Programming with C++ Daniel Kroening University of Oxford, Computer Science Department Version 1.0, 2014 Outline Classes and Objects Constructors

More information

HW1 due Monday by 9:30am Assignment online, submission details to come

HW1 due Monday by 9:30am Assignment online, submission details to come inst.eecs.berkeley.edu/~cs61c CS61CL : Machine Structures Lecture #2 - C Pointers and Arrays Administrivia Buggy Start Lab schedule, lab machines, HW0 due tomorrow in lab 2009-06-24 HW1 due Monday by 9:30am

More information

Classes. Classes as Code Libraries. Classes as Data Structures

Classes. Classes as Code Libraries. Classes as Data Structures Classes Classes/Objects/Interfaces (Savitch, Various Chapters) TOPICS Classes Public versus Private Static Data Static Methods Interfaces Classes are the basis of object-oriented (OO) programming. They

More information

Vector and Free Store (Vectors and Arrays)

Vector and Free Store (Vectors and Arrays) DM560 Introduction to Programming in C++ Vector and Free Store (Vectors and Arrays) Marco Chiarandini Department of Mathematics & Computer Science University of Southern Denmark [Based on slides by Bjarne

More information

C:\Temp\Templates. Download This PDF From The Web Site

C:\Temp\Templates. Download This PDF From The Web Site 11 2 2 2 3 3 3 C:\Temp\Templates Download This PDF From The Web Site 4 5 Use This Main Program Copy-Paste Code From The Next Slide? Compile Program 6 Copy/Paste Main # include "Utilities.hpp" # include

More information

CMSC 341 Lecture 10 Binary Search Trees

CMSC 341 Lecture 10 Binary Search Trees CMSC 341 Lecture 10 Binary Search Trees John Park Based on slides from previous iterations of this course Review: Tree Traversals 2 Traversal Preorder, Inorder, Postorder H X M A K B E N Y L G W UMBC CMSC

More information

Ticket Machine Project(s)

Ticket Machine Project(s) Ticket Machine Project(s) Understanding the basic contents of classes Produced by: Dr. Siobhán Drohan (based on Chapter 2, Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes,

More information

Principles of Object Oriented Programming. Lecture 4

Principles of Object Oriented Programming. Lecture 4 Principles of Object Oriented Programming Lecture 4 Object-Oriented Programming There are several concepts underlying OOP: Abstract Types (Classes) Encapsulation (or Information Hiding) Polymorphism Inheritance

More information

Use Cases for Compile-Time Reflection

Use Cases for Compile-Time Reflection Page 1 of 7 N3403=12-0093 2012-09-22 Mike Spertus, Symantec mike_spertus@symantec.com Use Cases for Compile-Time Reflection Overview There have been many proposals around compile-time reflection, some

More information

Heap Arrays and Linked Lists. Steven R. Bagley

Heap Arrays and Linked Lists. Steven R. Bagley Heap Arrays and Linked Lists Steven R. Bagley Recap Data is stored in variables Can be accessed by the variable name Or in an array, accessed by name and index Variables and arrays have a type Create our

More information

More on Classes. The job of this method is to return a String representation of the object. Here is the tostring method from the Time class:

More on Classes. The job of this method is to return a String representation of the object. Here is the tostring method from the Time class: More on Classes tostring One special method in Java is the tostring method. The method (regardless of which class it s added to) has the following prototype: public String tostring(); The job of this method

More information

Proceedings of the 8 th International Conference on Applied Informatics Eger, Hungary, January 27 30, Vol. 2. pp Virtuoso Virtuality

Proceedings of the 8 th International Conference on Applied Informatics Eger, Hungary, January 27 30, Vol. 2. pp Virtuoso Virtuality Proceedings of the 8 th International Conference on Applied Informatics Eger, Hungary, January 27 30, 2010. Vol. 2. pp. 375 381. Virtuoso Virtuality Marianna Sipos Department of Information Technology,

More information

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14 COSC 2P91 Introduction Part Deux Week 1b Brock University Brock University (Week 1b) Introduction Part Deux 1 / 14 Source Files Like most other compiled languages, we ll be dealing with a few different

More information

BM214E Object Oriented Programming Lecture 8

BM214E Object Oriented Programming Lecture 8 BM214E Object Oriented Programming Lecture 8 Instance vs. Class Declarations Instance vs. Class Declarations Don t be fooled. Just because a variable might be declared as a field within a class that does

More information

Programming assignment A

Programming assignment A Programming assignment A ASCII Minesweeper Official release on Feb 14 th at 1pm (Document may change before then without notice) Due 5pm Feb 25 th Minesweeper is computer game that was first written in

More information

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012 CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012 TOPICS TODAY Project 5 Pointer Basics Pointers and Arrays Pointers and Strings POINTER BASICS Java Reference In Java,

More information