CS 11 C++ track: lecture 1

Similar documents
CS11 Introduction to C++ Fall Lecture 1

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

CS11 Intro C++ Spring 2018 Lecture 1

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CSE 303: Concepts and Tools for Software Development

CS 11 java track: lecture 1

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

AN OVERVIEW OF C++ 1

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

CS250 Final Review Questions

CSE 333 Lecture 9 - intro to C++

CS 376b Computer Vision

CS11 Introduction to C++ Fall Lecture 6

C++ Constructor Insanity

CS3157: Advanced Programming. Outline

CS 162 Intro to CS II. Structs vs. Classes

Miri Ben-Nissan (Kopel) (2017)

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

EL2310 Scientific Programming

Intermediate Programming, Spring 2017*

Computer Science II Lecture 1 Introduction and Background

C++ basics Getting started with, and Data Types.

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

CS11 Introduction to C++ Fall Lecture 3

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Introduction to C++ Introduction to C++ 1

Classes and Objects in C++

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

CS

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

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

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

Introduction to Programming using C++

UEE1303(1070) S12: Object-Oriented Programming Constant Pointer and Class

COMP6771 Advanced C++ Programming

12/2/2009. The plan. References. References vs. pointers. Reference parameters. const and references. HW7 is out; new PM due date Finish last lecture

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

C++ Programming Classes. Michael Griffiths Corporate Information and Computing Services The University of Sheffield

Ch 6. Functions. Example: function calls function

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Lecture 10: Introduction to Inheritance

Lab#5 Due Wednesday, February 25, at the start of class. Purpose: To develop familiarity with C++ pointer variables

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

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

Abstract Data Types (ADT) and C++ Classes

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

Scope. Scope is such an important thing that we ll review what we know about scope now:

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

Dr. Md. Humayun Kabir CSE Department, BUET

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. C++ Kitchen Sink. Lecture 14.

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

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!

An Introduction to C++

Programming in C++: Assignment Week 8

EL2310 Scientific Programming

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!

cs3157: c++ lecture #2 (mon-11-apr-2005) chronology of some programming languages... C++ vs Java identifiers.

Fast Introduction to Object Oriented Programming and C++

COMP322 - Introduction to C++ Lecture 01 - Introduction

Introducing C++ to Java Programmers


A brief introduction to C++

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

Evolution of Programming Languages

C++ Intro. CSE 333 Summer Instructor: Hal Perkins. Teaching Assistants: Renshu Gu William Kim Soumya Vasisht

Scientific Computing

CS250 Final Review Questions

Programming in C++: Assignment Week 3

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

Functions. CS111 Lab Queens College, CUNY Instructor: Kent Chin

Lab 1: First Steps in C++ - Eclipse

W3101: Programming Languages C++ Ramana Isukapalli

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

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

A Tour of the C++ Programming Language

The Class. Classes and Objects. Example class: Time class declaration with functions defined inline. Using Time class in a driver.

CS93SI Handout 04 Spring 2006 Apr Review Answers

Data Structures and Programming with C++

Programmazione. Prof. Marco Bertini

CS 31 Discussion 1A, Week 1. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50

More About Classes. Gaddis Ch. 14, CS 2308 :: Fall 2015 Molly O'Neil

Operator overloading

CS24 Week 3 Lecture 1

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

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

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

CSC1322 Object-Oriented Programming Concepts

Getting started with C++ (Part 2)

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

Due Date: See Blackboard

Implementing an ADT with a Class

Due Date: See Blackboard

C C C C++ 2 ( ) C C++ 4 C C

CS102 C++ Exception Handling & Namespaces

Where do we go from here?

CS250 Final Review Questions

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING REWRAP TEST I CS6301 PROGRAMMING DATA STRUCTURES II

More Tutorial on C++:

Transcription:

CS 11 C++ track: lecture 1 Administrivia Need a CS cluster account http://www.cs.caltech.edu/cgi-bin/ sysadmin/account_request.cgi Need to know UNIX (Linux) www.its.caltech.edu/its/facilities/labsclusters/ unix/unixtutorial.shtml Need to know C at level of CS 11 C track Track home page: http://www.cs.caltech.edu/courses/cs11/ material/cpp/mike/index.html

Assignments 1st assignment is posted now due one week after class, midnight late penalty: 1 mark/day redos

Textbook suggested: Essential C++ by Stanley Lippman covers more material than this track Stroustrup NOT recommended! except as reference

Why use C++? speed and low-level control of C higher level of abstraction than C object-oriented programming generic programming can define own data types that work like built-in data types

What's not to like about C++? incredibly complex! tons of features that don t always interact well takes a long time to master suffers from many of C s problems memory leaks, crashes much less safe/portable than java "language for experts"

Some features of C++ everything that s in C object-oriented programming very powerful (and complex!) generic programming (templates) list<int>, multiset<window *> exception handling better way to deal with errors

Getting started The hello, world! program: #include <iostream> // Access values in namespace std : using namespace std; int main() { } cout << hello, world! << endl; return 0;

Compiling Save as hello.cc and do: % g++ -Wall hello.cc -o hello % hello hello, world! woo hoo!

Alternatively... The hello, world! program: #include <iostream> int main() { } std::cout << hello, world! return 0; << std::endl; std:: says use name in std namespace

cin and cout (1) Don t use printf() or scanf() for i/o Use cout for output, cin for input int i = 10; double d = 3.14159; string s = "I am a string!"; cout << "i = " << i << " d = " << d << " s = " << s << endl;

cin and cout (2) int i; double d; string s; cout << "enter i: "; cin >> i; cout << "enter d: "; cin >> d; cout << "enter s: "; cin >> s; cout << "i = " << i << " d = " << d << " s = " << s << endl;

Objects An object consists of: data functions (methods) that act on data Idea: rest of program only interacts with an object by calling its methods data in object stays private to that object C++ supports objects directly

Classes A class is a template for building an object most C++ code consists of class descriptions Classes include: constructors (functions that create instances of the class i.e. new objects of that class) data members or fields (data associated with each instance) member functions or methods (functions that can act directly on the data members) destructors (functions that destroy instances)

Example: 2d point class Point { private: int x_coord, y_coord; public: Point(); // constructor Point(int x, int y); // constructor void setx(int val); // mutator int getx(); // accessor ~Point(); // destructor };

Example: 2d point class Point { private: int x_coord, y_coord; public: Point(); // constructor class Point(int x, int y); // constructor void setx(int val); // mutator int getx(); // accessor ~Point(); // destructor };

Example: 2d point class Point { private: data members (fields) int x_coord, y_coord; public: Point(); // constructor Point(int x, int y); // constructor void setx(int val); // mutator int getx(); // accessor ~Point(); // destructor };

Example: 2d point class Point { private: int x_coord, y_coord; public: constructors Point(); // constructor Point(int x, int y); // constructor void setx(int val); // mutator int getx(); // accessor ~Point(); // destructor };

Example: 2d point class Point { private: int x_coord, y_coord; public: Point(); // constructor Point(int x, int y); // constructor void setx(int val); // mutator int getx(); // accessor ~Point(); // destructor member functions }; (methods)

Example: 2d point class Point { private: int x_coord, y_coord; public: Point(); // constructor Point(int x, int y); // constructor void setx(int val); // mutator int getx(); // accessor ~Point(); // destructor }; destructor

Example: 2d point Put previous code in Point.hh file Implementation goes in Point.cc file: #include Point.hh Point::Point(int x, int y) { x_coord = x; y_coord = y; } void Point::setX(int val) { x_coord = val; } // etc.

Using 2d points Using 2d points in other code: //... Point p(); // calls no-arg ctor Point p2(10, 10); // calls 2-arg ctor cout << p2.getx() << endl; // call method p.setx(20); // etc.

Destructors What about destructors? destructor is automatically called at end of block where Point objects created useful when allocating memory dynamically not mandatory (not really needed here)

References (1) contrast: void setxto10(point p) { // copies p p.setx(10); } with: void setxto10(point *p) { // doesn't copy p->setx(10); // (*p).setx(10); }

References (2) C++ shortcut: void setxto10(point &p) { // note the & p.setx(10); } no copy is made like pointers, but with nicer syntax use this instead of pointers when possible

Odds and ends C header files are included differently #include <cmath> // not <math.h> meaning is the same don t mix <cstdio> and <iostream> in the same code! results are unpredictable

That's all for now! First lab will walk you through these steps. Should be pretty easy. Future labs will be much more complicated matrix classes (regular, sparse) operator overloading templates inheritance So stay tuned!