Computer Programming

Similar documents
Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

CS24 Week 3 Lecture 1

Computer Programming. Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay

CSCE 110 Dr. Amr Goneid Exercise Sheet (6): Exercises on Structs and Dynamic Lists

Camera Placement for Ray Tracing

Multicore Challenge in Vector Pascal. P Cockshott, Y Gdura

Memory and Pointers written by Cathy Saxton

Homogeneous coordinates, lines, screws and twists

Graphics. Vector and Matrix Algebra. Korea Univ. Computer Graphics Lab. Graphics Korea University.

Graphics. Vector and Matrix Algebra. Korea Univ. Computer Graphics Lab. Graphics Korea University.

Projectile Launched Horizontally

BASIC ELEMENTS. Geometry is the study of the relationships among objects in an n-dimensional space

Principles of Computer Game Design and Implementation. Lecture 6

CS 101: Computer Programming and Utilization

Find the block in which the tuple should be! If there is free space, insert it! Otherwise, must create overflow pages!

Fall CSCI 420: Computer Graphics. 2.2 Transformations. Hao Li.

What have we learned about when we learned about function parameters? 1-1

pointers + memory double x; string a; int x; main overhead int y; main overhead

/INFOMOV/ Optimization & Vectorization. J. Bikker - Sep-Nov Lecture 5: Caching (2) Welcome!

/INFOMOV/ Optimization & Vectorization. J. Bikker - Sep-Nov Lecture 4: Caching (2) Welcome!

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #49. Structures in C -1

CS101 Computer programming and utilization

Single View Geometry. Camera model & Orientation + Position estimation. Jianbo Shi. What am I? University of Pennsylvania GRASP

-t:ma. u V:. 1! *VV; jf >*4: t i f n - v i ' ONABI E :VISITd:Rs ; 8 A EASTBOURNE, SAT. 5? -tesiste- COLLEGE. D ressm aker.

EP241 Computer Programming

CS 101: Computer Programming and Utilization

CPSC 427a: Object-Oriented Programming

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute

Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake

CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRY

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Lecture 5 - SIMD recap

Lecture Data layout on disk. How to store relations (tables) in disk blocks. By Marina Barsky Winter 2016, University of Toronto

Exam 3 Chapters 7 & 9

C++ Programming Lecture 4 Software Engineering Group

Computer Programming

1 Memory management in C++

Computational Photography Denoising

Contents Physics Vectors

Single View Geometry. Camera model & Orientation + Position estimation. What am I?

Basic Elements. Geometry is the study of the relationships among objects in an n-dimensional space

Transformations. CSCI 420 Computer Graphics Lecture 4

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

Practice question Answers

Welcome to Math 275 Topic 1-1: Introduction to Vectors

CSE030 Fall 2012 Final Exam Friday, December 14, PM

Structures. CSE 142 Programming I. Chapter 11. Review: Data Structures. Defining structs. Terminology. Structs: Heterogeneous Structures

Reading. 18. Projections and Z-buffers. Required: Watt, Section , 6.3, 6.6 (esp. intro and subsections 1, 4, and 8 10), Further reading:

Computer Science II CSci 1200 Test 1 Overview and Practice

Recap: Pointers. int* int& *p &i &*&* ** * * * * IFMP 18, M. Schwerhoff

Transformations Computer Graphics I Lecture 4

INFOGR Computer Graphics. Jacco Bikker - April-July Lecture 3: Geometry. Welcome!

C++ For Science and Engineering Lecture 12

LOWELL WEEKLY JOURNAL

If the center of the sphere is the origin the the equation is. x y z 2ux 2vy 2wz d 0 -(2)

Structures, Operators

Operating Systems 2INC0 C course Pointer Advanced. Dr. Ir. Ion Barosan

CS242: Probabilistic Graphical Models Lecture 3: Factor Graphs & Variable Elimination

Lecture 5: Outline. I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists

Programming in C++: Assignment Week 2

Lecture 2 More C++ Jon Macey

Midterm Exam #2 Spring (1:00-3:00pm, Friday, March 15)

COMP 11 Class 17 Outline

CISC 2200 Data Structure Fall, C++ Review:3/3. 1 From last lecture:

Transformations. CSCI 420 Computer Graphics Lecture 5

Transformations. OpenGL Transformations. 4x4 Model-view Matrix (this lecture) OpenGL Transformation Matrices. 4x4 Projection Matrix (next lecture)

C++ Support Classes (Data and Variables)

6.141: Robotics systems and science Lecture 10: Implementing Motion Planning

Pointer Data Type and Pointer Variables

6.141: Robotics systems and science Lecture 10: Motion Planning III

CHAPTER 4 FUNCTIONS. 4.1 Introduction

Data Structures using OOP C++ Lecture 3

EP578 Computing for Physicists

Computer Graphics using OpenGL, 3 rd Edition F. S. Hill, Jr. and S. Kelley Chapter Three-dimensional Viewing

CS 103 Unit 13 Slides

/INFOMOV/ Optimization & Vectorization. J. Bikker - Sep-Nov Lecture 5: SIMD (1) Welcome!

Transcription:

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Object-oriented Programming using Member Functions Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 1

Quick Recap of Relevant Topics Brief introduction to object-oriented programming Program as a collection of interacting objects Structures representing objects Groups of related variables, arrays, other structures Accessing members of structures Pointers to structures Dynamic allocation and de-allocation of structures 2 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Overview of This Lecture Member functions of structures Interfaces of objects in object-oriented programming Accessing member functions 3 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Acknowledgment Some examples in this lecture are from An Introduction to Programming Through C++ by Abhiram G. Ranade McGraw Hill Education 2014 All such examples indicated in slides with the citation AGRBook Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 4

Recap: Object-Oriented Programming Overview Identify entities (physical or conceptual) involved in the working of the system Entities also called objects Think of system functionality in terms of operations on and interactions between objects Abstract away (hide) details not necessary for an operation Implement system modularly by focusing on entities, their interfaces and their interactions Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 5

Recap: Entity or Object Contains information specific to the object Fixed information usually doesn t change as objects interact State information can change as objects interact Unambiguous, well-defined boundaries Clear specification of what information is part of an object Ideally, every interaction between two objects should happen through well-defined interfaces Focus of this lecture: interfaces of objects Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 6

Example: Vectors in 3 Dimensions [Ref. AGRBook] We want to write a program to reason about motion in 3-dimensional space Must deal with 3-dimensional vectors representing Position Velocity Acceleration 3-dimensional vectors are basic entities (objects) in this program Need to define a C++ structure to represent a vector For simplicity, we will use Cartesian coordinates Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 7

The V3 Structure struct V3 { double x, y, z; }; What functions might we need to operate on objects of type V3? Adding two vectors Scaling a vector by a scalar constant Euclidean length of a vector and several more Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 8

Functions on V3 Objects V3 sum (V3 const &a, V3 const &b) { v.x = a.x + b.x; v.y = a.y + b.y; v.z = a.z + b.z; return v; } Note the manner in which parameters are passed. struct V3 { double x, y, z; }; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 9

Functions on V3 Objects V3 scale (V3 const &a, double const factor) { v.x = a.x * factor; v.y = a.y * factor; v.z = a.z * factor; return v; } Note the manner in which parameters are passed. struct V3 { double x, y, z; }; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 10

Functions on V3 Objects double length (V3 const &a) { double temp; temp = a.x*a.x + a.y*a.y + a.z*a.z; return sqrt(temp);} struct V3 { double x, y, z; }; Assume sqrt function available from a library (e.g. cmath) Note the manner in which parameters are passed. Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 11

Motivating Member Functions Let s take a closer look at the functions sum, scale and length sum (V3 const &a, V3 const &b) scale (V3 const &a, double const factor) length (V3 const &a) Each of these functions can be thought of as doing some computation on an object a of type V3 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 12

Motivating Member Functions sum (V3 const &a, V3 const &b) scale (V3 const &a, double const factor) length (V3 const &a) Why not associate these functions with the object a itself? When adding b to a, call function sum associated with a and pass b as parameter When scaling a by factor, call function scale associated with a and pass factor as parameter When finding the Euclidean length of a, call function length associated with a Helps define interfaces for interaction with the object a Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 13

Member Functions in C++ In C++, structures can have member functions struct V3 { double x, y, z; double length() { return sqrt(x*x + y*y + z*z); } V3 sum (V3 const &b) { v.x = x + b.x; v.y = y + b.y; v.z = z = b.z; return v; } V3 scale (double const factor) { v.x = x*factor; v.y = y*factor; v.z = z*factor; return v; } }; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 14

Member Functions in C++ struct V3 { double x, y, z; double length() { return sqrt(x*x + y*y + z*z); } V3 sum (V3 const &b) { v.x = x + b.x; v.y = y + b.y; v.z = z = b.z; return v; } V3 scale (double const factor) { v.x = x*factor; v.y = y*factor; v.z = z*factor; return v; } }; Member data Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 15

Member Functions in C++ struct V3 { double x, y, z; double length() { return sqrt(x*x + y*y + z*z); } V3 sum (V3 const &b) { v.x = x + b.x; v.y = y + b.y; v.z = z = b.z; return v; } V3 scale (double const factor) { v.x = x*factor; v.y = y*factor; v.z = z*factor; return v; } }; Member function Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 16

Member Functions in C++ struct V3 { double x, y, z; double length() { return sqrt(x*x + y*y + z*z); } V3 sum (V3 const &b) { v.x = x + b.x; v.y = y + b.y; v.z = z = b.z; return v; } V3 scale (double const factor) { v.x = x*factor; v.y = y*factor; v.z = z*factor; return v; } }; Member function Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 17

Member Functions in C++ struct V3 { double x, y, z; double length() { return sqrt(x*x + y*y + z*z); } V3 sum (V3 const &b) { v.x = x + b.x; v.y = y + b.y; v.z = z = b.z; return v; } V3 scale (double const factor) { v.x = x*factor; v.y = y*factor; v.z = z*factor; return v; } }; Member function Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 18

Closer Look at a Member Function struct V3 { double x, y, z; double length() { return sqrt(x*x + y*y + z*z); } V3 sum (V3 const &b) { v.x = x + b.x; v.y = y + b.y; v.z = z = b.z; return v; } V3 scale (double const Member factor) x { of object passed as parameter v.x = x*factor; v.y = y*factor; v.z = z*factor; return v; } Member x of parent object }; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 19

Accessing Member Functions of Structures Recall how we accessed member data values of structures V3 p, *ptrp; cin >> p.x; ptrp = &p; Access using. operator cout << ptrp->x; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 20

Accessing Member Functions of Structures Recall how we accessed member data values of structures V3 p, *ptrp; cin >> p.x; ptrp = &p; cout << ptrp->x; Access using -> operator Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 21

Accessing Member Functions of Structures Member functions can be accessed in the same way V3 p, q, *ptrq; cin >> p.x >> p.y >> p.z; q = p.scale(0.5); ptrq = &q; cout << ptrq->length(); Access using. operator Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 22

Accessing Member Functions of Structures Member functions can be accessed in the same way V3 p, q, *ptrq; cin >> p.x >> p.y >> p.z; q = p.scale(0.5); ptrq = &q; cout << ptrq->length(); p: Receiver object Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 23

Accessing Member Functions of Structures Member functions can be accessed in the same way V3 p, q, *ptrq; cin >> p.x >> p.y >> p.z; q = p.scale(0.5); ptrq = &q; cout << ptrq->length(); scale: Member function of receiver object Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 24

Accessing Member Functions of Structures Member functions can be accessed in the same way V3 p, q, *ptrq; cin >> p.x >> p.y >> p.z; q = p.scale(0.5); ptrq = &q; cout << ptrq->length(); Parameter of member function Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 25

Accessing Member Functions of Structures Member functions can be accessed in the same way V3 p, q, *ptrq; cin >> p.x >> p.y >> p.z; q = p.scale(0.5); ptrq = &q; struct V3 { double x, y, z; V3 scale (double const factor) { v.x = x*factor; v.y = y*factor; v.z = z*factor; return v; } cout << ptrq->length() }; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 26

Accessing Member Functions of Structures Member functions can be accessed in the same way V3 p, q, *ptrq; cin >> p.x >> p.y >> p.z; q = p.scale(0.5); Access using -> operator ptrq = &q; cout << ptrq->length(); ptrq: Pointer to receiver object Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 27

Summary Member functions as interfaces of structures Accessing member functions of structures Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 28