Access the attributes of a class from one method of another

0

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();

    }
    
asked by Juan Castillo 28.04.2018 в 23:06
source

2 answers

0

The CostBase attribute is a class attribute, which means that you need to create a new object / instance of the Product class, then what you can do is that in your function Sale :: Saleprice, pass that parameter to you as a parameter you created Product ... Your code would then look something like this:

float Venta::precioVenta(Producto* producto)
{
    // como ya tenemos una referencia al objeto producto ya podemos usar sus métodos
    float costoBase = producto->getCostoBase();

    if (this->tipoVenta == "Detal")
    {
        float precio = (costoBase * 0.30) + costoBase;
        return precio;
    }
    else if (this->tipoVenta == "Mayor")
    {
        float precio = (costoBase * 0.15) + costoBase;
        return precio;
    }

    return 0;
}

/// Continuing with the response to the error that throws you ..., and in theory the changes you have made to your function Sale :: Price Sale. Remember that the function Sale :: PrecioVenta, receives as a parameter an instance of the Product class, which is not happening when you invoke the function in Sale :: Total Sale, that's why it says "not match", which means that there is no function that matches that function signature.

    
answered by 29.04.2018 / 00:44
source
0

CostBase is a Product attribute and you can not just call it. To be able to access the attributes of a class instance, you must first create it.

Greetings.

David

    
answered by 29.04.2018 в 00:18