Sledeci program radi OK, ali bih ipak imao par pitanja.
// Sve sto jednom gradjaninu moze zatrebati
// vezano za pravougaonik
# include <iostream.h>
# include <conio.h>
typedef unsigned short int USHORT;
typedef unsigned long int ULONG;
enum BOOL {FALSE, TRUE};
enum CHOICE {DrawRect = 1, GetArea,
GetPerim, ChangeDimensions, Quit};
//Deklaracija klase pravougaonik
class Rectangle
{
public:
//konstruktori
Rectangle (USHORT width, USHORT height);
~Rectangle();
//metode pristupa
USHORT GetHeight() const {return itsHeight;}
USHORT GetWidht() const {return itsWidht;}
ULONG GetArea() const {return itsHeight * itsWidht;}
ULONG GetPerim() const {return 2*itsHeight + 2*itsWidht;}
void SetSize(USHORT newWidht, USHORT newHeight);
//Razne metode (koje se ovde ne koriste)
void DrawShape()const;
private:
USHORT itsWidht;
USHORT itsHeight;
}; //kraj deklaracije klase
//Implementacija metoda klase
void Rectangle::SetSize(USHORT newWidht, USHORT newHeight)
{
itsWidht = newWidht;
itsHeight = newHeight;
}
Rectangle::Rectangle(USHORT widht, USHORT height)
{
itsWidht = widht;
itsHeight = height;
}
Rectangle::~Rectangle() {}
//deklaracija f-ja NECLANICA
USHORT DoMenu();
void DoDrawRect(Rectangle);
void DoGetArea(Rectangle);
void DoGetPerim(Rectangle);
void main()
{
//inicijalizuje pravougaonik na 30, 5
Rectangle theRect(30,5);
USHORT choice = DrawRect;
USHORT fQuit = FALSE;
while (!fQuit)
{
choice = DoMenu();
if (choice < DrawRect || choice > Quit )
{
cout << "\n Nevazeci izbor, molim Vas pokusajte ponovo.\n\n";
continue;
}
switch (choice)
{
case DrawRect:
DoDrawRect(theRect);
break;
case GetArea:
DoGetArea(theRect);
break;
case GetPerim:
DoGetPerim(theRect);
break;
case ChangeDimensions:
USHORT newLenght, newWidht;
cout << "\n Nova sirina: ";
cin >> newWidht;
cout << " \n Nova visina: " ;
cin >> newLenght;
theRect.SetSize(newWidht, newLenght);
DoDrawRect(theRect); //opciono
break;
case Quit:
fQuit = TRUE;
cout << "\nIzlazak....\n\n";
break;
default:
cout << "Greska u izboru!\n"; //ne vidim kako ikada
fQuit = TRUE;
break;
} // kraj switch
} // kraj while
getch();
} // kraj main
// Definicija f-ja NECLANICA
USHORT DoMenu()
{
USHORT choice;
cout << "\n\n ***Meni***\n";
cout << "(1) Nacrtaj pravougaonik\n";
cout << "(2) Izracunaj povrsinu\n";
cout << "(3) Izracunaj obim\n";
cout << "(4) Promeni velicinu\n";
cout << "(5) Izlaz\n";
cin >> choice;
return choice;
}
void DoDrawRect(Rectangle theRect)
{
USHORT height = theRect.GetHeight();
USHORT widht = theRect.GetWidht();
cout << "\n";
for (USHORT i=0; i<height; i++)
{
for (USHORT j=0; j<widht; j++)
cout << "*";
cout << "\n";
}
}
void DoGetArea(Rectangle theRect)
{
cout << "Povrsina: " << theRect.GetArea() << endl;
}
void DoGetPerim(Rectangle theRect)
{
cout << "Obim: " << theRect.GetPerim() << endl;
}
Autor je skrenuo paznju da ChangeDimensions ne moze pozvati f-ju, kao npr. DoChangeDimensions(), jer bi u tom sl. dimenzije bile promnjene za lokalnu kopiju pravougaonika u DoChangeDimensions() a ne za pravougaonik u main().
Ovo mi nije bas jasno.
Radi unifikacije u switch case, bilo bi bas zgodno da postoji upravo takva f-ja.
Ako bi postojala f-ja DoChangeDimensions(), koja bi bila identicna bloku ispod case ChangeDimensions (znaci radila bi:
theRect.SetSize(newWidht, newLenght); ne vidim kako ovo ne bi promenilo pravougaonik u main().
I jos nesto. Nije mi jasno kada moze doci do default-a u switch-u.
I jos nesto (2). Zasto su deklarisane nabrojane konstante tipa BOOL ?
Mislim, ocigledno je, da bi se koristile u while testu kao (0) i (1), ali zar TRUE i FALSE nisu sluzbene reci koje bi mogle da se koriste direktno u while testu ? (pa dakle ako je fQuit=FALSE onda je !fQuit=TRUE)
Hvala