Programming computer vision in C / C++

Size: px
Start display at page:

Download "Programming computer vision in C / C++"

Transcription

1 Appendix E Programming computer vision in C / C++ In this appendix we will describe one way of implementing two-dimensional (2D) images in the C and C++ languages. These languages are chosen because they enable the construction of large-scale, real-time, maintainable software. Our goal will be to strive for simplicity and generality. E.1 A grayscale image in C Let us begin by implementing an image in C. An image is simply a two-dimensional array of pixels, so the first thing we have to decide is the type of elements in the array. We will assume that the elements are unsigned characters, since that is the most common format for grayscale pixels. In its simplest form, an image consists of three things: the width of the image, the height of the image, and the pixel values themselves. We will use a struct to group these together: typedef struct int width, height; unsigned char* data; image; This code creates a struct called image that contains three member variables: width to hold the number of columns of the image, height to hold the number of rows of the image, and data, which is a pointer to the first data element (pixel). We will assume that pixel data are stored in row-major order, so that the one-dimensional array data contains the values for the first row of pixels, followed by the values for the second row of pixels, followed by the values for the third row of pixels, and so on. Some libraries also include in the image definition additional member variables to keep track of things like the type of pixel, whether there are any zero-padded pixels at the end of each row, or whether the origin of the image is in the top-left or bottom-right corner. Zero-padded pixels, however, are usually not worth the trouble, since most standard image sizes are already divisible by 8. For all practical purposes, the origin can be safely assumed to be in the top-left corner, since that is by far the most common convention. The other fields are not widely used either. As a result, we prefer this minimalist arrangement which also simplifies the present discussion. The next step is to create functions to initialize and destroy the image. We will create two initialization functions, one which creates an empty image, and another which creates an image of a specific size: 267

2 E.1. A GRAYSCALE IMAGE IN C 268 struct image init_image() struct image im; im.width = im.height = im.data = 0; return im; struct image init_image(int w, int h) struct image im; im.width = w; im.height = h; im.data = (unsigned char*) malloc(w * h * sizeof(unsigned char)); return im; void destroy_image(struct image im) free( im.data ); In the two init image functions, a local image variable is created on the stack, its member variables are initialized, and it is returned. Upon returning from the functions, a shallow copy is made of the member variables, but any pixel data will remain on the heap. The destroy image function frees any memory allocated for the pixels, which is safe because free(0) does not do anything. Another useful function is to be able to resize an image. For this, we will need to pass in a pointer to the image so that changes to its member variables will persist after the function call returns: void resize_image(struct image* im, int w, int h) im->width = w; im->height = h; im->data = (unsigned char*) realloc(im->data, w * h * sizeof(unsigned char)); The realloc function is particularly easy to use, because it will either resize the heap block as necessary, or it will copy the data to a new heap block and delete the old one. Keep in mind, however, that our resize function only ensures that the appropriate memory has been allocated; it does not ensure that the pixel data has been preserved in any meaningful way. Now let us add two functions to access the pixels: unsigned char get_pixel(struct image im, int x, int y) return im.data[ y * im.width + x ]; void set_pixel(struct image im, int x, int y, unsigned char val) im.data[ y * im.width + x ] = val; It is easy to verify that the formula y * im.width + x calculates the correct one-dimensional zerobased index, assuming that x and y are also zero-based indices. Note that set_pixel does not need to

3 APPENDIX E. PROGRAMMING COMPUTER VISION IN C / C take a pointer to the image, since the change is being made to an element in the array (which is stored on the heap) rather than a member variable of the struct itself (of which a copy is made on the stack). The image struct is now ready to be used in code: void main() struct image im1, im2; unsigned char val; // two images // a pixel im1 = init_image( 640, 480 ); // allocate memory for a 640 x 480 image im2 = init_image( ); // create an empty image resize_image( &im2, 640, 480 ); // allocate memory for a 640 x 480 image val = get_pixel( im1, 3, 2 ); // get the pixel value at (3, 2) set_pixel( im1, 3, 2, val + 10 ); // increase the value at (3, 2) by 10 gray levels destroy_image( im1 ); // free the memory destroy_image( im2 ); // free the memory Of course, in a real system we would want to check to make sure that (3,2) is not out of bounds, and that adding 10 to the pixel value does not cause it to exceed the maximum value, which is 255. These details are very important, and we hope our omission of them here for the sake of streamlining the presentation does not leave the reader with the wrong impression. Great care in attending to such details is highly recommended, and the programmer who does so will be amply rewarded with less development and debugging time, fewer headaches, less frustruation, and happier customers. E.2 A grayscale image in C++ Now let us turn our attention to C++. This language has been designed to make it much easier to program in an object oriented paradigm. In particular, while a struct in C can contain member variables (as we just saw), it cannot contain methods (i.e., functions) except by using function pointers, which lead to cumbersome and unnatural syntax. In contrast, a struct in C++ can contain both member variables and methods in a natural way. For example, in C++ we could program our image like this: struct ImageGray int width, height; unsigned char* data; unsigned char GetPixel( int x, int y ) return data[ y * width + x ]; void SetPixel( int x, int y, unsigned char val ) data[ y * width + x ] = val; Here we have capitalized the class and method names for the sake of convention. Note that the syntax in C++ is already simpler, because we do not need to use typedef when defining a struct, and the methods are now part of the struct rather than being outside. Of course, when programming in C++ we usually use classes rather than structs. The reason for this is more a matter of semantics, however, rather than any real change, because both are identical to the compiler. In other words, to a C++ compiler there is absolutely no difference between a struct and a class, except that structs are public by default, while classes are private by default. Changing a struct to private, or a class to public, results in the exact same behavior, respectively, as a class or a struct. (By the way, we recommend as a matter of convention using a class whenever the data members are private, and a struct whenever they are public.) Therefore, the following code is identical to that above:

4 E.2. A GRAYSCALE IMAGE IN C class ImageGray int width, height; unsigned char* data; unsigned char GetPixel( int x, int y ) return data[ y * width + x ]; void SetPixel( int x, int y, unsigned char val ) data[ y * width + x ] = val; The problem with this solution is that we are not taking advantage of the most powerful feature of C++, namely, data hiding, or encapsulation. To do this, we make the data members private. By convention, we will prefix the name of each private member variable with m_ to prevent confusion with local variables and parameters. These changes lead to the following: class ImageGray unsigned char GetPixel( int x, int y ) return m_data[ y * m_width + x ]; void SetPixel( int x, int y, unsigned char val ) m_data[ y * m_width + x ] = val; private: int m_width, m_height; unsigned char* m_data; Remember our init_image and destroy_image functions? C++ includes mechanisms for automating the initialization and destruction of objects. These mechanisms are the constructor and destructor. These methods are distinguished because their name is exactly the same as the name of the class, with an extra tilde for the destructor: class ImageGray ImageGray() m_width = m_height = m_data = 0; // constructor ImageGray(int w, int h) // another constructor m_data = (unsigned char*) malloc(w * h * sizeof(unsigned char)); ~ImageGray() free( m_data ); // destructor void Resize(int w, int h) m_data = (unsigned char*) realloc(m_data, w * h * sizeof(unsigned char)); unsigned char GetPixel( int x, int y ) return m_data[ y * m_width + x ]; void SetPixel( int x, int y, unsigned char val ) m_data[ y * m_width + x ] = val; private: int m_width, m_height; unsigned char* m_data;

5 APPENDIX E. PROGRAMMING COMPUTER VISION IN C / C A class can have as many constructors as you want, with each one being distinguished only by the number and type of parameters. Here we have two constructors, one that takes no parameters (called the default constructor) and one that takes two parameters (the width and height). A class has exactly one destructor, and it always takes no parameters. In the implementations of these methods, it is true that we could replace the C functions malloc, realloc, and free with their C++ counterparts new and delete, but since C++ is backward compatible, these work just as well, so we see no advantage to doing so. We have also added a Resize method, which works the same as the resize_image function above. Now we have a class that we can use: void main() ImageGray im1(640, 480); ImageGray im2; unsigned char val; // allocate memory for a 640 x 480 image (by calling constructor) // an empty image (by calling the default constructor) // a pixel im2.resize( 640, 480 ); // allocate memory for a 640 x 480 image val = im1.getpixel( 3, 2 ); // get the pixel value at (3, 2) im1.setpixel( 3, 2, val + 10 ); // increase the value at (3, 2) by 10 gray levels // automatically call destructors for im1 and im2 Notice how much C++ simplifies the syntax. We no longer have to use the keyword struct, and we no longer have to explicitly call init_image. Instead, the appropriate constructor is called whenever we define an ImageGray object. Moreover, the constructor is automatically called whenever the object falls out of scope, so that whether the memory is freed is not dependent on our vigilance as a programmer to remember to call destroy_image. By delegating this responsibility to the compiler, we greatly reduce the chance for bugs and memory leaks in our program. If you have understood the presentation so far, you are well on your way to being able to program computer vision in C++. It has been estimated that 90% of the value in the C++ language is contained in 10% of the features. As a result, once you become comfortable with constructors, destructors, and encapsulation, you can easily write clean C++ code that benefits from its major features. In fact, according to the inventor of C++, Bjarne Stroustrup, simple classes that hide their data and provide a clean interface, as we have done here sometimes called concrete classes are the foundation of elegant programming. E.3 A cleaner C++ class for a grayscale image Nevertheless, additional features of the C++ language, once understood, make our life even easier. Here we consider six features: operator overloading, references, the keyword const, the copy constructor, the assignment operator, and initializer lists: Operator overloading. Instead of saying, im1.getpixel( 3, 2 ), wouldn t it be nice if we could simply say im1( 3, 2 )? Well, this is actually possible in C++ by creating a method with a name that at first glance may look a bit strange: unsigned char operator()(int x, int y). With this method, called operator(), we can now call im1.operator()( 3, 2 ) to invoke it just as if it were any ordinary method. However, because the C++ compiler recognizes operator() as a special keyword, it also allows us to call the method using the shorthand im1( 3, 2 ). The value of a pixel can now be retrieved by simply calling val = im1(3, 2). References. A reference is an alias, i.e., it is another name for the object. A reference acts like an automatically dereferenced pointer, allowing us to access an object without copying it. References are very useful when passing parameters into methods, as well as when returning

6 E.3. A CLEANER C++ CLASS FOR A GRAYSCALE IMAGE 272 values from methods. References use the ampersand (&) symbol, which the reader will no doubt recognize as also the symbol used in C to take the address of. By adding this ampersand to the method above, we get unsigned char& operator()(int x, int y), which is similar to what we saw before except that now the method returns a reference (i.e, an alias) to the pixel itself rather than a copy of the pixel s value. This seemingly minor distinction has huge implications, because it makes it possible to put the method call on the left side of an equation, as in im1(3, 2) = val. const. The const keyword means read-only. It can be used for either variables on the stack, parameters passed into a method, the return value of a method, or a method itself. In the first three cases, the meaning is clear: The variable or parameter cannot be modified. In the latter case, the meaning is this: None of the member variables of the object on which the method is called can be modified. Since the GetPixel method does not change any of the member variables values, it is natural to make itconst. The syntax for making a method const is to add the keyword after the closing parenthesis: unsigned char operator()(int x, int y) const. The reason for making the method const is to protect the maintainer of the class from accidentally changing variables in the method, as well as to protect the user of the class from calling a non-read-only method on what is supposed to be a read-only object. Such compile-time checks greatly reduce chances for bugs. Copy constructor. The copy constructor is a special kind of constructor that takes a single argument of a special type, namely a const reference to the class itself. The copy constructor is called automatically whenever the compiler needs to make a copy of the object, such as when passing the object into a function (without using references or pointers), returning the object from a function (again, without references or pointers), and at construction time. The copy constructor can be made as complicated as you want (for example, to implement reference counting), but we prefer the simpler approach of making a deep copy of all the member variables. Assignment operator. The assignment operator is called whenever an object is followed by the equal (=) sign except for the line in which it is defined. The implementation of the assignment operator is often the same as the copy constructor. Initializer lists. In a constructor, a colon (:) followed by the names of the parameters, may be inserted just before the opening curly brace. This enables member variables to be initialized when they are constructed, before any code of the constructor is actually called. It is a good habit to initialize all member variables in this manner. If the reader feels overwhelmed by these concepts, which are hardly done justice by the cursory descriptions just given, consulting any good C++ tutorial or reference for further details is recommended. Once the reader is comfortable with these concepts, it will be worthwhile to examine the following C++ class, which incorporates them: class ImageGray ImageGray() : m_width(0), m_height(0), m_data(0) ImageGray(int w, int h) : m_width(0), m_height(0), m_data(0) // not needed, but makes code cleaner m_data = (unsigned char*) malloc(w * h * sizeof(unsigned char)); ImageGray(const ImageGray& other)

7 APPENDIX E. PROGRAMMING COMPUTER VISION IN C / C : m_width(0), m_height(0), m_data(0) // needed to initialize m_data *this = other; // call assignment operator to make copy ~ImageGray() free( m_data ); void Resize(int w, int h) m_data = (unsigned char*) realloc(m_data, w * h * sizeof(unsigned char)); ImageGray& operator=( const ImageGray& other ) // make a deep copy Resize( other.m_width, other.m_height ); memcpy( m_data, other.m_data, m_width * m_height * sizeof(unsigned char)); return *this; unsigned char operator()( int x, int y ) const return m_data[ y * m_width + x ]; unsigned char& operator()( int x, int y ) return m_data[ y * m_width + x ]; private: int m_width, m_height; unsigned char* m_data; An example using the class is as follows: void main() ImageGray im1(640, 480); // allocate memory for a 640 x 480 image (by calling constructor) ImageGray im2 = im1; // call copy constructor unsigned char val; // a pixel im2 = im1; // call assignment operator val = im1( 3, 2 ); // get the pixel value at (3, 2) im1( 3, 2) = val + 10; // increase the value at (3, 2) by 10 gray levels // automatically call destructors for im1 and im2 Note that operator overloading and the reference have allowed us to simplify the syntax, while const and the initializer list have not affected our use of the code here although they have made the code cleaner for future use and maintenance. The copy constructor and assignment operator are important; neglecting them will cause the default implementations to be used, which will cause unstable behavior due to the class use of pointers and the heap. Note that the equal sign, when it appears in the same line as the definition, actually calls the copy constructor rather than the assignment operator. This is one of the peculiarities of C++, but it doesn t make any difference to us since we have written both methods to perform the same. E.4 A color image in C++ The simplest color image is RGB, which stores three bytes for each pixel: the red, green, and blue values. For some reason, graphics cards and JPEG images have for decades used the convention that the bytes are stored in reverse order: first the blue, then the green, then the red. We will adopt this

8 E.4. A COLOR IMAGE IN C convention, if for no other reason to avoid unnecessary reordering of the pixels every time the image is displayed. Two options exist. One option is to represent the three color channels as planes, that is, we store the blue values of all the pixels, followed by the red values of all the pixels, followed by the green values of all the pixels. In this approach we have three images concatenated, so that the three values of any given pixel are not contiguous in memory. The other option is to interleave the color channels, so that the blue, green, and red values of the first pixel are stored, followed by the blue, green, and red values of the second pixel, and so on. The advantage of this approach is that it is the format used by graphics cards, and it also keeps all the values for a given pixel together. For these reasons we will adopt this latter approach. Let us create a struct to hold the three color values of a pixel: struct Bgr unsigned char b, g, r; Since there is no difference between a struct and a class, we could have used a class as well. But we use struct here to emphasize that the member variables themselves are public. A color image class is the same as the grayscale version above except that we replace unsigned char with Bgr. We also change the return type of the const version of the parenthesis operator to const Bgr& to avoid unnecessary copying of the struct: class ImageBgr ImageBgr() : m_width(0), m_height(0), m_data(0) ImageBgr(int w, int h) : m_width(0), m_height(0), m_data(0) // not needed, but makes code cleaner m_data = (unsigned char*) malloc(w * h * sizeof(bgr)); ImageBgr(const ImageBgr& other) : m_width(0), m_height(0), m_data(0) // needed to initialize m_data *this = other; // call assignment operator to make copy ~ImageBgr() free( m_data ); void Resize(int w, int h) m_data = (Bgr*) realloc(m_data, w * h * sizeof(bgr)); ImageBgr& operator=( const ImageBgr& other ) // make a deep copy Resize( other.m_width, other.m_height ); memcpy( m_data, other.m_data, m_width * m_height * sizeof(bgr)); return *this;

9 APPENDIX E. PROGRAMMING COMPUTER VISION IN C / C const Bgr& operator()( int x, int y ) const return m_data[ y * m_width + x ]; Bgr& operator()( int x, int y ) return m_data[ y * m_width + x ]; private: int m_width, m_height; Bgr* m_data; The class is used in the same manner as the grayscale version: void main() ImageBgr im1(640, 480); // allocate memory for a 640 x 480 image (by calling constructor) ImageBgr im2 = im1; // call copy constructor Bgr val; // a pixel im2 = im1; // call assignment operator val = im1( 3, 2 ); // get the pixel value at (3, 2) val.b += 10; val.g += 10; val.r += 10; im1( 3, 2) = val; // increase the value at (3, 2) by 10 for each color channel // automatically call destructors for im1 and im2 The C++ mechanism for handling multiple data types (at compile time, at least) is the template. The templated version of our image class is exactly the same the one above, replacing Bgr with a generic type T. This leads to our final class implementation: template <classname T> class Image Image() : m_width(0), m_height(0), m_data(0) Image(int w, int h) : m_width(0), m_height(0), m_data(0) // not needed, but makes code cleaner m_data = (unsigned char*) malloc(w * h * sizeof(t)); Image(const Image& other) : m_width(0), m_height(0), m_data(0) // needed to initialize m_data *this = other; // call assignment operator to make copy ~Image() free( m_data ); void Resize(int w, int h) m_data = (T*) realloc(m_data, w * h * sizeof(t)); Image& operator=( const Image& other )

10 E.4. A COLOR IMAGE IN C // make a deep copy Resize( other.m_width, other.m_height ); memcpy( m_data, other.m_data, m_width * m_height * sizeof(t)); return *this; const T& operator()( int x, int y ) const return m_data[ y * m_width + x ]; T& operator()( int x, int y ) return m_data[ y * m_width + x ]; private: int m_width, m_height; T* m_data; Now we can simply define our two classes as: typedef Image<unsigned char> ImageGray; typedef Image<Bgr> ImageBgr; With these definitions, these classes can be used exactly as they were when we defined them independently. The advantage of using templates is that it simplifies code maintenance and allows us to add new data types in the future with minimal extra work.

Assignment 1: grid. Due November 20, 11:59 PM Introduction

Assignment 1: grid. Due November 20, 11:59 PM Introduction CS106L Fall 2008 Handout #19 November 5, 2008 Assignment 1: grid Due November 20, 11:59 PM Introduction The STL container classes encompass a wide selection of associative and sequence containers. However,

More information

An Introduction to C++

An Introduction to C++ An Introduction to C++ Introduction to C++ C++ classes C++ class details To create a complex type in C In the.h file Define structs to store data Declare function prototypes The.h file serves as the interface

More information

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

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE? 1. Describe History of C++? The C++ programming language has a history going back to 1979, when Bjarne Stroustrup was doing work for his Ph.D. thesis. One of the languages Stroustrup had the opportunity

More information

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I Polynomial Class Polynomial(); Polynomial(const string& N, const vector& C); Polynomial operator+(const Polynomial& RHS) const; Polynomial operator-(const Polynomial& RHS) const; Polynomial operator*(const

More information

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

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor. 3.Constructors and Destructors Develop cpp program to implement constructor and destructor. Constructors A constructor is a special member function whose task is to initialize the objects of its class.

More information

CS201 Some Important Definitions

CS201 Some Important Definitions CS201 Some Important Definitions For Viva Preparation 1. What is a program? A program is a precise sequence of steps to solve a particular problem. 2. What is a class? We write a C++ program using data

More information

Kakadu and Java. David Taubman, UNSW June 3, 2003

Kakadu and Java. David Taubman, UNSW June 3, 2003 Kakadu and Java David Taubman, UNSW June 3, 2003 1 Brief Summary The Kakadu software framework is implemented in C++ using a fairly rigorous object oriented design strategy. All classes which are intended

More information

Fast Introduction to Object Oriented Programming and C++

Fast Introduction to Object Oriented Programming and C++ Fast Introduction to Object Oriented Programming and C++ Daniel G. Aliaga Note: a compilation of slides from Jacques de Wet, Ohio State University, Chad Willwerth, and Daniel Aliaga. Outline Programming

More information

Interview Questions of C++

Interview Questions of C++ Interview Questions of C++ Q-1 What is the full form of OOPS? Ans: Object Oriented Programming System. Q-2 What is a class? Ans: Class is a blue print which reflects the entities attributes and actions.

More information

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file? QUIZ on Ch.5 Why is it sometimes not a good idea to place the private part of the interface in a header file? Example projects where we don t want the implementation visible to the client programmer: The

More information

Chapter 13: Copy Control. Overview. Overview. Overview

Chapter 13: Copy Control. Overview. Overview. Overview Chapter 13: Copy Control Overview The Copy Constructor The Assignment Operator The Destructor A Message-Handling Example Managing Pointer Members Each type, whether a built-in or class type, defines the

More information

Cpt S 122 Data Structures. Introduction to C++ Part II

Cpt S 122 Data Structures. Introduction to C++ Part II Cpt S 122 Data Structures Introduction to C++ Part II Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Topics Objectives Defining class with a member function

More information

CS201 Latest Solved MCQs

CS201 Latest Solved MCQs Quiz Start Time: 09:34 PM Time Left 82 sec(s) Question # 1 of 10 ( Start time: 09:34:54 PM ) Total Marks: 1 While developing a program; should we think about the user interface? //handouts main reusability

More information

Object-Oriented Principles and Practice / C++

Object-Oriented Principles and Practice / C++ Object-Oriented Principles and Practice / C++ Alice E. Fischer October 10, 2016 OOPP / C++ Lecture 7... 1/15 Construction and Destruction Kinds of Constructors Move Semantics OOPP / C++ Lecture 7... 2/15

More information

Introduction Of Classes ( OOPS )

Introduction Of Classes ( OOPS ) Introduction Of Classes ( OOPS ) Classes (I) A class is an expanded concept of a data structure: instead of holding only data, it can hold both data and functions. An object is an instantiation of a class.

More information

Object-Oriented Programming for Scientific Computing

Object-Oriented Programming for Scientific Computing Object-Oriented Programming for Scientific Computing Dynamic Memory Management Ole Klein Interdisciplinary Center for Scientific Computing Heidelberg University ole.klein@iwr.uni-heidelberg.de 2. Mai 2017

More information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

Pointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory

Pointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory Pointers A pointer is simply a reference to a variable/object Compilers automatically generate code to store/retrieve variables from memory It is automatically generating internal pointers We don t have

More information

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program Syntax What the Compiler needs to understand your program 1 Pre-Processing Any line that starts with # is a pre-processor directive Pre-processor consumes that entire line Possibly replacing it with other

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

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

QUIZ Friends class Y;

QUIZ Friends class Y; QUIZ Friends class Y; Is a forward declaration neeed here? QUIZ Friends QUIZ Friends - CONCLUSION Forward (a.k.a. incomplete) declarations are needed only when we declare member functions as friends. They

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

In Java we have the keyword null, which is the value of an uninitialized reference type

In Java we have the keyword null, which is the value of an uninitialized reference type + More on Pointers + Null pointers In Java we have the keyword null, which is the value of an uninitialized reference type In C we sometimes use NULL, but its just a macro for the integer 0 Pointers are

More information

Object Oriented Software Design II

Object Oriented Software Design II Object Oriented Software Design II Introduction to C++ Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 20, 2012 G. Lipari (Scuola Superiore Sant Anna) C++ Intro February

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

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

Quiz Start Time: 09:34 PM Time Left 82 sec(s) Quiz Start Time: 09:34 PM Time Left 82 sec(s) Question # 1 of 10 ( Start time: 09:34:54 PM ) Total Marks: 1 While developing a program; should we think about the user interface? //handouts main reusability

More information

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator C++ Addendum: Inheritance of Special Member Functions Constructors Destructor Construction and Destruction Order Assignment Operator What s s Not Inherited? The following methods are not inherited: Constructors

More information

Object-Oriented Programming

Object-Oriented Programming - oriented - iuliana@cs.ubbcluj.ro Babes-Bolyai University 2018 1 / 56 Overview - oriented 1 2 -oriented 3 4 5 6 7 8 Static and friend elements 9 Summary 2 / 56 I - oriented was initially created by Bjarne

More information

Reminder: compiling & linking

Reminder: compiling & linking Reminder: compiling & linking source file 1 object file 1 source file 2 compilation object file 2 library object file 1 linking (relocation + linking) load file source file N object file N library object

More information

Dynamic Memory: Alignment and Fragmentation

Dynamic Memory: Alignment and Fragmentation Dynamic Memory: Alignment and Fragmentation Learning Objectives Explain the purpose of dynamic memory Define the terms arena, heap Identify common errors involving dynamic memory Explain how dynamic memory

More information

Chapter 10 Introduction to Classes

Chapter 10 Introduction to Classes C++ for Engineers and Scientists Third Edition Chapter 10 Introduction to Classes CSc 10200! Introduction to Computing Lecture 20-21 Edgardo Molina Fall 2013 City College of New York 2 Objectives In this

More information

Object-Oriented Programming

Object-Oriented Programming Object-Oriented Programming 1. What is object-oriented programming (OOP)? OOP is a technique to develop logical modules, such as classes that contain properties, methods, fields, and events. An object

More information

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

Ch. 11: References & the Copy-Constructor. - continued - Ch. 11: References & the Copy-Constructor - continued - const references When a reference is made const, it means that the object it refers cannot be changed through that reference - it may be changed

More information

CS24 Week 3 Lecture 1

CS24 Week 3 Lecture 1 CS24 Week 3 Lecture 1 Kyle Dewey Overview Some minor C++ points ADT Review Object-oriented Programming C++ Classes Constructors Destructors More minor Points (if time) Key Minor Points const Motivation

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

Lecture 18 Tao Wang 1

Lecture 18 Tao Wang 1 Lecture 18 Tao Wang 1 Abstract Data Types in C++ (Classes) A procedural program consists of one or more algorithms that have been written in computerreadable language Input and display of program output

More information

Design Principles for a Beginning Programming Language

Design Principles for a Beginning Programming Language Design Principles for a Beginning Programming Language John T Minor and Laxmi P Gewali School of Computer Science University of Nevada, Las Vegas Abstract: We consider the issue of designing an appropriate

More information

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012 MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012 Instructor: K. S. Booth Time: 70 minutes (one hour ten minutes)

More information

2 ADT Programming User-defined abstract data types

2 ADT Programming User-defined abstract data types Preview 2 ADT Programming User-defined abstract data types user-defined data types in C++: classes constructors and destructors const accessor functions, and inline functions special initialization construct

More information

COMP322 - Introduction to C++ Lecture 10 - Overloading Operators and Exceptions

COMP322 - Introduction to C++ Lecture 10 - Overloading Operators and Exceptions COMP322 - Introduction to C++ Lecture 10 - Overloading Operators and Exceptions Dan Pomerantz School of Computer Science 19 March 2012 Overloading operators in classes It is useful sometimes to define

More information

CS201- Introduction to Programming Current Quizzes

CS201- Introduction to Programming Current Quizzes CS201- Introduction to Programming Current Quizzes Q.1 char name [] = Hello World ; In the above statement, a memory of characters will be allocated 13 11 12 (Ans) Q.2 A function is a block of statements

More information

CSCI-1200 Data Structures Fall 2018 Lecture 7 Templated Classes & Vector Implementation

CSCI-1200 Data Structures Fall 2018 Lecture 7 Templated Classes & Vector Implementation CSCI-1200 Data Structures Fall 2018 Lecture 7 Templated Classes & Vector Implementation Announcements Lab 3 was a Frankenstein assembly of a new group exercise with part of a gdb lab. I hear many of you

More information

C++ PROGRAMMING LANGUAGE: CLASSES. CAAM 519, CHAPTER 13

C++ PROGRAMMING LANGUAGE: CLASSES. CAAM 519, CHAPTER 13 C++ PROGRAMMING LANGUAGE: CLASSES. CAAM 519, CHAPTER 13 This chapter focuses on introducing the notion of classes in the C++ programming language. We describe how to create class and use an object of a

More information

Object-Oriented Principles and Practice / C++

Object-Oriented Principles and Practice / C++ Object-Oriented Principles and Practice / C++ Alice E. Fischer May 13, 2013 OOPP / C++ Lecture 7... 1/27 Construction and Destruction Allocation and Deallocation Move Semantics Template Classes Example:

More information

C Programming Basics II

C Programming Basics II C Programming Basics II Xianyi Zeng xzeng@utep.edu Department of Mathematical Sciences The University of Texas at El Paso. September 20, 2016. Pointers and Passing by Address Upon declaring a variable,

More information

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012 MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012 Instructor: K. S. Booth Time: 70 minutes (one hour ten minutes)

More information

Programming in C and C++

Programming in C and C++ Programming in C and C++ 5. C++: Overloading, Namespaces, and Classes Dr. Neel Krishnaswami University of Cambridge (based on notes from and with thanks to Anil Madhavapeddy, Alan Mycroft, Alastair Beresford

More information

Object-Oriented Principles and Practice / C++

Object-Oriented Principles and Practice / C++ Object-Oriented Principles and Practice / C++ Alice E. Fischer April 20, 2015 OOPP / C++ Lecture 3... 1/23 New Things in C++ Object vs. Pointer to Object Optional Parameters Enumerations Using an enum

More information

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor CS 261 Fall 2017 Mike Lam, Professor C Introduction Variables, Memory Model, Pointers, and Debugging The C Language Systems language originally developed for Unix Imperative, compiled language with static

More information

Vector and Free Store (Pointers and Memory Allocation)

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

More information

Instantiation of Template class

Instantiation of Template class Class Templates Templates are like advanced macros. They are useful for building new classes that depend on already existing user defined classes or built-in types. Example: stack of int or stack of double

More information

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2 Discussion 1E Jie(Jay) Wang Week 10 Dec.2 Outline Dynamic memory allocation Class Final Review Dynamic Allocation of Memory Recall int len = 100; double arr[len]; // error! What if I need to compute the

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

Financial computing with C++

Financial computing with C++ Financial Computing with C++, Lecture 6 - p1/24 Financial computing with C++ LG Gyurkó University of Oxford Michaelmas Term 2015 Financial Computing with C++, Lecture 6 - p2/24 Outline Linked lists Linked

More information

Chapter 18 Vectors and Arrays [and more on pointers (nmm) ] Bjarne Stroustrup

Chapter 18 Vectors and Arrays [and more on pointers (nmm) ] Bjarne Stroustrup Chapter 18 Vectors and Arrays [and more on pointers (nmm) ] Bjarne Stroustrup www.stroustrup.com/programming Abstract arrays, pointers, copy semantics, elements access, references Next lecture: parameterization

More information

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 4 Thomas Wies New York University Review Last week Control Structures Selection Loops Adding Invariants Outline Subprograms Calling Sequences Parameter

More information

Chapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

Chapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo Chapter 10 Pointers and Dynamic Arrays 1 Learning Objectives Pointers Pointer variables Memory management Dynamic Arrays Creating and using Pointer arithmetic Classes, Pointers, Dynamic Arrays The this

More information

Programming, numerics and optimization

Programming, numerics and optimization Programming, numerics and optimization Lecture A-4: Object-oriented programming Łukasz Jankowski ljank@ippt.pan.pl Institute of Fundamental Technological Research Room 4.32, Phone +22.8261281 ext. 428

More information

Class Vector: Interface CSE 143. Dynamic Memory In Classes [Chapter 4, p ] Vector Implementation. Many Ways to Implement. Draw the picture!

Class Vector: Interface CSE 143. Dynamic Memory In Classes [Chapter 4, p ] Vector Implementation. Many Ways to Implement. Draw the picture! Dynamic Memory In Classes [Chapter 4, p 156-157] Class Vector: Interface Vector ( ); bool isempty( ); int length ( ); void vectorinsert (int newposition, Item newitem); Item vectordelete (int position);

More information

STRUCTURING OF PROGRAM

STRUCTURING OF PROGRAM Unit III MULTIPLE CHOICE QUESTIONS 1. Which of the following is the functionality of Data Abstraction? (a) Reduce Complexity (c) Parallelism Unit III 3.1 (b) Binds together code and data (d) None of the

More information

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26 COSC 2P95 Procedural Abstraction Week 3 Brock University Brock University (Week 3) Procedural Abstraction 1 / 26 Procedural Abstraction We ve already discussed how to arrange complex sets of actions (e.g.

More information

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Allocating Space

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Allocating Space CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Allocating Space Dynamic Memory Allocation All variables, arrays, structures and unions that we worked with so far are statically allocated,

More information

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com More C++ : Vectors, Classes, Inheritance, Templates with content from cplusplus.com, codeguru.com 2 Vectors vectors in C++ basically arrays with enhancements indexed similarly contiguous memory some changes

More information

Inheritance, and Polymorphism.

Inheritance, and Polymorphism. Inheritance and Polymorphism by Yukong Zhang Object-oriented programming languages are the most widely used modern programming languages. They model programming based on objects which are very close to

More information

Ch. 12: Operator Overloading

Ch. 12: Operator Overloading Ch. 12: Operator Overloading Operator overloading is just syntactic sugar, i.e. another way to make a function call: shift_left(42, 3); 42

More information

Constructors for classes

Constructors for classes Constructors for Comp Sci 1570 Introduction to C++ Outline 1 2 3 4 5 6 7 C++ supports several basic ways to initialize i n t nvalue ; // d e c l a r e but not d e f i n e nvalue = 5 ; // a s s i g n i

More information

Lecture 15: Even more pointer stuff Virtual function table

Lecture 15: Even more pointer stuff Virtual function table CIS 330: / / / / (_) / / / / _/_/ / / / / / \/ / /_/ / `/ \/ / / / _/_// / / / / /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / / /_/ / / / / / \ /_/ /_/_/_/ _ \,_/_/ /_/\,_/ \ /_/ \ //_/ /_/ Lecture 15:

More information

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a. Intro to OOP - Object and class - The sequence to define and use a class in a program - How/when to use scope resolution operator - How/when to the dot operator - Should be able to write the prototype

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 - 33 Overloading Operator for User - Defined Types: Part 1 Welcome

More information

11. Arrays. For example, an array containing 5 integer values of type int called foo could be represented as:

11. Arrays. For example, an array containing 5 integer values of type int called foo could be represented as: 11. Arrays An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier. That means that, for example,

More information

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example CS 311 Data Structures and Algorithms Lecture Slides Friday, September 11, 2009 continued Glenn G. Chappell

More information

eingebetteter Systeme

eingebetteter Systeme Praktikum: Entwicklung interaktiver eingebetteter Systeme C++-Tutorial (falk@cs.fau.de) 1 Agenda Classes Pointers and References Functions and Methods Function and Operator Overloading Template Classes

More information

C Introduction. Comparison w/ Java, Memory Model, and Pointers

C Introduction. Comparison w/ Java, Memory Model, and Pointers CS 261 Fall 2018 Mike Lam, Professor C Introduction Comparison w/ Java, Memory Model, and Pointers Please go to socrative.com on your phone or laptop, choose student login and join room LAMJMU The C Language

More information

CS103L PA4. March 25, 2018

CS103L PA4. March 25, 2018 CS103L PA4 March 25, 2018 1 Introduction In this assignment you will implement a program to read an image and identify different objects in the image and label them using a method called Connected-component

More information

Classes: Member functions // classes example #include <iostream> using namespace std; Objects : Reminder. Member functions: Methods.

Classes: Member functions // classes example #include <iostream> using namespace std; Objects : Reminder. Member functions: Methods. Classes: Methods, Constructors, Destructors and Assignment For : COP 3330. Object oriented Programming (Using C++) http://www.compgeom.com/~piyush/teach/3330 Piyush Kumar Classes: Member functions // classes

More information

COMP6771 Advanced C++ Programming

COMP6771 Advanced C++ Programming 1. COMP6771 Advanced C++ Programming Week 4 Part One: (continued) and 2016 www.cse.unsw.edu.au/ cs6771 2. Inline Constructors, Accessors and Mutators Question (from 2015): In the week 3 examples, constructors

More information

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++ CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 19 Introduction to C++ C++ C++ is an enormous language: All of C Classes and objects (kind of like Java, some crucial differences) Many

More information

Next week s homework. Classes: Member functions. Member functions: Methods. Objects : Reminder. Objects : Reminder 3/6/2017

Next week s homework. Classes: Member functions. Member functions: Methods. Objects : Reminder. Objects : Reminder 3/6/2017 Next week s homework Classes: Methods, Constructors, Destructors and Assignment Read Chapter 7 Your next quiz will be on Chapter 7 of the textbook For : COP 3330. Object oriented Programming (Using C++)

More information

Smart Pointers. Some slides from Internet

Smart Pointers. Some slides from Internet Smart Pointers Some slides from Internet 1 Part I: Concept Reference: Using C++11 s Smart Pointers, David Kieras, EECS Department, University of Michigan C++ Primer, Stanley B. Lippman, Jesee Lajoie, Barbara

More information

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 11 October 3, 2018 CPSC 427, Lecture 11, October 3, 2018 1/24 Copying and Assignment Custody of Objects Move Semantics CPSC 427, Lecture

More information

Functions, Pointers, and the Basics of C++ Classes

Functions, Pointers, and the Basics of C++ Classes Functions, Pointers, and the Basics of C++ Classes William E. Skeith III Functions in C++ Vocabulary You should be familiar with all of the following terms already, but if not, you will be after today.

More information

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved.

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved. Chapter 10 Pointers and Dynamic Arrays Copyright 2016 Pearson, Inc. All rights reserved. Learning Objectives Pointers Pointer variables Memory management Dynamic Arrays Creating and using Pointer arithmetic

More information

For Teacher's Use Only Q No Total Q No Q No

For Teacher's Use Only Q No Total Q No Q No Student Info Student ID: Center: Exam Date: FINALTERM EXAMINATION Spring 2010 CS201- Introduction to Programming Time: 90 min Marks: 58 For Teacher's Use Only Q No. 1 2 3 4 5 6 7 8 Total Marks Q No. 9

More information

explicit class and default definitions revision of SC22/WG21/N1582 =

explicit class and default definitions revision of SC22/WG21/N1582 = Doc No: SC22/WG21/ N1702 04-0142 Project: JTC1.22.32 Date: Wednesday, September 08, 2004 Author: Francis Glassborow & Lois Goldthwaite email: francis@robinton.demon.co.uk explicit class and default definitions

More information

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Cpt S 122 Data Structures. Course Review Midterm Exam # 2 Cpt S 122 Data Structures Course Review Midterm Exam # 2 Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Midterm Exam 2 When: Monday (11/05) 12:10 pm -1pm

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: 4 Date:

More information

SERIOUS ABOUT SOFTWARE. Qt Core features. Timo Strömmer, May 26,

SERIOUS ABOUT SOFTWARE. Qt Core features. Timo Strömmer, May 26, SERIOUS ABOUT SOFTWARE Qt Core features Timo Strömmer, May 26, 2010 1 Contents C++ refresher Core features Object model Signals & slots Event loop Shared data Strings Containers Private implementation

More information

Department of Computer science and Engineering Sub. Name: Object oriented programming and data structures Sub. Code: EC6301 Sem/Class: III/II-ECE Staff name: M.Kavipriya Two Mark Questions UNIT-1 1. List

More information

COIMBATORE EDUCATIONAL DISTRICT

COIMBATORE EDUCATIONAL DISTRICT COIMBATORE EDUCATIONAL DISTRICT REVISION EXAMINATION JANUARY 2015 STD-12 COMPUTER SCIENCE ANSEWR KEY PART-I Choose the Correct Answer QNo Answer QNo Answer 1 B Absolute Cell Addressing 39 C Void 2 D

More information

CSCI-1200 Data Structures Fall 2017 Lecture 10 Vector Iterators & Linked Lists

CSCI-1200 Data Structures Fall 2017 Lecture 10 Vector Iterators & Linked Lists CSCI-1200 Data Structures Fall 2017 Lecture 10 Vector Iterators & Linked Lists Review from Lecture 9 Explored a program to maintain a class enrollment list and an associated waiting list. Unfortunately,

More information

CPSC 427a: Object-Oriented Programming

CPSC 427a: Object-Oriented Programming CPSC 427a: Object-Oriented Programming Michael J. Fischer Lecture 5 September 15, 2011 CPSC 427a, Lecture 5 1/35 Functions and Methods Parameters Choosing Parameter Types The Implicit Argument Simple Variables

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

CSCI-1200 Data Structures Fall 2011 Lecture 24 Garbage Collection & Smart Pointers

CSCI-1200 Data Structures Fall 2011 Lecture 24 Garbage Collection & Smart Pointers CSCI-1200 Data Structures Fall 2011 Lecture 24 Garbage Collection & Smart Pointers Review from Lecture 23 Basic exception mechanisms: try/throw/catch Functions & exceptions, constructors & exceptions Today

More information

nptr = new int; // assigns valid address_of_int value to nptr std::cin >> n; // assigns valid int value to n

nptr = new int; // assigns valid address_of_int value to nptr std::cin >> n; // assigns valid int value to n Static and Dynamic Memory Allocation In this chapter we review the concepts of array and pointer and the use of the bracket operator for both arrays and pointers. We also review (or introduce) pointer

More information

CHAPTER 7 OBJECTS AND CLASSES

CHAPTER 7 OBJECTS AND CLASSES CHAPTER 7 OBJECTS AND CLASSES OBJECTIVES After completing Objects and Classes, you will be able to: Explain the use of classes in Java for representing structured data. Distinguish between objects and

More information

7.1 Optional Parameters

7.1 Optional Parameters Chapter 7: C++ Bells and Whistles A number of C++ features are introduced in this chapter: default parameters, const class members, and operator extensions. 7.1 Optional Parameters Purpose and Rules. Default

More information

Programming Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science Indian Institute of Technology, Madras

Programming Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science Indian Institute of Technology, Madras Programming Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science Indian Institute of Technology, Madras Module 12B Lecture - 41 Brief introduction to C++ Hello, welcome

More information

CSCI-1200 Data Structures Spring 2018 Lecture 8 Templated Classes & Vector Implementation

CSCI-1200 Data Structures Spring 2018 Lecture 8 Templated Classes & Vector Implementation CSCI-1200 Data Structures Spring 2018 Lecture 8 Templated Classes & Vector Implementation Review from Lectures 7 Algorithm Analysis, Formal Definition of Order Notation Simple recursion, Visualization

More information

Symbol Tables Symbol Table: In computer science, a symbol table is a data structure used by a language translator such as a compiler or interpreter, where each identifier in a program's source code is

More information