Jump to content

[C++/CLI] Dúvidas: Overloadable operators


Recommended Posts

Posted

Boas... Tenho umas duvidas... Eis a ficha... (atenção: nao entendam isto komo um pedido de resoluçao de um T.P.C kualker...)

http://img182.imageshack.us/img182/6999/digitalizar0001kd5.jpg

E eu até ao momento tenho o seguinte codigo:

#include "stdafx.h"

using namespace System;

ref class pai
{
private:
String^nome_pai;
Byte idade_pai;
Int16 salario_pai;
Single riqueza;
protected:		
String^apelido;
public:
pai():nome_pai("Paizinho"), apelido ("Mesminho"), idade_pai(52), salario_pai(2000), riqueza(7000){}
void imprime_pai()
{
	Console::WriteLine(nome_pai + " " + apelido + " " + idade_pai + " " + salario_pai + " " + riqueza);
}
virtual Int16 salario_anual()
{	
	return salario_pai * 12;
}

void ver_salario()
{
	Console::WriteLine("O salário é: {0} ", salario_anual());
}

};

ref class filho:public pai
{
private:
String^nome_filho;
Byte idade_filho;
Int16 salario_filho;
public:
filho():nome_filho("filhote"), idade_filho(18), salario_filho(150){};
property String^NomeFilho
{
	void set(String^m)
	{
		nome_filho=m;
	}
	String^get()
	{
		return nome_filho;
	}
}
property Byte IdadeFilho
{
	Byte get()
	{
		return idade_filho;
	}
}
void imprime_filho()
{
	Console::WriteLine(nome_filho + " " + apelido + " " + idade_filho + " " + salario_filho);
}
virtual Int16 salario_anual() override
{	
	return salario_filho * 12;
}

};
ref class familia abstract
{
public:
	virtual Single devolve_riqueza()=0;
		void imprime_riqueza(void)
		{
			Console::WriteLine(devolve_riqueza());
		}
};
ref class irmao:public familia
{
private:
	Single riqueza2;
public:
	irmao(): riqueza2(50000){};
	virtual Single devolve_riqueza() override
	{
		return 12*riqueza2;
	}
};



int main(array<System::String ^> ^args)
{
pai^ob1=gcnew pai();
filho^ob2=gcnew filho();
irmao^ob4=gcnew irmao();
ob1->imprime_pai();
ob2->imprime_filho();
ob1->ver_salario();
ob2->ver_salario();
ob4->imprime_riqueza();
    return 0;
}

Acho que tenho isto "bem" até agora, mas falta me a questão nr12, que gostava que alguem me explica-se...  :dontgetit:

Posted

Isso é uma coisa fantástica do C++ 😉

O que tu tens de acrescentar na classe irmão é isto:

irmao& irmao::operator+ (irmao)
{
/*codigo*/
}
irmao& irmao::operator> (irmao)
{
/*codigo*/
}

Para mais informações deixo aqui uns bons links sobre isso:

http://www.cplusplus.com/doc/tutorial/classes2.html

http://www.parashift.com/c++-faq-lite/operator-overloading.html

Espero ter ajudado.

Cumpr. bk@ero  😉

Posted

isso não devia estar separado em vários ficheiros?

dois ficheiros para cada classe (um .cpp e um .h)?

fica aqui um exemplo de sobrecarga de operadores, eu normalmente uso funções friend para fazer isso...

Par.h

class Par
{
  private:
    int x;
    int y;

  public:
    Par();
    Par(int x,int y);
    int getX(){return this->x;}
    int getY(){return this->y;}

  friend bool operator>(Par& x1,Par& x2);
  friend Par& operator+(const Par& x1,const Par& x2);
};

Par.cpp

#include "Par.h"

Par::Par()
{
  this->x=0;
  this->y=0;
}

Par::Par(int x,int y)
{
  this->x=x;
  this->y=y;
}

bool operator>(Par& x1,Par& x2)
{
  return (x1.x+x1.y)>(x2.x+x2.y);
}

Par& operator+(const Par& x1,const Par& x2)
{
  Par* res=new Par(x1.x+x2.x,x1.y+x2.y);
  return *res;
}
Posted

acho que deve ser uma coisa do género:

int irmao::operator+ (irmao other){

int res=riqueza;  // valor de res é igual ao da riqueza da classe em que estamos
res+=other.devolve_riqueza() ; soma se a riqueza do outro
return res;

}

bool irmao::operator> (irmao)
{

if(riqueza>other.devolve_riqueza()){
     return true;
     }else{return false}

}

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.