C/C++ Coursework Module. Ian Reid, Trinity 2008

Size: px
Start display at page:

Download "C/C++ Coursework Module. Ian Reid, Trinity 2008"

Transcription

1 C/C++ Coursework Module Ian Reid, Trinity 2008 The course takes you through a series of exercises designed to familiarise you with design and implementation issues in C++ programming. Objectives: Design and implementation of C++ programs. Functions, classes, recursion, inheritance, graphics,... References C/C++ course/lecture notes (IDR) ian/teaching/c++ Lipmann and Lajoie (3rd edition) Stroustrup (3rd edition) Pohl Structure: Exercises learning by example (Software-lab), Mini-lectures (9am, and 12noon or 2pm) Discussions design in small groups Project 3D graphical computer game (maybe?) 1 2 Introduction Interpreted language Source code Algorithms Data structures.m Interpreter Interpreter: a program that reads source line by line and carries out the action specified by the line of code Programs Interpreted language: Procedural programming: design focuses on algorithms e.g. matlab, C, Pascal, Fortran Object oriented programming: design focuses on data structures e.g. C++, Java good run-time error handling and identification rapid prototyping slow e.g matlab, various scripting languages 3 4

2 C/C++ compilation Source code.c,.c,.cc Compilation Object files.o Linking A simple program in matlab %% function to compute sum from 1 to n function res = sum(n) if (n <= 0) res = 0; else res = 0; for i=1:n res = res + i; end end Executable Example: CC -c prog.c CC -c main.c CC prog.o main.o -o myprog n = 0; n = input( Enter number: ); result = sum(n); printf( The sum is %d\n, result); 5 6 A simple program in C #include <iostream.h> // function to compute sum from 1 to n int sum(int n) if (n <= 0) return 0; int tot=0; for (int i=1; i<=n; i++) tot += i; return tot; int main(int argc, char *argv[]) int n=0; cout << "Enter number: "; cin >> n; int result = sum(n); cout << "The sum is " << result << endl; exit(0); Notes: function, arguments, main, control flow (for, if), input/output, compiler directives and standard libraries Variables A variable is associated with a memory location for storage of program data which can be retrieved and possibly modified during program execution. C/C++ are strongly typed languages the type of variables must be declared before use Choose variable name to be meaningful, without being overly verbose, e.g. counter, InputFile, window height A variable declaration: e.g. int num = 0; or int num(0); 7 8

3 Data types Classes Atomic types predefined in C/C++: bool, char, short, int, long, float, double Examples: c 40 \n \0 [char, 1 byte] x [int, 4 bytes] e [float, 4 bytes] C++ provides mechanisms for building up compound data structures; in particular classes and arrays A class is a compound data structure which encapsulates related data into a single data structure. class Complex double re, im; ; Members are accessed using the. operator Complex z, w1, w2; cout << z.re << endl; w1.re = 2.0*w2.re; w1.im = 2.0*w2.im; 9 10 Arrays An array is a data structure containing a numbered (indexed) collection of items of a single data type: int a[10]; res = a[0] + a[1] + a[4]; Complex zarray[20]; cout << zarray[5].re << endl; First index is always 0 The dimension must be a constant, and is fixed for the lifetime of the variable const int size = 3; float x[size]; or float x[] = 0.0, 1.0, 2.0; Multi-dimensional arrays e.g. double d[10][5]; has elements d[0][0]... d[0][4].. d[9][0]... d[9][4] Stored in the order of right-most index varying most rapidly. Note that C/C++ provides no inbuilt run-time checking of array bounds, so beware at best the program will crash (with something like Segmentation fault ), or worse, nonsense will result and be very difficult to trace

4 Methods In C++ a class encapsulates related data and functions. A class member function is sometimes called a method in the object-oriented programming literature. class Complex double re, im; ; double Mag() return sqrt(re*re + im*im); double Phase() return atan2(im, re); Methods are also accessed using the. operator Complex z; cout << "Magnitude=" << z.mag() << endl; cout << "Phase=" << z.phase() << endl; Constructor Whenever a variable is declared, space is allocated for it and it is possibly initialised. In general this is done by a constructor In C++ a user defined class can supply its own constructor. The compiler can supply one by default, but it is useful (required) for automatic initialisation of data members, or if the class requires some dynamic memory to be allocated. The constructor is a function with the same name as the class Complex(double x=0.0, double y=0.0) re = x; im = y; Example Data hiding / Encapsulation In procedural programming we want the functions to behave as black boxes with well defined interfaces. Avoiding side-effects means that we can use the functions as building blocks to create programs. In object-oriented programming we take this further and a class becomes a black box data structure, which has a public interface to private data. The outside program can only access the class through its well defined interface, minimising side-effects. class Complex Complex(double x=0.0, double y=0.0) re=x; im=y; double Re() return re; double Im() return im; double Mag() return sqrt(re*re + im*im); double Phase() return atan2(im, re); double re, im; ; The outside program can only access the private members through the public methods: Complex z(2.0, 5.2); cout << z.re() << endl; Re() is an accessor method

5 Data hiding ctd By encapsulating the representation of Complex in this way, the internal representation could change providing the interface remains the same For example, we could change the internal representation to polar coordinates class Complex Complex(double x=0.0; doubley=0.0) r = sqrt(x*x + y*y); theta = atan2(y,x); double Re() return r*cos(theta); double Im() return r*sin(theta); double Mag() return r; double Phase() return theta; double r; // magnitude double theta; // phase ; Functions and methods You should know how to program a simple function to take some values and return a result. We now discuss a number of more advanced function concepts: Scope Function prototype Class scoping Function call implementation Pass-by-value vs pass-by-reference parameters Function overloading Operators and operator overloading The interface of this class to the program remains unchanged Function prototype Scope A variable declared outside all functions is global. It can be accessed by any function. A variable declared inside a function is local to the function. It cannot be seen outside the function in which it is declared. If there exists a global variable of the same name then the local one takes precedence. It is redeclared/reallocated every time the function is called, so it does not remember its value from the last call. Function parameters are effectively local variables. 19 The function prototype provides enough information to the compiler so that it can check that it is being called correctly..h file: float exp(float x);.cc file: float exp(float x) const float precision = 1.0e-6; float term=1.0, res=0.0; int i=0; while (fabs(term)>precision) res += term; i++; term = pow(x,i)/factorial(i); The implementation of the function exp is in the.cc file, or even in a library somewhere. The prototype encapsulates the function interface and hides the implementation details. 20

6 Class scoping Class definitions can become cluttered with a lot of code if all methods are defined within them. Function call implementation Alternatively the class definition can contain a function prototype and the function declared elsewhere. We need a way of indicating that a given function implementation belongs to a particular class. This is done using the class scoping operator :: Memory CODE SEGMENT DATA SEGMENT machine code global variables.h file: class Complex double Mag();.cc file: double Complex::Mag() return sqrt(re*re + im*im); 21 STACK local variable m local variable 1 return location return value parameter n parameter 1 activation record 22 Parameter passing: rules and conventions Pass by value/reference int main() int i=5, j=10; swap(i, j); cout << i << j << endl; Pass by value Pass by reference The defaults in C/C++ are: atomic types: pass-by-value (i.e. copy) classes: pass-by-value (i.e. copy) arrays: pass-by-reference (i.e. location) Use pass-by-reference for an atomic type or a class if: void swap(int a, int b) int temp = a; a = b; b = temp; return; void swap(int& a, int& b) int temp = a; a = b; b = temp; return; modifications to the arguments must be preserved a large object must be passed the time and space costs to allocate and copy a large object onto the stack are often too great for real applications temp return location b = value of j a = value of i temp return location location of j location of i If using pass-by-reference for the latter and the data will not change, then use the const keyword void func(int x) void func(int &x) void func(const int &x) 23 24

7 Function overloading C++ allows several functions to share the same name to allow for different argument types. Example: double x, y;. y = exp(x); Complex w, z; z = exp(w); The two can be seen to be distinct names if we consider the argument types as part of the name. The same is true for class member functions. Function overloading ctd We can define exp for the Complex class as follows: #include <math.h> Complex exp(complex z) r = exp(z.re()); Complex zout(r*cos(z.im()), r*sin(z.im()); return zout; Operators/Expressions Arithmetic + * / - % (mod) << >> (shift left, right) Relational ==!= < > <= >= Boolean &&! They can be combined with the usual rules of precedence The precedence rules can be modified using round brackets ( ) Operator overloading An operator in C++ is just a function treated specially i.e. a + b is equivalent to operator+(a, b) Operators can be overloaded in the same way as functions: Complex operator+(complex z1, Complex z2) Complex zout(z1.re() + z2.re(), z1.im() + z2.im()); return zout; 27 28

8 Operator overloading, ctd Standard C/C++ does no checking on array bounds at run-time and this can result in catastrophic program crashes (one reason for the dreaded Segmentation Fault error is trying to access an array beyond its size). We can overload the [] operator to create a special array class that does does bounds checking: class MyArray float operator[](int i) if (i<0 i>=10) std::cerr << "Access out of bounds\n"; exit(1); else return a[i]; float a[10]; ; Advanced topics Inheritance Destructors Friend functions Templates Standard Template Library Recursive functions Lots of others not covered in this course MyArray x; Inheritance C++ allows programmers to build hierarchies of classes. An inherited or derived class is one which has all the functionality of the its parent, but with some extra data or methods. Inheritance, ctd Example class Window data: width, height posx, posy methods: raise(), hide() select(), iconify() Example: class GBall : public Ball class TextWindow data: methods: cursor_x, cursor_y backspace(), delete() clear() class GraphicsWindow data: background_colour Methods: clear(), fill() void Draw(); A GBall is identical to a Ball (same data and methods) with the addition of a method Draw which knows how to render a Ball onto a graphical window. class InteractiveGraphicsWindow data: Methods: MouseClick(), MouseDrag() 31 32

9 Destructor When an object ceases to exist (activation record is popped from the stack), the destructor is called. Like the constructor, the destructor has a default which the compiler can supply (this is what we have done throughout). The destructor has the same name as the class preceded by a : Recursion Recursion is the programming analogue of induction: if then p(0) and p(n) p(n + 1) p(n) n Define a function in terms of: Complex() Destructors are useful if memory has been dynamically allocated to the object (i.e. allocated at runtime). A destructor should ensure that this memory is freed up to the program. itself boundary conditions For example: Factorial: n! = n(n 1)!, 0! = Example: flood fill Consider a function to change the colour of a constant colour region to a new colour: const int SIZE=256; Bitmap im[size][size]; void fill(int x, int y, int old_colour, int new_colour) if (x>=0 && x<size && y>=0 && y<size) if (im[y][x]==old_colour) im[y][x] = new_colour; fill(x-1,y,old_colour,new_colour); fill(x+1,y,old_colour,new_colour); fill(x,y-1,old_colour,new_colour); fill(x,y+1,old_colour,new_colour); return; Templates Templating is a mechanism in C++ to create classes in which one or more types are parameterised. Recall our special array class, and suppose we wanted to store ints instead of floats. class MyArray float operator[](int i) if (i<0 i>=10) std::cerr << "Access out of bounds\n"; return 0.0; else return a[i]; float a[10]; ; 35 36

10 Templates ctd template <class Type> class MyArray Type operator[](int i) if (i<0 i>=10) std::cerr << "Access out of bounds\n"; return 0.0; else return a[i]; Type a[10]; ; MyArray<int> x; MyArray<double> y; MyArray<Complex> z; Templates ctd or even template <class Type, int N> class MyArray Type operator[](int i) if (i<0 i>=n) std::cerr << "Access out of bounds\n"; return 0.0; else return a[i]; Type a[n]; ; MyArray<int,10> x; MyArray<double,20> y; MyArray<Complex,5> z; Standard Template Library (STL) A number of data structures (container types) and algorithms that operate on them are so common that standard ways of representing and manipulating them have been developed. These comprise the Standard Template Library. Supports: stack (FILO structure) list (efficient insertion and deletion of elements) vector (extendible array) etc STL example std::vector<type> is an extendible array it can increase in size as the program needs it to it can be accessed just like an ordinary array (e.g. v[2]) it can tell you its current size (v.size()) you can add an item to the end without needing to know how big it is (v.push back(x)) Example: #include <vector> int main() std::vector<int> v; for (i=0; i<20; i++) v.push_back(i); for (i=0; i<v.size(); i++) std::cout << v[i] << std::endl; 39 40

11 STL example, ctd To create a new STL vector of a size specified at run-time: int size; std::vector<complex> z; std::cin >> size; z.resize(size); z[5] = Complex(2.0,3.0); or even std::vector< std::vector<float> > twodim; int width, height; twodim.resize(height); for (int i=0; i<height; i++) twodim[i].resize(width); twodim[2][3] = 10.0; Notes on style Write code which makes the meaning of each operation and each data structure clear. Use meaningful variable names Use comments to supplement the meaning Indent code for each block/loop Encapsulate groups of statements sensibly in functions Encapsulate related data sensibly in data structures Design top down Code bottom up Project notes The course takes you through a series of exercises designed to familiarise you with design and implementation issues in C++ programming. This should take at most two and half to three days, possibly less. After this you then have a loose specification for a labyrinth 3D graphical computer game. You have the remainder of the course to set about implementing and refining this. Write down what you hope to achieve before you start off Design your data structures before you start coding Don t be afraid to discard old ideas in favour of new ones during the life-cycle of the development, but keep old versions Discuss ideas with the demonstrators 43

C/C++ Coursework Module. Ian Reid, Trinity 2008

C/C++ Coursework Module. Ian Reid, Trinity 2008 C/C++ Coursework Module Ian Reid, Trinity 2008 The course takes you through a series of exercises designed to familiarise you with design and implementation issues in C++ programming. Objectives: Design

More information

B16 Object Oriented Programming

B16 Object Oriented Programming B16 Object Oriented Programming Michael A. Osborne mosb@robots.ox.ac.uk http://www.robots.ox.ac.uk/~mosb/teaching.html#b16 Hilary 2013 Topic 4: Constructors Recall our definition of the Complex class.

More information

B16 Object Oriented Programming

B16 Object Oriented Programming B16 Object Oriented Programming Michael A Osborne mosb@robots.ox.ac.uk http://www.robots.ox.ac.uk/~mosb/b16.html Hilary 2014 Topic 4: Constructors Recall our definition of the Complex class. class Complex

More information

B16 Object Oriented Programming

B16 Object Oriented Programming B16 Object Oriented Programming Frank Wood fwood@robots.ox.ac.uk http://www.robots.ox.ac.uk/~fwood/teaching/ Hilary 2015 This course will introduce object-oriented programming (OOP). It will introduce

More information

B16 Object Oriented Programming

B16 Object Oriented Programming B16 Object Oriented Programming Frank Wood fwood@robots.ox.ac.uk http://www.robots.ox.ac.uk/~fwood/teaching/ Hilary 2015 This course will introduce object-oriented programming (OOP). It will introduce

More information

B16 Object Oriented Programming

B16 Object Oriented Programming B16 Object Oriented Programming Michael A. Osborne mosb@robots.ox.ac.uk http://www.robots.ox.ac.uk/~mosb/teaching.html#b16 Hilary 2013 This course will introduce object-oriented programming (OOP). It will

More information

Dr Ian Reid B4, 4 lectures, Hilary Term

Dr Ian Reid B4, 4 lectures, Hilary Term Software Engineering Dr Ian Reid B4, 4 lectures, Hilary Term http://www.robots.ox.ac.uk/~ian/teaching/softeng Software Engineering vs structured programming Not really a course about software engineering

More information

B16 Object Oriented Programming

B16 Object Oriented Programming B16 Object Oriented Programming Michael A Osborne mosb@robots.ox.ac.uk http://www.robots.ox.ac.uk/~mosb/b16.html Hilary 2014 Topic 10: Templates Recall our array-bounds-checking class: unfortunately, it

More information

B16 Object Oriented Programming

B16 Object Oriented Programming B16 Object Oriented Programming Michael A Osborne mosb@robots.ox.ac.uk http://www.robots.ox.ac.uk/~mosb/b16.html Hilary 2014 Topic 7: A Complete Complex Example Let s bring it all together by creating

More information

Lipmann and Lajoie, and Goodrich et al, are C++ text books that give a general introduction as well as covering some of he more advanced topics.

Lipmann and Lajoie, and Goodrich et al, are C++ text books that give a general introduction as well as covering some of he more advanced topics. 1 2 3 Lipmann and Lajoie, and Goodrich et al, are C++ text books that give a general introduction as well as covering some of he more advanced topics. The C++ bible is by Stroustrup. Meyers is not a textbook.

More information

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings 19/10/2017 CE221 Part 2 1 Variables and References 1 In Java a variable of primitive type is associated with a memory location

More information

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p. Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p. 9 Self-Test Exercises p. 11 History Note p. 12 Programming and

More information

Introduction to C++ Introduction to C++ 1

Introduction to C++ Introduction to C++ 1 1 What Is C++? (Mostly) an extension of C to include: Classes Templates Inheritance and Multiple Inheritance Function and Operator Overloading New (and better) Standard Library References and Reference

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

G52CPP C++ Programming Lecture 9

G52CPP C++ Programming Lecture 9 G52CPP C++ Programming Lecture 9 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Last lecture const Constants, including pointers The C pre-processor And macros Compiling and linking And

More information

CE221 Programming in C++ Part 1 Introduction

CE221 Programming in C++ Part 1 Introduction CE221 Programming in C++ Part 1 Introduction 06/10/2017 CE221 Part 1 1 Module Schedule There are two lectures (Monday 13.00-13.50 and Tuesday 11.00-11.50) each week in the autumn term, and a 2-hour lab

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

CS 376b Computer Vision

CS 376b Computer Vision CS 376b Computer Vision 09 / 25 / 2014 Instructor: Michael Eckmann Today s Topics Questions? / Comments? Enhancing images / masks Cross correlation Convolution C++ Cross-correlation Cross-correlation involves

More information

Introduction to Programming using C++

Introduction to Programming using C++ Introduction to Programming using C++ Lecture One: Getting Started Carl Gwilliam gwilliam@hep.ph.liv.ac.uk http://hep.ph.liv.ac.uk/~gwilliam/cppcourse Course Prerequisites What you should already know

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

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst Department of Computer Science Why C? Low-level Direct access to memory WYSIWYG (more or less) Effectively

More information

Absolute C++ Walter Savitch

Absolute C++ Walter Savitch Absolute C++ sixth edition Walter Savitch Global edition This page intentionally left blank Absolute C++, Global Edition Cover Title Page Copyright Page Preface Acknowledgments Brief Contents Contents

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

Introduction to C++ Systems Programming

Introduction to C++ Systems Programming Introduction to C++ Systems Programming Introduction to C++ Syntax differences between C and C++ A Simple C++ Example C++ Input/Output C++ Libraries C++ Header Files Another Simple C++ Example Inline Functions

More information

CSCI-1200 Data Structures Spring 2016 Lecture 6 Pointers & Dynamic Memory

CSCI-1200 Data Structures Spring 2016 Lecture 6 Pointers & Dynamic Memory Announcements CSCI-1200 Data Structures Spring 2016 Lecture 6 Pointers & Dynamic Memory There will be no lecture on Tuesday, Feb. 16. Prof. Thompson s office hours are canceled for Monday, Feb. 15. Prof.

More information

Fibonacci in Lisp. Computer Programming: Skills & Concepts (CP1) Programming Languages. Varieties of Programing Language

Fibonacci in Lisp. Computer Programming: Skills & Concepts (CP1) Programming Languages. Varieties of Programing Language Fibonacci in Lisp Computer Programming: Skills & Concepts (CP1) Programming Languages (defun fibonacci (n) (if (or (= n 0) (= n 1)) 1 (+ (fibonacci (- n 1)) (fibonacci (- n 2))))) 22nd November 2010 defun-

More information

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

G52CPP C++ Programming Lecture 7. Dr Jason Atkin G52CPP C++ Programming Lecture 7 Dr Jason Atkin 1 This lecture classes (and C++ structs) Member functions inline functions 2 Last lecture: predict the sizes 3 #pragma pack(1) #include struct A

More information

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University (5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University Key Concepts 2 Object-Oriented Design Object-Oriented Programming

More information

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

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

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

Chapter 1: Object-Oriented Programming Using C++

Chapter 1: Object-Oriented Programming Using C++ Chapter 1: Object-Oriented Programming Using C++ Objectives Looking ahead in this chapter, we ll consider: Abstract Data Types Encapsulation Inheritance Pointers Polymorphism Data Structures and Algorithms

More information

Evolution of Programming Languages

Evolution of Programming Languages Evolution of Programming Languages 40's machine level raw binary 50's assembly language names for instructions and addresses very specific to each machine 60's high-level languages: Fortran, Cobol, Algol,

More information

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces Basic memory model Using functions Writing functions Basics Prototypes Parameters Return types Functions and memory Names and namespaces When a program runs it requires main memory (RAM) space for Program

More information

CS242 COMPUTER PROGRAMMING

CS242 COMPUTER PROGRAMMING CS242 COMPUTER PROGRAMMING I.Safa a Alawneh Variables Outline 2 Data Type C++ Built-in Data Types o o o o bool Data Type char Data Type int Data Type Floating-Point Data Types Variable Declaration Initializing

More information

Programming. C++ Basics

Programming. C++ Basics Programming C++ Basics Introduction to C++ C is a programming language developed in the 1970s with the UNIX operating system C programs are efficient and portable across different hardware platforms C++

More information

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object CHAPTER 1 Introduction to Computers and Programming 1 1.1 Why Program? 1 1.2 Computer Systems: Hardware and Software 2 1.3 Programs and Programming Languages 8 1.4 What is a Program Made of? 14 1.5 Input,

More information

Computer Science II Lecture 2 Strings, Vectors and Recursion

Computer Science II Lecture 2 Strings, Vectors and Recursion 1 Overview of Lecture 2 Computer Science II Lecture 2 Strings, Vectors and Recursion The following topics will be covered quickly strings vectors as smart arrays Basic recursion Mostly, these are assumed

More information

Where do we go from here?

Where do we go from here? Where do we go from here? C++ classes and objects, with all the moving parts visible operator overloading templates, STL, standards, Java components, collections, generics language and performance comparisons

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

Chapter 2. Procedural Programming

Chapter 2. Procedural Programming Chapter 2 Procedural Programming 2: Preview Basic concepts that are similar in both Java and C++, including: standard data types control structures I/O functions Dynamic memory management, and some basic

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

AN OVERVIEW OF C++ 1

AN OVERVIEW OF C++ 1 AN OVERVIEW OF C++ 1 OBJECTIVES Introduction What is object-oriented programming? Two versions of C++ C++ console I/O C++ comments Classes: A first look Some differences between C and C++ Introducing function

More information

Problem Solving with C++

Problem Solving with C++ GLOBAL EDITION Problem Solving with C++ NINTH EDITION Walter Savitch Kendrick Mock Ninth Edition PROBLEM SOLVING with C++ Problem Solving with C++, Global Edition Cover Title Copyright Contents Chapter

More information

C++ for Java Programmers

C++ for Java Programmers Basics all Finished! Everything we have covered so far: Lecture 5 Operators Variables Arrays Null Terminated Strings Structs Functions 1 2 45 mins of pure fun Introduction Today: Pointers Pointers Even

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

C:\Temp\Templates. Download This PDF From The Web Site

C:\Temp\Templates. Download This PDF From The Web Site 11 2 2 2 3 3 3 C:\Temp\Templates Download This PDF From The Web Site 4 5 Use This Main Program Copy-Paste Code From The Next Slide? Compile Program 6 Copy/Paste Main # include "Utilities.hpp" # include

More information

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS Contents Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS 1.1. INTRODUCTION TO COMPUTERS... 1 1.2. HISTORY OF C & C++... 3 1.3. DESIGN, DEVELOPMENT AND EXECUTION OF A PROGRAM... 3 1.4 TESTING OF PROGRAMS...

More information

CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic Review from Letctures 3 & 4 C++ class syntax, designing classes, classes vs. structs; Passing comparison functions to

More information

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach. CMSC 131: Chapter 28 Final Review: What you learned this semester The Big Picture Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach. Java

More information

Understand Execution of a Program

Understand Execution of a Program Understand Execution of a Program Prof. Zhang September 17, 2014 1 Program in Memory When you execute a program, the program (i.e., executable code) is loaded into the memory (i.e., main memory, RAM) in

More information

Announcements. Lecture 04b Header Classes. Review (again) Comments on PA1 & PA2. Warning about Arrays. Arrays 9/15/17

Announcements. Lecture 04b Header Classes. Review (again) Comments on PA1 & PA2. Warning about Arrays. Arrays 9/15/17 Announcements Lecture 04b Sept. 14 th, 2017 Midterm #1: Sept. 26 th (week from Tuesday) Code distributed one week from today PA2 test cases & answers posted Quiz #4 next Tuesday (before class) PA3 due

More information

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute C++, by Walter

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

Chapter 15 - C++ As A "Better C"

Chapter 15 - C++ As A Better C Chapter 15 - C++ As A "Better C" Outline 15.1 Introduction 15.2 C++ 15.3 A Simple Program: Adding Two Integers 15.4 C++ Standard Library 15.5 Header Files 15.6 Inline Functions 15.7 References and Reference

More information

CSE 333 Lecture 9 - intro to C++

CSE 333 Lecture 9 - intro to C++ CSE 333 Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington Administrivia & Agenda Main topic: Intro to C++ But first: Some hints on HW2 Labs: The

More information

CSE 333. Lecture 10 - references, const, classes. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

CSE 333. Lecture 10 - references, const, classes. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington CSE 333 Lecture 10 - references, const, classes Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington Administrivia New C++ exercise out today, due Friday morning

More information

COMP 2355 Introduction to Systems Programming

COMP 2355 Introduction to Systems Programming COMP 2355 Introduction to Systems Programming Christian Grothoff christian@grothoff.org http://grothoff.org/christian/ 1 Today Class syntax, Constructors, Destructors Static methods Inheritance, Abstract

More information

CSE 303: Concepts and Tools for Software Development

CSE 303: Concepts and Tools for Software Development CSE 303: Concepts and Tools for Software Development Hal Perkins Autumn 2008 Lecture 24 Introduction to C++ CSE303 Autumn 2008, Lecture 24 1 C++ C++ is an enormous language: All of C Classes and objects

More information

Makefiles Makefiles should begin with a comment section of the following form and with the following information filled in:

Makefiles Makefiles should begin with a comment section of the following form and with the following information filled in: CS 215 Fundamentals of Programming II C++ Programming Style Guideline Most of a programmer's efforts are aimed at the development of correct and efficient programs. But the readability of programs is also

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

Page. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team

Page. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team Page. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team Question No: 1 ( Marks: 2 ) Write a declaration statement for an array of 10

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

CS

CS CS 1666 www.cs.pitt.edu/~nlf4/cs1666/ Programming in C++ First, some praise for C++ "It certainly has its good points. But by and large I think it s a bad language. It does a lot of things half well and

More information

Lecture 8. Xiaoguang Wang. February 13th, 2014 STAT 598W. (STAT 598W) Lecture 8 1 / 47

Lecture 8. Xiaoguang Wang. February 13th, 2014 STAT 598W. (STAT 598W) Lecture 8 1 / 47 Lecture 8 Xiaoguang Wang STAT 598W February 13th, 2014 (STAT 598W) Lecture 8 1 / 47 Outline 1 Introduction: C++ 2 Containers 3 Classes (STAT 598W) Lecture 8 2 / 47 Outline 1 Introduction: C++ 2 Containers

More information

Operator overloading

Operator overloading 1 Introduction 2 The copy constructor 3 Operator Overloading 4 Eg 1: Adding two vectors 5 The -> operator 6 The this pointer 7 Overloading = 8 Unary operators 9 Overloading for the matrix class 10 The

More information

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance... CISC 2000 Computer Science II Fall, 2014 Note 12/1/2014 1 Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance... (a) What s the purpose of inheritance?

More information

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor Outline EDAF50 C++ Programming 4. Classes Sven Gestegård Robertz Computer Science, LTH 2018 1 Classes the pointer this const for objects and members Copying objects friend inline 4. Classes 2/1 User-dened

More information

Initializing and Finalizing Objects

Initializing and Finalizing Objects 4 Initializing and Finalizing Objects 147 Content Initializing and Finalizing Objects 4 Constructors Default Constructor Copy Constructor Destructor 148 Initializing Objects: Constructors Initializing

More information

Lecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

Lecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology Lecture 8: Object-Oriented Programming (OOP) 1 Introduction to C++ 2 Overview Additional features compared to C: Object-oriented programming (OOP) Generic programming (template) Many other small changes

More information

CSCI-1200 Data Structures Fall 2017 Lecture 2 STL Strings & Vectors

CSCI-1200 Data Structures Fall 2017 Lecture 2 STL Strings & Vectors Announcements CSCI-1200 Data Structures Fall 2017 Lecture 2 STL Strings & Vectors HW 1 is available on-line through the website (on the Calendar ). Be sure to read through this information as you start

More information

Part X. Advanced C ++

Part X. Advanced C ++ Part X Advanced C ++ topics Philip Blakely (LSC) Advanced C++ 158 / 217 References The following are highly regarded books. They are fairly in-depth, and I haven t read them in their entirity. However,

More information

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each): FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each): 1. If a function has default arguments, they can be located anywhere

More information

CSC 533: Organization of Programming Languages. Spring 2005

CSC 533: Organization of Programming Languages. Spring 2005 CSC 533: Organization of Programming Languages Spring 2005 Language features and issues variables & bindings data types primitive complex/structured expressions & assignments control structures subprograms

More information

Implementing Subprograms

Implementing Subprograms Implementing Subprograms 1 Topics The General Semantics of Calls and Returns Implementing Simple Subprograms Implementing Subprograms with Stack-Dynamic Local Variables Nested Subprograms Blocks Implementing

More information

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming Exam 1 Prep Dr. Demetrios Glinos University of Central Florida COP3330 Object Oriented Programming Progress Exam 1 is a Timed Webcourses Quiz You can find it from the "Assignments" link on Webcourses choose

More information

CS3157: Advanced Programming. Outline

CS3157: Advanced Programming. Outline CS3157: Advanced Programming Lecture #12 Apr 3 Shlomo Hershkop shlomo@cs.columbia.edu 1 Outline Intro CPP Boring stuff: Language basics: identifiers, data types, operators, type conversions, branching

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

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING THROUGH C++ Practical: OOPS THROUGH C++ Subject Code: 1618407 PROGRAM NO.1 Programming exercise on executing a Basic C++

More information

CSE 374 Programming Concepts & Tools

CSE 374 Programming Concepts & Tools CSE 374 Programming Concepts & Tools Hal Perkins Fall 2017 Lecture 8 C: Miscellanea Control, Declarations, Preprocessor, printf/scanf 1 The story so far The low-level execution model of a process (one

More information

Software Development with C++ Templates

Software Development with C++ Templates Software Development with C++ Templates Lab Submission 1 Exercises should be solved in groups of two. However, with approval from the lecturer, exercises may also be solved alone or in groups of three.

More information

Cpt S 122 Data Structures. Templates

Cpt S 122 Data Structures. Templates Cpt S 122 Data Structures Templates Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Topics Introduction Function Template Function-template and function-template

More information

Computing and Statistical Data Analysis Lecture 3

Computing and Statistical Data Analysis Lecture 3 Computing and Statistical Data Analysis Lecture 3 Type casting: static_cast, etc. Basic mathematical functions More i/o: formatting tricks Scope, namspaces Functions 1 Type casting Often we need to interpret

More information

COMP322 - Introduction to C++ Lecture 01 - Introduction

COMP322 - Introduction to C++ Lecture 01 - Introduction COMP322 - Introduction to C++ Lecture 01 - Introduction Robert D. Vincent School of Computer Science 6 January 2010 What this course is Crash course in C++ Only 14 lectures Single-credit course What this

More information

CS11 Introduction to C++ Fall Lecture 1

CS11 Introduction to C++ Fall Lecture 1 CS11 Introduction to C++ Fall 2006-2007 Lecture 1 Welcome! 8 Lectures (~1 hour) Slides posted on CS11 website http://www.cs.caltech.edu/courses/cs11 7 Lab Assignments on course website Available on Monday

More information

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

G52CPP C++ Programming Lecture 18. Dr Jason Atkin G52CPP C++ Programming Lecture 18 Dr Jason Atkin 1 Last lecture Operator Overloading Strings and streams 2 Operator overloading - what to know Know that you can change the meaning of operators Know that

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

PHY4321 Summary Notes

PHY4321 Summary Notes PHY4321 Summary Notes The next few pages contain some helpful notes that summarize some of the more useful material from the lecture notes. Be aware, though, that this is not a complete set and doesn t

More information

Procedures, Parameters, Values and Variables. Steven R. Bagley

Procedures, Parameters, Values and Variables. Steven R. Bagley Procedures, Parameters, Values and Variables Steven R. Bagley Recap A Program is a sequence of statements (instructions) Statements executed one-by-one in order Unless it is changed by the programmer e.g.

More information

Final CSE 131B Spring 2004

Final CSE 131B Spring 2004 Login name Signature Name Student ID Final CSE 131B Spring 2004 Page 1 Page 2 Page 3 Page 4 Page 5 Page 6 Page 7 Page 8 (25 points) (24 points) (32 points) (24 points) (28 points) (26 points) (22 points)

More information

CSCI-1200 Data Structures Spring 2015 Lecture 2 STL Strings & Vectors

CSCI-1200 Data Structures Spring 2015 Lecture 2 STL Strings & Vectors Announcements CSCI-1200 Data Structures Spring 2015 Lecture 2 STL Strings & Vectors HW 1 is available on-line through the website (on the Calendar ). Be sure to read through this information as you start

More information

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program: Welcome Back CSCI 262 Data Structures 2 - Review What you learned in CSCI 261 (or equivalent): Variables Types Arrays Expressions Conditionals Branches & Loops Functions Recursion Classes & Objects Streams

More information

CSE030 Fall 2012 Final Exam Friday, December 14, PM

CSE030 Fall 2012 Final Exam Friday, December 14, PM CSE030 Fall 2012 Final Exam Friday, December 14, 2012 3-6PM Write your name here and at the top of each page! Name: Select your lab session: Tuesdays Thursdays Paper. If you have any questions or need

More information

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy CSCI 334: Principles of Programming Languages Lecture 18: C/C++ Homework help session will be tomorrow from 7-9pm in Schow 030A instead of on Thursday. Instructor: Dan Barowy HW6 and HW7 solutions We only

More information

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING OBJECT ORIENTED PROGRAMMING STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING 1. Object Oriented Programming Paradigms 2. Comparison of Programming Paradigms 3. Basic Object Oriented Programming

More information

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

G52CPP C++ Programming Lecture 3. Dr Jason Atkin G52CPP C++ Programming Lecture 3 Dr Jason Atkin E-Mail: jaa@cs.nott.ac.uk 1 Revision so far C/C++ designed for speed, Java for catching errors Java hides a lot of the details (so can C++) Much of C, C++

More information

C++ Programming. Classes, Constructors, Operator overloading (Continued) M1 Math Michail Lampis

C++ Programming. Classes, Constructors, Operator overloading (Continued) M1 Math Michail Lampis C++ Programming Classes, Constructors, Operator overloading (Continued) M1 Math Michail Lampis michail.lampis@dauphine.fr Classes These (and the previous) slides demonstrate a number of C++ features related

More information

Contents A Little C++

Contents A Little C++ Contents 1 A Little C++ 3 1.1 Classes, Methods and Constructors...................................... 3 1.2 Inheritance and Data Encapsulation..................................... 4 1.2.1 Method Overriding...........................................

More information

Part V. Memory and pointers. Philip Blakely (LSC) C++ Introduction 145 / 370

Part V. Memory and pointers. Philip Blakely (LSC) C++ Introduction 145 / 370 Part V and pointers Philip Blakely (LSC) C++ Introduction 145 / 370 Outline 19 20 Function pointers 21 Global variables Philip Blakely (LSC) C++ Introduction 146 / 370 Heap and Stack The stack is a Last-In-First-Out

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

Part VII. Object-Oriented Programming. Philip Blakely (LSC) C++ Introduction 194 / 370

Part VII. Object-Oriented Programming. Philip Blakely (LSC) C++ Introduction 194 / 370 Part VII Object-Oriented Programming Philip Blakely (LSC) C++ Introduction 194 / 370 OOP Outline 24 Object-Oriented Programming 25 Member functions 26 Constructors 27 Destructors 28 More constructors Philip

More information