PDA

View Full Version : what is the difference between this two programs, I am talking about code behaviour



sauerplayer
6th November 2016, 04:25
What I had in mind before creating this program was, to declare and create all the ATM customer accounts
( database ) within Cuenta.h so that I can call every single one of them from main just by typing for example

Clase1 claseLuis( "Luis", 28, 3600 ) using program B or Clase1 ClaseLuis; using program A.

Clase2 claseDiana( "Diana", 23, 2200 ) using program B or Clase2 ClaseDiana; using program A.

Clase3 claseHector( "Hector", 26, 4500 ) using program B or Clase3 ClaseHector; using program A.

Clase2 and Clase3 does not yet exists in my current code but I guess I can add them all within "Cuenta.h" and
"cuenta.cpp"

within main obviously, with their respective passwords set for each one of them, but I wonder which program
is preferable to use "Program A or Program B"???


//****************( "Cuenta.h " )****************Part_Of_Program_"A"

#include<string>
using namespace std;

class Cuenta1{

public:

Cuenta1();

void establecerValores( string, int, int );

string obtenerUsuario( );

int obtenerEdad();

int obtenerMonto();

void pedirContrasenia();

void mostrarMensaje();

private:

string nombreUsuario;
int edadUsuario;
int montoUsuario;
int contraseniaUsuario;
};

//****************( " end of Cuenta.h " )****************Part_Of_Program_"A"



//****************( " Cuenta.cpp " )****************Part_Of_Program_"A"

#include<iostream>
#include"Cuenta.h"
using namespace std;

Cuenta1::Cuenta1(){

establecerValores( "Luis Alberto Sánchez M", 28, 3600 );
}

void Cuenta1::establecerValores( string nombre, int edad, int monto ){

nombreUsuario = nombre;
edadUsuario = edad;
montoUsuario = monto;
pedirContrasenia();
}

string Cuenta1::obtenerUsuario(){

return nombreUsuario;
}

int Cuenta1::obtenerEdad(){

return edadUsuario;
}

int Cuenta1::obtenerMonto(){

return montoUsuario;
}

void Cuenta1::pedirContrasenia(){

int contraseniaActual = 1988;
cout << "Por favor introduzca su contraseña : ";
cin >> contraseniaUsuario;
cout << "\n\n";

if( contraseniaUsuario == contraseniaActual )

mostrarMensaje();

if( contraseniaUsuario != contraseniaActual )

cout << "\n\n";
cout << "Usted ingreso una contraseña incorrecta se se reinicializara el sistema\n"
<< "por favor introduzca la contraseña correcta la proxima vez" << "\n\n" ;

}

void Cuenta1::mostrarMensaje(){

cout << "Bienvenido a cajeros Nature : " << obtenerUsuario() << " Edad " << obtenerEdad() << "usted tiene $ " << obtenerMonto() << "\n\n";
}

//****************( " end of cuenta.cpp " )****************Part_Of_Program_"A"



//****************( " main.cpp " )****************Part_Of_Program_"A"

#include<iostream>
#include<cstdlib>
#include "Cuenta.h"
using namespace std;

int main(){

Cuenta1 cuentaLuis;

system("PAUSE");
return 0;
}

//****************( " end of main.cpp " )****************Part_Of_Program_"A"


---------------------------------------------------------------------------------------------------------------------------------------------------------



//****************( "Cuenta.h " )****************Part_Of_Program_"B"

#include<string>
using namespace std;

class Cuenta1{

public:

Cuenta1( string, int, int );

void establecerValores( string, int, int );

string obtenerUsuario( );

int obtenerEdad();

int obtenerMonto();

void pedirContrasenia();

void mostrarMensaje();

private:

string nombreUsuario;
int edadUsuario;
int montoUsuario;
int contraseniaUsuario;
};

//****************( " end of Cuenta.h " )****************Part_Of_Program_"B"



//****************( " Cuenta.cpp " )****************Part_Of_Program_"B"

#include<iostream>
#include"Cuenta.h"
using namespace std;

Cuenta1::Cuenta1( string nombre, int edad, int monto ){

establecerValores( nombre, edad, monto );
}

void Cuenta1::establecerValores( string nombre, int edad, int monto ){

nombreUsuario = nombre;
edadUsuario = edad;
montoUsuario = monto;
pedirContrasenia();
}

string Cuenta1::obtenerUsuario(){

return nombreUsuario;
}

int Cuenta1::obtenerEdad(){

return edadUsuario;
}

int Cuenta1::obtenerMonto(){

return montoUsuario;
}

void Cuenta1::pedirContrasenia(){

int contraseniaActual = 1988;
cout << "Por favor introduzca su contraseña : ";
cin >> contraseniaUsuario;
cout << "\n\n";

if( contraseniaUsuario == contraseniaActual )

mostrarMensaje();

if( contraseniaUsuario != contraseniaActual )

cout << "\n\n";
cout << "Usted ingreso una contraseña incorrecta se se reinicializara el sistema\n"
<< "por favor introduzca la contraseña correcta la proxima vez" << "\n\n" ;

}

void Cuenta1::mostrarMensaje(){

cout << "Bienvenido a cajeros Nature : " << obtenerUsuario() << " Edad " << obtenerEdad() << "usted tiene $ " << obtenerMonto() << "\n\n";
}

//****************( " end of cuenta.cpp " )****************Part_Of_Program_"B"



//****************( " main.cpp " )****************Part_Of_Program_"B"

#include<iostream>
#include<cstdlib>
#include "Cuenta.h"
using namespace std;

int main(){

Cuenta1 cuentaLuis( "Luis Alberto Sanchez M", 28, 3600 );

system("PAUSE");
return 0;
}

//****************( " end of main.cpp " )****************Part_Of_Program_"B"

anda_skoa
6th November 2016, 13:02
You really don't want to need different classes for different values, but one class for the type of data and instances for each value.

I.e. one class that can hold the information for a single customer and one instance of that class for each customer.

So what your program B does.

Cheers,
_

sauerplayer
7th November 2016, 01:33
ok so know I modified the program to add the class functions retirarSaldo(), and abonarSaldo() and some private data.

I also make use of: cuentaLuis.pedirContrasenia(); within main instead of within the constructor.
and also cuentaDiana.pedirContrasenia();

but for me the question remains why not to allow the constructor to do all the processing??.

Will it cause conflicts by compiling the program that way or is just a matter of readability?

how much further must I allow a constructor to handle the processing of any program?


//****************( "Cuenta.h " )****************

#include<string>
using namespace std;

class Cuenta1{

public:

Cuenta1( string, int, int, int, string );

void establecerValores( string, int, int, int, string );

string obtenerUsuario( );

int obtenerEdad();

int obtenerMonto();

void pedirContrasenia();

void mostrarMensaje();

void retirarSaldo( int );

void depositarSaldo( int );

private:

string nombreUsuario;
int edadUsuario;
int montoUsuario;
int contraseniaUsuario;
string referenciaUsuario;
};

//****************( " end of Cuenta.h " )****************



//****************( " Cuenta.cpp " )****************

#include<iostream>
#include"Cuenta.h"
using namespace std;

Cuenta1::Cuenta1( string nombre, int edad, int monto, int contrasenia, string referencia ){

establecerValores( nombre, edad, monto, contrasenia, referencia );
}

void Cuenta1::establecerValores( string nombre, int edad, int monto, int contrasenia, string referencia ){

nombreUsuario = nombre;
edadUsuario = edad;
montoUsuario = monto;
contraseniaUsuario = contrasenia;
referenciaUsuario = referencia;
}

string Cuenta1::obtenerUsuario(){

return nombreUsuario;
}

int Cuenta1::obtenerEdad(){

return edadUsuario;
}

int Cuenta1::obtenerMonto(){

return montoUsuario;
}

void Cuenta1::pedirContrasenia(){

int contraseniaActual;
cout << "Por favor : " << referenciaUsuario << " introduzca su contraseña : ";
cin >> contraseniaActual;
cout << "\n\n";

if( contraseniaActual == contraseniaUsuario ){

mostrarMensaje();
}

if( contraseniaActual != contraseniaUsuario ){

cout << "\n\n";
cout << "Usted ingreso una contraseña incorrecta se se reinicializara el sistema\n"
<< "por favor introduzca la contraseña correcta la proxima vez" << "\n\n" ;
}

}

void Cuenta1::mostrarMensaje(){

int opcion;
int cantidadRetiro;
int cantidadAbono;
cout << "Bienvenido a cajeros Nature : " << obtenerUsuario() << " Edad " << obtenerEdad() << " usted tiene $ " << obtenerMonto() << "\n\n";
cout << "\n\nque desea realizar? \t1)Retiro\t2)Deposito? ";
cin >> opcion;

if( opcion == 1 ){

cout << "\n\nCout Cuanto desea retirar? ";
cin >> cantidadRetiro;
retirarSaldo( cantidadRetiro );
}

if( opcion == 2 ){

cout << "\n\nCuanto desea abonar? ";
cin >> cantidadAbono;
depositarSaldo( cantidadAbono );
}
}

void Cuenta1::retirarSaldo( int monto ){

if(( montoUsuario - monto ) < 0 ){

montoUsuario = montoUsuario;

cout << "\n\nUsted no puede retirar mas de lo que tiene : " << obtenerMonto() << "\n\n";
}

if(( montoUsuario - monto ) == 0 ){

montoUsuario = montoUsuario - monto;

cout << "\n\nUsted retiro : " << monto << " Su saldo actual es : " << obtenerMonto() << "\n\n";
}

if(( montoUsuario - monto ) > 0 ){

montoUsuario = montoUsuario - monto;

cout << "\n\nUsted retiro : " << monto << " Su saldo actual es : " << obtenerMonto() << "\n\n";
}
}

void Cuenta1::depositarSaldo( int monto ){

if(( montoUsuario <= 0)){

montoUsuario = montoUsuario + monto;

cout << "\n\nUsted deposito : " << monto << " su saldo actual es : " << obtenerMonto() << "\n\n";
}

if(( montoUsuario > 0 )){

montoUsuario = montoUsuario + monto;

cout << "\n\nUsted deposito : " << monto << " su saldo actual es : " << obtenerMonto() << "\n\n";
}
}

//****************( " end of cuenta.cpp " )****************



//****************( " main.cpp " )****************

#include<iostream>
#include<cstdlib>
#include "Cuenta.h"
using namespace std;

int main(){

Cuenta1 cuentaLuis( "Luis Alberto Sanchez M", 28, 3600, 1988, "Luis" );
cuentaLuis.pedirContrasenia();
Cuenta1 cuentaDiana( "Diana Carolina Sanchez M", 25, 4500, 1991, "Diana" );
cuentaDiana.pedirContrasenia();

system("PAUSE");
return 0;
}

//****************( " end of main.cpp " )****************

also in the part of my code where I make use of Cuenta1::mostrarMensaje(), originally I wanted the program to decide depending on the input of the user if option "a" user input were "A" and option b user input were "B" using getline( cin, opcion ) where "opcion" is a string variable whether it can be "A" or "B" but it does not seems to work that way that is why i used an int variable "opcion" to make the work done, still I am curious about changing that part of my code so that the input of the user is "A" or "B" rather than 1 or 2.


string opcion;
getline( cin, opcion )

if( opcion == "A" ){

cout << "\n\nCout Cuanto desea retirar? ";
cin >> cantidadRetiro;
retirarSaldo( cantidadRetiro );
}

if( opcion == "B" ){

cout << "\n\nCuanto desea abonar? ";
cin >> cantidadAbono;
depositarSaldo( cantidadAbono );
}

anda_skoa
7th November 2016, 08:46
I also make use of: cuentaLuis.pedirContrasenia(); within main instead of within the constructor.
and also cuentaDiana.pedirContrasenia();

but for me the question remains why not to allow the constructor to do all the processing??.

You are mixing questions/answers from two different threads.
jefftee answered that one on the other thread.



Will it cause conflicts by compiling the program that way or is just a matter of readability?

As it compiled for you it couldn't have caused conflicts for compiling, could it?



how much further must I allow a constructor to handle the processing of any program?

A constructor initializes an object of a class. It has to do whatever is necessary to achieve that.
It can do other things as well, but again, see jefftee's comments in your other thread.



also in the part of my code where I make use of Cuenta1::mostrarMensaje(), originally I wanted the program to decide depending on the input of the user if option "a" user input were "A" and option b user input were "B" using getline( cin, opcion ) where "opcion" is a string variable whether it can be "A" or "B" but it does not seems to work that way that is why i used an int variable "opcion" to make the work done, still I am curious about changing that part of my code so that the input of the user is "A" or "B" rather than 1 or 2.

What have you debugged so far?
Is the input either "A" or "B"?

Cheers,
_