Professional Documents
Culture Documents
LENGUAJE C++
2
Entrada y salida
Salida: cout
Entrada: cin
Ingresar 2 edades y
Ejercicio determinar quién es mayor
#include <iostream.h>
int main() {
cout << "Ingrese su edad: ";
int miEdad;
cin >> miEdad;
cout << "Ingrese la edad de un amigo: ";
int suEdad;
cin >> suEdad;
if (miEdad < suEdad)
cout << "Yo soy menor que mi amigo“ << endl;
else
if (miEdad > suEdad)
cout << "Mi amigo es menor que yo \n ";
else
cout << “Ambos tenemos la misma edad \n ";
return 0;
}
7
Tipos de Datos
ELEMENTALES
8
Tipos numéricos
Punteros
Declaración de punteros
tipo *nombre_variable;
p=&i;
r=p;
p=q; es un ERROR porque son punteros de diferente
tipo
11
cin>>*p;
if (p==r)
cout<<"El valor de i es: "<<*p;
*p=*p+3;
p=p+3;
12
Ejemplo ilustrativo
4. q=p; 5. *q=85;
a a
p p
8 85
q q
14
Ejemplo ilustrativo
8. q=&b;
NOTA
a
p 9 a, b, p, q : variables
b locales automáticas
q 1
15
Ejemplo 1:
int main() {
int x, y, *p, *q;
p = &x;
*p = 5;
q = &y;
*q = 23;
cout << *p << " “ << *q << endl;
q = p;
*p = 35;
cout << *p << " “ << *q << endl;
q = NULL;
cout << x << " “ << y << endl;
return 0;
}
16
Ejemplo 2:
int main() {
int *p, *q, x, y;
p = &x;
*p = 14;
q = p;
cout << *p << " " << *q << endl;
q = NULL;
cout << x << " “ << y << endl;
return 0;
}
17
Ejemplo 3:
int main() {
int x, y, *p, *q;
p = &x;
*p = 12;
q = &y;
*q = 23;
cout << *p <<" "<<*q<<endl;
*p = *q;
cout << *p <<" "<<*q<<endl;
q = NULL;
cout<<*p<<endl;
cout<<x<<" "<<y<<endl;
return 0;
}
18
Ejemplo 4:
int main() {
int x, y, *p, *q;
p = &x;
*p = 14;
q = p;
if ( p == q) cout<<"Mensaje 1";
q = &y;
if ( p == q) cout<<"Mensaje 2";
*q = 14;
if (p == q) cout<<"Mensaje 3";
if (*p == *q)cout<<"Mensaje 4";
return 0;
}
19
Tipos Enumerados
int main() {
Color c=Verde;
if (c==Verde)
cout<<"Color VERDE";
return 0;
}
20
Tipos de Datos
ESTRUCTURADOS
21
Arreglos
Arreglos
Strings
char s[5];
char s1[5] = {'h','o','l','a','\0'};
char s2[5] = "hola";
char s3[] = "hola";
Strings
Ingresar:
i = 1; Este es un ejemplo
do {
cin >> s;
cout << i << ": " << s << endl;
i++;
}
while (i<5);
25
Strings
Strings
Captura de un String
i=1;
do {
cin.getline(s,20);
cout << i << ": " << s << endl;
i++;
}
while (i<5);
27
Strings
strlen(string); // largo
strcpy(destino, origen); // copia
strcmp(string1, string2); // compara
0: Si string1 == string2
1: Si string1 > string2
-1: Si string1 < string2
28
Strings
#include <iostream.h>
#include <string.h>
int main() {
char x[5];
char y[] = "Hola";
strcpy(x,y);
cout << "x:" << x << endl;
cout << "y:" << y << endl;
return 0;
}
29
Strings
#include <iostream.h>
#include <string.h>
int main() {
char *x;
char y[] = "Hola";
x = new char[strlen(y)+1];
cin.getline(x, 20);
cout << "x:" << x << endl;
cout << "y:" << y << endl;
return 0;
}
30
Strings
#include <iostream.h>
#include <string.h>
void Copiar(char a[], char b[]) {
strcpy(a, b);
}
int main() {
char *x;
char y[] = "Hola";
x = new char[strlen(y)+1];
Copiar(x,y);
cout << "x:" << x << endl;
cout << "y:" << y << endl;
return 0;
}
31
Strings
#include <iostream.h>
#include <string.h>
void Copiar(char *a, char *b) {
strcpy(a, b);
}
int main() {
char *x;
char y[] = "Hola";
x=new char[strlen(y)+1];
Copiar(x,y);
cout<< "x:" << x << endl;
cout<< "y:" << y << endl;
return 0;
}
32
Strings
#include <iostream.h>
#include <string.h>
char *Mayor(char a[], char *b) {
if (strcmp(a,b)>0)
return a;
return b;
}
int main() {
char *x;
char y[] = "Hola";
x = new char[strlen(y)+1];
cin.getline(x,20);
cout << "El mayor: " << Mayor(x,y);
return 0;
}
33
Estructuras
Estructuras
struct {
int hh;
int mm;
int ss;
} Hora;
Hora h, v[10], *p;
h.hh = 8;
p = &h;
v[3].hh = 5;
v[7] = *p;
cout << p->hh << h.hh << v[7].hh
35
OBJETOS CONSTANTES
38
Objetos Constantes
FUNCIONES
40
Definición
Definición
#include <iostream.h>
void f1() {
cout << "Función sin argumentos \n";
}
void f2(void) {
cout << “También sin argumentos \n";
}
int main() {
f1();
f2();
return 0;
}
42
Tipos de retorno
<tipo> &<nombre>(<parámetros>)
43
Declaración (prototipo)
Parámetros
Parámetros Formales
O de definición, son los que aparecen en la
declaración de la función
Parámetros Actuales
O de invocación, son los que aparecen en la llamada
a la función
Se usan para
inicializar los parámetros formales, o
recibir valores de los parámetros formales
46
Parámetros actuales
Modos de parametrización
Modos de parametrización
Ejemplos
int main( ) {
int x = 2, y = 4, &z = x; // z es ALIAS de x
Ejemplo(x, y);
cout << x << " " << y << " " << z;
cout << endl;
return 0;
}
50
Ejemplos
int cuadXVal(int a) {
return a = a*a;
}
void cuadXInd(int *b) {
*b = (*b) * (*b);
}
void cuadXRef(int &c) {
c = c*c;
}
51
Ejemplos
int main() {
int x = 2, y = 3, z = 4;
cout << "Por valor : x = " << cuadXVal(x) << '\n';
cuadXInd(&y);
cout << "Por indirección : y = " << y << '\n';
cuadXRef(z);
cout << "Por referencia : z = " << z << '\n';
return 0;
}
52
Prototipos de funciones
#include <iostream.h>
void f1();
void f2(void);
int main() {
f1();
f2();
return 0;
}
void f1() {
cout << "Función sin argumentos \n";
}
void f2(void) {
cout << "Función también sin argumentos \n";
}
53
Funciones en línea
Funciones en línea
Memoria Dinámica
int *p;
p = new int;
:::
delete p;
Ejemplo ilustrativo
p ? p ? p 10
q ? q ? q ?
4. q=p; 5. *q=85;
p 10 p 85
q q
57
Ejemplo ilustrativo
7. *p= 9;
p 9
q 85
58
Ejemplo ilustrativo
p 85 p 9
q q 85
8. delete q; 9. delete q;
p ? p 9
q ? q ?
59
Memoria Dinámica
typedef struct {
int num;
float saldo;
} Cuenta;
Cuenta *c;
c = new Cuenta;
60
Memoria Dinámica
int *v;
delete [] v;
// libera la memoria ocupada por v
61
Resolución de alcance
Operador ::
float x = 1.23;
int main() {
int x = 7
cout << "x local = "<< x <<endl;
cout << "x global = " << ::x << endl;
return 0;
}
62
Sobrecarga de funciones
Sobrecarga de funciones
float cuadrado(int k) {
return k*k;
}
float cuadrado(float x) {
return x*x;
}
int main() {
int i = 5; float x = 3.3;
cout << i << " al cuadrado = " << cuadrado(i);
cout << endl;
cout << x << " al cuadrado = " << cuadrado(x);
return 0;
}