Cátedra: Veiga
Fecha: 2do Cuatrimestre 2006
Día: 12/12/2006
Tema: 1
#include <iostream> class SomeAbstractClass { public: virtual void doSomething() = 0; /*El "=0" la marca como virtual pura*/ }; class SomeConcreteClass: public SomeAbstractClass { public void doSomething() { std::cout << "I'm doing it!" << std::endl; } };Si SomeConcreteClass no implementase doSomething, tendríamos un error de compilación al intentar crear una instancia de esa clase (ya que sería abstracta). ==== Punto IV ====
/** * Author: Mariano Szklanny **/ static GdkPoint* points = 0; static gboolean onDraw(GtkWidget* sender, GdkEvent* eventArgs, gpointer data) { if (points == 0) { points = (GdkPoint*)malloc(4 * sizeof(GdkPoint)); points[0].x = sender->allocation.width / 2; points[0].y = 0; points[1].x = sender->allocation.width; points[1].y = sender->allocation.height / 2; points[2].x = sender->allocation.width / 2; points[2].y = sender->allocation.height; points[3].x = 0; points[3].y = sender->allocation.height / 2; } gdk_draw_polygon (sender->window, sender->style->fg_gc[GTK_WIDGET_STATE (sender)], FALSE, points, 4); return TRUE; } int main(int argc, char* argv[]) { GtkWidget* window; GtkWidget* drawingArea; gtk_init(&argc, &argv); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL); gtk_window_set_title(GTK_WINDOW(window), "Drawing"); drawingArea = gtk_drawing_area_new(); g_signal_connect(G_OBJECT(drawingArea), "expose_event", G_CALLBACK(onDraw), NULL); gtk_container_add(GTK_CONTAINER(window), drawingArea); gtk_widget_show(drawingArea); gtk_widget_show(window); gtk_main(); free(points); return 0; }==== Punto V ====
void replace(FILE* file, long int foundPos) { char c; fseek(file, foundPos, SEEK_SET); fputc('*', file); fgetc(file); while ((c = fgetc(file)) != EOF) { fseek(file, -2*sizeof(char), SEEK_CUR); fputc(c, file); fgetc(file); } fflush(file); } void process(FILE* file) { char c; long int foundPos; while ((c = fgetc(file)) != EOF) { if ((c == '/') && ((c = fgetc(file)) == '/')) { foundPos = ftell(file) - 2; if (fgetc(file) == '"') { fseek(file, -4*sizeof(char), SEEK_CUR); if (fgetc(file) == '"') { fseek(file, foundPos + 3, SEEK_SET); continue; } } replace(file, foundPos); fseek(file, foundPos + 3, SEEK_SET); } } } int main(int argc, char* argv[]) { FILE* file; file = fopen("file.txt", "r+"); if (file == 0 || feof(file) || ferror(file)) return 1; process(file); fclose(file); return 0; }==== Punto VI ====
#ifndef STACK_H_ #define STACK_H_ typedef struct Node { struct Node* next; void* element; } Node; typedef struct { Node* top; } Stack; /* Crea la estructura */ void create(Stack* stack); /* Destruye la estructura */ void destroy(Stack* stack); /* Agrega un elemento */ void push(Stack* stack, void* element); /* Quita el último elemento agregado a la estructura */ void* pop(Stack* stack); /* Retorna el último elemento agregado a la estructura */ void* top(Stack* stack); /* Retorna 0 si la estructura está vacía. En caso contrario retorna un número distinto de 0 */ int is_empty(Stack* stack); #endifSe presenta una declaración de un tipo de dato Stack que tendrá una implementación con nodos simplemente enlazados. ==== Punto VII ====
#include <iostream> using namespace std; string process(const string& first, const string& second) { string result = first; string::size_type loc = result.find(second); while (loc != string::npos) { result.erase(loc,second.size()); loc = result.find(second,loc); } return result; } int main(int argc, char **argv) { string first; string second; cout << "Ingrese el primer string" << endl; getline(cin,first); cout << "Ingrese el segundo string" << endl; getline(cin,second); cout << "Se procesaran:\n1. " << first << "\n2." << second << endl; cout << process(first,second); return 0; }==== Punto VIII ====
#ifndef NUMBINARIO_H_ #define NUMBINARIO_H_ #include <ostream> class NumBinario { private: bool signo; bool numero[7]; static NumBinario intToNumBin(int n); public: NumBinario(); NumBinario(int n); NumBinario(const NumBinario& n); virtual ~NumBinario(); bool operator<(const NumBinario& n); bool operator==(const NumBinario& n); NumBinario operator=(const NumBinario& n); NumBinario operator+(const NumBinario& n) { int a = (int)*this; int b = (int)(NumBinario)n; int c = a+b; return intToNumBin(c); } operator int(); friend std::ostream& operator << (std::ostream& os, NumBinario& n); }; #endifLa implementación mostrada tiene la ventaja de no meterse a trabajar con números binarios (posible fuente de errores) durante el exámen. Se suponen implementados los métodos operator int() y un conversor de ints a numeros binarios. A nivel diseño tampoco es una mala idea, ya que queda completamente encapsulado el manejo de binarios y permite trabjar en el resto de los métodos de manera transparente.
==== Punto IX ====
#include <iostream> class A { public: virtual ~A() {} virtual void doSomething() { std::cout << "I'm A" << std::endl; } }; class B: public A { public: virtual ~B() {} virtual void doSomething() { std::cout << "I'm B" << std::endl; } }; void testVirtual(const A& instance) { instance.doSomething(); } int main(int argc, char* argv[]) { A myA; B myB; testVirtual(myA); /* Muestra "I'm A" */ testVirtual(myB); /* Muestra "I'm B" */ return 0; }Esto sucede así, porque se está mandando una referencia a una instancia a la función testVirtual. En runtime se tiene información sobre la clase a la que efectivamente pertenece cada objeto que llega ahí y se hace Late Binding correctamente (Observar que si testVirtual recibiera no una referencia, sino un objeto, dicha información se pierde, y siempre se llamaría al método de la clase definida como parámetro - esto se conoce como Object Slicing). ==== Punto X ====
class Universo { private: static int contador; public: Universo() {contador++;} ~Universo() {contador--;} static int cantidadInstancias(); }; int Universo::contador = 0; int Universo::cantidadInstancias() { return contador; }===== Discusión =====