OCI - session 2: Object programming in C++ - Thibault Cholez

Feb 25, 2017 - A class can be considered as an extended concept of a data structure : instead of holding only data, it can hold both data and functions.
139KB taille 4 téléchargements 270 vues
OCI - session 2: Object programming in C++

Thibault CHOLEZ - [email protected] TELECOM Nancy - Universit´ e de Lorraine LORIA - INRIA Nancy Grand-Est From Nicolas Rougier’s C++ crash course

25/02/2017

Classes

Inheritance

Genericity with templates

Plan

1

Classes

2

Inheritance

3

Genericity with templates

OCI - session 2:, Object programming in C++ 2 / 26

Classes

Inheritance

Genericity with templates

Plan

1

Classes

2

Inheritance

3

Genericity with templates

OCI - session 2:, Object programming in C++ 3 / 26

Classes

Inheritance

Genericity with templates

Introduction

A class can be considered as an extended concept of a data structure : instead of holding only data, it can hold both data and functions. An object is an instantiation of a class. By default, all attributes and functions of a class are private (see below Access control). If you want a public default behavior, you can use keyword struct instead of keyword class in the declaration.

OCI - session 2:, Object programming in C++ 4 / 26

Classes

Inheritance

Genericity with templates

Introduction class Foo { int attribute; int function( void ) { }; }; struct Bar { int attribute; int function( void ) { }; }; Foo foo; foo.attribute = 1; // WRONG Bar bar; bar.attribute = 1;

// OK OCI - session 2:, Object programming in C++ 5 / 26

Classes

Inheritance

Genericity with templates

Constructors It is possible to specify zero, one or more constructors for the class. class Foo { public: Foo( void ) { std::cout