1 . #include <iostream>
2 . using namespace std;
3 .
4 . // This is a tradition
5 .
6 . int main() /* you can comment like this to*/
7 . {
8 . cout << "Hello World\n"; // Print 'Hello Word'
9 . return 0;
10 . }
11 .
12 . /* The general form is:
13 . * int main (int argc, char *argv[] , other_parameters ) { body }
14 . * argc - Non-negative value representing the number of arguments passed
15 . * to the program from the environment in which the program is run.
16 . * argv - Pointer to the first element of an array of pointers to
17 . * null-terminated multibyte strings that represent the arguments
18 . * passed to the program from the execution environment (argv[0]
19 . * through argv[argc-1]). The value of argv[argc] is guaranteed to be 0.
20 . * body - The body of the main function
21 . *
22 . */
g++ -v
Using built-in specs. COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Debian 5.3.1-8' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --with-arch-32=i586 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 5.3.1 20160205 (Debian 5.3.1-8)
g++ helloworld.cpp -o helloworld.out
./programa.out
#include <iostream>
using namespace std;
main()
{
int a = 21;
int b = 10;
int c ;
cout << "a=" << a << endl ;
cout << "b=" << b << endl ;
c = a + b;
cout << "a+b=" << c << endl ;
c = a - b;
cout << "a-b=" << c << endl ;
c = a * b;
cout << "a*b=" << c << endl ;
c = a / b;
cout << "a/b=" << c << endl ;
c = a % b;
cout << "a%b=" << c << endl ;
c = a++;
cout << "a++=" << c << endl ;
c = a--;
cout << "a--=" << c << endl ;
return 0;
}#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
main(int argc, char *argv[])
{
bool c;
cout << "el valor 0 equivale a FALSE, y 1 a TRUE" << endl ;
if(argc != 3)
{
fprintf(stdout,"Ingreso %d argumentos, el número de argumentos es 2\n",argc);
return 1;
}//endif
int a = atoi(argv[1]);
int b = atoi(argv[2]);
cout << "a=" << a << endl;
cout << "b=" << b << endl;
c = (a == b);
cout << "a == b es:" << c << endl ;
c= ( a < b );
cout << "a < b es:"<< c << endl ;
c= ( a > b );
cout << "a > b es:"<< c << endl ;
c= ( a <= b );
cout << "a <= b es:"<< c << endl ;
c= ( b >= a );
cout << "b >= a es:"<< c << endl ;
return 0;
}#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
main(int argc, char *argv[])
{
if(argc != 3)
{
fprintf(stdout,"Ingreso %d argumentos, el número de argumentos es 2\n",argc);
return 1;
}//endif
bool a = atoi(argv[1]); /*si es diferente de cero es uno*/
bool b = atoi(argv[2]);
cout << "a=" << a<< endl;
cout << "b=" << b<< endl;
if ( a && b )
{
cout << "a && b es TRUE" << endl;
}else
{
cout << "a && b es FALSE" << endl;
}
if ( a || b )
{
cout << "a || b es TRUE" << endl;
}else
{
cout << "a || b es FALSE" << endl;
}
if ( !(a && b) )
{
cout << "!(a && b) es TRUE" << endl;
}else
{
cout << "!(a && b) es FALSE" << endl;
}
if ( !(a || b) )
{
cout << "!(a || b) es TRUE" << endl;
}else
{
cout << "!(a || b) es FALSE" << endl;
}
return 0;
}#include <iostream> /*cin,cout*/
#include <stdio.h> /*fprintf */
#include <stdlib.h> /* strtol */
#include <string.h> /* strcat */
using namespace std;
/*Creamos una función para convertir los numeros a su forma binaria*/
const char *byte_to_binary(int x)
{
static char b[64];
b[0] = '\0';
int z;
for (z = 1024; z > 0; z >>= 1)
{
if ((x & z) == z)
strcat(b,"1");
else
strcat(b,"0");
}
return b;
}
int main(int argc, char *argv[])
{
if(argc != 3)
{
cout << "Ingreso" << argc << "argumentos, el número de argumentos es 2"<< endl;
return 1;
}//endif
int a = atoi(argv[1]);
int b = atoi(argv[2]);
int c;
if((a>=1024| b>=1024))
{
cout << "Alguno de los argumentos,es mayor que 1024"<< endl;
return 1;
}//endif
printf ("a= %d\n",a);
printf ("binario: a= %s\n",byte_to_binary(a));
printf ("b= %d\n",b);
printf ("binario b=: %s\n",byte_to_binary(b));
c = a & b;
printf ("a & b= %d\n",c);
printf ("binario a & b=: %s\n",byte_to_binary(c));
c = a | b;
printf ("a | b= %d\n",c);
printf ("binario a | b=: %s\n",byte_to_binary(c));
c = a ^ b;
printf ("a ^ b= %d\n",c);
printf ("binario a ^ b=: %s\n",byte_to_binary(c));
c = ~a;
printf ("~a= %d\n",c);
printf ("binario ~a=: %s\n",byte_to_binary(c));
c = a << 2;
printf ("a << 2= %d\n",c);
printf ("binario a << 2=: %s\n",byte_to_binary(c));
c = a >> 2;
printf ("a >> 2= %d\n",c);
printf ("binario a >> 2=: %s\n",byte_to_binary(c));
return 0;
}usuario@debian:/C++/Ejemplos$ ./operador_bitwise.out 11 12 a= 11 binario: a= 00000001011 b= 12 binario b=: 00000001100 a & b= 8 binario a & b=: 00000001000 a | b= 15 binario a | b=: 00000001111 a ^ b= 7 binario a ^ b=: 00000000111 ~a= -12 binario ~a=: 11111110100 a << 2= 44 binario a << 2=: 00000101100 a >> 2= 2 binario a >> 2=: 00000000010
#include <iostream> /*cin,cout*/
#include <stdlib.h> /* atoi */
using namespace std;
int main(int argc, char *argv[])
{
if(argc != 3)
{
cout << "Ingreso" << argc << "argumentos, el número de argumentos es 2"<< endl;
return 1;
}//endif
int a = atoi(argv[1]);
int b = atoi(argv[2]);
int c;
cout << "a= " <<a<< endl ;
cout << "b= " <<a<< endl ;
c = a;
cout << "Operador c = a : entonces c= " <<c<< endl ;
c += a;
cout << "Operador c -+= : entonces c= " <<c<< endl ;
c -= a;
cout << "Operador c -= : entonces c= " <<c<< endl ;
c *= a;
cout << "Operador c *= : entonces c= " <<c<< endl ;
c /= a;
cout << "Operador c /= : entonces c= " <<c<< endl ;
c = b;
cout << "Operador c = b : entonces c= " <<c<< endl ;
c %= a;
cout << "Operador c %= : entonces c= " <<c<< endl ;
c <<= 2;
cout << "Operador c <<= : entonces c= " <<c<< endl ;
c >>= 2;
cout << "Operador c >>= : entonces c= " <<c<< endl ;
c &= 2;
cout << "Operador c &= : entonces c= " <<c<< endl ;
c ^= 2;
cout << "Operador c ^= : entonces c= " <<c<< endl ;
c |= 2;
cout << "Operador c |= : entonces c= " <<c<< endl ;
return 0;
}//End_main#include <iostream>
#include <string>
using namespace std;
int main()
{
int ch=sizeof(char) ;
int in=sizeof(int) ;
int si=sizeof(short int);
int li=sizeof(long int) ;
int fl=sizeof(float) ;
int du=sizeof(double) ;
int wc=sizeof(wchar_t) ;
cout << "tamaño de una variable en bytes : " << endl;
cout << endl;
cout << "tamaño de una variable tipo char : " << ch << endl;
cout << "tamaño de una variable tipo int : " << in << endl;
cout << "tamaño de una variable tipo short int : " << si << endl;
cout << "tamaño de una variable tipo long int : " << li << endl;
cout << "tamaño de una variable tipo float : " << fl << endl;
cout << "tamaño de una variable tipo double : " << du << endl;
cout << "tamaño de una variable tipo wchar_t : " << wc << endl;
int bch=ch*8;
int bin=in*8;
int bsi=si*8;
int bli=li*8;
int bfl=fl*8;
int bdu=du*8;
int bwc=wc*8;
cout << endl << endl;
cout << "tamaño de una variable en bits : " << endl;
cout << endl;
cout << "tamaño de una variable tipo char : " << bch << endl;
cout << "tamaño de una variable tipo int : " << bin << endl;
cout << "tamaño de una variable tipo short int : " << bsi << endl;
cout << "tamaño de una variable tipo long int : " << bli << endl;
cout << "tamaño de una variable tipo float : " << bfl << endl;
cout << "tamaño de una variable tipo double : " << bdu << endl;
cout << "tamaño de una variable tipo wchar_t : " << bwc << endl;
cout << endl << endl;
string flag; //esto es nuevo!! y depende de la librería string
cout << "Uso operador 'condition? X:Y': " << endl << endl;
flag= (ch > li) ? "Yes":"No" ;
cout << "(char > long int)? " << flag <<endl<< endl<< endl;
int var=0;
int a=100;
cout << "Uso operador 'coma': " << endl<< endl;
cout << "Valor inicial var= "<< var << " ,Valor inicial a= "<< a << endl;
var = (a--, a+10, a*2);
cout << "var = (a--, a+10, a*2)" << endl;
cout << "Valor final var= " <<var<< endl;
cout << endl << endl;
int c;
float b=2.342345;
cout << "Uso operador 'cast': " << endl<< endl;
cout << "b= "<< b << endl;
c = (int) b;
cout << "(int)b = " << c << endl<< endl ;
return 0;
}#include <iostream>
using namespace std;
int main ()
{
int var;
int *ptr; // en ptr se guarda direcciones de memoria
int val;
var = 3000;
ptr = &var;
val = *ptr;
cout << "var = 3000" << endl;
cout << "var = " << var << endl;
cout << "ptr = &var" << endl;
cout << "ptr = " << ptr << endl;
cout << "val = *ptr" << endl;
cout << "val = " << val << endl;
return 0;
}if(expresión lógica)
{
// Instrucciones que se ejecutan para el caso en que expresión lógica = TRUE
}if(expresión lógica)
{
// Instrucciones que se ejecutan para el caso en que expresión lógica = TRUE
}
else
{
// Instrucciones que se ejecutan para el caso en que expresión lógica = FALSE
}switch(expresión){
case valor-1 :
Comandos;
break; //Opcional
case valor-2 :
Comandos;
break; //Opcional
.
.
.
case valor-n :
Comandos;
break; //Opcional
// Se pueden tener los casos que se quieran.
default : //Opcional
Comandos;
}while(condition)
{
statement(s);
}#include <iostream>
using std::cout;
using std::endl;
int main ()
{
int i = 20;
// while loop execution
while( i > 1 )
{
cout << "i= " << i << endl;
i=i-1;
}
return 0;
}for ( init; condition; increment )
{
statement(s);
}#include <iostream>
int main ()
{
for( int i = 20; i > 1; i-- )
{
std::cout << "i= " << i << std::endl;
}
return 0;
}{CODE(colors="cpp")}#include <iostream>
using std::cout;
using std::endl;
int main ()
{
int i = 20;
// while loop execution
while( i > 1 )
{
cout << "i= " << i << endl;
i=i-1;
}
return 0;
}{CODE}break;
continue;
goto label; .. . label: statement;
#include <iostream> /*cin,cout*/
#include <stdlib.h> /* strtol */
using namespace std;
int main(int argc, char *argv[])
{
if(argc != 2)
{
cout << "Ingreso" << argc << "argumentos, el número de argumentos es un entero"<< endl;
return 1;
}//endif
int a = atoi(argv[1]);
int i=0;
cout << "Número elegido:" << a << endl;
COMP:for( ; ; )//esto es un loop infinito //COMP es la etiqueta para el goto
{
i++;
cout << "Número de loop:" << i << endl;
cout << "Número:" << a << endl;
if (a>0)
{
cout << "a=" << a <<" ,Número mayor que cero (uso continue)" << endl;
a=a-1;
continue;
}else if (a==0)
{
cout << "a=" << a <<" ,Número igual a cero (uso breake)" << endl;
break;
}else if (a<0)
{
cout << "a=" << a <<" ,Numero menor que cero (uso goto)" << endl;
a++;
goto COMP;
}else
{
cout << "Error en el cast" << endl;
return 1;
}
}
return 0;
}Tipo Nombre( lista de parámetros )
{
Cuerpo de la función
}#include <stdio.h> /* printf */
#include <string.h> /* strcat */
#include <stdlib.h> /* strtol */
// Este codigo convierte enteros en binarios menores de 2^10
const char *byte_to_binary(int x) //Función tipo puntero a char, y tiene un entero como parámetro
{
static char b[64];
b[0] = '\0';
int z;
for (z = 1024; z > 0; z >>= 1)// 1024 es 2^10 un 1 y 10 ceros en binario
{ // z>>= mueve el numero bit por bit a la derecha
if ((x & z) == z) // compara (x&z) con z (ver tabla de &)
strcat(b,"1");
else
strcat(b, "0" );
}
return b; // devuelve el vector de char
}
int main(void)
{
/* byte to binary string */
printf("%s\n", byte_to_binary(250));
return 0;
}tipo Nombre [ tamaño ];
double arreglo[5] = {200.2, 26.1, 30.24, 12.0, 20.0};
double arreglo[] = {200.2, 26.1, 30.24, 12.0, 20.0};
double otro[5];
otro[0] =200.2;
otro[1] =26.1;
otro[2] =30.24;
otro[3] =12.0;
otro[3] =20.0;#include <iostream>
using std::cout;
using std::endl;
#include <iomanip>
using std::setw;//esta funcion genera espacios en el output
#include <stdlib.h> /*atoi*/
int main(int argc, char *argv[])
{
if(argc != 2)
{
cout << "Ingreso" << argc << "argumentos, el número de argumentos es 1"<< endl;
return 1;
}//endif
int n = atoi(argv[1]);
int m[ 10 ]; // se declara el arreglo
// se inicializa el arreglo usando un for
for ( int i = 0; i < 10; i++ )
{
m[ i ] = i* n;
}
cout << "La Tabla de multiplicar del número " << n << endl;
cout << "Número" << setw( 13 ) << "Valor" << endl;
// output each array element's value
for ( int j = 0; j < 10; j++ )
{
cout << setw( 4 )<< j << setw( 13 ) << m[ j ] << endl;
}
return 0;
}tipo Nombre[Tamaño1][Tamaño2]...[TamañoN];
#include <iostream>
using namespace std;
int main ()
{
// Declaramos e inicializamos una matriz de 4X3 de enteros, llamada a.
int a[4][3] = { {0,0,0}, {1,2,3}, {2,4,6}, {3,6,9}};
// Ahora se muestran en pantalla
for ( int i = 0; i < 4; i++ )
for ( int j = 0; j < 3; j++ )
{
cout << "a[" << i << "][" << j << "]: ";
cout << a[i][j]<< endl;
}
return 0;
}#include <iostream>
using namespace std;
int main ()
{
double vector[4] = {10,20,30,40};
double *p;
p = vector;
cout << "muestro los valores arreglos " << endl;
for ( int i = 0; i < 4; i++ )
{
cout << "vector[" << i << "]= ";
cout << vector[i] << endl;
}
cout << "muestro los valores usando punteros " << endl;
for ( int i = 0; i < 4; i++ )
{
cout << "*(p + " << i << ") : ";
cout << *(p + i) << endl;
}
cout << "Muestro los valores usando el nombre como dirección " << endl;
for ( int i = 0; i < 4; i++ )
{
cout << "*(vector + " << i << ") : ";
cout << *(vector + i) << endl;
}
return 0;
}tipo_funcion nombre_funcion(tipo_arreglo nombre_arreglo[tamaño_arreglo])
{
.
}tipo_funcion nombre_funcion(tipo_arreglo nombre_arreglo[])
{
.
}tipo_funcion nombre_funcion(tipo_arreglo *nombre_arreglo)
{
.
}tipo_funcion * nombre_funcion()
{
.
}char saludo[5] = {'H', 'o', 'l', 'a','\0'};char saludo[] = "Hola";
#include <iostream>
#include <string> //llamo la librería
using namespace std;
int main ()
{
string str1 = "Hola";
string str2 = "Mundo";
string str3, str4;
int len ;
//se igualan a los valores
str3= " "; // guardo un espacio en str3
// se pueden concatenar las cadenas de caracteres
str4 = str1 + str3 + str2;
cout << "str1 + str3 + str2 : " << str4 << endl;
// las funciones asociadas al objeto str4 se llaman con el operador .
len = str4.size();
cout << "tamaño" << endl;
cout << "str4.size() : " << len << endl;
return 0;
}tipo *nombre;
#include <iostream>
using namespace std;
int main ()
{
int var = 20;
int *ip; // Declaración de variable tipo puntero a entero
ip = &var; // Asignación de valor del puntero
//Muestro el valor almacenado en var
cout << "Valor de variable var: " << var << endl;
//Muestro la dirección de memoria de var que está guardada en ip
cout << "Dirección de memoria guardada en ip: " << ip << endl;
//Acceso a la valor almacenado en var a través del puntero
cout << "Valor de *ip: " << *ip << endl;
//Tambien puedo mostrar la dirección de memoria del puntero
cout << "Dirección de memoria de ip, &ip : " << &ip << endl;
return 0;
}#include <iostream>
#include <stdlib.h> /*atoi*/
using namespace std;
int main(int argc, char *argv[])
{
int *ptr = NULL;
if(argc >=2)
{
int a = atoi(argv[1]);
ptr = &a;
} //endif
if(!ptr)
{
cout << "Puntero a NULL"<< endl ; //si trata de acceder a *ptr, Segmentation fault!!!
}
if(ptr)
{
cout << "Asignado, con valor:" << *ptr << endl;
}
return 0;
}#include <iostream>
using namespace std;
const int MAX = 4;
int main ()
{
int var[MAX] = {1, 3, 5,7};
int *ptr;
ptr = var; //asigna la dirección del primer elemento de var a ptr (ojo var es un arreglo)
int i = 0;
while ( ptr <= &var[MAX - 1] ) /* compara la direccion en ptr con la del último
elemento del arreglo var, para parar el ciclo*/
{
cout << "Dirección de var[" << i << "]: " << ptr << endl;
cout << "Valor de var[" << i << "]= " << *ptr << endl;
ptr++;// Aumento el puntero en una pocición (memoria contigua)
i++;
}
return 0;
}tipo *nombre[tamaño];
#include <iostream>
using namespace std;
#include <iomanip>
using std::setw;
const int MAX = 5;
int main ()
{
//recordando que un string es una cadena de caracteres, caracterizada por un puntero
const char *nombres[] = {"Camilo","Omar","Abdul","Alejo","Alephys"};
cout << "Número" << setw( 10 ) << "Nombre" << setw( 15 )<< "Direccion" << endl;
for (int i = 0; i < MAX; i++)
{
cout << setw( 4 ) << i << setw( 12 ) << nombres[i] << setw( 17 );
cout << &nombres[i] <<endl;
}
return 0;
}tipo**var;
#include <iostream>
using namespace std;
int main ()
{
int var;
int *ptr;
int **pptr;
var = 3000;
// La dirección de var se guarda en ptr
ptr = &var;
// La direccion de ptr se guarda en pptr
pptr = &ptr;
// Accediendo al valor a través de la variable, puntero y puntero a puntero.
cout << "var= " << var << endl;
cout << "*ptr= " << *ptr << endl;
cout << "**pptr= " << **pptr << endl;
cout << "Direcciones de memoria" << endl;
cout << "&var= " << &var << endl;
cout << "ptr= " << ptr << endl;
cout << "*pptr= " << *pptr << endl;
cout << "pptr= " << pptr << endl;
cout << "&pptr= " << &pptr << endl;
return 0;
}#include <iostream>
using namespace std;
// Declaramos la funcion, encuentra el numero mayor del arreglo
double mayor(double *arr, int tamano);
int main ()
{
//
double arreglo[] = {1,2,15,8,2};
double M;
// Se pasa el puntero a la funcion como argumento.
M = mayor( arreglo, 5 ) ;
// output the returned value
cout << "El número mayor es: " << M << endl;
return 0;
}
//Funciones
double mayor(double *arr, int tamano)
{
double may;
may=arr[0]; //le asigno el primer elemento
for (int i = 1; i < tamano; ++i)
{
if ( may < arr[i])
{
may = arr[i];
}//end if
}//end for
return may;
}tipo* nombre(argumentos)
{
.
}#ifndef POS_H
#define POS_H
namespace mylib
{
class posicion
{
public:
posicion():x(0),y(0),z(0){}
posicion(double _x,double _y,double _z)//constructor
{
x=_x;
y=_y;
z=_z;
}
void setPosicion(double _x,double _y,double _z)//metodo setter
{
x=_x;
y=_y;
z=_z;
}
//metodos getter
double getX(){return x;}
double getY(){return y;}
double getZ(){return z;}
//sobre carga de operador =
posicion &operator=(posicion &pos);
double distancia(posicion &pos);
protected:
double x,y,z;
};
}
#endif#include<posicion.h>
#include<cmath>
using namespace mylib;
posicion &posicion::operator=(posicion &pos)
{
this->x=pos.x;
this->y=pos.y;
this->z=pos.z;
return *this;
}
double posicion::distancia(posicion &pos)
{
return sqrt(pow(x-pos.x,2)+pow(y-pos.y,2)+pow(z-pos.z,2));
}#include<posicion.h>
#include<iostream>
int main()
{
mylib::posicion p1(0,0,0);
mylib::posicion p2(1,1,1);
std::cout<<"distancia entre dos puntos = "<<p1.distancia(p2)<<std::endl;
return 0;
}CXXFLAGS=-I. main: posicion.cxx posicion.h main.cxx $(CXX) $(CXXFLAGS) $< $@.cxx -o $@
El documento original está disponible en https://clustercien.udea.edu.co/web/tiki-index.php?page=Tutorial+Cpp