Adsence

sábado, 30 de abril de 2011

-CLASE-

Leave a Comment
[caption id="attachment_1084" align="aligncenter" width="675"]Clases en C++ Clases en C++[/caption]

MANEJO DE UNA CLASE EN C++ DONDE TIENE INCORPORADA CUATRO FUNCIONES MIEMBROS QUE EFECTUAN LAS CUATRO OPERACIONES BASICAS DE LAS MATEMATICAS



[cpp]

#include <iostream.h>
#include <conio.h>

class Mate { //clase llamada mate

private:
double n1; //variables de la clase
double n2;

public: //conjunto de funciones miembro de la clase
Mate(double s1,double s2); //constructor
void suma (void);
void resta (void);
void multiplicacion(void);
void division (void);
};

Mate::Mate(double s1,double s2) //el operador de los dos puntos quiere decir
{ //que la funcion miembro pertenece a la clase mate
n1=s1;
n2=s2;
}

void Mate::suma(void) //tipo de dato clase::funcionmiembro(argumentos)
{
cout<<"\nLA SUMA DE LOS DOS NUMEROS ES: "<<n1+n2<<endl;
}


void Mate::resta(void)
{
if(n2<0)
cout<<"\nLA RESTA DE LOS DOS NUMEROS ES: "<<n1+n2<<endl;
else
cout<<"\nLA RESTA DE LOS DOS NUMEROS ES: "<<n1-n2<<endl;
}

void Mate::multiplicacion(void)
{
if(n1!=0&&n2!=0)
cout<<"\nLA MULTIPLICACION DE LOS DOS NUMEROS ES: "<<n1*n2<<endl;
else
cout<<"\nNO SE PUEDE REALIZAR";
}

void Mate::division(void)
{
if(n1!=0&&n2!=0)
cout<<"\nLA DIVISION DE LOS DOS NUMEROS ES: "<<n1/n2<<endl;
else
cout<<"\nNO SE PUEDE REALIZAR";
}

main()
{
double s1,s2;
int op,con;

do
{
cout<<"INTRODUZCA PRIMER NUMERO: ";
cin>>s1;

cout<<"\nINTRODUZCA SEGUNDO NUMERO: ";
cin>>s2;

Mate ad(s1,s2); //declaracion del objeto que recibe dos parametros

cout<<"\nQUE DESEA HACER: "<<endl<<"1-SUMAR"<<endl<<"2-RESTAR"<<endl<<"3-MULTIPLICAR"<<endl<<"4-DIVIDIR"<<endl<<"ELIJA OPCION: ";
cin>>op;
switch(op)
{
case 1:
ad.suma();
break;
case 2:
ad.resta();
break;
case 3:
ad.multiplicacion();
break;
case 4:
ad.division();
break;
};

cout<<"\nDESEA CONTINUAR"<<endl<<"1-SI"<<endl<<"2-NO"<<"\nELIJA OPCION: ";
cin>>con;
system("cls");
}while(con!=2);

getch();
}

[/cpp]

0 comentarios :