|
. 1.1.
(class head), class, . (class body), . public private , . C++, C++. :
class Class_Name
{
private: //
//
//
public: //
//
//
};
, , . - . .
1.
Rectangle
. . . ADT , 1, Rectangle C++, ADT. GetLength, PutLength, GetWidth PutWidth, . Rectangle :
class Rectangle
{
private:
//
float length, width
public:
//
Rectangle(float l=0, float w=0);
//
float GetLength(void) const;
void PutLength(float 1);
float GetWidth(void) const;
void PutWidth(float w);
//
float Perimeter(void) const;
float Area(void) const; I
}
, GetLength, GetWidth, Perimeter Area const . . . , , const, Rectangle.
|
|
, . . C++ , , .
, (constructor) , , . C++, , - . Rectangle Rectangle, l w, , . , , , 0, l w .
1 (class definition), . C++ (class implementation).
. (instance) . - . . , , :
ClassName object(<parameters>);//
//
, Rectangle:
Rectangle room(12, 10);
Rectangle t; // (0, 0).
- , . , . (). :
= room.Area(); // 12*10=120
t.PutLength(20); // 20 Rectangle
// 0
// ,
cout<<t.GetWidth(); // , = 0
//
Room , 12, 10. , PutLength PutWidth:
room.PutLength(15); // 15 12
room.PutWidth(12);
. , , . , Rectangle , . , . - .
Rectangle float. , C++, . , .
|
|
. ( ) . , , . "::" (scope resolution operator) , . . Rectangle "Rectangle::" .
GetLength(), Rectangle:
float Regtangle::GetLength(void) const
{
return length; // length
}
, const.
- . (expanded inline), , . GetLength :
class Rectangle
{
private:
float length;
float width;
public:
float GetLength(void) const // inline
{
return(length);
}
};
- , .
inline . , Rectangle:
Rectangle::Rectangle(float 1, float w)
{
length = l;
width = w;
}
C++ . (member initialization list) - , , , . , - . :
ClassName::ClassName(parm list):data1(parm1),..., datan(parmn)
, l w - length width:
Rectangle::Rectangle(float l, float w): length(l), width(w)
{}
- . , :
Rectangle square(10, 10), yard = square, S;
square length width, 10. yard , square. S length width, 0.
. , - . ,
S = yard;
yard S. length width yard length width S.
. , Rectangle(10,5) lengh = 10 width = 5. rectangle S:
S = Rectangle(10,5);
2.
1.
S = Rectangle(10,5);
cout << S.Area() << endl;
cout 50.
2.
cout << Rectangle(10,5).GetWidth() << endl;
5.
1. Rectangle
. , . , . .
|
|
, . , . , "Quit". . $2 . $0,50 .
. .
//Example1.
#include <iostream.h>
class Rectangle
{
private:
//
float length, width;
public:
//
Rectangle(float l=0, float w = 0);
//
float GetLength(void) const;
void PutLength(float 1);
float GetWidth (void) const;
void PutWidthffloat w);
//
float Perimeter (void) const;
float Area(void) const;
}
//, : length=l, width=w
Rectangle:: Rectangle(float l, float w): length(l), width(w)
{}
//
float Rectangle:: GetLength(void) const
{
return length;
}
//
void Rectangle:: PutLength(float l)
{
length = l;
}
//
float Rectangle:: GetWidth(void) const
{
return width;
}
//
void Rectangle:: PutWidth(float w)
{
width = w;
}
//
float Rectangle:: Perimeter(void) const
{
return 2.0*(length + width);
}
//
float Rectangle:: Area(void) const
{
return length*width;
}
void main(void)
{
// -
const float sidingCost = 2.00, moldingCost = 0.50;
int completedSelections = 0;
// ,
char doorOption;
///
float glength, gwidth, doorCost;
// , ,
float totalCost;
cout << " : ";
cin >> glength >> gwidth;
// garage()
// door()
Rectangle garage(glength, gwidth);
Rectangle door;
while (!completedSelections)
{
cout << " 1-4 'q' " << endl << endl;
cout << " 1 (128; $380) "
cout << " 2 (12x10; $420) "<<endl;
cout << " 3 (16x8; $450) "
cout << " 4 (16 x10; $480)" << endl;
cout << endl;
cin >> doorOption;
if(doorOption == 'q')
completedSelections = 1;
else
switch (doorOption)
{
case '1':door.PutLength(12); // 12 x 8 ($380)
door.PutWidth(8);
doorCost = 380;
break;
case '2':door.PutLength (12); // 12 x 10 ($420)
door.PutWidth(10);
doorCost = 420;
break;
case '3':door.PutLength (16); // 16 x 8 ($450)
door.PutWidth(8);
|
|
doorCost = 450;
break;
case '4':door.PutLength (12); //16x10 ($480)
door.PutWidth(10);
doorCost = 480;
break;
}
totalCost=doorCost+moldingCost*(garage.Perimeter()+
door.Perimeter())+sidingCost* (garage.Area()-door.Area());
cout <<" , :$" <<
totalCost << endl << endl;
}
}
}
/*
< 3.1>
: 1-4 'q'
1 (128; $380) 2 (1210; $420)
3 (168; $450) 4 (1610; $480)
, : $720
1-4 ' q'
1 (128; $380) 2 (1210; $420)
3 (16x8; $450) 4 (16x10; $480)
q
*/
++. Temperature . ( ) ( ) . ADT RandomNumber . C++ - .
Temperature
Temperature . - highTemp lowTemp, . UpdateTemp , . , lowTemp. , highTemp. : GetHighTemp , a GetLowTemp .
Temperature
class Temperature
{
private:
float highTemp, lowTemp; // -
public:
Temperature(float h, float l);
void UpdateTemp(float temp);
float GetHighTemp(void) const;
float GetLowTemp(void) const;
};
. UpdateTemp. GetLowTemp GetHighTemp , - .
// /
Temperature fwater(212,32);
// /
Temperature cwater(100, 0);
cout << <<cwater.GetLowtemp << " "<< endl;
cout << << fwater.GetHighTemp << " F" << endl;
:
0
212 F
Temperature
. , highTemp lowTemp. UpdateTemp, . GetHighTemp GetLowTemp .
//: : highTemp=h lowTemp=l
Temperature::Temperature(float h, float l): highTemp(h),
lowTemp(l)
{}
//
void Temperature::UpdateTemp (float temp)
{
if (temp>highTemp)
highTemp = temp;
else if (temp<lowTemp)
lowTemp = temp;
}
// high ( )
float Temperature::GetHighTemp (void) const
{
return highTemp;
}
// low ( )
float Temperature::GetLowTemp (void) const
{
return lowTemp;
}
2. Temperature
// Example2.cpp
#include <iostream.h>
#include "temp.h"
void main (void)
{
Temperature today (70,50);
float temp;
cout << " : ";
cin >> temp;
//
today.UpdateTemp (temp);
cout << " : :" << today.GetHighTemp ();
cout << " " << today.GetLowTemp ()<<endl;
cout << " : ";
cin >> temp;
//
|
|
today.UpdateTemp (temp);
cout << " :" << today.GetHighTemp();
cout << " " << today.GetLowTemp() << endl;
}
/*
<3 pr03_02.cpp>
: 80
: :80 50
: 40
:80 40
*/
, . , , , , , , , , . (random number generator), , . , , , , seed-. . , . , . , , (pseudorandom numbers), . seed-, seed-. , , . , . . , seed- , . , .
, , , . , , RandomNumber, seed-, . seed- . seed-, , .
RandomNumber
#include <time.h>
//
// seed-
const unsigned long maxshort = 65536L;
const unsigned long multiplier = 1194211693L;
const unsigned long adder = 12345L;
class RandomNumber
private:
// , seed-
unsigned long randSeed;
public:
//, 0
// seed-
RandomNumber(unsigned long s = 0);
// [0, n-1]
unsigned short Random(unsigned long n);
// [0, 1.0]
double fRandom(void);
};
seed- . Random n <= 65536 16- 0,..., n1. , Random , , n n<215=32768. fRandom 0 < fRandom() < 1.0.
RandomNumber rnd; // seed-
RandomNumber R(1); //
// seed 1
cout << R.fRandom(); //
// 01
// 5 099
for (int i = 0; i < 5; i++)
cout << R.Random(100) << " "; // <sample> 93 21 45 5 3
3.
1. 1¸6 ( ). die.Random(6), 0¸5. 1 .
RandomNumber Die // seeding
dicevalue = die.Random(6) +1;
2. FNum seed- :
RandomNumber FNum;
50<=<75 0¸25, f Random 25. 1- (0<<1) 25 (0<<25). , 50:
value=FNum.fRandom()*25+50; // 25; 50