Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Producto{
- private:
- string Nombre;
- int Cantidad;
- public:
- Producto(){
- return;
- }
- Producto(string nom,int cant){
- Nombre=nom;
- Cantidad=cant;
- return;
- }
- //operador de entrada
- friend istream& operator>>(istream &medio, Producto p){
- cout<<"Nombre del producto: "; medio>>p.Nombre;
- cout<<"Cuantos quiere?: "; medio>>p.Cantidad;
- return medio;
- }
- //operador de salida
- friend ostream& operator<<(ostream &medio, Producto p){
- medio<<p.Cantidad<<" de "<<p.Nombre<<endl;
- return medio;
- }
- };
- class Compra{
- private:
- Producto *V;
- int N;
- void borrar(){
- //Esto es lo que tendria q tener el destructor
- //liberar la memoria
- if (V!=NULL){
- delete[]V;
- }
- //pone todo a 0
- V=NULL;
- N=0;
- return;
- }
- void copiar(const Compra &origen){
- //lo que haria el contructor de copia. hace una copia de todos los elementos
- N=origen.N;
- V=NULL;
- if(N>0){
- V=new Producto[N];
- for(int i=0;i<N;i++){
- V[i]=origen.V[i];
- }
- }
- return;
- }
- public:
- Compra(){
- N=0;
- V=NULL;
- return;
- }
- Compra(const Compra &origen){
- copiar(origen);
- return;
- }
- ~Compra(){
- borrar();
- }
- Compra operator=(Compra origen){
- //libera memoria y pone todo a 0
- borrar();
- //hace una copia de todos los elementos
- copiar(origen);
- return(*this);
- }
- friend ostream& operator<<(ostream &medio, Compra c){
- for(int i=0;i<c.N;i++){
- medio<<c.V[i]<<endl;
- }
- return medio;
- }
- Compra operator+(const Producto &p){
- Compra nueva;
- nueva.V = new Producto [N + 1];
- nueva.N = N + 1;
- for(int i = 0; i < N; i++){
- nueva.V[i] = V[i];
- }
- nueva.V[nueva.N-1] = p;
- return nueva;
- }
- Compra operator+=(const Producto &p){
- *this = *this + p;
- return *this;
- }
- };
- int main(void){
- Compra c;
- c=c+Producto("queso",2);
- c=c+Producto("salchichon",3);
- Producto p;
- cout<<"Introduzca producto\n";
- cin>>p;
- c+=Producto("salchichon",5);
- cout<<"\n Compra: "<<endl<<c;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement