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

Similar documents
CE221 Programming in C++ Part 1 Introduction

CSE 303: Concepts and Tools for Software Development

CS 376b Computer Vision

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

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Fast Introduction to Object Oriented Programming and C++

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

CSE 333 Lecture 9 - intro to C++

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

A Tour of the C++ Programming Language

Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

Lecture 2, September 4

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

Object Oriented Design

A Tour of the C++ Programming Language

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

CS61C : Machine Structures

Announcements. CSCI 334: Principles of Programming Languages. Lecture 19: C++

CS11 Intro C++ Spring 2018 Lecture 1

CS

C and C++ I. Spring 2014 Carola Wenk

Your first C and C++ programs

Dr. Md. Humayun Kabir CSE Department, BUET

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

PIC 10A Objects/Classes

Lecture 7. Log into Linux New documents posted to course webpage

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Call-by-Type Functions in C++ Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #5

Dynamic arrays / C Strings

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

Assumptions. History

Lecture 15a Persistent Memory & Shared Pointers

G52CPP C++ Programming Lecture 20

6.096 Introduction to C++ January (IAP) 2009

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

COMP 2355 Introduction to Systems Programming

377 Student Guide to C++

CMSC 202 Midterm Exam 1 Fall 2015

Variables and literals

Project. C++: Inheritance III. Plan. Project. Before we begin. The final exam. Advanced Topics. Project. This week in the home stretch

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

CS 106B, Lecture 1 Introduction to C++

COMP322 - Introduction to C++ Lecture 01 - Introduction

CS 11 C++ track: lecture 1

the gamedesigninitiative at cornell university Lecture 6 C++: Basics

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Lab 1: First Steps in C++ - Eclipse

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!

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

Introduction to C++ Introduction to C++ 1

377 Student Guide to C++

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

Homework 4. Any questions?

PROGRAMMING IN C++ CVIČENÍ

C++ Inheritance I. CSE 333 Spring Instructor: Justin Hsia

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

Welcome to MCS 360. content expectations. using g++ input and output streams the namespace std. Euclid s algorithm the while and do-while statements

APT Session 4: C. Software Development Team Laurence Tratt. 1 / 14

COMP322 - Introduction to C++

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

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!

CS3157: Advanced Programming. Outline

A brief introduction to C++

Name: Username: I. 20. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

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

AN OVERVIEW OF C++ 1

Scientific Computing

Introducing C++ to Java Programmers

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

Short Notes of CS201

Introduction to C++ Systems Programming

Data Abstraction. Hwansoo Han

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

Programmazione. Prof. Marco Bertini

C Introduction. Comparison w/ Java, Memory Model, and Pointers

CS201 - Introduction to Programming Glossary By

C++ Constructor Insanity

EL2310 Scientific Programming

Where do we go from here?

Introduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers

C++ For C Programmers Week 1 Part 1

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

Crash Course into. Prof. Dr. Renato Pajarola

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

Introduction to Programming using C++

Chapter 2. Procedural Programming

4. Structure of a C++ program

Evolution of Programming Languages

CSE 333 Midterm Exam 7/22/12

EL2310 Scientific Programming

Language comparison. C has pointers. Java has references. C++ has pointers and references

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

Java Basic Syntax. Java vs C++ Wojciech Frohmberg / OOP Laboratory. Poznan University of Technology

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

EMBEDDED SYSTEMS PROGRAMMING Language Basics

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

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!

Transcription:

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 have three weeks of class left! Start thinking about the final exam now. I am still happy to meet privately with anyone who wants to review their midterm with me.

no memory abstraction pointers a pointer is not a data type; it s just an int! operations address of operator: & takes any variable and returns its memory address (i.e., pointer) dereference operator: * takes any pointer and returns the value at that memory address member selection operator:. pointer member selection operator: -> p->foo equivalent to (*p).foo Separate compilation C does not have a module system How does C find printf below? #include <stdio.h> int main(int argc, char** argv) { printf("hello world!\n"); statements of the form #<command> are preprocessor directives The C preprocessor is a programmable copy and paste tool $ clang -E helloworld.c > helloworld_pre.c support code #include <stdio.h> Typedef (demo) int main(int argc, char** argv) { printf("hello world!\n"); (demo)

Activity Activity Write a swap() function with the following function header: void swap(int *p1, int *p2); Use this function to swap the values of two integers. int main(int argc, char **argv){ int x = 10; int y = 20; Create a function print_addr(int x) whose sole purpose is to print the address of the integer x passed to it. Create an integer variable in main, print out its address, and then pass that variable to print_addr. Any observations about the behavior of this function? swap(/* what do I put here? */); printf("x: %d, y: %d\n", x, y); History of C++ Began originally in 1979 with Bjarne Stroustrup s C with Classes C++ released in 1983 with most of the major features we know today. C++ Design was strongly influenced by Simula, but Simula was too slow. Stroustrup wanted a fast, portable, language with object-oriented features. C had everything but OO. C++ is largely a superset of C. Until C+ +98, every C program was a valid C++ program. Still relatively easy to convert C to C++. Major driving philosophy: only pay for what you use. C++ has many features. We will cover only the essential ones here.

in file helloworld.cpp: compile code: How to use C++ #include <iostream> using namespace std; int main() { cout << "Hello world!" << std::endl; return 0; $ clang++ helloworld.cpp -o helloworld run program: $./helloworld I strongly suggest that you explicitly specify a recent C++ standard: $ clang++ -std=c++14 helloworld.cpp -o helloworld C++ Classes C++ classes are similar in spirit to Java classes: private: void (); ; compilation. Person::Person(string name) : a(name) { void Person::() { cout << "Hello " << n << "!" << endl; This is called a scope qualifier. Without it, the compiler would not know that Person is the constructor for the Person class under separate Note that in C++, we conventionally put member function definitions after the class declaration. Purpose: separate compilation. Also note that C++ has a convenient string type that is much easier and safer to use than C-style strings (null-terminated char arrays). C++ Classes As in Java, C++ classes can also inherit from a superclass. ; We changed this to protected. Why? We added the virtual keyword. Why? Inheritance and Visibility Rules As in Java, C++ classes can also inherit from a superclass. ; class Pirate : public Person { Pirate(string name); Pirate::Pirate(name) : Person(name) { void Pirate::() { cout << Arr << n <<! <, endl; Class definitions end with a semicolon (ouch!) Person is a public superclass. What does this mean? We used weird constructor syntax here. Whaaaaaa?!! instance variable or member function is visible to both inheriting classes and users of class. instance variable or member function is visible to inheriting class but not users of class. private: instance variable or member function is not visible to either inheriting class or users of class.

Virtual Dispatch In C++, you only pay for what you use. ; Dynamic dispatch is expensive compared to static dispatch (two pointer dereferences and jump vs. direct jump) Therefore, the default is static dispatch; dynamic dispatch needs to be requested using the virtual keyword. This is often counterintuitive for Java programmers where dynamic dispatch is the default (as in Smalltalk). vptr n vptr n is_scalawag Person object Virtual Dispatch Person vtable std::string on heap Pirate object Pirate vtable Pirate code true getdrunk std::string on heap Person code getdrunk C++ virtual dispatch does never searches as in SmallTalk; vtable/instance variable offsets known at compile-time. Next Class Templates Overloading Multiple inheritance Casting (eeew!) C++ lambdas Java!