Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.

Slides:



Advertisements
Similar presentations
Lesson 13 Introduction to Classes CS1 Lesson Introduction to Classes1.
Advertisements

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 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
A C LOSER L OOK AT C LASSES 1. A SSIGNING O BJECTS One object can be assigned to another provided that both objects are of the same type. It is not sufficient.
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.
1 Pointers & functions Pointers allow us to simulate pass by reference. void swap(int &a, int &b) { int temp = a; a = b; b = temp; } int main () { int.
C++ data types. Structs vs. Classes C++ Classes.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
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.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Review of C++ Programming Part II Sheng-Fang Huang.
Constructors and Other Tools Version 1.0 Topics Constructors & Destructors Composition const Parameter Modifier const objects const functions In-line.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Pointer Data Type and Pointer Variables
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
 200 Total Points ◦ 74 Points Writing Programs ◦ 60 Points Tracing Algorithms and determining results ◦ 36 Points Short Answer ◦ 30 Points Multiple Choice.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Stack and Heap Memory Stack resident variables include:
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
CS 11 C++ track: lecture 4 Today: More on memory management the stack and the heap inline functions structs vs. classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Class Members Class Definition – class Name – { – public: » constructor(s) » destructor » function members » data members – protected: » function members.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
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:
CLASSES : A DEEPER LOOK Chapter 9 Part I 1. 2 OBJECTIVES In this chapter you will learn: How to use a preprocessor wrapper to prevent multiple definition.
Chapter 9 Classes: A Deeper Look, Part I Part II.
Data Structures Using C++1 Chapter 3 Pointers Dr. Liu.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
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.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
1 Classes classes and objects - from object-oriented programming point of view class declaration class class_name{ data members … methods (member functions)
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
[C++] CRASH COURSE KSU Unreal 4 Workshop Zach Ginn.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Introduction to Classes
Memberwise Assignment / Initialization
Introduction to Classes
9-10 Classes: A Deeper Look.
Class rational part2.
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
C++ data types.
9-10 Classes: A Deeper Look.
Corresponds with Chapter 5
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
SPL – PS3 C++ Classes.
Presentation transcript:

Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays

Class Scope is by default private Requires a header file (.h) Constructor  Called when instantiating the class  A default constructor is always provided Destructor  Called when the removing allocated memory for a class (Only when instantiating dynamic objects)

Constructor and Destructor Header (.h) Source file (.cpp)

Instantiation

Assignment What happens? Is c the same as c1? What is being called?

Copy constructor There’s a default copy constructor! : after a class constructor is an initialization list

Copy constructor Called by passing in an instantiated object Called by the operator = //Calls copy constructor

Struct C data type Public scope Otherwise it’s the same as a class

Enums Useful for switch statements Default values -> 0, 1, 2, 3…

Gears are set to 0

Whenever we call a function e.g. y = sqrt(x);  A stack frame is created that holds The return memory address (where to go when the function ends) The parameters (if any) Local variables (if any) Return data type and value (if any)  The stack frame is then placed on the program stack  The function code is executed  When the function ends the stack frame is removed from the program stack  Any return value is retrieved  The program counter is loaded with the return memory address  The stack frame is destroyed Everything in red can be ‘removed’ using inline functions

Function call versus Inline functions Our compiled code Function implementation code (compiled) Function overhead code (compiled) Function call Standard Functions Inline Functions When the compiler finds a function call it replaces it with a compiled version of the function implementation code.

Value If I want the value of a memory address  * operator Is a dereferencing operator E.g. int x = 10 If I cout << *&x  It will print 10  **& will error as there is no value of the value  &*& will return the memory address of the variable  *&*& will return 10

Reference & – Address of operator  Gets the memory address of a variable  E.g. int x = 10;  10 is the value  If I cout << &x Something like: 0020F9D0

Pick ‘n pay (PnP) is a value 59 is it’s address &PnP returns 59

Passing by value

Passing by reference

Pass using const when the value doesn’t need to be changed

Advantages  No copy is made  Function can modify value Disadvantages  Difficult to tell if passing to a function is giving you input or output  Dereferencing a Pointer to a variable is slow

Pass by value 1 2 Inside function SPAR Exit function

Pass by reference Now I can change pick n pay Without recreating PnP Function SPAR

Passing by address Function takes a pointer

What happens here?

Arrays Arrays can be defined, which allows a number of consecutive variables / objects to be created. Arrays are particularly useful with for- loops. Static arrays must be declared with a maximum size. Run-time errors can occur if attempting to access outside of an array’s index.

Example

Accessing an array of objects