I'm starting with C ++, I'd like to know how to use the attributes of one class in another, for example, the attribute CostoBase
should use it in the method PrecioVenta()
, try to create a variable in the class Venta
and equal it to the method setCostoBase
, so I could use what I get in that method but I keep giving error.
class Producto {
private:
string Codigo;
float CostoBase; // necesito utilizarlo en la 2da clase
public:
Producto ( );
void setCodigo(string codigo);
void setCostoBase( float costo);
string getCodigo( );
float getCostoBase( );
};
class Venta {
private:
int CantProdVend;
string TipoVenta;
public:
Venta( );
void setCantProdVend(int cantidad);
void setTipoVenta (string tipo);
int getCantProdVend( );
string getTipoVenta( );
float PrecioVenta( ); // aqui la debo utilizar
float VentaTotal( );
};
________________________________________________________________________________
float Venta::PrecioVenta( ) {
float precio;
if (TipoVenta == "Detal") {
precio = (CostoBase * 0.30) + CostoBase;
return precio;
} else if ( TipoVenta== "Mayor") {
precio= (CostoBase * 0.15) + CostoBase;
return precio;
} else {
precio= 0;
return precio;}
}
Following the suggestions given to me, create and instance a new object of the product class and pass it by parameter in the PriceOnline function, but now it generates an error in the following function Total Sales:
I need to change something in the class statement?
#include <string>
using namespace std;
class Producto {
private:
string Codigo;
float CostoBase;
public:
Producto ( );
void setCodigo(string codigo);
void setCostoBase( float costo);
string getCodigo( );
float getCostoBase( );
};
class Venta {
private:
int CantProdVend;
string TipoVenta;
public:
Venta( );
void setCantProdVend(int cantidad);
void setTipoVenta (string tipo);
int getCantProdVend( );
string getTipoVenta( );
float PrecioVenta(Producto elProducto);
float VentaTotal();
};
The implementation of the methods:
#include "venta_producto.h"
Producto::Producto( ) { };
void Producto::setCodigo (string codigo) {
Codigo = codigo;
}
string Producto::getCodigo( ) {
return Codigo;
}
void Producto::setCostoBase( float costo) {
CostoBase = costo;
}
float Producto::getCostoBase ( ) {
return CostoBase;
}
Venta::Venta( ) { };
void Venta::setCantProdVend (int cantidad) {
CantProdVend = cantidad;
}
int Venta::getCantProdVend( ) {
return CantProdVend;
}
void Venta::setTipoVenta(string tipo) {
TipoVenta = tipo;
}
string Venta::getTipoVenta( ) {
return TipoVenta;
}
float Venta::PrecioVenta(Producto elProducto) {
float costo = elProducto.getCostoBase();
float precio;
if (TipoVenta == "Detal") {
precio = (costo * 0.30) + costo;
return precio;
} else if ( TipoVenta== "Mayor") {
precio= (costo * 0.15) + costo;
return precio;
} else {
precio= 0;
return precio;}
}
float Venta::VentaTotal() {
float total;
total = PrecioVenta(Producto elProducto) * CantProdVend;// aca se genera el error: [Error] expected primary-expression before 'elProducto'
return total;
}
principal.cpp:
#include "venta_producto.h"
#include <iostream>
void IEProducto (Producto & elProducto);
void IEVenta (Venta & laVenta);
void IS (Producto elProducto, Venta laVenta);
int main () {
Producto elProducto;
Venta laVenta;
IEProducto (Producto & elProducto);
IEVenta (Venta & laVenta);
IS (Producto elProducto, Venta laVenta);
return 0;
}
void IEProducto (Producto & elProducto) {
string codigo;
float costo;
cout << "Introduzca el codigo del producto: ";
cin >> codigo;
elProducto.setCodigo(codigo);
cout << "Coloque el precio base del producto: ";
cin >> costo;
elProducto.setCostoBase(costo);
}
void IEVenta (Venta & laVenta) {
int cantidad;
string tipo;
cout << "Introduzca la cantidad de productos a vender: ";
cin >> cantidad;
laVenta.setCantProdVend(cantidad);
cout << "Coloque el tipo de venta segun cantidad de articulos a
vender (Mayor/Detal): ";
cin >> tipo;
laVenta.setTipoVenta(tipo);
}
void IS (Producto elProducto, Venta laVenta) {
cout << "el precio de venta por articulo es: " <<
laVenta.PrecioVenta(Producto elProducto);
cout << "Monto a pagar: " << laVenta.VentaTotal();
}