Introducción a clases

Ejemplo de uso de clases, como introducción para comenzar a programar orientado a objetos.
				/* menu generico
Utilizando una introduccion
a las clases "menu_cl"
Para pasarle los item usar
el array indicando texto y
numero de ubicacion */

#include <dos.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>

class menu_cl
{
/* DATOS Y FUNCIONES PRIVADOS */
int dato;
int num_op;
struct date d;
typedef struct
{
char texto[30];
int fila;
}opcion;
//
opcion *ptr_opcion;
int tecla;
//void mostrar(opcion *,int);
void subir();
void bajar();
void vista(opcion *,int);
//int seleccion(opcion *,int *);
int seleccion();
/* DATOS Y FUNCIONES PUBLICAS */
public:
menu_cl();
void presentar(opcion *,int);
int go(void);
};

// II INICIO DE CLASE
// dato =0;

menu_cl::menu_cl()
{
textbackground(BLUE);
clrscr();
gotoxy(1,1);
getdate(&d);
textbackground(BLACK);
cprintf("BASE DE DATOS :hugo-jgeact2003");
textbackground(BLACK);
gotoxy(1,2);
cprintf("FECHA: %d/%d/%d",d.da_day,d.da_mon,d.da_year);
dato=0;
}

int menu_cl::go(void)
{
//seleccion(opcion *ptr_opcion,int num_op)
return(seleccion());
}

void menu_cl::presentar(opcion *ptr_op,int num)
{
ptr_opcion=ptr_op;
num_op=num-1;
for (int r=0;r<num;r++) vista(ptr_opcion+r,0);
vista(ptr_opcion,1);
}
//
int menu_cl::seleccion()
{
do{
tecla=getch();
if (tecla==0)
{
switch(tecla=getch())
{
case 72:
subir();
break;
case 80:
bajar();
break;
default:
break;
}
}
}while(tecla!=13 && tecla!=27);
return(dato);
}

//vista de fila seleccionada
void menu_cl::vista(opcion *ptr_opcion,int f)
{
if (f)
textbackground(RED);
else
textbackground(BLUE);
gotoxy(10,ptr_opcion->fila+10);
cputs(ptr_opcion->texto);
}
//tecla arriba presionada;
void menu_cl::bajar()
{

vista(ptr_opcion+dato,0);
dato++;
if (dato>num_op)dato=0;
vista(ptr_opcion+dato,1);
}
//tecla abajo presionada;
void menu_cl::subir()
{
vista(ptr_opcion+dato,0);
dato--;
if (dato<0)dato=num_op;
vista(ptr_opcion+dato,1);
}

// Modificar segun la necesidad
void main(void)
{
menu_cl nuevo_menu;
opcion lista[7]={{"1-CREACION Y ALTA",0},{"2-CONSULTA",1},{"3-LISTADO BAJO STOCK",2},{"4-ACTUALIZACIONES",3},{"5-BAJAS",4},{"6-GRAFICO DE BARRAS",5},{"7-ELIMINACION REG MARCADO",6}};
int sel;
//le paso array de opcion, int cantidad de item menu
//para mostrar en pantalla;
nuevo_menu.presentar(lista,7);
//menu_cl.go comienzo seleccion
//retorna la opcion elegida
sel=nuevo_menu.go();
//salida imprime opcion elegida;
gotoxy(1,20);
printf("opcion %d",sel);
getch();
}

Descargar adjuntos
COMPARTE ESTE TUTORIAL

COMPARTIR EN FACEBOOK
COMPARTIR EN TWITTER
COMPARTIR EN LINKEDIN
COMPARTIR EN WHATSAPP
TUTORIAL ANTERIOR

SIGUIENTE TUTORIAL