Нахождение ошибки в наследовании и приведении типов - C#
Формулировка задачи:
имеется рабочий код на с++, пыталась переделать его в С#, возникают ошибки,которые я не в состоянии сама исправить. Буду безумно признательна, если кто - то попробует мне помочь
прилагаю код на с++ и мою попытку на С#
и вдруг понадобиться на с++ код
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace ComplexNumbers
{
class Program
{
static void Main(string[] args)
{
Complex a = new Complex(200,10);
Complex b = new Complex(4, 5);
Complex g = a - b;
Console.WriteLine("a =" + "(" + a.re + "," + a.im + ")");
Console.WriteLine("b =" + "(" + b.re + "," + b.im + ")");
Console.WriteLine("-------------------------------------");
Console.WriteLine("c="+"("+g.re.ToString() + "," + g.im.ToString()+")");
Complex v = a + b;
Console.WriteLine( "c=" +"(" + v.re.ToString() + "," + v.im.ToString() + ")");
//
Complex d = a * b;
Console.WriteLine("c=" + "(" + d.re.ToString() + "," + d.im.ToString() + ")");
Complex c = a / b;
Console.WriteLine("c=" + "(" + c.re.ToString() + "," + c.im.ToString() + ")");
//1! ////**********полиморизм.один указатель на базовый класс указывает на объекты базового
//и производного класса и вызывают виртуальный метод
Complex *pComplex;//указ на баз.клас и произ
Vector x=new Vector(7, 8);
Console.WriteLine("d =" + "(" + d.re + "," + d.im + ")");
Console.WriteLine("x =" + "(" + x.re + "," + x.im + ")");
pComplex = &x;
Vector t=x*d;
Console.WriteLine("c="+"("+t.re.ToString() + "," + t.im.ToString()+")");
pComplex ->Show();//вызов вирт.метода
Console.WriteLine("vector d ");
pComplex = &d;
pComplex ->Show();
Console.WriteLine("vector b*x ");
(b*x).Show();
Console.WriteLine("vector e+x");
(b+x).Show();
////
Console.ReadKey(true);
}
//класс комплекс
public class Complex
{
private float _re;
private float _im;
public float re
{
get { return _re; }
private set { _re = value; }
}
public float im
{
get { return _im; }
private set { _im = value; }
}
public Complex(float r, float m)
{
re = r;
im = m;
}
public Complex() //конструктор без параметров
{
re = im = 0;
}
public static Complex operator +(Complex C1, Complex C2)
{
return new Complex(C1.re + C2.re, C1.im + C2.im);
}
public static Complex operator -(Complex C1, Complex C2)
{
return new Complex(C1.re - C2.re, C1.im - C2.im);
}
///
public static Complex operator *(Complex C1, Complex C2)
{
Complex C = new Complex();
C.re = (C1.re * C2.re - C1.im * C2.im);
C.im = (C2.re * C1.im + C1.re * C2.im);
return C;
}
public static Complex operator /(Complex C1, Complex C2)
{
Complex C = new Complex();
C.re = (C1.re * C2.re + C1.im * C2.im) / (C2.re * C2.re + C2.im * C2.im);
C.im = (C2.re * C1.im - C2.im * C1.re) / (C2.re * C2.re + C2.im * C2.im);
return C;
}
float priv (float re)
{
return re;
}
float mnim (float im)
{
return im;
}
}
//класс вектор, наследование
public class Vector: Complex
{
private float _re;
private float _im;
public float re
{
get { return _re; }
private set { _re = value; }
}
public float im
{
get { return _im; }
private set { _im = value; }
}
public Vector(float r, float m)
{
re = r;
im = m;
}
public Vector ()
{
re = im = 0;
}
//2! //----приведение типов-----//
public static Vector& operator *(Complex C1, Complex C2)
{
Vector C3 = new Vector();
Complex C = new Complex();
C.re=C3.priv(re);
Complex.im=C3.mnin(im);
return Vector(re,im);
}
}
}
}class Complex
{protected:
float Re, Im; //private
public:
Complex(float R,float M) //конструкторы с параметрами
{
//Re = Im = 0;
Re=R;
Im=M;
}
Complex(void) //конструктор без параметров
{
Re = Im = 0;
}
Complex& operator - (Complex&C2);
Complex& operator *(Complex C2); //объявляем в классе
Complex& operator /(Complex C2);
virtual void Show() //метод для отображения переменной на экране Complex *pComplex;
{
if (Im > 0)
printf("(%.2f%;%.2f%)\n", Re, Im);
else printf("(%.2f%;%.2f%)\n", Re, Im);
}
Complex operator + (Complex C2) //а здесь в классе Complex -оператор
{
Complex C;
C.Re = Re + C2.Re;
C.Im = Im + C2.Im;
return C;
}
float priv (float Re)
{
return Re;
};
float mnim (float Im)
{
return Im;
}
};
//закрывается класс
class Vector:public Complex
{
public:
Vector (float re, float im)
{
Re=re; Im=im;
}
virtual void Show()
{printf("(%.2f%;%.2f%)\n",Re, Im); }
/*Vector& Complex& Complex:: operator *(Complex&C2)*/
Vector& operator *(float c)
{
Vector tmp(0,0);
tmp.Re=Re*c;//для которого вызван метод перед *
tmp.Im=Im*c;
return tmp;
}
Vector& operator=(Complex c)
{
Re=c.priv(Re);
Im=c.mnim(Im);
return Vector(Re,Im);
}
};
//------------------
Complex& Complex::operator - (Complex&C2)
{
Complex C;
C.Re = Re - C2.Re;
C.Im = Im - C2.Im;
return C;
}
Complex& Complex::operator *(Complex C2) // Complex:: означает,что оператор относиться к классу
{Complex C;
C.Re=(Re*C2.Re-Im*C2.Im);
C.Im=(C2.Re*Im+Re*C2.Im);
return C;
}
Complex& Complex::operator /(Complex C2)
{
Complex C;
C.Re=(Re*C2.Re+Im*C2.Im)/(C2.Re*C2.Re+C2.Im*C2.Im);
C.Im=(C2.Re*Im-C2.Im*Re)/(C2.Re*C2.Re+C2.Im*C2.Im);
return C;
}
int main()
{
Complex a(7,8);
printf("object a ");
a.Show();
//Complex C;
//C.Show();
Complex b(10,10);
printf("object b ");
b.Show();
Complex c = a.operator +(b);
printf("a+b ");
c.Show();
//c=a+b;//вызов в операторном вмде
//c.Show();
c = a.operator - (b); //
c=a-b;
printf("a-b ");
c.Show();
c=a*b;
printf("a*b ");
c.Show();
c=a/b;
printf("a/b ");
c.Show();
//-----------------
//////////
Complex *pComplex;//указ на баз.клас и произ
//Complex e(10, 10);
Vector d(7, 8);
printf("vector b ");
pComplex = &b;
pComplex ->Show();//вызов вирт.метода
printf("vector d ");
pComplex = &d;
pComplex ->Show();
//////////
printf("vector b*d ");
(b*d).Show();
printf("vector e+d");
(b+d).Show();
////
_getch();
return 0;
};Решение задачи: «Нахождение ошибки в наследовании и приведении типов»
textual
Листинг программы
public override string ToString()
{
return string.Format("x={0}; y={1}", X, Y);
}