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. 

Slides:



Advertisements
Similar presentations
Chapter 18 Vectors and Arrays
Advertisements

C++ Language Fundamentals. 2 Contents 1. Introduction to C++ 2. Basic syntax rules 3. Declaring and using variables.
Chapter 18 Vectors and Arrays John Keyser’s Modification of Slides by Bjarne Stroustrup
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,
CSE 374 Programming Concepts & Tools Hal Perkins Winter 2012 Lecture 19 – Introduction to C++
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Classes Separating interface from implementation
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles C/C++ Emery Berger and Mark Corner University of Massachusetts.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
OOP Languages: Java vs C++
11 Introduction to Object Oriented Programming (Continued) Cats II.
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
Introduction to C++. Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation.
Programming Languages and Paradigms Object-Oriented Programming.
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.
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 13: An Introduction to C++
Programming Languages and Paradigms Object-Oriented Programming (Part II)
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.
Object Oriented Programming Elhanan Borenstein copyrights © Elhanan Borenstein.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
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.
ACM C++ Tutorial Nathan Ratliff. Development Environments Windows – Microsoft Visual C++ Note: It’s not ansi standard Linux / Unix – GCC / G++
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
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.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
C ++ Basics by Bindra Shrestha sce.uhcl.edu/shresthab CSCI 3333 Data Structures.
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
C++ Programming Part 2 Michael Griffiths Corporate Information and Computing Services The University of Sheffield
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
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.
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,
CSC241 Object-Oriented Programming (OOP) Lecture No. 5.
More About Data Types & Functions. General Program Structure #include statements for I/O, etc. #include's for class headers – function prototype statements.
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.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Introducing C++ to Java Programmers by Kip Irvine updated 2/27/2003.
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.
Cop3530sp12. Parameter passing call by value- appropriate for small objects that should not be altered by the function call by constant reference- appropriate.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.
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.
Pointer to an Object Can define a pointer to an object:
Eugene Hsu.
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.
Brought to you by C++ Tutorial Brought to you by
Created by Hwansoo Han Edited by Ikjun Yeom
From C to C++: Summary of weeks 1 - 4
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Introduction to Classes and Objects
Presentation transcript:

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.  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

#include 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.

// This is main.cc #include #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 Functions are declared in mymath.h, but not defined. They are implemented separately in mymath.cc.

main.ccmymath.ccmydraw.cc g++ -c main.ccg++ -c mymath.ccg++ -c mydraw.cc    g++ -o myprogram main.o mathstuff.o drawstuff.o main.omymath.omydraw.o  myprogram 

// This is main.cc #include 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. % 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.837/public/includ e 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.

#include 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.

#include 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.

#include using namespace std; int main() { int n; cin >> n; vector 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.

#include using namespace std; int main() { int n; cin >> n; vector 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.

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.

vector twice(vector x) { int n = x.size(); for (int i=0; i<n; i++) x[i] = 2*x[i]; return x; } int main() { vector y( ); 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?

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.

 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  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.

 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.  Java to C++ Transition Tutorial  Probably the most helpful, since you’ve all taken