-
, C++, -, : , . , : , .
, (information hiding), , , . . ( , ) . , , , , , . # ++ public, private protected.
(kind-of relationship) . , . , . . . (derived class) , (base class). , .
, , EmployeeApp. , : , . Employee , . , CalculatePay , . , CalculatePay , .
""
"" (substitutability). , , . , .
, , . (upcasting). , ( ) , . , .
|
|
-, : , . , , , , .
, , Employee CakulatePay. , : . . , Employee, . , ! , , . - .
Employee, ( ). CakulatePay. . , - , , , , .
. -, , , (, ) . (SalariedEmployee, ContractorEmployee Hourly-Employee), Employee, Employee. , Employee. , , .
: . : PolyApp.Calculate Pay Employee. Employee, , , Employee, !
, . -, . , . () , .
- , . "" , ,
|
|
. , , , , , . ? : .
. , . , , (cursors), (commitment control) (tuples). , , . , , .
, , , . . , , , , . - . , , . , .
, , , . , , . . Employee , , , CalculatePay, GetAddress GetEmployeeType. , , . : , , , .
.
using System;
class Employee
{
public Employee(string firstName, string lastName, int age, double payRate)
{
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.payRate = payRate; }
protected string firstName;
protected string lastName;
protected int age;
protected double payRate;
public virtual double CalculatePay(int hoursWorked)
{
Console.WriteLine("Employee.CalculatePay"); return 42;
}
class SalariedEmployee: Employee {
public SalariedEmployee(string firstName, string lastName,
int age, double payRate): base(firstName, lastName, age, payRate) {}
public override double CalculatePay(int hoursWorked) {
Console.WriteLine("SalariedEmployee.CalculatePay"); return 42; // } }.
class ContractorEmployee: Employee {
public ContractorEmployee(string firstName, string lastName, int age, double payRate)
: base(firstName, lastName, age, payRate)}
public override double CalculatePay(int hoursWorked) {
|
|
Console.WriteLineC'ContractorEmployee.CalculatePay");
return 42; // } }
class HourlyEmployee: Employee {
public HourlyEmployee(string firstName, string lastName, int age, double payRate)
: base(firstName, lastName, age, payRate){}
public override double CalculatePay(int hoursWorked)
Console.WriteLine("Hou rlyEmployee.CalculatePay");
return 42; // }class PolyApp {
protected Employee[] employees;
protected void LoadEmployeesQ
{
Console.WriteLine(" ...");
// // , , , employees = new Employee[3];
employees[0] = new SalariedEmployee ("Amy", "Ariderson", 28, 100);
employees[1] = new ContractorEmployee ("John", "Maffei", 35, 110); employees[2] = new HourlyEmployee ("Lani", "Ota", 2000, 5);
Console. Writel_ine("\n"); }
protected void CalculatePayO.
{foreach(Employee emp in employees)
emp.CalculatePay(40);
} }
public static void Main()
{PolyApp app = new PolyAppQ;
app.LoadEmployees(); app. CalculatePayO; } }
() ++#. STL.
, ? , , Vector. , , , "" :
template < class T > class Vector // T
{ T * v;
int sz;
public:
Vector (int s)
{ if (s <= 0)
error (" Vector ");
v = new T [ sz = s ]; // s T
}
T & operator [] (int i);
int size () { return sz; }};
. . template<class T>, , , -.
void f ()
{ Vector < int > v1 (100); // 100
Vector < complex > v2 (200); // 200
v2 [ i ] = complex (v1 [ x ], v1 [ y ]);}
template, . :
template< typename T >
void sort(T array[], int size); // sort ,
template< typename T >
void sort(T array[], int size) { /* */} //
template< int BufferSize > //
char* read()
{ char *Buffer = new char[ BufferSize ]; /* */
return Buffer;}
typename , class typename:
template< class T >
, , , , . , , , , .
template< class T >
class List
{public:
void Add(const T& Element);
bool Find(const T& Element);};
, :
|
|
List<int> li;
List<string> ls;
C++ C#
C# C++ , . . C# , C++. , C# , C++. , C# , , .
C++ C#:
C# , C++.
C# .
C# , .. .
C# : .
C# .
C# .
C# , . C++ .
C++ , , . C# , , . , .
STL
STL (Standard Template Library) - , .
STL:
- , ;
- ;
- , ;
- .
- . C++ , . ( ), , , .
:
(input);
(output);
(forward);
(bidirectional);
(random access).
. , , , . , . , , : , . . . , ++ .
STL , . , , . .
, , (end-of-range), . . , . ++, '\0'. .
STL , , , , , . . . , , . . , . , , , , .
|
|
equal. , , :
template <class InputIterator1, class InputIterator2>
bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
- , . . - . () v1 v2:
bool isEqual = equal(v1.begin(), v1.end(), v2.begin());
: begin() , , end() . , equal "" (true).
, : , , , .
STL , . - , . STL :
vector - , , ;
list - , ;
deque - , ;
set - , ;
multiset - , set, ;
map - ;
multimap - , map, ;
stack - , ;
queue - ;
priority queue - , queue, .
, , . , , , "operator = = ()", .
- , "operator ()()". . , , , . inline, .
. (equal_to), (not_equal_to), "" (greater), "" (less), " " (greater_equal), " " (less_equal).
#. . . : -, , , , . . .
. () . - . :
, . . , .
. , . .
. . , - "". .
, (), . . (). , , . . C# C++, .
C++ .
(bool).
(char).
. - short, int, long, signed unsigned, , , - .
. - float, double, long double.
void, .
:
(, int* - int).
(, double& - double).
(, char[] - char).