C++ programming professional Notes
Contents
THEORETICAL BASES . INTRODUCTION TO THE POO................................................ 1
I NTRODUCTION
PROGRAMMING P ARADIGMS
P ROGRAMMING I MPERATIVA
Data types 2
Operators and expressions 3
Algorithms and control structures 4
Functions and procedures 4
Constants and variables 5
P ROGRAMMING M odular
T IPOS A BSTRACTOS OF D ATOS
P ROGRAMING O RENTED TO O BJETOS
Objects and messages 7
Classes 7
Inheritance and polymorphism 7
Programming with objects 7
THE C ++ LANGUAGE ..................................................................................... 9
I NTRODUCTION
BASIC C ONCEPTS
Program structure 9
Data types and operators 10
Control structures 11
Functions 12
Modular programming support 12
Support for Abstract Data Types 13
Support for Object Oriented programming 13
T IPOS DATA , OPERATORS AND EXPRESSIONS
Data types 13
Elementary types 13
Listed types 14
Derived types 15
Compound types 16
Constants (literals) 18
Variables 19
Type conversions 20
Operators and expressions 21
E STRUCTURES OF CONTROL
Selection structures 24
Repeating structures 26
Jumping structures 27
F UNCTIONS
Function declaration 28
Defining functions 28
Parameter step 29
Array parameters 29
Return of values 30
Function overload 30
Default parameters 30
Undefined parameters 31
Recursion 32
Pointers to functions 33
The main () function 33
DYNAMIC V ARIABLES
Pointers and directions 34
The NULL pointer 35
Pointers void 35
Pointer Arithmetic 36
Function pointers and parameters 37
iii
Pointers and arrays 37
New and delete operators 38
Pointers and structures 39
Pointers to Pointers 39
EFFICIENT P ROGRAMMING
Program structure 40
Preprocessor 42
Inline functions 43
Inclusion of routines in assembler 43
Efficiency and clarity of programs 44
C LASES
Introduction 44
Classes and Members 45
Static Methods and Friendly Functions 50
Construction and destruction 51
H Erencia AND POLYMORPHISM
Derived classes or subclasses 56
Abstract classes 60
Multiple inheritance 61
Access control 63
Memory management 64
S OVERLOADING OPERATORS
Operator functions 64
Type Conversions 66
Operators and large objects 67
Assignment and Initialization 67
Subscripts 68
Function calls 68
Reference 69
Increase and decrease 69
New and delete overload 69
Friendly functions or methods 70
T EMPLATES
Genericity 70
Generic functions 71
Generic classes 72
M ANAGEMENT OF E xceptions
Programming and errors 73
Exception handling in C ++ (throw - catch - try) 73
E NTRADA AND S ALIDA
Introduction 76
Stream Objects 76
Entry and exit 76
Files 79
P ROGRAMMING IN C ++ 80
The development process 80
Maintainability and documentation. 80
Design and implementation 81
Choice of classes 81
Interfaces and implementation 81
L IBRERAS CLASS
Library Design 82
Classes Container 82
Classes for applications 83
Interface Classes 83
Time efficiency and memory management 83
Standardization 84
R ELATION C / C ++ 84
Cannot be used in ANSI C 84
Differences between C and C ++ 85
iv
Basic bibliography
- Brian W. Kernighan, Dennis M. Ritchie, The C Programming Language, Second Edition , Prentice Hall, 1988
- Bjarne Stroustrup, The C ++ Programming Language, Second Edition , Addison-Wesley, 1991
- Enrique Hernndez Orallo, Jos Hernndez Orallo, C ++ Programming , Paraninfo, 1993
Theoretical bases. Introduction to OOP.
I NTRODUCTION
To start studying any programming language, you must know what concepts it supports, that is, the type of programming that we will be able to carry out with it. As C ++ incorporates new features with respect to languages such as Pascal or C, in the first place we will give a description of the concepts that this language supports, reviewing the programming paradigms and focusing on the evolution from Functional programming to Program-Oriented programming. Objects. Later we will study the language in the same way, first we will see its functional characteristics (actually the part that the language inherits from C) and then we will study the extensions that support object-oriented programming (the ++ of C ++).
PROGRAMMING P ARADIGMS
According to the concepts on which a programming language is based, we have different ways of approaching problem solving and different programming styles. We can classify programming languages into several types:
- Imperatives
- Object Oriented
- Functional
- Logic
The first two options are based on the abstraction of data types. Basically, it is about representing the variable characteristics of objects using types that the computer can handle, such as integers or alphanumeric characters. Our program will be a collection of algorithms that operate on the data that we have modeled. The difference between the two approximations will be seen in later points.
Functional languages, contrary to imperatives, completely eliminate the idea of data type, they limit themselves to treating all data as symbols and emphasize the operations that we can apply on these symbols, grouped in lists or trees. It is important to note that in these languages only the concept of function applied to symbols is used, one of its main characteristics being the use of recursive functions. As an example of this type of language we could cite the LISP .
Logical languages are those that work directly with formal logic, it is about representing relationships between sets, in order to later be able to determine if certain predicates are verified. The most widely used logical language is Prolog .
P ROGRAMMING I MPERATIVA
As we have already mentioned, imperative programming deals with data types and algorithms, the former represent the information used by the programs, while the latter refer to the way in which we treat that information.
In the following points we will briefly review the fundamental concepts of classical imperative programming, also called procedural programming . The basic idea of this approach is to define the most efficient algorithms or procedures to treat the data of our problem.
Types of data
When we consider solving problems using a computer, the most usual thing is that we want to deal with data that are variable and quantifiable, that is, they take a set of different values from a set of possible values, in addition to being able to store the values of these data in some form acceptable to the computer (either in memory or external storage peripherals).
In a programming language, the concept of data type refers to the set of values that a variable can take. This idea is similar to the one used in mathematics, where we classify variables based on certain characteristics, distinguishing between whole, real or complex numbers. However, in mathematics, we are able to differentiate the type of variables depending on the context, but for compilers this is much more difficult. For this reason we must explicitly declare each variable as belonging to a type. This mechanism is useful for the computer to store the variable in the most appropriate way, as well as allowing to verify what type of operations can be performed with it.