���� 14. ������� �� �������� �� ���������
������ ���������, ����� ������������, � ���������� �� ����������� �� �������� ������� � ���� 5, �� ������� �������� � ���� ���������� � ��������� ���� void ���� ����� ��. ������������� �������� �� ����������� �� (���������� ��) ������, ����� ������� ���� ���������. �� ��� ����������, ����� ������ �� ���� ������� �� ���������, �� � �� ���� �� ������, ����� ������ � ���������� � ��������� ������ �� �� �������� �� ����� ��������.
1. �������� ������ �� ���������� �����. � ���� 10 ��������� ����, ����� ��������� ������������ �����. ���� ����, � ������� �������� �� �������� �� ���������� � ������������ ����� � ����� �������� � ���, ����� � ����� � ���� ����� �� ���������� �����, ����� ��� �� ������ �������� ��� ��������� �������� �� ������������ �����. ��� ���� ������ ��������, ����� �������� ������ �� ���������� �����:
//
complex1.cpp
//
a class that models a complex number data type
#include <iostream.h>
class
Complex_number
{
private:
float real; // real
part
float imaginary; // imaginary part
public:
void set()
{
char dummy; // for comma
cout << "Enter number (format Re, Im): ";
cin >> real >> dummy >> imaginary;
}
void display()
{
cout << '('
<< real // real part
<< ','
<< imaginary // imaginary part
<< ')';
}
float module()
{
float m = real*real + imaginary*imaginary;
return sqrt(m);
}
};
// class Complex_number
void
main()
{
Complex_number c1; // create a complex_number variable
char choice;
do
{
cout << "For c1, ";
c1.set(); // set c1
// calculate module of c1 and assign it to mod
float mod = c1.module();
cout << "Module of ";
c1.display(); // display
t3
cout << " = " << mod;
cout << "\nDo another (y/n)? ";
cin >> choice;
} while(choice != 'n');
} //end main
��������� module() �������� ������ �� ������ (������������ �����), �� ����� �� �������. �� �� �������� �� ����� �������� �� ������ ��� float ���� �� ����� ���� ����� �� ������� ����� �� ��������� ��������: float module(). � ������ ��� �� ������ �� ��������� �� ��������� ���� �� ���������� �� ���������� ����� �� ������������ �����. �������� �������� �� ��������� ���� (keyword) return � ��������� ��� - �� ���� ��� �������� �� ��������� ����� ��������� �� ��������� ���� � ��������� sqrt() � ���� ����� �������� �� ����� �� ���������. � ���������� (� main()) ����������� ����������� �� ��������� �� ��������� ����� � ����������� float mod = c1.module();, ����� ������� �� �� ��������� ������� �� ������������ ����� c1 � ���� ����� �� ��������� �� ������������ �� ������ ��� mod.
2. ����������� (statement) return. ����� ���� ������� ���� ���� �������� ��������� ���� � ������ ���� � ���� ��������� �� ������������ �� ����� �� ����, ������� �� ���� ����, ����� � ������� (����� �� - ��������� �� ���� - ������ �������� �������� � �� �� ���� ���� ��������� ��������� (statement) �� ���� ��������� ���). ��� ���������, ����� ������ �������� � ���������� ������������ �� ����������� return � ������ �� ���������. �� ������� � ��������� ���� return � ���� ��� ������ ��������� ��������, �������� return m;. ������ ��������� �������� ���� return ���� �� ���� �����, ����� ����� � ��� ��������� module().
return sqrt(m);
���� ����������� return ���������� ������� ������ �� ��������� � ��������� ���������� ��������� ���� ����������� �� ���������. ���� � ��������� ������ �� ������������ �� ��������� � ��� � ������� �������� �� ����������� �����������, ����� �� ������, �� ������� �� ��� ��� ���� ����������� return �� �� �������. ���� ���� �� �� ���������, ��� ��� ��������� module() �������� ���� ������������ ��� �� ��� �������� �����.
1 �������
float module()
{
float m = real*real + imaginary*imaginary;
cout << "+++++++++++++++++++++++";
return sqrt(m);
}
2 �������
float module()
{
float m = real*real + imaginary*imaginary;
return sqrt(m);
cout << "+++++++++++++++++++++++";
}
��������� ����������� �� ������ �� �� ��������:
1 �������
For
c1, Enter number (format Re, Im): 2,3
+++++++++++++++++++++++Module
of (2,3) = 3.60555
Do
another (y/n)?
2 �������
For
c1, Enter number (format Re, Im): 2,3
Module
of (2,3) = 3.60555
Do
another (y/n)?
� ���������, ����� �� ������ ��������, ����� �� ���� ����������� return, �� ��������� ���� �� � ���������� �� ������� �������� ��� �����, � ������ ����� �� ����������� �� �������� �� ���������. ��������, ��������� �� ������ �� ������ ���������� ����� �� �����, ����� ���� �������� �� ������� ������ � ���� 11 �� ���������� �� ���� �����:
// divide the complex number by another one (compl_num)
void div(complex_number compl_num)
{
complex_number cn;
cn.real = real;
cn.imaginary = imaginary;
float sqr_mod = compl_num.module();
if (sqr_mod == 0)
{
cout << "Devision by zero!" << endl;
return;
}
sqr_mod *= sqr_mod;
real = (cn.real*compl_num.real + compl_num.imaginary*cn.imaginary)/sqr_mod;
imaginary = (compl_num.real*cn.imaginary - cn.real*compl_num.imaginary)/sqr_mod;
}
��� ���������� �� ��������� �� ������ �� ������������ ����� �� ����, ��� ������������ sqr_mod (����� � ������� �� ������ �� ������������ �����, �� �� ����� �� ���������� � �������) � ����, ���������� ������� Devision by zero! � ������ �� ��������� div(complex_number compl_num). ��� ������ ���� ������� �� �����, div(complex_number compl_num), ������� ����� ������� �� ����� - module().
3. ����������� ���������� � ����. ������������ cn � sqr_mod �� ���� ���������� ����������� ���������� (Automatic Variables). �� �� �������� ����������� �� ���������� ������ ��������� �� ������� � ���� ������������ �� ���������� �� �� ���������� � ������������ ������������ �� ��� �����. ������� ���������� cn �� ��������� � ���� ���� �� ���������� ��� ��������, ������ sqr_mod �� ��������� � ������������ ��� ��������, ����� �� ��������� �� ��������� module(). ������������� ���������� �� ���������� � ����� (Stack).
������ � ���� �� ������������ �����, � ����� ���� �� ���������� ������������ �� ���������. ������ �������� � �������� ������ ��������� �� ��������, � ���������, ���������� ������ ����������. ������ ����������� ���������� �� ������� �� ��� ���������� ��������. ���� � ����, ������ ������ �� � �������� � ���� ���� ����� �� �������, ������ � ����� ���� ��������� ����� ����. ������ �� ��������� ������������� ���������� �� ���� �������� ����, � �� ��������������� �������������.
��������� ����������� ���������� (Nameless Automatic Variables): ��� ��������� module() ����� ��������� ���� ����� �� �� ����� �� ���� �� ������� �����:
float module()
{
return sqrt(real*real + imaginary*imaginary);
}
������������ � ������� �� �� �������� � �������� �� ��������� ����������, ����� �� ���� ���������� � ���������� �� ���� ������ �� ���������� �� ��� float, ������ ��������� ����� ����� ��� ��������. ����������� ����������� ���������� �� ���� ��������� ��� �������� �� ���������.
4. �������� �� ����������� �������� � ���������� �����. ������� ��� �� ���������� �������� � ��������:
//
complex2.cpp
//
a class that models a complex number data type
#include
<iostream.h>
#include
<iomanip.h>
class
complex_number
{
private:
float real; // real
part
float imaginary; // imaginery part
public:
void set()
{
char dummy; // for comma
cout << "Enter number (format Re, Im): ";
cin >> real >> dummy >> imaginary;
}
void display()
{
cout << '('
<< showpoint
<< setprecision(6) << real
// real part
<< ','
<< setprecision(6) << imaginary // imaginary part
<< ')';
}
// add to the complex number another one (compl_num)
void add(complex_number compl_num)
{
real += compl_num.real;
imaginary += compl_num.imaginary;
}
// subtract from the complex number another one (compl_num)
void sub(complex_number compl_num)
{
real -= compl_num.real;
imaginary -= compl_num.imaginary;
}
// multiply the complex number by another one (compl_num)
void mult(complex_number compl_num)
{
complex_number cn;
cn.real = real;
cn.imaginary = imaginary;
real = cn.real*compl_num.real - compl_num.imaginary*cn.imaginary;
imaginary = cn.real*compl_num.imaginary + compl_num.real*cn.imaginary;
}
// divide the complex number by another one (compl_num)
void div(complex_number compl_num)
{
complex_number cn;
cn.real = real;
cn.imaginary = imaginary;
float sqr_mod = compl_num.module();
if (sqr_mod == 0)
{
cout << "Devision by zero!" << endl;
return;
}
sqr_mod *= sqr_mod;
real = (cn.real*compl_num.real + compl_num.imaginary*cn.imaginary)/sqr_mod;
imaginary = (compl_num.real*cn.imaginary - cn.real*compl_num.imaginary)/sqr_mod;
}
// calculate the module of the complex number
float module()
{
float m = real*real + imaginary*imaginary;
return sqrt(m);
}
}; // class complex_number
void
main()
{
complex_number c1, c2; // create two complex_number variables
char choice;
do
{
// enter c1
cout << "For c1, ";
c1.set(); // set c1
// enter c1
cout << "For c2, ";
c2.set(); // set c2
cout << "\nAdd, Subtract, Multiply or Divide (a/s/m/d)? ";
cin >> choice;
switch (choice)
{
// add c2 to c1 and save the result into c1
case 'a':
{
c1.add(c2);
c1.display(); // display
c1
break;
}
// subtract c2 from c1 and save the result into c1
case 's':
{
c1.sub(c2);
c1.display(); // display
c1
break;
}
// multiply c1 by c2 and save the result into c1
case 'm':
{
c1.mult(c2);
c1.display(); // display
c1
break;
}
// divide c1 by c2 and save the result into c1
case 'd':
{
c1.div(c2);
c1.display(); // display
c1
}
} // switch choice
cout << "\nDo another (y/n)? ";
cin >> choice;
} while(choice != 'n');
} //end main
��� ��������� display() ��� ��� ���� ������������ showpoint � setprecision. ������� ���������� �������� �� �������� ������ ��������� ����� �� ������� � ������� �������, � ������� �������� ��������� � ����� �� �� �������� ���� �����.
� �������� ������� �� ���������� �� ���������� ��� ���������� c1 � c2 �� ��� complex_number. � ���� ����� �� ������� ����������� �� ����� ���������� � ������������ �� ���� �� ���� �� ������������� ��������, ����� ������ �� �� �������. � ������������� switch �� ����� ����� ��� ������� �� �� �������.
5. ������� �� �������� �� ���� complex_number . �������� ������� �� �������� ����������� �������� �� ���������� � ��������� ���� void � �� �������� �� ������� �����:
c1.add(c2);
c1.sub(c2);
c1.mult(c2);
c1.div(c2);
� ������� ���������, ������������ ����� c2 �� ������� ��� c1., ��� ������� c2 �� ������� �� c1 � �.�., ���� ������ ���������� �� ��������� � c1, �.�. c1 �� �������. ���� � ����� ��������� � �� ������ ��������� �� ���� �� �� ������� ��� ����� �� ������� (��� ����� ��������� ���������), ������ ���� ������� ����� �� ����� ���������, ���� �� �� �� �������� �� ������� �����:
c3
= c1.add(c2);
c3
= c1.sub(c2);
c3
= c1.mult(c2);
c3
= c1.div(c2);
���� ����� � ����� ��-��������� � ���������������. ��� �������� ����������� �� ��������� mult(c2) � ����� complex_number.
//
multiply two complex numbers and assign the result to third one
complex_number
mult(complex_number compl_num)
{
complex_number cn;
cn.real = real*compl_num.real - imaginary*compl_num.imaginary;
cn.imaginary = real*compl_num.imaginary + compl_num.real*imaginary;
return (cn);
}
� ���� ������ cn � ����������� ����������, ����� ��������� �������� ���������. � ��������� ��� �� �� ������� �� ���������. ������� ��� ������� �� �������� ���� �� �������� � ���� �������. ����������� �� ������� � �������� ������� �� ���������� �� �. 4 ������ �� ���� ��������� �� ������� �����, �� �� ���� �� �� ���������:
c3
= c1.add(c2);
c3
= c1.sub(c2);
c3
= c1.mult(c2);
c3
= c1.div(c2);
�������� �� ���� �� ��������� ����������� �� �. 4. ��� ���� ������� � ���������� �� ����� ��� �� ��������� - complex3.cpp
5. ����� ������� �� �������. �� �������� �� �������� � ��� ��������, ����� �� ������� �� ������. �� ���������� ������������� � �������� �� ����� airtime - ����� ���������� �������� timecnv1 �� ��������� ��������.
� ������� �������� ��������, timecnv2.cpp, ����� �� ��� airtime �� �������� � ����� �� ��� int � ��������� AirtimeToMins.
int AirtimeToMins() // convert airtime to minutes
{
int imins = hours*60 + minutes;
return imins;
}
imins � ����������� ����������, ����� �� ������� � ����� � �� ��������� � ������������ �� ������������ �� ���������. ����� ���������� ��-���� �� ����������� ����������� ���������� � ��� ���� ��� ���� � ������ �� ��������� ����� �� �� ������� ���� ����:
int AirtimeToMins() // convert airtime to minutes
{
return hours*60 + minutes;
}
��� ������ ������, timeret.cpp, �� ������� ��� ������� (������ �� ���� airtime) � ������ �� ��������� �� ����� �����. ����������� ������� �:
airtime add(airtime at2)
{
airtime temp;
temp.minutes = minutes + at2.minutes; // add minutes
temp.hours = hours + at2.hours;
// add hours
if(temp.minutes > 59)
// if carry,
{
temp.minutes = temp.minutes - 60; // adjust minutes
temp.hours = temp.hours + 1;
// and hours
}
if(temp.hours > 23)
// if carry,
temp.hours = temp.hours - 24;
// adjust hours
return temp;
}
������� ��, � ������ �� �������� ������� ����������� �� ��������� add �:
t3 = t1.add(t2); // add t1 and t2, result in t3
� ���� ���������� ����� ��� ��������� �� ��������� � � ���������� ������ �� �� �������� � �������� (arrays) � �������� (strings).
����������
[1] Robert Lafore; C++ Interactive Course. Waite Group Press, Macmillan Computer Publishing, 1996.
�����: ����. �������� �����������
Keywords: �++,
OOP programming , C++ , Classes , Inheritance , Reusability , Creating
New Data Types , Polymorphism and Overloading
������� ����: ����
, �����, ������� ����������� ������������ , ������������ switch if else
?