CSE 303: Concepts and Tools for Software Development

Similar documents
CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

CS24 Week 3 Lecture 1

PIC 10A Objects/Classes

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington

CS3157: Advanced Programming. Outline

Lecture 14: more class, C++ streams

CSE 333 Lecture 9 - intro to C++

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

CE221 Programming in C++ Part 1 Introduction

CS 11 C++ track: lecture 1

C++ Mini-Course. Part 1: Mechanics Part 2: Basics Part 3: References Part 4: Const Part 5: Inheritance Part 6: Libraries Part 7: Conclusion. C Rulez!

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

Fast Introduction to Object Oriented Programming and C++

Interview Questions of C++

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

EL2310 Scientific Programming

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

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

Evolution of Programming Languages

EL2310 Scientific Programming

C++ Mini-Course. Part 1: Mechanics Part 2: Basics Part 3: References Part 4: Const Part 5: Inheritance Part 6: Libraries Part 7: Conclusion. C Rulez!

CS250 Final Review Questions

PROGRAMMING IN C++ KAUSIK DATTA 18-Oct-2017

Documentation. Programming / Documentation Slide 42

AN OVERVIEW OF C++ 1

G52CPP C++ Programming Lecture 9

And Even More and More C++ Fundamentals of Computer Science

Object-Oriented Principles and Practice / C++

D Programming Language

Where do we go from here?

CSE 374 Programming Concepts & Tools

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

Lecture 13: more class, C++ memory management

CS11 Introduction to C++ Fall Lecture 1

CSE 307: Principles of Programming Languages

CS 376b Computer Vision

G52CPP C++ Programming Lecture 13

CPSC 427: Object-Oriented Programming

CS

Object Oriented Programming. Solved MCQs - Part 2

III. Classes (Chap. 3)

Introduction to C++ (Extensions to C)

Intermediate Programming, Spring 2017*

Classes, Constructors, etc., in C++

Object Reference and Memory Allocation. Questions:

Ruby logistics. CSE341: Programming Languages Lecture 19 Introduction to Ruby and OOP. Ruby: Not our focus. Ruby: Our focus. A note on the homework

CS11 Intro C++ Spring 2018 Lecture 1

CSE 333. Lecture 11 - constructor insanity. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

Introduction to Programming using C++

Slide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Looping and Counting. Lecture 3 Hartmut Kaiser hkaiser/fall_2012/csc1254.html

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management

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

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

Introduction Of Classes ( OOPS )

Contents A Little C++

Reliable C++ development - session 1: From C to C++ (and some C++ features)

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

This chapter introduces the notion of namespace. We also describe how to manage input and output with C++ commands via the terminal or files.

Data Abstraction. Hwansoo Han

QUIZ Friends class Y;

CSE 303: Concepts and Tools for Software Development

Lesson 13 - Vectors Dynamic Data Storage

Functions and Methods. Questions:

CPSC 427: Object-Oriented Programming

Understanding main() function Input/Output Streams

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

Classes in C++98 and C++11

VIRTUAL FUNCTIONS Chapter 10

A brief introduction to C programming for Java programmers

Intermediate Programming, Spring 2017*

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

CS93SI Handout 04 Spring 2006 Apr Review Answers

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

CSE341: Programming Languages Lecture 19 Introduction to Ruby and OOP. Dan Grossman Winter 2013

PHY4321 Summary Notes

Object Oriented Software Design II

Object Oriented Design

QUIZ. What is wrong with this code that uses default arguments?

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

C++ Class Details, Heap

COMP 2355 Introduction to Systems Programming

Programming C++ Lecture 3. Howest, Fall 2012 Instructor: Dr. Jennifer B. Sartor

Object Oriented Software Design II

Suppose we find the following function in a file: int Abc::xyz(int z) { return 2 * z + 1; }

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

EL2310 Scientific Programming

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay


QUIZ. Source:

CS201 Some Important Definitions

Programming in C and C++

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

QUIZ. What are 3 differences between C and C++ const variables?

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Inheritance, and Polymorphism.

Transcription:

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 (kind of like Java, some crucial differences) Many more little conveniences (I/O, new/delete, function overloading, pass-by-reference, bigger standard library) Namespaces (kind of like Java packages) Stuff we won t do: const, different kinds of casts, exceptions, templates, multiple inheritance,... We will focus on a couple themes rather than just a big bag of new features to memorize... CSE303 Autumn 2008, Lecture 24 2

Our focus OOP in a C-like language may help you understand C and Java better? We can put objects on the stack or the heap; an object is not a pointer to an object Still have to manage memory manually Still lots of ways to HCBWKMSCOD a Still distinguish header files from implementation files Allocation and initialization still separate concepts, but easier to construct and destruct Programmer has more control on how method-calls work (different defaults from Java) a (hopefully crash, but who knows might silently corrupt other data) CSE303 Autumn 2008, Lecture 24 3

Hello World #include <iostream> int main() { // Use standard output stream cout // and operator << to send "Hello World" // and an end line to stdout std::cout << "Hello World" << std::endl; return 0; } Differences from C: new-style headers (no.h), namespace access (::), I/O via stream operators,... Differences from Java: not everything is in a class, any code can go in any file,... CSE303 Autumn 2008, Lecture 24 4

Compiling Need a different compiler than for C; use g++ on attu. Example: g++ -Wall -o hello hello.cc The.cc extension is a convention (just like.c for C), but less universal (also see.cpp,.cxx,.c). Uses the C preprocessor (no change there). Now: A few niceties before our real focus (classes and objects). CSE303 Autumn 2008, Lecture 24 5

I/O Operator << takes a ostream and (various things) and outputs it; returns the stream, which is why this works: std::cout << 3 << "hi" << f(x) << \n ; Easier and safer than printf Operator >> takes istream and (various things) and inputs into it. Easier and safer than scanf. Do not use pointers; e.g., int x; std::cin >> x; Can think of >> and << as keywords, but they are not: Operator overloading redefines them for different pairs of types. In C they mean left-shift and right-shift (of bits); undefined for non-numeric types. Lack of address-of for input done with call-by-reference (later). CSE303 Autumn 2008, Lecture 24 6

Namespaces In C, all non-static functions in the program need different names Even operating systems with tens of millions of lines. Namespaces (cf. Java packages) let you group top-level names: namespace myspace {... definitions... } Of course, then different namespaces can have the same function names and they are totally different functions. Can nest them Can reuse the same namespace in multiple places Pariticularly common: in the.h and the.cc For example, the whole C++ standard library is in namespace std. To use a function/variable/etc. in another namespace, do thespace::somefun() (not. like in Java) CSE303 Autumn 2008, Lecture 24 7

Using To avoid having to write namespaces and :: constantly, use a using declaration Example: #include <iostream> using namespace std; int main() { cout << "Hello World" << endl; return 0; } CSE303 Autumn 2008, Lecture 24 8

Onto OOP Like Java: Fields vs. methods, static vs. instance, constructors Method overloading (functions, operators, and constructors too) Not quite like Java: access-modifiers (e.g., private) syntax and default declaration separate from implementation (like C) funny constructor syntax, default parameters (e.g.,... = 0) Nothing like Java: Objects vs. pointers to objects Destructors and copy-constructors virtual vs. non-virtual (to be discussed) CSE303 Autumn 2008, Lecture 24 9

Stack vs. heap Java: cannot stack-allocate an object (only a pointer to one). C: can stack-allocate a struct, then initialize it. C++: stack-allocate and call a constructor (where this is the object s address, as always) Property p1(10000); Java: new Property(...) calls constructor, returns heap-allocated pointer. C: Use malloc and then initialized, must free exactly once later. C++: Like Java, but can also do new int(42). Like C must deallocate, but must use delete instead of free. CSE303 Autumn 2008, Lecture 24 10

Destructors An object s destructor is called just before the space for it is reclaimed. A common use: Reclaim space for heap-allocated things pointed to (first calling their destructors). But not if there are other pointers to it (aliases)?! Meaning of delete x: call the destructor of pointed-to heap object, then reclaim space. Destructors also get called for stack-objects (when they leave scope). Advice: Always make destructors virtual (learn why soon) CSE303 Autumn 2008, Lecture 24 11

Arrays Create a heap-allocated array of objects: new A[10]; Calls default (zero-argument) constructor for each element. Convenient if there s a good default initialization. Create a heap-allocated array of pointers to objects: new A*[10] More like Java (but not initialized?) As in C, new A() and new A[10] have type A*. new A* and new A*[10] both have type A**. Unlike C, to delete a non-array, you must write delete e Unlike C, to delete an array, you must write delete [] e Else HYCSBWK the deleter must know somehow what is an array. CSE303 Autumn 2008, Lecture 24 12

Digression: Call-by-reference In C, we know function arguments are copies But copying a pointer means you still point to the same (uncopied) thing Same in C++, but a reference parameter (the & character after it) is different. Callee writes: void f(int& x) { x = x + 1; } Caller writes: f(y) But it s as though the caller wrote f(&y) and everywhere the callee said x they really said *x. So that little & has a big meaning. CSE303 Autumn 2008, Lecture 24 13

Copy Constructors In C, we know x=y or f(y) copies y (if a struct, then member-wise copy). Same in C++, unless a copy-constructor is defined, then do whatever it says. A copy-constructor by definition takes a reference parameter (else we d need to copy, but that s what we re defining) of the same type. Let s not talk about the const. CSE303 Autumn 2008, Lecture 24 14

Now more OOP: Subclassing In many ways, OOP is all about subclasses overriding methods. Often not what you want, but what makes OOP fundamentally different from, say, functional programming (CSE341) C++ gives you lots more options than Java with different defaults, so it s easy to scream compiler bug when you mean I m using the wrong feature... Basic subclassing: class D : public C {... } This is public inheritance; C++ has other kinds too (won t cover) Differences affect visibility and issues when you have multiple superclasses (won t cover) So do not forget the public keyword CSE303 Autumn 2008, Lecture 24 15

More on subclassing Not all classes have superclasses (unlike Java with Object) (and classes can have multiple superclasses more general and complexity-prone than Java, where a class has one superclass and can also implement interfaces) Terminology Java (and others): superclass and subclass C++ (and others): base class and derived class Our example code: House derives from Land which derives from Property As in Java, can add fields/methods/constructors, and override methods. CSE303 Autumn 2008, Lecture 24 16

Construction and destruction Constructor of base class gets called before constructor of derived class Default (zero-arg) constructor unless you specify a different one after the : in the constructor. Destructor of base class gets called after destructor of derived class So constructors/destructors really extend rather than override, since that is typically what you want. Java is the same CSE303 Autumn 2008, Lecture 24 17