object-oriented programming, classes
TRANSCRIPT
I. Hrivnacova @ Data Processing Course 2021 1
Object-Oriented Programming, Classes
Data Processing Course, I. Hrivnacova, IJCLab Orsay
● Object-Oriented vs. Procedural Programming
● Classes and Instances● Class Definition● Using A Class● Data Encapsulation● Header and Implementation
files
I. Hrivnacova @ Data Processing Course 2021 2
Procedural Programming
● Traditional procedural-oriented programming language: C, Fortran, Cobol, or Pascal– The programs are made up of functions.
Functions are often not reusable.– The procedural languages are not suitable
of high-level abstraction for solving real life problems.
● The data structures and algorithms of the software entities are separated.
● The traditional procedural language forces you to think in terms of the structure of the computer (e.g. memory bits and bytes, array, decision, loop)
Chua Hock-Chuan: Programming Notes
I. Hrivnacova @ Data Processing Course 2021 3
Object-Oriented Programming (OOP)
● The basic unit of OOP is a class● A class is an implementation of
an abstract data type:– It describes both the attributes
(data) of an object and its operations (methods)
● The OOP languages let you think in the problem space, and use software objects to represent and abstract entities of the problem space to solve the problem.
Chua Hock-Chuan: Programming Notes
I. Hrivnacova @ Data Processing Course 2021 4
OOP Example
● As an example: a computer soccer games
● Modeling accordingly to the "real things":– Player:
● Attributes: name, number, location in the field, and etc.
● Operations: run, jump, kick-the-ball, and etc.
– Ball, Field, Referee, Audience, Weather
● Some of these classes (Ball, Audience) can be reused in another application, e.g., computer basketball game
Chua Hock-Chuan: Programming Notes
I. Hrivnacova @ Data Processing Course 2021 5
A Class
A class can be visualized as a three-compartment box, as illustrated:
● Classname (or identifier): identifies the class.● Data Members (or variables, attributes, states,
fields): – contain the static attributes of the class.
● Member Functions (or methods, behaviors, operations): – contain the dynamic operations of the class.
● In other words, a class encapsulates the static attributes (data) and dynamic behaviors (operations that operate on the data) in a box.
● Class Members: The data members and member functions are collectively called class members.
Chua Hock-Chuan: Programming Notes
I. Hrivnacova @ Data Processing Course 2021 6
Example Of Classes, Instances
● The class and instances diagrams are drawn according to the Unified Modeling Language (UML) notations.
Chua Hock-Chuan: Programming Notes
I. Hrivnacova @ Data Processing Course 2021 7
Class Definition
● A car can be described using properties– such as mileage, license plate
and year of manufacture.– These attributes indicate the
object, its state
● The car object is described also with the operations, its behaviour– A car can be driven from A to B,
we can query mileage or year of manufacture.
class Car { int yearOfManufacture; float mileage; int licensePlate;
int getYearOfManufacture(); void drive(float m); float getMileage();
void Car(); void ~Car();};
I. Hrivnacova @ Data Processing Course 2021 8
Using A Class
int main(){ Car car;
car.drive(77); float mileage = car.getMileage();
cout << “Car mileage is: “ << mileage << endl;}
create car and initialize it
drive 77 miles
query mileage
print mileage on the screen
Object-oriented syntax for operations that are defined for the objects of a class (we have seen it in the “Strings” presentation for the first time): objectName.function()
I. Hrivnacova @ Data Processing Course 2021 9
A Car as a Class
class Car { int yearOfManufacture; int mileage; int licensePlate;
int getYearOfManufacture(); void drive(float m); float getMileage();
void Car(); void ~Car();};
attributes
operations
Special functions which can be used to influence the behaviour of the car during construction and destruction:
● The year of manufacture, milage and license plate will be initialized in Car()
I. Hrivnacova @ Data Processing Course 2021 10
Classes
● A class is a definition of objects of the same kind.– In other words, a class is a blueprint, template, or prototype that defines and
describes the static attributes and dynamic behaviors common to all objects of the same kind.
● An instance is a realization of a particular item of a class.– In other words, an instance is an instantiation of a class.– All the instances of a class have similar properties, as described in the class
definition. – For example, you can define a class called "Student" and create three instances
of the class "Student" for "Peter", "Paul" and "Pauline".
● The term "object" usually refers to instance. But it is often used quite loosely, which may refer to a class or an instance.
I. Hrivnacova @ Data Processing Course 2021 11
Data Encapsulation
● To prevent any application programmer from doing anything they want with an object, access to an object is restricted via a well defined interface
● An application programmer can only call the functions for which the designer of the class granted public access
● C++: keyword public:, private:– Allow to define the access to class data and
operations
I. Hrivnacova @ Data Processing Course 2021 12
Data Encapsulation
class Car { private: int yearOfManufacture; int mileage; int licensePlate;
public: int getYearOfManufacture(); void drive(float m); float getMileage();
void Car(); void ~Car();};
The members declared afterprivate are protected from being accessed by applicationprogramers
For the application developeronly the public interface is of interest.
I. Hrivnacova @ Data Processing Course 2021 13
Using A Class
int main(){ Car car;
car.drive(77); float mileage = car.getMileage();
cout << “Car mileage is: “ << mileage << endl;}
create car and initialize it
drive 77 miles
query mileage
print mileage on the screen
Only functions declared in the public: area can be called in a client code(for example in the main() function)● Calling a function declared the private: area would cause a compilation error
I. Hrivnacova @ Data Processing Course 2021 14
Separating Header and Implementation
● For better software engineering the class declaration and implementation are usually kept be kept in 2 separate files: – Declaration is a header file ".h"– Implementation in a ".cxx".
● This is known as separating the public interface (header declaration) and the implementation.
● Interface is defined by the designer, implementation can be supplied by others. – Only the header files are exposed to the users, the implementation can be
provided in an object file ".o" (or in a library).
● The source code needs not to be given to the users.
I. Hrivnacova @ Data Processing Course 2021 15
Reminder: Functions and Modules
getArea.h
getArea.cxx testFunction.cxx
// Function Prototype double getArea(double radius);
#include ”getArea.h”// Function Definitiondouble getArea(double radius) { return radius*radius*PI;}
#include ”getArea.h”// 'testFunction’ programint main() { double area1 = getArea(radius1); // ...}
Header File- extension .h
Source Files- extension .cxx
I. Hrivnacova @ Data Processing Course 2021 16
Classes and Modules
Car.h
Car.cxx testCar.cxx
#include ”Car.h”// 'testCar’ programint main() { // ...}
Header File- extension .h
Source Files- extension .cxx
// Class Car Definition// ...
// Class Car Implementation// ...
I. Hrivnacova @ Data Processing Course 2021 17
Compiling & Linking
Car.cxx
Car.h
testCar.cxx
include
include
compiler
compiler Car.o
testCar.o
testCar
linker
linker
● The header file (.h) is included by both the source file with the class implementation and by the application source file
● Both source files (.cxx) are converted into object files (.o) by the C++ compiler ● The object files are then linked to the executable program (testCircle) by a
linker
I. Hrivnacova @ Data Processing Course 2021 18
Reminder - C++ Preprocessor
● Compilation of C++ code is preceded by preprocessing via the preprocessor, which executes the preprocessor commands (directives) present in the code.
● The commands of preprocessor begin with #
● Conditional compilation of header files
#ifndef getArea_h
#define getArea_h
// Function Prototype
double getArea(double radius);
#endif
● Including other files: #include <iostream>
● Conditional compilation: #ifdef DEBUG
cout <<"Variable x = " << x << endl;
#endif
I. Hrivnacova @ Data Processing Course 2021 19
Reminder: Variable
● It allocates memory● The current memory content
is its value ● counter has the value 0● The size of memory
allocated and the operations we can do with a variable are defined by its type
counter 0 int
NAME VALUE TYPE
number 1556 int
pi 3.141592 double
A variable has a name, stores a value of the declared type
I. Hrivnacova @ Data Processing Course 2021 20
Reminder: Variable Type● Defines how the object (its binary configuration in memory) can be manipulated● Fundamental types
– Numeric: integer (int), floating point numbers (float, double)
– Characters: char● Enclosed in single quotes: 'a', '0'● Special characters must be masked with a backslash: \', \”
– Boolean: bool: can have only two values: true, false
● Object types– Defined in the C++ standard library (for example string, vector, ..) and by the programmer,
for example the class Car● Have their own defined functions; can also have their own defined operators
● enum: for enumeration types (names that represent integral value) ● void: “nothing” (for functions without a return value)
I. Hrivnacova @ Data Processing Course 2021 21
Reminder: Variable & Type
i 0 int
NAME VALUE TYPE
name “Agatha” string
car 30000, 2016 Car
A variable has a name, stores a value of the declared type
C++ CODE
Car car;
string name = “Agatha”;
{ 1, 2, 3 } vector<int>
int i; int i = 0;
myVector vector<int> myVector;
I. Hrivnacova @ Data Processing Course 2021 22
OOP Benefits
● Ease in software design as you could think in the problem space rather than the machine's bits and bytes.– You are dealing with high-level concepts and abstractions. Ease in
design leads to more productive software development.
● Ease in software maintenance: object-oriented software are easier to understand, therefore easier to test, debug, and maintain.
● Reusable software: you don't need to keep re-inventing the wheels and re-write the same functions for different situations– The fastest and safest way of developing a new application is to reuse
existing codes - fully tested and proven codes.