Cátedra: Veiga
Fecha: 2do Cuatrimestre 2007
Día: 12/02/2008
Tema: 2
#include <iostream> class Base { public: virtual void f1 (void) { cout << "B.f1" << endl; f3(); } void f2 (void) { cout << "B.f2" << endl; f3(); f1(); } static void f3 (void) { cout << "B.f3" << endl; } }; class Derivada : Base { public: void f1 (void) { cout << "D.f1" << endl; f3(); } virtual void f2 (void) { cout << "D.f2" << endl; f3(); f1(); } static void f3 (void) { cout << "D.f3" << endl; } }; void main (void) { Derivada* pD = new Derivada; Base* pB = (Base *) pD; pB->f1(); pB->f2(); pD->f1(); pD->f3(); }
D.f1
D.f3
B.f2
B.f3
D.f1
D.f3
D.f1
D.f3
D.f3
#include <stdio.h> #include <stdlib.h> #include <string.h> int parseint(char *s){ char *strBase, *strNum; int num, base, pos=0; strBase = (char *) malloc(3*sizeof(char)); while (s[pos] != ':') pos++; strNum = (char *) malloc(pos*sizeof(char)); strcpy(strBase, &s[pos+1]); strncpy(strNum, s, pos); base = atoi(strBase); num = strtol(strNum, NULL, base); free(strBase); free(strNum); return num; } /* El main es solo para probarlo. */ int main(int argc, char *argv[]){ if (argc >1) printf("%s es %i\n", argv[1], parseint(argv[1])); else printf("Cantidad de parámetros incorrecta.\n"); return 0; }
Otra forma:
#include <stdio.h> #include <stdlib.h> #include <string.h> int parseint(char *s){ char *strBase, c[2]={'\0','\0'}; int num, base, pos=0, suma=0, i; strBase = (char *) malloc(3*sizeof(char)); while (s[pos] != ':') pos++; strcpy(strBase, &s[pos+1]); base = atoi(strBase); num = 1; for (i=(pos-1); i>=0;i--){ c[0] = s[i]; suma += (num *atoi(c)); num *=base; } free(strBase); return suma; } /* El main es solo para probarlo. */ int main(int argc, char *argv[]){ if (argc >1) printf("%s es %i\n", argv[1], parseint(argv[1])); else printf("Cantidad de parámetros incorrecta.\n"); return 0; }
No esta probado.
#include <sys/socket.h> #include <arpa/inet.h> #include <stdlib.h> #define PORT 257 #define CONEXIONES_EN_COLA 1 int main(int argc, char *argv[]){ char *buffer = (char *) malloc(301*sizeof(char)); struct sockaddr_in addr; int fd, fdnuevo, i; addr.sin_family = AF_INET; addr.sin_port = htons(PORT); addr.sin_addr.s_addr = htonl(INADDR_ANY); // Le hago el htonl por más que no es necesario ya que INADDR_ANY es 0, y 0 en big-endean o litle-endean es 0. memset(&addr.sin_zero, '\0', 8); // Creo el socket. fd = socket(AF_INET, SOCK_STREAM, 0); // Asocio el socket al puerto. bind(fd, (struct sockaddr *) &addr, sizeof(struct sockaddr)); // Me pongo a escuchar y defino un máximo de CONEXIONES_EN_COLA conexiones esperando. listen(fd, CONEXIONES_EN_COLA); // Acepto una conexión. No guardo los datos de quien se conecta. Le paso dos punteros a NULL. fdnuevo = accept(fd, NULL, NULL); // Tengo en fdnuevo la conexión con el socket conectado. // Recibo los 300 bytes de una. recv(fdnuevo, buffer, 300, 0); // Envío los 300 bytes de a uno y comenzando por el último. for (i=299; i>=0; i--) send(fdnuevo, buffer[i], 1, 0); // Cierro ambos sockets. close(fd); close(fdnuevo); return 0; }
class Bytes{ private: char *pBuffer; int Largo; public: Bytes(); // Constructor default. Bytes(const Bytes &b); // Constructor de copia. ~Bytes(); // Destructor. Bytes operator+ (const Bytes &b) const; // Operador +. friend std::ostream & operator<<(std::ostream & os, const Bytes &b); // Operador de salida estándar. }; void convertir(int num, int base, std::ostream &os){ if (num < base) os << num; else { convertir(num/base, base, os); os << num%base; } } std::ostream &operator<<(std::ostream & os, const Bytes &b){ for(int i=0; i<Largo; i++) convertir(pBuffer[i], 2, os); return os; }
std::list<int> & (*func)(std::array<unsigned long int> &refArray, char &refChar, char *ptrChar=NULL);