Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
информатика отчет.doc
Скачиваний:
1
Добавлен:
01.03.2025
Размер:
241.15 Кб
Скачать

12)Работа с шаблонами

#include <iostream.h>

const int DefaultSize=10;

class Animal{

public:

Animal(int);

Animal();

~Animal(){}

int GetWeight(){return itsWeight;}

void Display(){cout<<itsWeight;}

private:

int itsWeight;

};

Animal::Animal(int weight):itsWeight(weight){}

Animal::Animal():itsWeight(0){}

template <class T>

class Array{

public:

Array(int itsSize=DefaultSize);

Array(const Array &rhs);

~Array(){delete [] pType;}

Array& operator=(const Array&);

T& operator [](int offSet){return pType[offSet];}

const T& operator [](int offSet) const

{return pType[offSet];}

int GetSize() const{return itsSize;}

friend ostream& operator<<(ostream&, Array<T>&);

private:

T *pType;

int itsSize;

};

template <class T>

ostream& operator<< (ostream& output, Array<T>& theArray){

for(int i=0; i<theArray.GetSize(); i++)

output<<"["<<i<<"]"<<theArray[i]<<endl;

return output;

}

template <class T>

Array<T>::Array(int size):

itsSize(size){

pType=new T[size];

for(int i=0; i<size; i++)

pType[i]=0;

}

template <class T>

Array<T>::Array(const Array &rhs){

itsSize=rhs.GetSize();

pType=new T[itsSize];

for(int i=0; i<itsSize; i++)

pType[i]=rhs[i];

}

template <class T>

Array<T>&Array<T>::operator=(const Array &rhs){

if(this==&rhs)

return *this;

delete [] pType;

itsSize=rhs.GetSize();

pType=new T[itsSize];

for(int i=0; i<itsSize; i++)

pType[i]=rhs[i];

return *this;

}

int main(){

bool Stop=false;

int offset, value;

Array<int> theArray;

while(!Stop){

cout<<"Enter an offset(0-9)";

cout<<"and a value(-1 to stop):";

cin>>offset>>value;

if(offset<0)

break;

if(offset>9){

cout<<"***Please use value between 0 and 9.***\n";

continue;

}

theArray[offset]=value;

}

cout<<"\nHere's the entire array:\n";

cout<<theArray<<endl;

return 0;

}

13)Банковские операции (чековая и сберегательная книжка)

#include<cstdio>

#include<cstdlib>

#include <iostream>

using namespace std;

//максимальное количество счетов

const int maxAccounts = 5;

// Checking — здесь описан чековый счет

class Checking

{

public:

Checking(int initializeAN = 0)

:accountNumber(initializeAN), balance(0.0){}

// функции обращения

int accountNo()

{

return accountNumber;

}

double acntBalance()

{

return balance;

}

//функции транзакций

void deposit(double amount)

{

balance += amount;

}

void withdrawal(double amount);

// функция вывода объекта в cout

Void display()

{

cout << "Schet " << accountNumber

<< " = " << balance<< "\n";}

protected:

unsigned accountNumber;

double balance;

};

// withdrawal — эта функция-член слишком велика

// для inline-функции

void Checking::withdrawal(double amount)

{

if (balance < amount)

{

cout<<"no mani you balance =";

cout<< balance;

cout<<" summa cheka ravna" ;

cout<< amount;

}

else

{

balance -= amount;

// если баланс падает слишком низко...

if (balance < 500.00)

{

// ...удержать деньги за обслуживание

balance -= 0.20;}

}

}

// Savings — вы и сами можете написать этот класс

class Savings

{

public:

Savings(int initialAN = 0)

:accountNumber(initialAN),

balance ( 0.0), noWithdrawals (0){}

// функции обращения

int accountNo()

{

return accountNumber;

}

double acntBalance()

{

return balance;

}

// функции транзакций

void deposit(double amount)

{

balance += amount;

}

void withdrawal(double amount);

// функция display — отображает объект на 'cout