Professional Documents
Culture Documents
(Practical)
Introduction to C++ and OOP
For use in the Geant4 course
Largely incomplete
Not meant to replace good C++, UML, and OOP books!
A “superset” of C
You can write procedural code in C++
Preprocessor Compiler
Translates into machine code
Inlines #includes etc.
Associates calls with functions
Object files
Executable Linker
Associates functions with definitions
myFirstProgram
External Libraries, libc.so, libcs123.so
Maria Grazia Pia
Using namespace
#include <iostream>
#include <string>
...
std::string question = “What do I learn this week?”;
std::cout << question << std::endl;
if ( someCondition ) {
…
i = new int;
} else if ( anotherCondition ) {
…
i = new int;
} “null pointer exception”
*i = someVariable;
for (initialization; condition; increase) statement; for (int n=10; n>0; n--) {
cout << n << ", ";
for (n=10; n>0; n--) }
{
cout << n << ", ";
if (n==3)
{
loop:
cout << "countdown aborted!"; cout << n << ", ";
break; n--;
} if (n>0) goto loop;
}Maria Grazia Pia cout << “Procedural programming!“ ;
Functions
In C++ all function parameters are passed by copy
class Car {
public:
// Members can be accessed by any object
protected:
// Can only be accessed by Car and its derived objects
private:
// Can only be accessed by Car for its own use.
};
f: f: f:
save_regs save_regs save_regs
ld r0, sp(0) ld r0, sp(4) ld r0, sp(8)
add 4 add 8 add 16
ret ret ret
f<char>:
void save_regs
f(char i) ld r0, sp(0)
{…} add 4
ret
template f<int>:
<typename T> void save_regs
void f(int i) ld r0, sp(4)
f(T i) {…} add 8
ret
{…}
f<double>:
void save_regs
f(double i) ld r0, sp(8)
{…} add 16
ret
#include <string>
void FunctionExample()
{
std::string s, t;
char c = 'a';
s.push_back(c); // s is now “a”;
const char* cc = s.c_str(); // get ptr to “a”
const char dd[] = ‘like’;
t = dd; // t is now “like”;
t = s + t; // append “like” to “a”
}
#include <vector>
#include <algorithm>
void FunctionExample()
{
std::vector<int> v(10);
v[5] = 3; // set fifth element to 3
std::vector<int>::const_iterator it
= std::find(v.begin(), v.end(), 3);
bool found = it != v.end();
if (found) {
int three = *it;
}
}
Maria Grazia Pia
Iterators
void FunctionExample() {
stl::vector<int> v;
stl::vector<int>::const_iterator it = v.begin();
for (it = v.begin() ; it != v.end() ; it++) {
int val = *it;
}
}
myImage.SetAllPixels(clearColor);
class Controller
{
//...
private:
Gui* myGui;
//...
};
Maria Grazia Pia
Header file and implementation
File Segment.hh File Segment.cc
#ifndef SEGMENT_HEADER #include “Segment.hh”
#define SEGMENT_HEADER #include “Point.hh”
Segment::Segment() // constructor
class Point; {
class Segment p0 = new Point(0.,0.);
{ p1 = new Point(1.,1.);
public: }
Segment(); Segment::~Segment() // destructor
virtual ~Segment(); {
double length(); delete p0;
private: delete p1;
}
Point* p0,
double Segment::length()
Point* p1; {
}; function implementation …
#endif // SEGMENT_HEADER
Maria Grazia Pia }
“Segmentation fault (core dumped)”
Typical causes:
Name
+SomePublicAttribute : SomeType
Attributes -SomePrivateAttribute : SomeType
Operations #SomeProtectedAttribute : SomeType
Responsibilities +ClassMethodOne()
+ClassMethodTwo()
Classes can show
Responsibilities
visibility and types -- can optionally be described here.
Shape
Models inheritance
+points
1 3..∗
Contains
Polygon Point
{ordered}
1
GraphicsBundle
1
-bundle color
texture
density
A aggregates B A contains B
= =
B is part of A, but their B is part of A, and their
lifetimes may be different lifetimes are the same
Customer
1
Class Aggregation
Rental Item
{abstract}
1..*
1 0..1
Composition
Association
Generalization
Checkout Screen
DVD Movie CD Music Video Game
:Rented Items
5: add(customer, movies)
1: enter_customer()
8: generateRentalTotal()
3: enter_movies()
7: print invoice() 2: IsValidCustomer(CustId) Object
:Check-out :Customer
Manager
:Clerk
4:GetMovieByBarcode()
Actor
:Inventory
1: find customer()
2: search (string)
Message Activation
5: rent (movie)
6: add(Cust, item)
Lifeline
7: printInvoice() 8: generateRentalTotal()
lifeline
stimulus
name (…)
activation
new (…)
: Class
delete
return create
(to business
Customer Data «facade» system)
Business
System
Client
Class
Rental Screen Package
«library»
Interface
Application
Framework
(appfr.dll)
Dependency
«application»
Video
Workstation Note Supplied by
(vstation.exe) Microsoft
Store
Check Out Terminal Server
:Clerk Client «TCP/IP» App
AbstractFactory
declares an interface for operations that create product objects
ConcreteFactory
implements the operations to create concrete product objects
Maria Grazia Pia
Abstract Factory example