PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Chapter 17 vector and Free Store Bjarne Stroustrup
Chapter 19 Vectors, templates, and exceptions Bjarne Stroustrup
Chapter 18 Vectors and Arrays
Chapter 18 Vectors and Arrays John Keyser’s Modification of Slides by Bjarne Stroustrup
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Chapter 17 vector and Free Store John Keyser’s Modifications of Slides By Bjarne Stroustrup
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
A pointer is the memory address of a variable. A memory address is a physical location within a system’s memory space. A pointer variable is variable used.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
A RRAYS, P OINTERS AND R EFERENCES 1. A RRAYS OF O BJECTS Arrays of objects of class can be declared just like other variables. class A{ … }; A ob[4];
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
CS 225 Lab #2 - Pointers, Copy Constructors, Destructors, and DDD.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Class Constructors a class constructor is a member function whose purpose is to initialize the private data members of a class object the name of a constructor.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Review of C++ Programming Part II Sheng-Fang Huang.
OOP Languages: Java vs C++
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
Pointer Data Type and Pointer Variables
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ REVIEW – POINTERS AND TEST DRIVEN DEVELOPMENT.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
1 Memory as byte array Pointers Arrays relationship to pointers Operator ‘new’ Operator ‘delete’ Copy ctor Assignment operator ‘this’ const pointer Allocating.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Dynamic Array Allocation char *ptr; // ptr is a pointer variable that // can hold the address of a char ptr = new char[ 5 ]; // dynamically, during run.
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
Data Structures in C++ Pointers & Dynamic Arrays Shinta P.
Learners Support Publications Constructors and Destructors.
POINTERS AND MEMORY ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
Constructors and Destructors
Pointer to an Object Can define a pointer to an object:
Pointers and Dynamic Arrays
CMSC 341 Lecture 2 – Dynamic Memory and Pointers (Review)
Motivation and Overview
Class Operations Pointer and References with class types
Pointers Revisited What is variable address, name, value?
Dynamic Memory CSCE 121 J. Michael Moore.
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
group work #hifiTeam
Dynamic Memory Allocation
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Pointers, Dynamic Data, and Reference Types
Pointers And Memory Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
Constructors and Destructors
Indirection.
Summary: Abstract Data Type
Class and Objects In a class, all the functions that operate on the data structure are grouped together in one place along with the data Like a struct.
Review Chapter 10 PPT for full coverage.
Dynamic Memory CSCE 121.
8.3 Vectors Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Presentation transcript:

PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1

OVERVIEW  Implement vector, list and stack using vector and list as underlying container.  Interface is modeled after Standard Template Library.  Reference use: to know more about what each function behaveshttp://  Use Prog01/Docs/html/index.html to find the list of things you need to do and other instructions 2

COMPILING AND RUNNING YOUR CODE  Change directory to where your code lies : cd Prog01  Compile your code: make  Test your code:./test_vector.o 3

SUGGESTED PLAN  Start with vector.h  Complete default and copy constructor  Assignment operator  reserve, push_back, push_back_incremental, pop_back, resize 4

CONSTRUCTORS (CH 1.5.2)  Constructors are the member functions that initializes an object for a class  Default constructor: No initialization information  Parameterized constructor: Initialization information passed as function parameters  Copy Constructor: Object of same the class is passed as parameter to the constructor  class Point{ int x; int y; public: Point(); //default constructor Point(int _a, int _b); //Parameterized Constructor Point(const Point& _p); //Copy constructor }  Exercise: Which constructor invoked?  Point A;  Point B(A);  Point C(10,15); 5

DEFAULT PARAMETERS & INITIALIZER LIST  Default function (constructor) parameters: The members are assigned the default values when not passed as parameter in the function call.  Point(int x=0, int y=0);  Point A; //creates a Point object with x=0, y=0  Point B(10); //creates a Point object with x=10 and y=0  Point C(10,10); //creates a Point object with x=10 and y=10  Note the default parameters must be set from rightmost parameter  Point(int x, int y=0); //allowed  Point(int x=0, int y); //not allowed  Initializer list: Invoking the constructor of the member variables before the body of constructor being executed  Point(int _a, int_b) : x(_a), y(_b) { …} 6

ARRAYS, POINTERS AND DYNAMIC ALLOCATION (CH 1.1.3)  Pointer is a variable that holds the address of another data  & - address operator, *- dereferencing a pointer  int x = 10;  int* y = &x; //y is a pointer containing address of x  cout<<*y; //output 10  Name of the array is pointer to first element in the array  int arr[10];  *(arr+4); //same as arr[4]  Dynamic allocation and new operator:  new allocates memory for an object, calls the objects constructor and returns a pointer to the object  Point* A = new Point(10,20);  Dereference a member of pointer variable using ->  cout x; //output 10  Can be used to allocate or create an array of dynamic size.  int *arr = new int[10]; 7

DESTRUCTORS  Invoked when an object cease to exist.  What it should contain ? Releasing of resources (ie., memory)  delete operator helps in deallocating memory  delete [] arr; // deletes dynamically allocated array  delete A; //deletes a pointer 8