Design objectives in C++

Object-Oriented Programming
Part 1

INTRODUCTION

Over time, data abstraction has become essential as programs became complicated.
Benefits:
1. Reduce conceptual load (minimum detail).
2. Fault containment.
3. Independent program components.
(difficult in practice).

Code reuse possible by extending and refining abstractions.
A methodology of programming
Four (Five ?) major principles:

Data Abstraction.
Encapsulation.
Information Hiding.
Polymorphism (dynamic binding).
Inheritance. (particular case of polymorphism ?)
Will describe these using C++, because …

THE C++ LANGUAGE

An object-oriented, general-purpose programming language, derived from C (C++ = C plus classes).

C++ adds the following to C:

Inlining and overloading of functions.
Default argument values.
Argument pass-by-reference.
Free store operators new and delete, instead of malloc() and free().
Support for object-oriented programming, through classes, information hiding, public interfaces, operator overloading, inheritance, and templates.

DESIGN OBJECTIVES IN C++
Compatibility. Existing code in C can be used. Even existing, pre-compiled libraries can be linked with new C++ code.
Efficiency. No additional cost for using C++. Overheadof function calls is eliminated where possible.
Strict type checking. Aids debugging, allows generation of efficient code.
C++ designed by Bjarne Stroustrup of Bell Labs (now at TAMU).
Standardization: ANSI, ISO.
Major improvements over C.
Stream I/O.
Strong typing.
Parameter passing by reference.
Default argument values.
Inlining.
We’ve discussed some of these already.

STREAM I/O IN C++

Input and output in C++ is handled by streams.
The directive #include declares 2 streams: cin and cout.
cin is associated with standard input. Extraction: operator>>.
cout is associated with standard output. Insertion: operator<<.
In C++, input is line buffered, i.e. the user must press before any characters are processed.

EXAMPLE OF STREAM I/O
A function that returns the sum of
the numbers in the file Number.in

int fileSum();
{
ifstream infile(« Number.in »);
int sum = 0;
int value;
//read until non-integer or
while(infile >> value)
sum = sum + value;
return sum;
}

Cours gratuitTélécharger le cours complet

Télécharger aussi :

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *