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.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Chapter 7: User-Defined Functions II
Class and Objects.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Chapter 15 Memory Management: Four main memory areas for a C++ program: Code: code for instructions, methods, etc. static data: Global variables (declared.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Classes Separating interface from implementation
1 CSE 303 Lecture 21 Classes and Objects in C++ slides created by Marty Stepp
Chapter 11: Classes and Data Abstraction
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.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Review of C++ Programming Part II Sheng-Fang Huang.
OOP Languages: Java vs C++
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
Copy Control Joe Meehean. More Class Responsibilities When making a new type (i.e., class) we must specify what happens when it is: Copied Assigned Destroyed.
Inheritance Joe Meehean. Object Oriented Programming Objects state (data) behavior (methods) identity (allocation of memory) Class objects definition.
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Pointers OVERVIEW.
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 4 – August 30, 2001.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Dynamically Allocated Arrays December 4, Skip the Rest of this PowerPoint.
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Dynamic Memory. We will follow different order from Course Book We will follow different order from Course Book First we will cover Sect The new.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
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:
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
1 Chapter 1 C++ Basics Review Reading: Sections 1.4 and 1.5.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
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.
CSC241 Object-Oriented Programming (OOP) Lecture No. 5.
Effective C++, 2nd Ed. By Scott Myers. Constructors, Destructors, and Assignment Operators 11.Define a copy constructor and an assignment operator for.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 05: Classes and Data Abstraction.
Cop3530sp12. Parameter passing call by value- appropriate for small objects that should not be altered by the function call by constant reference- appropriate.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Classes - Part II (revisited) n Constant objects and member functions n Definition Form of Member Functions n friend functions and friend classes n Constructors.
Recap Stale Pointers and Double Delete Reference Variables Reference Type vs Pointer Type Structures Pointers to Structures Exogenous vs Indigenous Data.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
1 // SPECIFICATION FILE (dynarray.h) // Safe integer array class allows run-time specification // of size, prevents indexes from going out of bounds, //
1 Chapter 1 C++ Templates Readings: Sections 1.6 and 1.7.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
Chapter 2 Objects and Classes
Yan Shi CS/SE 2630 Lecture Notes
Chapter 1 C++ Basics Review
Introduction to Classes
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
Chapter 2 Objects and Classes
Introduction to Classes
Dr. Bhargavi Dept of CS CHRIST
SPL – PS3 C++ Classes.
Presentation transcript:

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 Hiding Labels  public  private  protected Constructors  We have two in this example  Why?

Additional Syntax and Accessors Initializer list  Init data members directly in the constructor Explicit constructor  Avoids automatic type conversion (and resulting bugs) Constant member functions  Examines, but does not change the object state  Also called ‘accessor’  Non-const functions are called ‘mutators’

Interface Vs. Implementation Interface typically defined in.h files  #include in.c file Preprocessor commands  Guards against multiple inclusion of.h files Interface

Interface Vs. Implementation (contd.) Scoping operator  To identify the class corresponding to each function Remember  Function signatures must match in both interface and implementation  Default parameters are specified only in the interface Implementation

main() function Objects are declared just like primitive data types. Legal Declarations  Intcell obj1; // zero parameter constructor  Intcell obj2(12); // one parameter constructor Illegal declarations  Intcell obj3 = 37; // explicit constructor used  Intcell obj4(); // function declaration main() function

Vectors Replaces built-in C++ arrays  Built-in arrays do not act as proper C++ objects Standard vector class  Gives a size() function  Can be assigned using = Similarly C++ also provides standard string class.

Pointers Pointer variable  Stores the address of another object in memory. Declaration  * before the variable name indicates a pointer declaration  Pointers are uninitialized at declaration time.  Reading uninitialized pointer values results in bugs. Dynamic object creation  Using the new keyword

Memory leaks= errors and grade penalties in your programming assignment (we will check for those) Pointers (contd) Garbage collection  Objects allocated using new must be explicitly deleted.  Else your program will have memory leaks  There’s no automatic GC in C++. Accessing members of an object  Use the -> operator Address-of operator  &obj gives the address where obj is stored.

Parameter Passing double avg( const vector & arr, int n, bool & errorFlag); Call by value  Copies the value of parameter being passed.  Called function an modify the parameter, but cannot alter the original variable.  What happens if the parameter is an object? Call by reference  Used when the function needs to change the value of original argument Call by constant reference  Typically used when parameter is a large object Should not be changed by the function Using call-by-value would result in large copying overhead.

Return Passing Return by value  Makes a copy of the variable returned Return by reference  Return the address of the variable returned Return by constant reference  Return the address of the variable returned  Return value cannot be modified by caller. Last two techniques  Lifetime of returned value should extend beyond the function called Correct Incorrect Why??

Reference Variables Synonyms of objects they reference  Reference are not pointers Can be used for  Parameter passing  Local variables Avoid the cost of copying E.g. string x = findMax(a); string &y = x; cout << y << endl; Also used for referencing objects with complex expression  list &whichList = theLists[ hash(x, theLists.size()) ];

Destructor Called whenever  Object goes out of scope  delete called Frees up resource allocated for the object

Copy constructor Initializes a new object to another of its own type Invoked during  Declaration IntCell B = C; Intcell B (C);  Call by value  Return by value But not in  B = C; (assignment operator)

operator= Copy assignment operator Called when both LHS and RHS objects have been created

Problem with defaults Usually don’t work when data member is a pointer type. What is the output of f() in the adjacent example? In this example, default operator= and copy constructor copy the pointer instead of the value

Exercise Find out the difference between  Shallow copy, and  Deep copy