0% found this document useful (0 votes)
29 views

Class Struct

The document describes a programming exercise in C# that defines a class and structure to represent complex numbers. It defines a ComplexNumberClass and ComplexNumberStructure with real and imaginary parts. Constructors are added to initialize the parts. The ToString method is overridden to print the numbers. Instances of each are created and their parts printed. New instances are assigned the values of the first but changing the new instances does not change the original, showing the difference between reference and value types.

Uploaded by

Ivan Bitunjac
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Class Struct

The document describes a programming exercise in C# that defines a class and structure to represent complex numbers. It defines a ComplexNumberClass and ComplexNumberStructure with real and imaginary parts. Constructors are added to initialize the parts. The ToString method is overridden to print the numbers. Instances of each are created and their parts printed. New instances are assigned the values of the first but changing the new instances does not change the original, showing the difference between reference and value types.

Uploaded by

Ivan Bitunjac
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 1

Programiranje u C#

Laboratorijske vjebe
Zadatak: Razlika izmeu klase i strukture (referentni, odn. vrijednosni tip)
Definirati klasu i strukturu koje e svaka za sebe predstavljati kompleksni broj:
class KompleksniBrojKlasa { public double RealniDio; public double ImaginarniDio } struct KompleksniBrojStruktura { public double RealniDio; public double ImaginarniDio }

Dodati konstruktore za oba tipa, koji e kao argumente primati dva doublebroja: realni i imaginarni dio novog kompleksnog broja. Takoer, za oba tipa pregaziti (override) metodu ToStringkoja e omoguiti formatirani ispis kompleksnog broja:
public override string ToString() { return string.Format("{0} + {1}i", RealniDio, ImaginarniDio); }

Inicijalizirati po jedan kompleksni broj referentnog, odnosno vrijednosnog tipa:


KompleksniBrojKlasa kbk = new KomplesniBrojKlasa(1, 2); KompleksniBrojStruktura kbs = new KompleksniBrojStruktura(3, 4);

te ispisati njihove realne i imaginarne dijelove. Definirati nove instance oba tipa i pridruiti im vrijednosti postojeih:
KompleksniBrojKlasa kbk2 = kbk; KompleksniBrojStruktura kbs2 = kbs;

te ispisati njihove realne i imaginarne dijelove. Promijeniti vrijednosti lanova novih instanci, npr.:
kbk2.RealniDio = 5; kbs2.RealniDio = 6;

te ispisati realne (i imaginarne) dijelove svih instanci: kbk, kbs, kbk2 i kbs2. to moete zakljuiti na osnovu ispisanih vrijednosti?

You might also like