object oriented software design ii - retis...
TRANSCRIPT
![Page 1: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/1.jpg)
Object Oriented Software Design IIIntroduction to C++
Giuseppe Liparihttp://retis.sssup.it/~lipari
Scuola Superiore Sant’Anna – Pisa
February 20, 2012
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 1 / 54
![Page 2: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/2.jpg)
Outline
1 Course contents
2 Introduction to C++
3 Classes
4 Access Control
5 Memory layout
6 Pointers
7 Function Overloading
8 Exercise
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 2 / 54
![Page 3: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/3.jpg)
Outline
1 Course contents
2 Introduction to C++
3 Classes
4 Access Control
5 Memory layout
6 Pointers
7 Function Overloading
8 Exercise
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 3 / 54
![Page 4: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/4.jpg)
Prerequisites
To understand this course, you should at least know the basic Csyntax
functions declaration and function call,global and local variablespointers (will do again during the course)structures
First part of the course: classesClasses, objects, memory layoutPointer and referencesCopyingInheritance, multiple inheritanceAccess rulesPublic, protected and private inheritanceExceptions
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 4 / 54
![Page 5: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/5.jpg)
Summary - cont.
Second part: templatesTemplatesThe Standard Template Library
Third part: new standardWhat does it changelambda functionsautomove semanticnew STL classesSafety to exceptions
Fourth part: patternsSome patterns in C++Function objectsTemplate patternsMeta-programming with templates
Fifth part: librariesThread library, synchronizationFutures and promisesThe Active Object pattern
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 5 / 54
![Page 6: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/6.jpg)
Outline
1 Course contents
2 Introduction to C++
3 Classes
4 Access Control
5 Memory layout
6 Pointers
7 Function Overloading
8 Exercise
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 6 / 54
![Page 7: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/7.jpg)
Classes
C is not a high-level language.
– Brian Kernighan, inventor of C with D. M. Ritchie
Those types are not abstract: they are as real as int and float
– Doug McIlroy
Actually I made up the term ’object-oriented’, and I can tellyou I did not have C++ in mind.
– Alan Kay
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 7 / 54
![Page 8: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/8.jpg)
Abstraction
An essential instrument for OO programming is the support fordata abstraction
C++ permits to define new types and their operationsCreating a new data type means defining:
Which elements it is composed of (internal structure);How it is built/destroyed (constructor/destructor);How we can operate on this type (methods/operations).
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 8 / 54
![Page 9: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/9.jpg)
Data abstraction in C
We can do data abstraction in C (and in almost any language)
typedef struct __complex {double real_;double imaginary_;
} cmplx;
void add_to(cmplx *a, cmplx *b);void sub_from(cmplx *a, cmplx *b);double get_module(cmplx *a);
We have to pass the maindata to every function
name clashing: if anotherabstract type defines afunction add_to(), thenames will clash!
No information hiding: anyuser can access the internaldata using them improperly
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 9 / 54
![Page 10: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/10.jpg)
Classical example
class Complex {double real_;double imaginary_;
public:Complex();Complex(double a, double b);~Complex();
double real() const;double imaginary() const;double module() const;Complex &operator =(const Complex &a);Complex &operator+=(const Complex &a);Complex &operator-=(const Complex &a));
};
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 10 / 54
![Page 11: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/11.jpg)
How to use complex
Complex c1; // default constructorComplex c2(1,2); // constructorComplex c3(3,4); // constructor
cout << "c1=(" << c1.real() << ","<< c1.imaginary() << ")" << endl;
c1 = c2; // assignmentc3 += c1; // operator +=c1 = c2 + c3; // ERROR: operator + not yet defined
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 11 / 54
![Page 12: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/12.jpg)
Using new data types
The new data type is used just like a predefined data typeit is possible to define new functions for that type:
real(), imaginary() and module()It is possible to define new operators
=, += and -=
The compiler knows automatically which function/operator must beinvoked
C++ is a strongly typed languagethe compiler knows which function to invoke by looking at the type
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 12 / 54
![Page 13: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/13.jpg)
Outline
1 Course contents
2 Introduction to C++
3 Classes
4 Access Control
5 Memory layout
6 Pointers
7 Function Overloading
8 Exercise
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 13 / 54
![Page 14: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/14.jpg)
Class
Class is the main construct for building new types in C++A class is almost equivalent to a struct with functions inside
In the C-style programming, the programmer defines structs, andglobal functions to act on the structsIn C++-style programming, the programmer defines classes withembedded functions
class MyClass {int a;
public:int myfunction(int param);
};
Class declaration
Remember the semicolon!
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 14 / 54
![Page 15: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/15.jpg)
Members
A class contains membersA member can be
any kind of variable (member variables)any kind of function (member functions or methods)
class MyClass {int a;double b;
public:int c;
void f();int getA();int modify(double b);
};
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 15 / 54
![Page 16: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/16.jpg)
Members
A class contains membersA member can be
any kind of variable (member variables)any kind of function (member functions or methods)
class MyClass {int a;double b;
public:int c;
void f();int getA();int modify(double b);
};
member variables (private)
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 15 / 54
![Page 17: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/17.jpg)
Members
A class contains membersA member can be
any kind of variable (member variables)any kind of function (member functions or methods)
class MyClass {int a;double b;
public:int c;
void f();int getA();int modify(double b);
};
member variables (private)
member variable (public)
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 15 / 54
![Page 18: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/18.jpg)
Members
A class contains membersA member can be
any kind of variable (member variables)any kind of function (member functions or methods)
class MyClass {int a;double b;
public:int c;
void f();int getA();int modify(double b);
};
member variables (private)
member variable (public)
member functions (public)
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 15 / 54
![Page 19: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/19.jpg)
Declaring objects of a class: constructor
An object is an instance of a classAn object is created by calling a special function called constructor
A constructor is a function that has the same name of the class andno return valueIt may or may not have parameters;It is invoked in a special way
class MyClass {public:MyClass(){cout << "Constructor"<<endl;
}};
MyClass obj;
Declaration of the constructor
Invoke the constructor to create anobject
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 16 / 54
![Page 20: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/20.jpg)
Constructor - II
Constructors with parameters
class MyClass {int a;int b;
public:MyClass(int x);MyClass(int x, int y);
};
MyClass obj;MyClass obj1(2);MyClass obj2(2,3);
int myvar(2);double pi(3.14);
A class can have manyconstructors
This is an error, no constructorwithout parameters
Calls the first constructor
Calls the second constructor
Same syntax is valid for primitivedata types
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 17 / 54
![Page 21: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/21.jpg)
Default constructor
Rules for constructorsIf you do not specify a constructor, a default one with no parametersis provided by the compilerIf you provide a constructor (any constructor) the compiler will notprovide a default one for you
Constructors are used to initialise members
class MyClass {int a;int b;
public:MyClass(int x, int y){
a = x; b = 2*y;}
};
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 18 / 54
![Page 22: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/22.jpg)
Initialization list
Members can be initialised through a special syntaxThis syntax is preferable (the compiler can catch some obviousmistake)use it whenever you can (i.e. almost always)
class MyClass {int a;int b;
public:MyClass(int x, int y) :a(x), b(y)
{// other initialisation
}};
A comma separated list of constructors,following the :
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 19 / 54
![Page 23: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/23.jpg)
Accessing member objects
Members of one object can be accessed using the dot notation,similarly to structs in C
class MyClass {public:int a;int f();void g(int i, int ii);
};
MyClass x;MyClass y;
x.a = 5;y.a = 7;x.f();y.g(5, 10);
Assigning to a member variable of ob-ject x
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 20 / 54
![Page 24: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/24.jpg)
Accessing member objects
Members of one object can be accessed using the dot notation,similarly to structs in C
class MyClass {public:int a;int f();void g(int i, int ii);
};
MyClass x;MyClass y;
x.a = 5;y.a = 7;x.f();y.g(5, 10);
Assigning to a member variable of ob-ject x
Assigning to a member variable of ob-ject y
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 20 / 54
![Page 25: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/25.jpg)
Accessing member objects
Members of one object can be accessed using the dot notation,similarly to structs in C
class MyClass {public:int a;int f();void g(int i, int ii);
};
MyClass x;MyClass y;
x.a = 5;y.a = 7;x.f();y.g(5, 10);
Assigning to a member variable of ob-ject x
Assigning to a member variable of ob-ject y
Calling member function f() of object x
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 20 / 54
![Page 26: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/26.jpg)
Accessing member objects
Members of one object can be accessed using the dot notation,similarly to structs in C
class MyClass {public:int a;int f();void g(int i, int ii);
};
MyClass x;MyClass y;
x.a = 5;y.a = 7;x.f();y.g(5, 10);
Assigning to a member variable of ob-ject x
Assigning to a member variable of ob-ject y
Calling member function f() of object x
Calling member function g() of object y
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 20 / 54
![Page 27: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/27.jpg)
Implementing member functions
You can implement a member function (including constructors) ina separate .cpp file
complex.h
class Complex {double real_;double img_;
public:...double module() const;...
};
complex.cpp
double Complex::module(){double temp;temp = real_ * real_ +
img_ * img_;return temp;
}
This is preferable most of the timesput implementation in include files only if you hope to use in-liningoptimisation
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 21 / 54
![Page 28: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/28.jpg)
Accessing internal members
double Complex::module() const{double temp;temp = real_ * real_ + img_ * img_;return temp;
}
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 22 / 54
![Page 29: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/29.jpg)
Accessing internal members
double Complex::module() const{double temp;temp = real_ * real_ + img_ * img_;return temp;
}
scope resolution
The :: operator is called scope resolution operator
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 22 / 54
![Page 30: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/30.jpg)
Accessing internal members
double Complex::module() const{double temp;temp = real_ * real_ + img_ * img_;return temp;
}
scope resolution
The :: operator is called scope resolution operator
like any other function, we can create local variables
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 22 / 54
![Page 31: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/31.jpg)
Accessing internal members
double Complex::module() const{double temp;temp = real_ * real_ + img_ * img_;return temp;
}
scope resolution
access to internal variable
The :: operator is called scope resolution operator
like any other function, we can create local variables
member variables and functions can be accessed without dot orarrow
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 22 / 54
![Page 32: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/32.jpg)
Outline
1 Course contents
2 Introduction to C++
3 Classes
4 Access Control
5 Memory layout
6 Pointers
7 Function Overloading
8 Exercise
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 23 / 54
![Page 33: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/33.jpg)
Access control
A member can be:private: only member functions of the same class can access it;other classes or global functions can’tprotected: only member functions of the same class or of derivedclasses can access it: other classes or global functions can’tpublic: every function can access it
class MyClass {private:
int a;public:
int c;};
MyClass data;
cout << data.a; // ERROR!cout << data.c; // OK
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 24 / 54
![Page 34: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/34.jpg)
Access control
Default is private
An access control keyword defines access until the next accesscontrol keyword
class MyClass {int a;double b;
public:int c;
void f();int getA();
private:int modify(double b);
};
private (default)
public
private again
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 25 / 54
![Page 35: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/35.jpg)
Access control and scope
int xx;
class A {int xx;
public:void f();
};
void A::f(){
xx = 5;::xx = 3;
xx = ::xx + 2;}
global variable
member variable
access local xx
access global xx
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 26 / 54
![Page 36: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/36.jpg)
Why access control?
The technique of declaring private members is also calledencapsulation
In this way we can precisely define what is interface and what isimplementationThe public part is the interface to the external worldThe private part is the implementation of that interfaceWhen working in a team, each group take care of a moduleTo ensure that the integration is done correctly and withoutproblems, the programmers agree on interfaces
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 27 / 54
![Page 37: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/37.jpg)
Private
Some people think that private is synonym of secretthey complain that the private part is visible in the header file
private means not accessible from other classes and does notmean secret
The compiler needs to know the size of the object, in order toallocate memory to it
In an hypothetical C++, if we hide the private part, the compilercannot know the size of the object
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 28 / 54
![Page 38: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/38.jpg)
Friends
class A {friend class B;int y;void f();
public:int g();
};
class B {int x;
public:void f(A &a);
};
void B::f(A &a){
x = a.y;a.f();
}
B is friend of A
B can access private members of A
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 29 / 54
![Page 39: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/39.jpg)
Friend functions and operator
Even a global function or a single member function can be friendof a class
class A {friend B::f();friend h();int y;void f();
public:int g();
};
friend member function
friend global function
It is better to use the friend keyword only when it is reallynecessary because it breaks the access rules.
"Friends, much as in real life, are often more trouble than theirworth." – Scott Meyers
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 30 / 54
![Page 40: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/40.jpg)
Nested classes
It is possible to declare a class inside another class
Access control keywords apply
class A {class B {
int a;public:
int b;}B obj;
public:void f();
};
Class B is private to class A: itis not part of the interface of A,but only of its implementation.
However, A is not allowed toaccess the private part of B!!(A::f() cannot accessB::a).
To accomplish this, we have todeclare A as friend of B
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 31 / 54
![Page 41: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/41.jpg)
Outline
1 Course contents
2 Introduction to C++
3 Classes
4 Access Control
5 Memory layout
6 Pointers
7 Function Overloading
8 Exercise
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 32 / 54
![Page 42: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/42.jpg)
Memory layout
Let us recapitulate the rules for the lifetime and visibility ofvariables
Global variables are defined outside of any function. Their lifetimeis the duration of the program: they are created when the programis loaded in memory, and deleted when the program exitsLocal variables are defined inside functions or inside code blocks(delimited by curly braces { and }). Their lifetime is the execution ofthe block: they are created before the block starts executing, anddestroyed when the block completes execution
Global and local variables are in different memory segments,and are managed in different ways
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 33 / 54
![Page 43: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/43.jpg)
Memory segments
The main data segments of a program are shown below
The BSS segment containsglobal variables. It isdivided into two segments,one for initialised data (i.e.data that is initialised whendeclared), and non-initialiseddata.
The size of this segment isstatically decided when theprogram is loaded inmemory, and can neverchange during execution
BSS
STACK
HEAP
Global variables
Local variables
Dynamic memory
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 34 / 54
![Page 44: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/44.jpg)
Memory segments
The main data segments of a program are shown below
The STACK segmentcontains local variables
Its size is dynamic: it cangrow or shrink, dependingon how many localvariables are in the currentblock
BSS
STACK
HEAP
Global variables
Local variables
Dynamic memory
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 34 / 54
![Page 45: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/45.jpg)
Memory segments
The main data segments of a program are shown below
The HEAP segment containsdynamic memory that ismanaged directly by theprogrammer
BSS
STACK
HEAP
Global variables
Local variables
Dynamic memory
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 34 / 54
![Page 46: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/46.jpg)
Example
Here is an example:
int a = 5; // initialised global dataint b; // non initialised global data
int f(int i) // i, d and s[] are local variables{ // will be created on the stack when
double d; // function f() is invokedchar s[] = "Lipari";...
}
int main(){
int s, z; // local variables, are created on the stack// when the program starts
f(); // here f() is invoked, so the stack for f() is created}
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 35 / 54
![Page 47: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/47.jpg)
Outline
1 Course contents
2 Introduction to C++
3 Classes
4 Access Control
5 Memory layout
6 Pointers
7 Function Overloading
8 Exercise
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 36 / 54
![Page 48: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/48.jpg)
Pointer
A pointer is a variable that can hold a memory addressBasic syntax:
int a = 5;int b = 7;int *p;
p = &a;
cout << p << endl;
cout << *p << endl;
*p = 6;
p = &b;
cout << *p << endl;
Declaration of a pointer to an integer vari-able
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 37 / 54
![Page 49: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/49.jpg)
Pointer
A pointer is a variable that can hold a memory addressBasic syntax:
int a = 5;int b = 7;int *p;
p = &a;
cout << p << endl;
cout << *p << endl;
*p = 6;
p = &b;
cout << *p << endl;
Declaration of a pointer to an integer vari-able
p takes the address of a
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 37 / 54
![Page 50: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/50.jpg)
Pointer
A pointer is a variable that can hold a memory addressBasic syntax:
int a = 5;int b = 7;int *p;
p = &a;
cout << p << endl;
cout << *p << endl;
*p = 6;
p = &b;
cout << *p << endl;
Declaration of a pointer to an integer vari-able
p takes the address of a
print the address
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 37 / 54
![Page 51: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/51.jpg)
Pointer
A pointer is a variable that can hold a memory addressBasic syntax:
int a = 5;int b = 7;int *p;
p = &a;
cout << p << endl;
cout << *p << endl;
*p = 6;
p = &b;
cout << *p << endl;
Declaration of a pointer to an integer vari-able
p takes the address of a
print the address
prints the value in a
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 37 / 54
![Page 52: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/52.jpg)
Pointer
A pointer is a variable that can hold a memory addressBasic syntax:
int a = 5;int b = 7;int *p;
p = &a;
cout << p << endl;
cout << *p << endl;
*p = 6;
p = &b;
cout << *p << endl;
Declaration of a pointer to an integer vari-able
p takes the address of a
print the address
prints the value in a
changes the value in a = 6
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 37 / 54
![Page 53: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/53.jpg)
Pointer
A pointer is a variable that can hold a memory addressBasic syntax:
int a = 5;int b = 7;int *p;
p = &a;
cout << p << endl;
cout << *p << endl;
*p = 6;
p = &b;
cout << *p << endl;
Declaration of a pointer to an integer vari-able
p takes the address of a
print the address
prints the value in a
changes the value in a = 6
now p points to b
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 37 / 54
![Page 54: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/54.jpg)
Pointer
A pointer is a variable that can hold a memory addressBasic syntax:
int a = 5;int b = 7;int *p;
p = &a;
cout << p << endl;
cout << *p << endl;
*p = 6;
p = &b;
cout << *p << endl;
Declaration of a pointer to an integer vari-able
p takes the address of a
print the address
prints the value in a
changes the value in a = 6
now p points to b
prints the value in b
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 37 / 54
![Page 55: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/55.jpg)
Arrays
The name of an array is equivalent to a constant pointer to the firstelementWith non-const pointers we can do pointer arithmetic
char name[] = "Giuseppe";
cout << *name << endl;
char *p = name;
p++;
assert(p == name+1);
while (*p != 0)cout << *(p++);
cout << endl;
prints “G”
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 38 / 54
![Page 56: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/56.jpg)
Arrays
The name of an array is equivalent to a constant pointer to the firstelementWith non-const pointers we can do pointer arithmetic
char name[] = "Giuseppe";
cout << *name << endl;
char *p = name;
p++;
assert(p == name+1);
while (*p != 0)cout << *(p++);
cout << endl;
prints “G”
declares a pointer to the first element of thearray
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 38 / 54
![Page 57: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/57.jpg)
Arrays
The name of an array is equivalent to a constant pointer to the firstelementWith non-const pointers we can do pointer arithmetic
char name[] = "Giuseppe";
cout << *name << endl;
char *p = name;
p++;
assert(p == name+1);
while (*p != 0)cout << *(p++);
cout << endl;
prints “G”
declares a pointer to the first element of thearray
Increments the pointer, now points to “i”
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 38 / 54
![Page 58: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/58.jpg)
Arrays
The name of an array is equivalent to a constant pointer to the firstelementWith non-const pointers we can do pointer arithmetic
char name[] = "Giuseppe";
cout << *name << endl;
char *p = name;
p++;
assert(p == name+1);
while (*p != 0)cout << *(p++);
cout << endl;
prints “G”
declares a pointer to the first element of thearray
Increments the pointer, now points to “i”
this assertion is correct
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 38 / 54
![Page 59: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/59.jpg)
Arrays
The name of an array is equivalent to a constant pointer to the firstelementWith non-const pointers we can do pointer arithmetic
char name[] = "Giuseppe";
cout << *name << endl;
char *p = name;
p++;
assert(p == name+1);
while (*p != 0)cout << *(p++);
cout << endl;
prints “G”
declares a pointer to the first element of thearray
Increments the pointer, now points to “i”
this assertion is correct
zero marks the end of the string
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 38 / 54
![Page 60: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/60.jpg)
Arrays
The name of an array is equivalent to a constant pointer to the firstelementWith non-const pointers we can do pointer arithmetic
char name[] = "Giuseppe";
cout << *name << endl;
char *p = name;
p++;
assert(p == name+1);
while (*p != 0)cout << *(p++);
cout << endl;
prints “G”
declares a pointer to the first element of thearray
Increments the pointer, now points to “i”
this assertion is correct
zero marks the end of the string
prints the content of address pointed by p,and increments it
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 38 / 54
![Page 61: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/61.jpg)
Dynamic memory
Dynamic memory is managed by the userIn C:
to allocate memory, call function mallocto deallocate, call freeBoth take pointers to any type, so they are not type-safe
In C++to allocate memory, use operator newto deallocate, use operator deletethey are more type-safe
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 39 / 54
![Page 62: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/62.jpg)
The new operator
The new and delete operators can be applied to primitive types,and classes
operator new automatically calculates the size of memory to beallocated
int *p = new int(5);
class A { ... };
A *q = new A();
delete p;
delete q;
Allocates an integer pointed by p
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 40 / 54
![Page 63: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/63.jpg)
The new operator
The new and delete operators can be applied to primitive types,and classes
operator new automatically calculates the size of memory to beallocated
int *p = new int(5);
class A { ... };
A *q = new A();
delete p;
delete q;
Allocates an integer pointed by p
Does two things:1) Allocates memory for an object of class A2) calls the constructor of A()
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 40 / 54
![Page 64: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/64.jpg)
The new operator
The new and delete operators can be applied to primitive types,and classes
operator new automatically calculates the size of memory to beallocated
int *p = new int(5);
class A { ... };
A *q = new A();
delete p;
delete q;
Allocates an integer pointed by p
Does two things:1) Allocates memory for an object of class A2) calls the constructor of A()
Deallocates the memory pointed by p
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 40 / 54
![Page 65: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/65.jpg)
The new operator
The new and delete operators can be applied to primitive types,and classes
operator new automatically calculates the size of memory to beallocated
int *p = new int(5);
class A { ... };
A *q = new A();
delete p;
delete q;
Allocates an integer pointed by p
Does two things:1) Allocates memory for an object of class A2) calls the constructor of A()
Deallocates the memory pointed by p
Does two things:1) Calls the destructor for A2) deallocates the memory pointed by q
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 40 / 54
![Page 66: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/66.jpg)
Destructor
The destructor is called just before the object is deallocated.
It is always called both for all objects (allocated on the stack, inglobal memory, or dynamically)
If the programmer does not define a constructor, the compilerautomatically adds one by default (which does nothing)
Syntax
class A {...
public:A() { ... } // constructor~A() { ... } // destructor
};
The destructor nevertakes any parameter
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 41 / 54
![Page 67: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/67.jpg)
Example
See ./examples/01.summary-examples/destructor.cpp
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 42 / 54
![Page 68: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/68.jpg)
Why a destructor
A destructor is useful when an object allocates memory
so that it can deallocate it when the object is deleted
class A { ... };
class B {A *p;
public:B() {
p = new A();}~B() {
delete p;}
};
p is initialised when theobject is created
The memory isdeallocated when theobject is deleted
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 43 / 54
![Page 69: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/69.jpg)
New and delete for arrays
To allocate an array, use this form
int *p = new int[5]; // allocates an array of 5 int...delete [] p; // notice the delete syntax
A *q = new A[10]; // allocates an array of 10... // objects of type Adelete [] q;
In the second case, the default constructor is called to build the 10objects
Therefore, this can only be done is a default constructor (withoutarguments) is available
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 44 / 54
![Page 70: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/70.jpg)
Null pointer
The address 0 is an invalid address(no data and no function can be located at 0)
therefore, in C/C++ a pointer to 0 is said to be a null pointer, whichmeans a pointer that points to nothing.
Dereferencing a null pointer is always a bad error (null pointerexception, or segmentation fault)In C, the macro NULL is used to mark 0, or a pointer to 0
however, 0 can be seen to be of integer type, or a null pointer
In the new C++, the null pointer is indicated with the constantnullptr
this constant cannot be automatically converted to an integer
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 45 / 54
![Page 71: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/71.jpg)
Outline
1 Course contents
2 Introduction to C++
3 Classes
4 Access Control
5 Memory layout
6 Pointers
7 Function Overloading
8 Exercise
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 46 / 54
![Page 72: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/72.jpg)
Function overloading
In C++, the argument list is part of the name of the functionthis mysterious sentence means that two functions with the samename but with different argument list are considered two differentfunctions and not a mistake
If you look at the internal name used by the compiler for afunction, you will see three parts:
the class namethe function namethe argument list
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 47 / 54
![Page 73: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/73.jpg)
Function overloading
class A {public:
void f(int a);void f(int a, int b);void f(double g);
};class B {public:
void f(int a);};
__A_f_int
__A_f_int_int
__A_f_double
__B_f_int
To the compiler, they are all different functions!
beware of the type...
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 48 / 54
![Page 74: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/74.jpg)
Which one is called?
class A {public:
void f(int a);void f(int a, int b);void f(double g);
};class B {public:
void f(int a);};
A a;B b;
a.f(5);
b.f(2);
a.f(3.0);a.f(2,3);a.f(2.5, 3);
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 49 / 54
![Page 75: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/75.jpg)
Which one is called?
class A {public:
void f(int a);void f(int a, int b);void f(double g);
};class B {public:
void f(int a);};
A a;B b;
a.f(5);
b.f(2);
a.f(3.0);a.f(2,3);a.f(2.5, 3);
__A_f_int
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 49 / 54
![Page 76: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/76.jpg)
Which one is called?
class A {public:
void f(int a);void f(int a, int b);void f(double g);
};class B {public:
void f(int a);};
A a;B b;
a.f(5);
b.f(2);
a.f(3.0);a.f(2,3);a.f(2.5, 3);
__A_f_int
__B_f_int
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 49 / 54
![Page 77: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/77.jpg)
Which one is called?
class A {public:
void f(int a);void f(int a, int b);void f(double g);
};class B {public:
void f(int a);};
A a;B b;
a.f(5);
b.f(2);
a.f(3.0);a.f(2,3);a.f(2.5, 3);
__A_f_int
__B_f_int
__A_f_double
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 49 / 54
![Page 78: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/78.jpg)
Which one is called?
class A {public:
void f(int a);void f(int a, int b);void f(double g);
};class B {public:
void f(int a);};
A a;B b;
a.f(5);
b.f(2);
a.f(3.0);a.f(2,3);a.f(2.5, 3);
__A_f_int
__B_f_int
__A_f_double
__A_f_int_int
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 49 / 54
![Page 79: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/79.jpg)
Which one is called?
class A {public:
void f(int a);void f(int a, int b);void f(double g);
};class B {public:
void f(int a);};
A a;B b;
a.f(5);
b.f(2);
a.f(3.0);a.f(2,3);a.f(2.5, 3);
__A_f_int
__B_f_int
__A_f_double
__A_f_int_int
__A_f_int_int
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 49 / 54
![Page 80: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/80.jpg)
Return values
Notice that return values are not part of the namethe compiler is not able to distinguish two functions that differs onlyon return values
class A {int floor(double a);double floor(double a);
};
This causes a compilation error
It is not possible to overload a return value
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 50 / 54
![Page 81: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/81.jpg)
Default arguments in functions
Sometime, functions have long argument lists
Some of these arguments do not change often
We would like to set default values for some argumentThis is a little different from overloading, since it is the samefunction we are calling!
int f(int a, int b = 0);
f(12); // it is equivalent to f(12,0);
The combination of overloading with default arguments can beconfusing
it is a good idea to avoid overusing both of them
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 51 / 54
![Page 82: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/82.jpg)
Outline
1 Course contents
2 Introduction to C++
3 Classes
4 Access Control
5 Memory layout
6 Pointers
7 Function Overloading
8 Exercise
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 52 / 54
![Page 83: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/83.jpg)
Time to do an example
Let us implement a Stack of integers class
Stack stack;...stack.push(12);stack.push(7);...cout << stack.pop();cout << stack.pop();
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 53 / 54
![Page 84: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/84.jpg)
Interface
class Stack {...
public:Stack(int maxsize);~Stack();
void push(int a);int pop();int peek();int size();
};
Constructor: maxsize is the max-imum number of elements on thestack
Hint: Use an array to store the elements
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 54 / 54
![Page 85: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/85.jpg)
Interface
class Stack {...
public:Stack(int maxsize);~Stack();
void push(int a);int pop();int peek();int size();
};
Constructor: maxsize is the max-imum number of elements on thestack
Destructor
Hint: Use an array to store the elements
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 54 / 54
![Page 86: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/86.jpg)
Interface
class Stack {...
public:Stack(int maxsize);~Stack();
void push(int a);int pop();int peek();int size();
};
Constructor: maxsize is the max-imum number of elements on thestack
Destructor
Returns the top element
Hint: Use an array to store the elements
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 54 / 54
![Page 87: Object Oriented Software Design II - ReTiS Labretis.sssup.it/~lipari/courses/oosd2011-2/01.summary.pdf · Outline 1 Course contents 2 Introduction to C++ 3 Classes 4 Access Control](https://reader034.vdocuments.net/reader034/viewer/2022042313/5ee0ca27ad6a402d666be5d5/html5/thumbnails/87.jpg)
Interface
class Stack {...
public:Stack(int maxsize);~Stack();
void push(int a);int pop();int peek();int size();
};
Constructor: maxsize is the max-imum number of elements on thestack
Destructor
Returns the top element
Returns the current number of ele-ments
Hint: Use an array to store the elements
G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 20, 2012 54 / 54