SlideShare a Scribd company logo
Brought to you by
www.freshmaza.com
 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>
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.
CppTutorial.ppt
// 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
Functions are declared in mymath.h, but not defined.
They are implemented separately in mymath.cc.
main.cc mymath.cc mydraw.cc
g++ -c main.cc g++ -c mymath.cc g++ -c mydraw.cc
  
  
  
g++ -o myprogram main.o mathstuff.o drawstuff.o
main.o mymath.o mydraw.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.
% 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.
CppTutorial.ppt
#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.
#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.
#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.
#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.
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<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?
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.
CppTutorial.ppt
 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
Ad

More Related Content

Similar to CppTutorial.ppt (20)

ES6: The Awesome Parts
ES6: The Awesome PartsES6: The Awesome Parts
ES6: The Awesome Parts
Domenic Denicola
 
Oops presentation
Oops presentationOops presentation
Oops presentation
sushamaGavarskar1
 
C++ Overview PPT
C++ Overview PPTC++ Overview PPT
C++ Overview PPT
Thooyavan Venkatachalam
 
C++
C++C++
C++
VishalMishra313
 
Performance measurement and tuning
Performance measurement and tuningPerformance measurement and tuning
Performance measurement and tuning
AOE
 
C++ manual Report Full
C++ manual Report FullC++ manual Report Full
C++ manual Report Full
Thesis Scientist Private Limited
 
cppt-170218053903.pptxhjkjhjjjjjjjjjjjjjjj
cppt-170218053903.pptxhjkjhjjjjjjjjjjjjjjjcppt-170218053903.pptxhjkjhjjjjjjjjjjjjjjj
cppt-170218053903.pptxhjkjhjjjjjjjjjjjjjjj
supriyaharlapur1
 
All I know about rsc.io/c2go
All I know about rsc.io/c2goAll I know about rsc.io/c2go
All I know about rsc.io/c2go
Moriyoshi Koizumi
 
ppl unit 3.pptx ppl unit 3 usefull can understood
ppl unit 3.pptx ppl unit 3 usefull can understoodppl unit 3.pptx ppl unit 3 usefull can understood
ppl unit 3.pptx ppl unit 3 usefull can understood
divasivavishnu2003
 
C Programming Homework Help
C Programming Homework HelpC Programming Homework Help
C Programming Homework Help
Programming Homework Help
 
Столпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай МозговойСтолпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай Мозговой
Sigma Software
 
Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to Javascript
Anjan Banda
 
Effective Object Oriented Design in Cpp
Effective Object Oriented Design in CppEffective Object Oriented Design in Cpp
Effective Object Oriented Design in Cpp
CodeOps Technologies LLP
 
cppt-170218053903 (1).pptx
cppt-170218053903 (1).pptxcppt-170218053903 (1).pptx
cppt-170218053903 (1).pptx
WatchDog13
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
somu rajesh
 
ES6 is Nigh
ES6 is NighES6 is Nigh
ES6 is Nigh
Domenic Denicola
 
Getting started with ES6
Getting started with ES6Getting started with ES6
Getting started with ES6
Nitay Neeman
 
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docxfilesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
ssuser454af01
 
Day 1
Day 1Day 1
Day 1
Pat Zearfoss
 
Lambdas puzzler - Peter Lawrey
Lambdas puzzler - Peter LawreyLambdas puzzler - Peter Lawrey
Lambdas puzzler - Peter Lawrey
JAXLondon_Conference
 
Performance measurement and tuning
Performance measurement and tuningPerformance measurement and tuning
Performance measurement and tuning
AOE
 
cppt-170218053903.pptxhjkjhjjjjjjjjjjjjjjj
cppt-170218053903.pptxhjkjhjjjjjjjjjjjjjjjcppt-170218053903.pptxhjkjhjjjjjjjjjjjjjjj
cppt-170218053903.pptxhjkjhjjjjjjjjjjjjjjj
supriyaharlapur1
 
All I know about rsc.io/c2go
All I know about rsc.io/c2goAll I know about rsc.io/c2go
All I know about rsc.io/c2go
Moriyoshi Koizumi
 
ppl unit 3.pptx ppl unit 3 usefull can understood
ppl unit 3.pptx ppl unit 3 usefull can understoodppl unit 3.pptx ppl unit 3 usefull can understood
ppl unit 3.pptx ppl unit 3 usefull can understood
divasivavishnu2003
 
Столпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай МозговойСтолпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай Мозговой
Sigma Software
 
Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to Javascript
Anjan Banda
 
cppt-170218053903 (1).pptx
cppt-170218053903 (1).pptxcppt-170218053903 (1).pptx
cppt-170218053903 (1).pptx
WatchDog13
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
somu rajesh
 
Getting started with ES6
Getting started with ES6Getting started with ES6
Getting started with ES6
Nitay Neeman
 
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docxfilesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
ssuser454af01
 

More from HODZoology3 (8)

6_2019_03_09!01_32_17_AM.ppt
6_2019_03_09!01_32_17_AM.ppt6_2019_03_09!01_32_17_AM.ppt
6_2019_03_09!01_32_17_AM.ppt
HODZoology3
 
Indian Silk Industry for Kashmir University.ppt
Indian Silk Industry for Kashmir University.pptIndian Silk Industry for Kashmir University.ppt
Indian Silk Industry for Kashmir University.ppt
HODZoology3
 
Phylum Porifera.pptx
Phylum Porifera.pptxPhylum Porifera.pptx
Phylum Porifera.pptx
HODZoology3
 
Phylum Mollusca.pptx
Phylum Mollusca.pptxPhylum Mollusca.pptx
Phylum Mollusca.pptx
HODZoology3
 
Phylum Arthropoda.pptx
Phylum Arthropoda.pptxPhylum Arthropoda.pptx
Phylum Arthropoda.pptx
HODZoology3
 
PARENTAL CARE.pptx
PARENTAL CARE.pptxPARENTAL CARE.pptx
PARENTAL CARE.pptx
HODZoology3
 
Larval%20forms%20of%20trematodes%20%5e0%20cestodes%20ppt%5e (2).pptx
Larval%20forms%20of%20trematodes%20%5e0%20cestodes%20ppt%5e (2).pptxLarval%20forms%20of%20trematodes%20%5e0%20cestodes%20ppt%5e (2).pptx
Larval%20forms%20of%20trematodes%20%5e0%20cestodes%20ppt%5e (2).pptx
HODZoology3
 
Arterial System-WPS Office.pptx
Arterial System-WPS Office.pptxArterial System-WPS Office.pptx
Arterial System-WPS Office.pptx
HODZoology3
 
6_2019_03_09!01_32_17_AM.ppt
6_2019_03_09!01_32_17_AM.ppt6_2019_03_09!01_32_17_AM.ppt
6_2019_03_09!01_32_17_AM.ppt
HODZoology3
 
Indian Silk Industry for Kashmir University.ppt
Indian Silk Industry for Kashmir University.pptIndian Silk Industry for Kashmir University.ppt
Indian Silk Industry for Kashmir University.ppt
HODZoology3
 
Phylum Porifera.pptx
Phylum Porifera.pptxPhylum Porifera.pptx
Phylum Porifera.pptx
HODZoology3
 
Phylum Mollusca.pptx
Phylum Mollusca.pptxPhylum Mollusca.pptx
Phylum Mollusca.pptx
HODZoology3
 
Phylum Arthropoda.pptx
Phylum Arthropoda.pptxPhylum Arthropoda.pptx
Phylum Arthropoda.pptx
HODZoology3
 
PARENTAL CARE.pptx
PARENTAL CARE.pptxPARENTAL CARE.pptx
PARENTAL CARE.pptx
HODZoology3
 
Larval%20forms%20of%20trematodes%20%5e0%20cestodes%20ppt%5e (2).pptx
Larval%20forms%20of%20trematodes%20%5e0%20cestodes%20ppt%5e (2).pptxLarval%20forms%20of%20trematodes%20%5e0%20cestodes%20ppt%5e (2).pptx
Larval%20forms%20of%20trematodes%20%5e0%20cestodes%20ppt%5e (2).pptx
HODZoology3
 
Arterial System-WPS Office.pptx
Arterial System-WPS Office.pptxArterial System-WPS Office.pptx
Arterial System-WPS Office.pptx
HODZoology3
 
Ad

Recently uploaded (20)

Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
Unit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdfUnit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdf
KanchanPatil34
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
Handling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptxHandling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptx
AuthorAIDNationalRes
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
How to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 WebsiteHow to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 Website
Celine George
 
GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdfBiophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
PKLI-Institute of Nursing and Allied Health Sciences Lahore , Pakistan.
 
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Library Association of Ireland
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Political History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptxPolitical History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
Unit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdfUnit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdf
KanchanPatil34
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
Handling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptxHandling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptx
AuthorAIDNationalRes
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
How to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 WebsiteHow to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 Website
Celine George
 
GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Library Association of Ireland
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
Ad

CppTutorial.ppt

  • 1. Brought to you by www.freshmaza.com
  • 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> 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.
  • 6. // 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 Functions are declared in mymath.h, but not defined. They are implemented separately in mymath.cc.
  • 7. main.cc mymath.cc mydraw.cc g++ -c main.cc g++ -c mymath.cc g++ -c mydraw.cc          g++ -o myprogram main.o mathstuff.o drawstuff.o main.o mymath.o mydraw.o  myprogram 
  • 8. // 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. % g++ -c main.cc % g++ -o myprogram –lglut main.o % ./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. 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.
  • 12. #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.
  • 13. #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.
  • 14. #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.
  • 15. #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.
  • 16. 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.
  • 17. 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?
  • 18. 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.
  • 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.
  • 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