C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.

Slides:



Advertisements
Similar presentations
Introduction to C++ Templates Speaker: Bill Chapman, C++ developer, financial company in Mid-Manhattan. Can be reached via: ' This.
Advertisements

Chapter 17 vector and Free Store John Keyser’s Modifications of Slides By Bjarne Stroustrup
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Programming and Data Structure
Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Animation Mrs. C. Furman. Animation  We can animate our crab by switching the image between two pictures.  crab.png and crab2.png.
. Templates. Example… A useful routine to have is void Swap( int& a, int &b ) { int tmp = a; a = b; b = tmp; }
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
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.
Pointers Applications
Templates. Example… A useful routine to have is void swap(int &a, int &b){ int tmp = a; a = b; b = tmp; }
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Data Structures Using C++ 2E
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
 200 Total Points ◦ 74 Points Writing Programs ◦ 60 Points Tracing Algorithms and determining results ◦ 36 Points Short Answer ◦ 30 Points Multiple Choice.
Stack and Heap Memory Stack resident variables include:
CSE 232: C++ pointers, arrays, and references Overview of References and Pointers Often need to refer to another object –Without making a copy of the object.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Recap Visual Perception and Data Visualization Types of Information Display Examples of Diagrams used for Data Display Planning Requirement for Data Visualization.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
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.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
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:
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
 200 Total Points ◦ 75 Points Writing Programs ◦ 60 Points Tracing Algorithms and determining results ◦ 35 Points Short Answer ◦ 30 Points Multiple Choice.
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++
C++ Class. © 2005 Pearson Addison-Wesley. All rights reserved 3-2 Abstract Data Types Figure 3.1 Isolated tasks: the implementation of task T does not.
POINTERS Introduction to Systems Programming - COMP 1002, 1402.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Copyright © 2009 – Curt Hill Standard Template Library An Introduction.
Variables and memory addresses
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Pointer Variables A pointer is a variable that contains a memory address The address is commonly the location of another variable in memory This pointer.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
14-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
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.
Motivation and Overview
Java Primer 1: Types, Classes and Operators
Chapter 15 Pointers, Dynamic Data, and Reference Types
Java Programming Language
C Programming Pointers
Presentation transcript:

C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library

Classes A class is a way of making a new variable type that is very powerful. It does not just contain data, but also comes with functions (methods) that can be called. This allows us to define a new type for a 3 vector (for example) have it do more than just store 3 numbers, it can tell you the magnitude, direction, dot product with another vector, etc, etc. 2

Constructor When you make a new int or double the compiler just finds sufficient memory and if you have set an initial value in the code, that value is copied to this new memory location. A class is more sophisticated and so we have the ability to specify exactly what should happen when an instance of our class is created. This code is called the constructor. 3

Destructor Similarly, when a simple type is no longer needed that memory is freed up, but nothing else is done. In a class, we can define specific code that should happen just as the instance of that class is being destroyed. This code is called the destructor. 4

Functions and Variables In addition to the constructor and destructor, a class can have any number of functions. It can also have variables of either the basic types, or of other classes. We typically store internal information in the variables and then provide functions (often called methods) that use this information to perform whatever tasks the class is meant to do. 5

Public and Private Methods and variables can be declared as being public or private. If something is public, it means that code outside of the class can see it and call it (if it is a function) or use it in an expression (if it is a variable). If something is declared to be private, then it is only visible to other code that is in that class. 6

Methods and Variables What we usually do is to have the variables that are part of a class private. We then make some (or all) of the methods of the class public. This means that the code that uses a class can call a well defined method that should perform a certain task without any need to know how that task is performed. 7

A Very Simple Class class MyClass { public : MyClass() {};// this is the constructor ~MyClass() {};// this is the destructor void increment() { val++; } // this function increments the value void decrement() { val--; } // this function decrements the value int value() { return val; } // this function returns the value private : int val; }; 8

Inline Functions In the previous example, the code for each function was provided in the class declaration. This is known as an inline function. It is a good idea to do this when the function is very short. For more complicated code, it should be compiled separately (we will see this shortly). 9

Simple Class Example Download the file simpleClass.cc and compile it. Run it and check that you understand what is happening. Uncomment the line that attempts to use the variable _val and try to compile. 10

Pointers Pointers are effectively a different type and refer to the address in memory of a variable. double x = 5; double* y = & x; y holds the address in memory where the variable x is located. You can de-reference a pointer using * e.g. std::cout << *y << std::endl; 11

References Are like pointers, except: –They do not need to be dereferenced –They must always refer to something (i.e. they cannot be NULL, like a pointer!) A reference IS whatever it is referring to. double x = 5; double& y = x; y = 9; 12

Pointers & References Download the pointers.cc and references.cc files and compile them. Run them and check that you understand what is happening. In the pointers.cc file, uncomment the code at the end, recompile and see what happens when you run. 13

Makefiles When we start making G4MICE applications, we will use a tool that generates Makefiles for us. For now, I will provide some Makefiles for the remaining exercises. A Makefile allows you to specify what actions are required to compile the code and how one file depends on another. Use the example Makefile for this session. 14

STL Standard Template Library: – We have already seen one class from the library, string. Another useful tool from the STL is the vector template. This template allows you to create a vector (array) of any type. 15

vector std::vector numbers; Defines a vector (container) of integers called numbers. size() – return the number of elements in the vector [] – refers to the ith element (numbering starts at 0, e.g. numbers[10] push_back( x ) – add an element to the end of the vector. 16

Vector Example The Makefile should already have built the vector example for you (if you used the tarball). If not, download the code and compile it. Run the vector example to understand how the vector template works. 17

More STL We don’t have enough time to go into the STL in any detail, but it is worth spending some time to learn what it can provide. There are many different templates that provide useful ways of storing information as well as algorithms such as sorting routines. 18