CS 101: Computer Programming and Utilization

Similar documents
CS 101: Computer Programming and Utilization

Computer Programming

Review: C++ Basic Concepts. Dr. Yingwu Zhu

CS 101: Computer Programming and Utilization

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

G52CPP C++ Programming Lecture 13

Fast Introduction to Object Oriented Programming and C++

Vectors and Pointers CS 16: Solving Problems with Computers I Lecture #13

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 8: Abstract Data Types

CS304 Object Oriented Programming Final Term

Lecture 7: General Loops (Chapter 7)

CS 101: Computer Programming and Utilization

Ch. 12: Operator Overloading

Object Oriented Programming

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:

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

C++ Input/Output: Streams

PIC 10A Objects/Classes

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

Stream States. Formatted I/O

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:

CPSC 427: Object-Oriented Programming

1 Memory management in C++

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Come and join us at WebLyceum

CS 101: Computer Programming

Lecture 14: more class, C++ streams

Classes and Objects. Class scope: - private members are only accessible by the class methods.

Instantiation of Template class

Suppose we find the following function in a file: int Abc::xyz(int z) { return 2 * z + 1; }

SHORT REVIEW OF CS TOPICS RANDOM NUMBERS (2 MARKS) which generates a random number in the range of 0 to n-1. For example;

More class design with C++ Starting Savitch Chap. 11

CSI33 Data Structures

Lecture 8. Xiaoguang Wang. February 13th, 2014 STAT 598W. (STAT 598W) Lecture 8 1 / 47

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

Note 11/13/2014. They are like those i s, j s, and temp s that appear and disappear when the function starts and finishes...

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

Assignment of Objects

SAURASHTRA UNIVERSITY

C++ Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Chapter 14 Sequential Access Files

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

Polymorphism Part 1 1

Documentation. Programming / Documentation Slide 42

Today in CS162. External Files. What is an external file? How do we save data in a file? CS162 External Data Files 1

File I/O Christian Schumacher, Info1 D-MAVT 2013

Intermediate Programming, Spring 2017*

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

C Pointers. ENGG1002 Computer Programming and Applica;ons Dr. Hayden Kwok Hay So Week 2. GeGng Hold of Memory. Sta;c: When we define a variable

Operator overloading

COMP322 - Introduction to C++

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Operator overloading: extra examples

CS 247: Software Engineering Principles. ADT Design

AN OVERVIEW OF C++ 1

Intermediate Programming & Design (C++) Classes in C++

Interview Questions of C++

Objects and streams and files CS427: Elements of Software Engineering

Vectors of Pointers to Objects. Vectors of Objects. Vectors of unique ptrs C++11. Arrays of Objects

Object Oriented Design

Programming Languages: OO Paradigm, Objects

Chapter 10 Introduction to Classes

IS 0020 Program Design and Software Tools

CMSC 341 Lecture 10 Binary Search Trees

IS0020 Program Design and Software Tools Midterm, Fall, 2004

cs3157: c++ lecture #2 (mon-11-apr-2005) chronology of some programming languages... C++ vs Java identifiers.

Operator Overloading in C++ Systems Programming

CS11 Intro C++ Spring 2018 Lecture 3

ADTs in C++ In C++, member functions can be defined as part of a struct

Lecture 18 Tao Wang 1

Chapter 18 - C++ Operator Overloading

G52CPP C++ Programming Lecture 17

CSE 143. Linked Lists. Linked Lists. Manipulating Nodes (1) Creating Nodes. Manipulating Nodes (3) Manipulating Nodes (2) CSE 143 1

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Financial computing with C++

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

2. It is possible for a structure variable to be a member of another structure variable.

CPSC 427: Object-Oriented Programming

A class is a user-defined type. It is composed of built-in types, other user-defined types and

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

Classes Ch

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

Classes - 2. Data Processing Course, I. Hrivnacova, IPN Orsay

Plan of the day. Today design of two types of container classes templates friend nested classes. BABAR C++ Course 103 Paul F. Kunz

I BSc(IT) [ Batch] Semester II Core: Object Oriented Programming With C plus plus - 212A Multiple Choice Questions.

Programming, numerics and optimization

CS201 Latest Solved MCQs

COMP 2355 Introduction to Systems Programming

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Writing a Fraction Class

CPSC 427: Object-Oriented Programming

SFU CMPT Topic: Classes

Object Oriented Programming CS250

11.2. Overloading Operators

Absolute C++ Walter Savitch

Ch. 11: References & the Copy-Constructor. - continued -

Transcription:

CS 101: Computer Programming and Utilization Jul-Nov 2017 Umesh Bellur (cs101@cse.iitb.ac.in) Lecture 14: Object Oritented Programming and Classes

About These Slides Based on Chapter 18 of the book An Introduction to Programming Through C++ by Abhiram Ranade (Tata McGraw Hill, 2014) Original slides by Abhiram Ranade First update by Varsha Apte Second update by Uday Khedker

Main Recommendations From The Previous Chapter Define a struct to hold informa0on related to each en0ty that your program deals with Define member func0ons corresponding to ac0ons/ opera0ons associated with the en0ty

Outline Constructors Copy Constructors Destructors Operator overloading Overloading the assignment operator Access control Classes Graphics and input/output classes

Mo0va0onal Example: The Queue Struct in Taxi Dispatch const int N=100; struct queue{ int elements[n], nwai0ng,front; bool insert(int v){ book remove(int &v){ ; Once the queue is created, we expect it to be used only through the member func0ons, insert and remove We do not expect elements, nwai0ng, front to be directly accessed

Main Program Using Queue int main(){ Queue q; q.front = q.nwai0ng = 0; while(true){ char c; cin >> c; if(c == d ){ int driver; cin >> driver; if(!q.insert(driver)) cout << Q is full\n ; else if(c == c ){ int driver; if(!q.remove(driver)) cout << No taxi.\n ; else cout << Assigning << driver<< endl; Main program does use q through opera0ons insert and remove However, at the beginning, q.front and q.nwai0ng is directly manipulated This is against the philosophy of so_ware packaging When we create a queue, we will always set q.nwai0ng and q.front to 0 C++ provides a way by which the ini0aliza0on can be made to happen automa0cally, and also such that programs using Queue do not need to access the data members directly Just defining Queue q; would by itself set q.nwai0ng and q.front to 0! Next

Constructor Example In C++, the programmer may define a special member func0on called a constructor which will always be called when an instance of the struct is created A constructor has the same name as the struct, and no return type The code inside the constructor can perform ini0aliza0ons of members When q is created in the main program, the constructor is called automa0cally struct Queue{ ; int elements[n], front, nwai0ng; Queue(){ // constructor nwai0ng = 0; front = 0; // other member func0ons int main(){ Queue q; // no need to set // q.nwai0ng, q.front // to 0.

Constructors In General struct A{ ; A(parameters){ int main(){ A a(arguments); Constructor can take arguments The crea0on of the object a in main can be thought of as happenning in two steps Memory is allocated for a in main The constructor is called on a with the given arguments You can have many constructors, provided they have different signatures

Another example: Constructor for V3 struct V3{ double x,y,z; V3(){ x = y = z = 0; V3(double a){ x = y = z = a; ; int main(); V3 v1(5), v2; When defining v1, an argument is given So the constructor taking a single argument is called. Thus each component of v1 is set to 5 When defining v2, no argument is given. So the constructor taking no arguments gets called. Thus each component of v2 is set to 0

Remarks If and only if you do not define a constructor, will C++ defines a constructor for you which takes no arguments, and does nothing If you define a constructor taking arguments, you implicitly tell C++ that you want programmers to give arguments. So if some programmer does not give arguments, C++ will flag it as an error If you want both kinds of ini0aliza0on, define both kinds of constructor A constructor that does not take arguments (defined by you or by C++) is called a default constructor If you define an array of struct, each element is ini0alized using the default constructor

The Copy Constructor Suppose an object is passed by value to a func0on It must be copied to the variable denoted by the parameter Suppose an object is returned by a func0on The value returned must be copied to a temporary variable in the calling program By default the copying opera0ons are implemented by copying each member of one object to the corresponding member of the other object You can change this default behaviour by defining a copy constructor

Example struct Queue{ int elements[n], nwai0ng, front; ; Queue(const Queue &source){ // Copy constructor front = source.front; nwai0ng = source.nwai0ng; for(int i=front, j=0; j<nwai0ng; j++){ elements[i] = source.elements[i]; i = (i+1) % N;

Copy Constructor in the Example The copy constructor must take a single reference argument: the object which is to be copied Note that the argument to the copy constructor must be a reference, otherwise the copy constructor will have to be called to copy the argument! This is will result in an unending recursion Member elements is not copied fully. Only the useful part of it is copied More efficient More interes0ng use later

Destructors When control goes out of a block in which a variable is defined, that variable is destroyed Memory allocated for that variable is reclaimed You may define a destructor func0on, which will get executed before the memory is reclaimed

Destructor Example If a queue that you have defined goes out of scope, it will be destroyed If the queue contains elements at the 0me of destruc0on, it is likely an error So you may want to print a message warning the user It is usually an error to call the destructor explicitly. It will be called automa0cally when an object is to be destroyed. It should not get called twice. More interes0ng uses of the destructor will be considered in later chapters.

Destructor Example struct Queue{ int elements[n], nwai0ng, front; ; ~Queue(){ //Destructor if(nwai0ng>0) cout << Warning: << non- empty queue being destroyed. << endl;

Operator Overloading In Mathema0cs, arithme0c operators are used with numbers, but also other objects such as vectors Something like this is also possible in C++! An expression such as x @ y where @ is any infix operator is considered by C++ to be equivalent to x.operator@(y) in which operator@ is a member func0on If the member func0on operator@ is defined, then that is called to execute x @ y

Example: Arithme0c on V3 objects struct V3{ double x, y, z; V3(double a, double b, double c){ x=a; y=b; z=c; ; V3 operator+(v3 b){ // adding two V3s return V3(x+b.x, y+b.y, z+b.z); // constructor call V3 operator*(double f){ return V3(x*f, y*f, z*f); // mul0plying a V3 by f // constructor call

Using V3 Arithme0c int main(){ V3 u(1,2,3), a(4,5,6), s; double t=10; s = u*t + a*t*t*0.5; cout << s.x << << s.y << << s.z << endl;

Remarks Expression involving vectors can be made to look very much like what you studied in Physics Other operators can also be overloaded, including unary operators (see the book) Overload operators only if they have a natural interpreta0on for the struct in ques0on Otherwise you will confuse the reader of your program

The this pointer So far, we have not provided a way to refer to the receiver itself inside the defini0on of a member func0on. Within the body of a member func0on, the keyword this points to the receiver i.e. the struct on which the member func0on has been invoked. Trivial use: write this->member instead of member directly. struct V3{ double x, y, z; double length(){ return sqrt(*this.x * *this.x + *this.y * *this.y + *this.z * *this.z); More interes0ng use later.

Overloading The Assignment Operator Normally if you assign one struct to another, each member of the rhs is copied to the corresponding member of the lhs You can change this behaviour by defining member func0on operator= for the struct A return type must be defined if you wish to allow chained assignments, i.e. v1 = v2 = v3; which means v1 = (v2 = v3); The opera0on must return a reference to the le_ hand side object

Example struct Queue{... Queue & operator=(queue &rhs){ front = rhs.front; nwai0ng = rhs.nwai0ng; for(int i=0; i<nwai0ng; i++){ elements[i] = rhs.elements[i]; i = (i+1) % N; return *this; ; // only the relevant elements are copied

Access Control It is possible to restrict access to members or member func0ons of a struct Members declared public: no restric0on Members declared private: Can be accessed only inside the defini0on of the struct Typical strategy: Declare all data members to be private, and some subset of func0on members to be public

Access Control Example struct Queue{ private: int elements[n], nwai0ng, front; public: ; Queue(){ bool insert(int v){.. bool remove(int &v){..

Remarks public:, private: : access specifiers An access specifier applies to all members defined following it, un0l another specifier is given Thus elements, nwai0ng, front are private, while Queue(), insert, remove are public

Remarks The default versions of the constructor, copy constructor, destructor, assignment operator are public If you specify any of these as private, then they cannot be invoked outside of the struct defini0on Thus if you make the copy constructor of a struct X private, then you will get an error if you try to pass a struct of type X by value Thus, as a designer of a struct, you can exercise great control over how the struct gets used

Classes A class is essen0ally the same as a struct, except: Any members/member func0ons in a struct are public by default Any members/member func0ons in a class are private by default

Classes Example: a Queue class: class Queue{ int elements[n], nwai0ng, front; public: Queue(){ bool remove(int &v){ bool insert(int v){ ; Members elements, nwai0ng and front will be private.

Example struct V3{ double x,y,z; V3(double v){ x = y = z = v; ; double X(){ return x; struct V3{ double x,y,z; ; V3(double v); double X(); //implementa0ons V3::V3(double v){ x = y = z = v; double V3::X(){ return x;

Input Output Classes cin, cout : objects of class istream, ostream resp. predefined in C++ <<, >> : operators defined for the objects of these classes ifstream: another class like istream You create an object of class ifstream and associate it with a file on your computer Now you can read from that file by invoking the >> operator! ofstream: a class like ostream, to be used for wri0ng to files Must include header file <fstream> to uses ifstream and ofstream

Example of file i/o #include <fstream> #include <simplecpp> int main(){ ifstream infile( f1.txt ); // constructor call. object infile is created and associated // with f1.txt, which must be present in the current directory ofstream ou ile( f2.txt ); // constructor call. Object ou ile is created and associated // with f2.txt, which will get created in the current directory

Example of file i/o repeat(10){ int v; infile >> v; ou ile << v; // f1.txt must begin with 10 numbers. These will be read and // wrien to file f2.txt

Concluding Remarks The no0on of a packaged so_ware component is important. Making data members private: hiding the implementa0on from the user Making some member func0ons public: providing an interface using which the object can be used Separa0on of the concerns of the developer and the user Idea similar to what we discussed in connec0on with ordinary func0ons The specifica0on of the func0on must be clearly wrien down (analogous to interface) The user should not worry about how the func0on does its work (analogous to hiding data members)