SlideShare une entreprise Scribd logo
1  sur  23
Brought to you by
www.facebook.com/rohithiphopper
   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 <iostream>           Includes function definitions
using namespace std;           for
                               console input and output.
float c(float x) {
   return x*x*x;              Function declaration.
}                             Function definition.

int main() {
   float x;                   Program starts here.
   cin >> x;                  Local variable declaration.
   cout << c(x) << endl;
                              Console input.
    return 0;                 Console output.
}                             Exit main function.
// This is main.cc             // This is mymath.h
#include <iostream>            #ifndef MYMATH
#include “mymath.h”            #define MYMATH
using namespace std;
                               float c(float x);
int main() {                   float d(float x);
   // ...stuff...
}                              #endif



    Functions are declared in m at h. h, but not defined.
                               ym
      They are implemented separately in m at h. c c .
                                          ym
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

      ↓
  myprogram      →
// This is main.cc
#include <GL/glut.h>                  Include OpenGL functions.
#include <iostream>                   Include standard IO
using namespace std;                   functions.
                                      Long and tedious
int main() {                           explanation.
   cout << “Hello!” << endl;
   glVertex3d(1,2,3);
   return 0;                          Calls function from standard
}                                      IO.
                                      Calls function from OpenGL.




% g++ -c main.cc                      Make object file.
                                      Make executable, link GLUT.
% g++ -o myprogram –lglut main.o      Execute program.
% ./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.
Most assignments include
INCFLAGS = 
       -
       I/afs/csail/group/graphics/courses/6.837/public/includ

                                                                makef i l es , which describe
       e
LINKFLAGS = 
       -L/afs/csail/group/graphics/courses/6.837/public/lib 

CFLAGS
       -lglut -lvl
          = -g -Wall -ansi
                                                                the files, dependencies, and
CC
SRCS
          = g++
          = main.cc parse.cc curve.cc surf.cc camera.cc            steps for compilation.
OBJS      = $(SRCS:.cc=.o)
PROG      = a1



                                                                  You can just type m
all: $(SRCS) $(PROG)

$(PROG): $(OBJS)
                                                                                     ake.
        $(CC) $(CFLAGS) $(OBJS) -o $@ $(LINKFLAGS)

.cc.o:

                                                                So you don’t have to know
          $(CC) $(CFLAGS) $< -c -o $@ $(INCFLAGS)



                                                                the stuff from the past few
depend:
          makedepend $(INCFLAGS) -Y $(SRCS)

clean:
          rm $(OBJS) $(PROG)                                                slides.
main.o: parse.h curve.h tuple.h



                                                                   But it’s nice to know.
# ... LOTS MORE ...
#include <iostream>
using namespace std;

int main() {                  Arrays must have known
   int n;
                               sizes at compile time.
   cin >> n;
   float f[n];
                               This doesn’t compile.
    for (int i=0; i<n; i++)
       f[i] = i;

    return 0;
}
#include <iostream>
                              Allocate the array during
using namespace std;
                                 runtime using new.
int main() {
   int n;
   cin >> n;                  No garbage collection, so
   float *f = new float[n];    you have to delete.
    for (int i=0; i<n; i++)
       f[i] = i;                Dynamic memory is
                               useful when you don’t
    delete [] f;
    return 0;                  know how much space
}                                    you need.
#include <iostream>
                              STL vector is a resizable
#include <vector>
using namespace std;           array with all dynamic
                              memory handled for you.
int main() {
   int n;
   cin >> n;                   STL has other cool stuff,
   vector<float> f(n);         such as strings and sets.
    for (int i=0; i<n; i++)
       f[i] = i;
                               If you can, use the STL
    return 0;                    and avoid dynamic
}                                     memory.
#include <iostream>
#include <vector>
using namespace std;           An alternative method
                              that does the same thing.
int main() {
   int n;
   cin >> n;                   Methods are called with
   vector<float> f;
                              the dot operator (same as
    for (int i=0; i<n; i++)             Java).
       f.push_back(i);

    return 0;                 vector is poorly named,
}                             it’s actually just an array.
float twice1(float x) {    This works as expected.
   return 2*x;
}

void twice2(float x) {
   x = 2*x;
                           This does nothing.
}

int main() {
   float x = 3;
   twice2(x);
   cout << x << endl;      The variable is
   return 0;                 unchanged.
}
vector<float>
twice(vector<float> x) {
   int n = x.size();           There is an incredible
   for (int i=0; i<n; i++)   amount of overhead here.
      x[i] = 2*x[i];
   return x;
}
                             This copies a huge array
int main() {                  two times. It’s stupid.
   vector<float>
   y(9000000);
   y = twice(y);              Maybe the compiler’s
   return 0;                 smart. Maybe not. Why
}
                                     risk it?
void twice3(float *x) {    Pass pointer by value
   (*x) = 2*(*x);
                             and
}
                             access data using
void twice4(float &x) {      asterisk.
   x = 2*x;
}
                           Pass by reference.
int main() {
   float x = 3;
   twice3(&x);
   twice4(x);
   return 0;
}                          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 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

Contenu connexe

Tendances

ES6 - Next Generation Javascript
ES6 - Next Generation JavascriptES6 - Next Generation Javascript
ES6 - Next Generation JavascriptRamesh Nair
 
Imugi: Compiler made with Python
Imugi: Compiler made with PythonImugi: Compiler made with Python
Imugi: Compiler made with PythonHan Lee
 
Free Monads Getting Started
Free Monads Getting StartedFree Monads Getting Started
Free Monads Getting StartedKent Ohashi
 
NativeBoost
NativeBoostNativeBoost
NativeBoostESUG
 
Explaining ES6: JavaScript History and What is to Come
Explaining ES6: JavaScript History and What is to ComeExplaining ES6: JavaScript History and What is to Come
Explaining ES6: JavaScript History and What is to ComeCory Forsyth
 
iOS Development with Blocks
iOS Development with BlocksiOS Development with Blocks
iOS Development with BlocksJeff Kelley
 
The best of AltJava is Xtend
The best of AltJava is XtendThe best of AltJava is Xtend
The best of AltJava is Xtendtakezoe
 
Objective-C Blocks and Grand Central Dispatch
Objective-C Blocks and Grand Central DispatchObjective-C Blocks and Grand Central Dispatch
Objective-C Blocks and Grand Central DispatchMatteo Battaglio
 
Arrry structure Stacks in data structure
Arrry structure Stacks  in data structureArrry structure Stacks  in data structure
Arrry structure Stacks in data structurelodhran-hayat
 
Swiftの関数型っぽい部分
Swiftの関数型っぽい部分Swiftの関数型っぽい部分
Swiftの関数型っぽい部分bob_is_strange
 
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)jeffz
 
JavaScript - new features in ECMAScript 6
JavaScript - new features in ECMAScript 6JavaScript - new features in ECMAScript 6
JavaScript - new features in ECMAScript 6Solution4Future
 
Building fast interpreters in Rust
Building fast interpreters in RustBuilding fast interpreters in Rust
Building fast interpreters in RustIngvar Stepanyan
 
Study of aloha protocol using ns2 network java proram
Study of aloha protocol using ns2 network java proramStudy of aloha protocol using ns2 network java proram
Study of aloha protocol using ns2 network java proramMeenakshi Devi
 
Python opcodes
Python opcodesPython opcodes
Python opcodesalexgolec
 

Tendances (20)

ES6 - Next Generation Javascript
ES6 - Next Generation JavascriptES6 - Next Generation Javascript
ES6 - Next Generation Javascript
 
Imugi: Compiler made with Python
Imugi: Compiler made with PythonImugi: Compiler made with Python
Imugi: Compiler made with Python
 
MP in Clojure
MP in ClojureMP in Clojure
MP in Clojure
 
Free Monads Getting Started
Free Monads Getting StartedFree Monads Getting Started
Free Monads Getting Started
 
NativeBoost
NativeBoostNativeBoost
NativeBoost
 
Explaining ES6: JavaScript History and What is to Come
Explaining ES6: JavaScript History and What is to ComeExplaining ES6: JavaScript History and What is to Come
Explaining ES6: JavaScript History and What is to Come
 
iOS Development with Blocks
iOS Development with BlocksiOS Development with Blocks
iOS Development with Blocks
 
The best of AltJava is Xtend
The best of AltJava is XtendThe best of AltJava is Xtend
The best of AltJava is Xtend
 
Objective-C Blocks and Grand Central Dispatch
Objective-C Blocks and Grand Central DispatchObjective-C Blocks and Grand Central Dispatch
Objective-C Blocks and Grand Central Dispatch
 
Arrry structure Stacks in data structure
Arrry structure Stacks  in data structureArrry structure Stacks  in data structure
Arrry structure Stacks in data structure
 
Swiftの関数型っぽい部分
Swiftの関数型っぽい部分Swiftの関数型っぽい部分
Swiftの関数型っぽい部分
 
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
 
ECMAScript 6
ECMAScript 6ECMAScript 6
ECMAScript 6
 
JavaScript - new features in ECMAScript 6
JavaScript - new features in ECMAScript 6JavaScript - new features in ECMAScript 6
JavaScript - new features in ECMAScript 6
 
Building fast interpreters in Rust
Building fast interpreters in RustBuilding fast interpreters in Rust
Building fast interpreters in Rust
 
Bind me if you can
Bind me if you canBind me if you can
Bind me if you can
 
Study of aloha protocol using ns2 network java proram
Study of aloha protocol using ns2 network java proramStudy of aloha protocol using ns2 network java proram
Study of aloha protocol using ns2 network java proram
 
EcmaScript 6
EcmaScript 6 EcmaScript 6
EcmaScript 6
 
Python opcodes
Python opcodesPython opcodes
Python opcodes
 
Constructor,destructors cpp
Constructor,destructors cppConstructor,destructors cpp
Constructor,destructors cpp
 

En vedette

C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorialskailash454
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorialsMayank Jain
 
Python in big data world
Python in big data worldPython in big data world
Python in big data worldRohit
 
Python programming - Everyday(ish) Examples
Python programming - Everyday(ish) ExamplesPython programming - Everyday(ish) Examples
Python programming - Everyday(ish) ExamplesAshish Sharma
 
C++ Tutorial
C++ TutorialC++ Tutorial
C++ Tutorialfreema48
 
Why Learn PHP Programming?
Why Learn PHP Programming?Why Learn PHP Programming?
Why Learn PHP Programming?XtreemHeights
 
Tutorial classes meeting and workshop november 17th 2015
Tutorial classes  meeting and workshop november 17th 2015Tutorial classes  meeting and workshop november 17th 2015
Tutorial classes meeting and workshop november 17th 2015Mr Bounab Samir
 
C# Tutorial
C# Tutorial C# Tutorial
C# Tutorial Jm Ramos
 
Beginners PHP Tutorial
Beginners PHP TutorialBeginners PHP Tutorial
Beginners PHP Tutorialalexjones89
 
Ms1 test 1 second term 2016 2017
Ms1 test 1  second term 2016 2017Ms1 test 1  second term 2016 2017
Ms1 test 1 second term 2016 2017Mr Bounab Samir
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).pptAlok Kumar
 
C Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointC Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointJavaTpoint.Com
 

En vedette (20)

C++ tutorials
C++ tutorialsC++ tutorials
C++ tutorials
 
Oop c++ tutorial
Oop c++ tutorialOop c++ tutorial
Oop c++ tutorial
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
Tutorial csharp
Tutorial csharpTutorial csharp
Tutorial csharp
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
Python in big data world
Python in big data worldPython in big data world
Python in big data world
 
C++ TUTORIAL 8
C++ TUTORIAL 8C++ TUTORIAL 8
C++ TUTORIAL 8
 
Python programming - Everyday(ish) Examples
Python programming - Everyday(ish) ExamplesPython programming - Everyday(ish) Examples
Python programming - Everyday(ish) Examples
 
C++ TUTORIAL 1
C++ TUTORIAL 1C++ TUTORIAL 1
C++ TUTORIAL 1
 
C++ Tutorial
C++ TutorialC++ Tutorial
C++ Tutorial
 
Why Learn PHP Programming?
Why Learn PHP Programming?Why Learn PHP Programming?
Why Learn PHP Programming?
 
C++ classes
C++ classesC++ classes
C++ classes
 
Php tutorial
Php tutorialPhp tutorial
Php tutorial
 
C++ Inheritance
C++ InheritanceC++ Inheritance
C++ Inheritance
 
Tutorial classes meeting and workshop november 17th 2015
Tutorial classes  meeting and workshop november 17th 2015Tutorial classes  meeting and workshop november 17th 2015
Tutorial classes meeting and workshop november 17th 2015
 
C# Tutorial
C# Tutorial C# Tutorial
C# Tutorial
 
Beginners PHP Tutorial
Beginners PHP TutorialBeginners PHP Tutorial
Beginners PHP Tutorial
 
Ms1 test 1 second term 2016 2017
Ms1 test 1  second term 2016 2017Ms1 test 1  second term 2016 2017
Ms1 test 1 second term 2016 2017
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
 
C Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointC Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpoint
 

Similaire à C++ Tutorial Covers Syntax, Memory, Classes

C++tutorial
C++tutorialC++tutorial
C++tutorialdips17
 
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Chris Adamson
 
C++11: Feel the New Language
C++11: Feel the New LanguageC++11: Feel the New Language
C++11: Feel the New Languagemspline
 
Take advantage of C++ from Python
Take advantage of C++ from PythonTake advantage of C++ from Python
Take advantage of C++ from PythonYung-Yu Chen
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib웅식 전
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++somu rajesh
 
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Abu Saleh
 
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloadingkinan keshkeh
 
Cocoa for Web Developers
Cocoa for Web DevelopersCocoa for Web Developers
Cocoa for Web Developersgeorgebrock
 
Pydiomatic
PydiomaticPydiomatic
Pydiomaticrik0
 
Functions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrupFunctions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrupSyedHaroonShah4
 

Similaire à C++ Tutorial Covers Syntax, Memory, Classes (20)

Cpp tutorial
Cpp tutorialCpp tutorial
Cpp tutorial
 
CppTutorial.ppt
CppTutorial.pptCppTutorial.ppt
CppTutorial.ppt
 
C++tutorial
C++tutorialC++tutorial
C++tutorial
 
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
 
Day 1
Day 1Day 1
Day 1
 
C++ idioms.pptx
C++ idioms.pptxC++ idioms.pptx
C++ idioms.pptx
 
C++11: Feel the New Language
C++11: Feel the New LanguageC++11: Feel the New Language
C++11: Feel the New Language
 
Take advantage of C++ from Python
Take advantage of C++ from PythonTake advantage of C++ from Python
Take advantage of C++ from Python
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
 
Oops presentation
Oops presentationOops presentation
Oops presentation
 
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
 
Oops lecture 1
Oops lecture 1Oops lecture 1
Oops lecture 1
 
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
 
C++ Overview PPT
C++ Overview PPTC++ Overview PPT
C++ Overview PPT
 
C++
C++C++
C++
 
Cocoa for Web Developers
Cocoa for Web DevelopersCocoa for Web Developers
Cocoa for Web Developers
 
Pydiomatic
PydiomaticPydiomatic
Pydiomatic
 
Python idiomatico
Python idiomaticoPython idiomatico
Python idiomatico
 
Functions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrupFunctions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrup
 

Dernier

Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 

Dernier (20)

Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 

C++ Tutorial Covers Syntax, Memory, Classes

  • 1. Brought to you by www.facebook.com/rohithiphopper
  • 2. 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?
  • 3. Basic syntax  Compiling your program  Argument passing  Dynamic memory  Object-oriented programming
  • 4. #include <iostream>  Includes function definitions using namespace std; for console input and output. float c(float x) { return x*x*x;  Function declaration. }  Function definition. int main() { float x;  Program starts here. cin >> x;  Local variable declaration. cout << c(x) << endl;  Console input. return 0;  Console output. }  Exit main function.
  • 5.
  • 6. // This is main.cc // This is mymath.h #include <iostream> #ifndef MYMATH #include “mymath.h” #define MYMATH using namespace std; float c(float x); int main() { float d(float x); // ...stuff... } #endif Functions are declared in m at h. h, but not defined. ym They are implemented separately in m at h. c c . ym
  • 7. 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 ↓ myprogram →
  • 8. // This is main.cc #include <GL/glut.h>  Include OpenGL functions. #include <iostream>  Include standard IO using namespace std; functions.  Long and tedious int main() { explanation. cout << “Hello!” << endl; glVertex3d(1,2,3); return 0;  Calls function from standard } IO.  Calls function from OpenGL. % g++ -c main.cc  Make object file.  Make executable, link GLUT. % g++ -o myprogram –lglut main.o  Execute program. % ./myprogram
  • 9. 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.
  • 10. Most assignments include INCFLAGS = - I/afs/csail/group/graphics/courses/6.837/public/includ makef i l es , which describe e LINKFLAGS = -L/afs/csail/group/graphics/courses/6.837/public/lib CFLAGS -lglut -lvl = -g -Wall -ansi the files, dependencies, and CC SRCS = g++ = main.cc parse.cc curve.cc surf.cc camera.cc steps for compilation. OBJS = $(SRCS:.cc=.o) PROG = a1 You can just type m all: $(SRCS) $(PROG) $(PROG): $(OBJS) ake. $(CC) $(CFLAGS) $(OBJS) -o $@ $(LINKFLAGS) .cc.o: So you don’t have to know $(CC) $(CFLAGS) $< -c -o $@ $(INCFLAGS) the stuff from the past few depend: makedepend $(INCFLAGS) -Y $(SRCS) clean: rm $(OBJS) $(PROG) slides. main.o: parse.h curve.h tuple.h But it’s nice to know. # ... LOTS MORE ...
  • 11.
  • 12. #include <iostream> using namespace std; int main() { Arrays must have known int n; sizes at compile time. cin >> n; float f[n]; This doesn’t compile. for (int i=0; i<n; i++) f[i] = i; return 0; }
  • 13. #include <iostream> Allocate the array during using namespace std; runtime using new. int main() { int n; cin >> n; No garbage collection, so float *f = new float[n]; you have to delete. for (int i=0; i<n; i++) f[i] = i; Dynamic memory is useful when you don’t delete [] f; return 0; know how much space } you need.
  • 14. #include <iostream> STL vector is a resizable #include <vector> using namespace std; array with all dynamic memory handled for you. int main() { int n; cin >> n; STL has other cool stuff, vector<float> f(n); such as strings and sets. for (int i=0; i<n; i++) f[i] = i; If you can, use the STL return 0; and avoid dynamic } memory.
  • 15. #include <iostream> #include <vector> using namespace std; An alternative method that does the same thing. int main() { int n; cin >> n; Methods are called with vector<float> f; the dot operator (same as for (int i=0; i<n; i++) Java). f.push_back(i); return 0; vector is poorly named, } it’s actually just an array.
  • 16. float twice1(float x) {  This works as expected. return 2*x; } void twice2(float x) { x = 2*x;  This does nothing. } int main() { float x = 3; twice2(x); cout << x << endl;  The variable is return 0; unchanged. }
  • 17. vector<float> twice(vector<float> x) { int n = x.size(); There is an incredible for (int i=0; i<n; i++) amount of overhead here. x[i] = 2*x[i]; return x; } This copies a huge array int main() { two times. It’s stupid. vector<float> y(9000000); y = twice(y); Maybe the compiler’s return 0; smart. Maybe not. Why } risk it?
  • 18. void twice3(float *x) {  Pass pointer by value (*x) = 2*(*x); and } access data using void twice4(float &x) { asterisk. x = 2*x; }  Pass by reference. int main() { float x = 3; twice3(&x); twice4(x); return 0; }  Address of variable.  The answer is 12.
  • 19. 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.
  • 20.
  • 21. 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.
  • 23. 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

Notes de l'éditeur

  1. 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
  2. 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.
  3. 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
  4. 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
  5. why? examples of purely functional programming languages… haskell, basic scheme…
  6. why? examples of purely functional programming languages… haskell, basic scheme…
  7. why? examples of purely functional programming languages… haskell, basic scheme…
  8. why? examples of purely functional programming languages… haskell, basic scheme…
  9. So why don’t we just use the first function?
  10. So why don’t we just use the first function?
  11. So why don’t we just use the first function?