Eugene Hsu.

Slides:



Advertisements
Similar presentations
C++ Language Fundamentals. 2 Contents 1. Introduction to C++ 2. Basic syntax rules 3. Declaring and using variables.
Advertisements

Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles C/C++ Emery Berger and Mark Corner University of Massachusetts.
C++ fundamentals.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Introduction to C++. Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation.
Java and C++, The Difference An introduction Unit - 00.
STL Standard Template Library ● Good reference book: – The C++ Standard Library ● A Tutorial and Reference ● by Nicolai M. Josuttis ● 1999 – Addison Wesley.
By – Tanvir Alam.  This tutorial offers several things.  You’ll see some neat features of the language.  You’ll learn the right things to google. 
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
Rossella Lau Lecture 1, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 1: Introduction What this course is about:
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Software Design 8.1 A Rose by any other name…C or Java? l Why do we use Java in our courses (royal we?)  Object oriented  Large collection of libraries.
C ++ Basics by Bindra Shrestha sce.uhcl.edu/shresthab CSCI 3333 Data Structures.
C++ Programming Part 2 Michael Griffiths Corporate Information and Computing Services The University of Sheffield
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
CS 11 C++ track: lecture 1 Administrivia Need a CS cluster account sysadmin/account_request.cgi Need to know UNIX (Linux)
Lecture 19 CIS 208 Wednesday, April 06, Welcome to C++ Basic program style and I/O Class Creation Templates.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
1 Today’s Objectives  Announcements Homework #3 is due on Monday, 10-Jul, however you can earn 10 bonus points for this HW if you turn it in on Wednesday,
Computing and Statistical Data Analysis Lecture 6 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Introduction to classes and objects:
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
February 28, 2005 Introduction to Classes. Object Oriented Programming An object is a software bundle of related variables and methods. Software objects.
Learners Support Publications Introduction to C++
Lecture 01a: C++ review Topics: Setting up projects, main program Memory Diagrams Variables / Types (some of) the many-types-of-const's Input / Output.
CSE 332: C++ template examples Today: Using Class and Function Templates Two examples –Function template for printing different types –Class template for.
11 Introduction to Object Oriented Programming (Continued) Cats.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Lecture 01d: C++ review Topics: functions scope / lifetime preprocessor directives header files C structures ("simple classes")
CSCE Introduction to Program Design and Concepts J. Michael Moore Spring 2015 Set 6: Miscellaneous 1 Based on slides created by Bjarne Stroustrup.
Week 13 - Friday.  What did we talk about last time?  Server communications on a socket  Function pointers.
Lecture 3: Getting Started & Input / Output (I/O)
CSE 374 Programming Concepts & Tools
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
EGR 2261 Unit 11 Pointers and Dynamic Variables
C++ Templates.
Motivation and Overview
CMSC202 Computer Science II for Majors Lecture 08 – Overloaded Constructors Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Introduction to Classes
Pointers and Pointer-Based Strings
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Data Structures with C++
Introduction to Classes
Chapter 5 Function Basics
Brought to you by C++ Tutorial Brought to you by
Constructors and destructors
Functions Pass By Value Pass by Reference
Created by Hwansoo Han Edited by Ikjun Yeom
From C to C++: Summary of weeks 1 - 4
CSE 303 Concepts and Tools for Software Development
Arrays an array of 5 ints is filled with 3,2,4,1,7
Pointers and Pointer-Based Strings
Pointers and dynamic objects
Today’s Objectives 28-Jun-2006 Announcements
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
Class rational part2.
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
SPL – PS1 Introduction to C++.
SPL – PS3 C++ Classes.
Introduction to Classes and Objects
Presentation transcript:

Eugene Hsu

This tutorial offers several things. You’ll see some neat features of the language. You’ll learn the right things to google. You’ll find a list of useful books and web pages. But don’t expect too much! It’s complicated, and you’ll learn by doing. But I’ll give it my best shot, okay?

Basic syntax Compiling your program Argument passing Dynamic memory Object-oriented programming

Includes function definitions for console input and output. #include <iostream> using namespace std; float c(float x) { return x*x*x; } int main() { float x; cin >> x; cout << c(x) << endl; return 0; Includes function definitions for console input and output. Function declaration. Function definition. Program starts here. Local variable declaration. Console input. Console output. Exit main function. about as simple as it gets – just get a feel for the syntax but you’ll have more complicated programs so you want to organize better first way to do that is by separating into multiple files

Functions are declared in mymath.h, but not defined. // This is main.cc #include <iostream> #include “mymath.h” using namespace std; int main() { // ...stuff... } // This is mymath.h #ifndef MYMATH #define MYMATH float c(float x); float d(float x); #endif same program, but we’ve pulled c functions out we put it in a separate file … or rather, two separate files header file (you see on the right) declares the functions – that is, gives name, parameters, return type. but doesn’t include the implementation, which is done in a separate file. so when you code up the main program file, you can include the header file, and call the functions because in c++ you can only call functions that are declared. Functions are declared in mymath.h, but not defined. They are implemented separately in mymath.cc.

g++ -o myprogram main.o mathstuff.o drawstuff.o main.cc mymath.cc mydraw.cc    g++ -c main.cc g++ -c mymath.cc g++ -c mydraw.cc    main.o mymath.o mydraw.o    g++ -o myprogram main.o mathstuff.o drawstuff.o so here’s the basic setup you write a bunch of cc files that implement functions (or objects, as we’ll see later) the headers include the declarations of functions (or objects) include the headers in the cc files if you’re using those functions compile to object files link all object files together get program make graphics  myprogram 

% g++ -o myprogram –lglut main.o % ./myprogram // This is main.cc #include <GL/glut.h> #include <iostream> using namespace std; int main() { cout << “Hello!” << endl; glVertex3d(1,2,3); return 0; } Include OpenGL functions. Include standard IO functions. Long and tedious explanation. Calls function from standard IO. Calls function from OpenGL. Make object file. Make executable, link GLUT. Execute program. almost all c++ will make use of libraries bunch of convenient functions that you can use two libraries you’ll be using for almost assignments are glut (exp) and iostream (exp) so main here actually calls functions defined in both these libraries and here’s how we might compile % g++ -c main.cc % g++ -o myprogram –lglut main.o % ./myprogram

Software engineering reasons. Separate interface from implementation. Promote modularity. The headers are a contract. Technical reasons. Only rebuild object files for modified source files. This is much more efficient for huge programs.

INCFLAGS = \ -I/afs/csail/group/graphics/courses/6 INCFLAGS = \ -I/afs/csail/group/graphics/courses/6.837/public/include LINKFLAGS = \ -L/afs/csail/group/graphics/courses/6.837/public/lib \ -lglut -lvl CFLAGS = -g -Wall -ansi CC = g++ SRCS = main.cc parse.cc curve.cc surf.cc camera.cc OBJS = $(SRCS:.cc=.o) PROG = a1 all: $(SRCS) $(PROG) $(PROG): $(OBJS) $(CC) $(CFLAGS) $(OBJS) -o $@ $(LINKFLAGS) .cc.o: $(CC) $(CFLAGS) $< -c -o $@ $(INCFLAGS) depend: makedepend $(INCFLAGS) -Y $(SRCS) clean: rm $(OBJS) $(PROG) main.o: parse.h curve.h tuple.h # ... LOTS MORE ... Most assignments include makefiles, which describe the files, dependencies, and steps for compilation. You can just type make. So you don’t have to know the stuff from the past few slides. But it’s nice to know.

Arrays must have known sizes at compile time. This doesn’t compile. #include <iostream> using namespace std; int main() { int n; cin >> n; float f[n]; for (int i=0; i<n; i++) f[i] = i; return 0; } Arrays must have known sizes at compile time. This doesn’t compile. why? examples of purely functional programming languages… haskell, basic scheme…

#include <iostream> using namespace std; int main() { int n; cin >> n; float *f = new float[n]; for (int i=0; i<n; i++) f[i] = i; delete [] f; return 0; } Allocate the array during runtime using new. No garbage collection, so you have to delete. Dynamic memory is useful when you don’t know how much space you need. why? examples of purely functional programming languages… haskell, basic scheme…

#include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<float> f(n); for (int i=0; i<n; i++) f[i] = i; return 0; } STL vector is a resizable array with all dynamic memory handled for you. STL has other cool stuff, such as strings and sets. If you can, use the STL and avoid dynamic memory. why? examples of purely functional programming languages… haskell, basic scheme…

#include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<float> f; for (int i=0; i<n; i++) f.push_back(i); return 0; } An alternative method that does the same thing. Methods are called with the dot operator (same as Java). vector is poorly named, it’s actually just an array. why? examples of purely functional programming languages… haskell, basic scheme…

float twice1(float x) { return 2. x; } void twice2(float x) { x = 2 float twice1(float x) { return 2*x; } void twice2(float x) { x = 2*x; int main() { float x = 3; twice2(x); cout << x << endl; return 0;  This works as expected.  This does nothing.  The variable is unchanged. So why don’t we just use the first function?

vector<float> twice(vector<float> x) { int n = x vector<float> twice(vector<float> x) { int n = x.size(); for (int i=0; i<n; i++) x[i] = 2*x[i]; return x; } int main() { vector<float> y(9000000); y = twice(y); return 0; There is an incredible amount of overhead here. This copies a huge array two times. It’s stupid. Maybe the compiler’s smart. Maybe not. Why risk it? So why don’t we just use the first function?

access data using asterisk. void twice3(float *x) { (*x) = 2*(*x); } void twice4(float &x) { x = 2*x; int main() { float x = 3; twice3(&x); twice4(x); return 0;  Pass pointer by value and access data using asterisk.  Pass by reference.  Address of variable.  The answer is 12. So why don’t we just use the first function?

You’ll often see objects passed by reference. Functions can modify objects without copying. To avoid copying objects (often const references). Pointers are kind of old school, but still useful. For super-efficient low-level code. Within objects to handle dynamic memory. You shouldn’t need pointers for this class. Use the STL instead, if at all possible.

Classes implement objects. You’ve probably seen these in 6.170. C++ does things a little differently. Let’s implement a simple image object. Show stuff we’ve seen, like dynamic memory. Introduce constructors, destructors, const, and operator overloading. I’ll probably make mistakes, so some debugging too.

Live Demo!

The C++ Programming Language A book by Bjarne Stroustrup, inventor of C++. My favorite C++ book. The STL Programmer’s Guide Contains documentation for the standard template library. http://www.sgi.com/tech/stl/ Java to C++ Transition Tutorial Probably the most helpful, since you’ve all taken 6.170. http://www.cs.brown.edu/courses/cs123/javatoc.shtml