Brought to you by www.freshmaza.com C++ Tutorial Brought to you by www.freshmaza.com.

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.
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.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
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.
11 Introduction to Object Oriented Programming (Continued) Cats II.
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.
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,
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Computing and Statistical Data Analysis Lecture 6 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Introduction to classes and objects:
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.
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??
1 Introduction to Object Oriented Programming Chapter 10.
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
Eugene Hsu.
C++ Templates.
Motivation and Overview
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.
Introduction to Classes
Chapter 5 Function Basics
Constructors and Other Tools
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
Pointers and Pointer-Based Strings
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:

Brought to you by www.freshmaza.com C++ Tutorial Brought to you by www.freshmaza.com

Introduction 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?

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

Basic C++ Program #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

Program Structure and Compilation

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 Multiple Files 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 

Libraries % g++ -c main.cc % g++ -o myprogram –lglut main.o // 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

Why? Software engineering reasons. Technical 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.

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

Memory and Functions

Arrays must have known sizes at compile time. This doesn’t compile. Dynamic Memory #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…

Dynamic Memory #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…

Standard Template Library #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…

Standard Template Library #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…

Argument Passing 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?

Argument Passing 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?

Argument Passing 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?

Argument Passing 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.

Object Oriented Programming

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

Classes Live Demo!

Resources The C++ Programming Language The STL Programmer’s Guide 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