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

5 Дружественная функция (класс)

Задача 13

Дан класс. Добавьте дружественную функцию isneg(), которая получает один параметр типа myclass и возвращает true, если значение num отрицательное и false в противном случае.

сlass myclass

{int num;

public:

myclass(int x)

{num=x;}

};

 

  

#include <iostream.h>

#include <conio.h>

class myclass

{ int num;

public:

myclass(int x) {num=x;}

friend int isneg( myclass ob);

};

Int isneg(myclass ob)

{return(ob.num<0)?1:0;}

 

int main()

{ myclass a(-1), b(2);

cout <<isneg(a)<< ' '<<isneg(b)<<endl;

getch() ;

return 0;

}

Тест 13

Введите 5

Ответ 0(false)

Введите -5

Ответ 1(True)

 

Задача

Дан класс определяющий температуру воздуха(t). Добавьте дружественную функцию isneg(), которая получает один параметр типа myclass и возвращает true, если температуру воздуха ниже нуля и false в противном случае.

сlass myclass

{int t;

public:

myclass(int x)

{t=x;}

};

 

  

#include <iostream.h>

#include <conio.h>

class myclass

{ int t;

public:

myclass(int x) {t=x;}

friend int isneg( myclass ob);

};

Int isneg(myclass ob)

{return(ob.t<0)?1:0;}

 

int main()

{ myclass a(-1), b(2);

cout <<isneg(a)<< ' '<<isneg(b)<<endl;

getch() ;

return 0;}

 

 

способ 2

 #include "stdafx.h" #include "iostream" #include "conio.h" using namespace std;

class myclass{  int t;     public:  myclass(int x) {    t=x; }  friend int isneg(myclass);}; int isneg(myclass obj) {  return obj.t<0?1:0; } int _tmain(int argc, _TCHAR* argv[]) {  myclass a(1);  cout<<isneg(a)<<endl;  myclass b(-10);  cout<<isneg(b)<<endl;  getch();  return 0;}

6 Вычисление выражения

Задача.14

Разработать программу, которая позволит вычислить выражение

Пользовательский класс X должен содержать необходимые элементы-данные,

метод установки их начальных значений:void Set (double X,...);

метод печати:void Print (void);

метод, решающий поставленную задачу:void Run (void);

Коды методов - вне пространства определения класса

#include <iostream.h>

#include <math.h>

#include <conio.h>

class X

{

public:

double b,z;

void Set(void);

void Print(void);

void Run();

};

void X::Set()

{

z=1;

}

void X::Print()

{

cout<<"Dano z = "<<z<<"\n";

cout<<"b = "<<b;

}

void X::Run()

{

b = (1+pow(z,2)) / (3+pow(z,2)/5);

}

void main()

{

X s;

X *M;

M=&s;

M->Set();

M->Run();

M->Print();

getch();}

Тест 14

Введите 5

Ответ 4.125

Тест 15

Введите 2

Ответ 2.05

 

 способ 2

 

#include "stdafx.h" #include <math.h> #include <iostream> #include <conio.h> using namespace std; class X {  double x,y,z,t;     double pi;        public:  void Set(double x,double y,double z)  {     this->pi=3.14;     this->x=x;     this->y=y;     this->z=z;  }  void Run(void)  {     t=(2*cos(x-pi/6))*(1+(pow(z,2)/(3-pow(z,2)/5)))/(0.5+pow(sin(y),2));  }  void Print(){   cout<<"t="<<t;  } };

int _tmain(int argc, _TCHAR* argv[]) {  setlocale(LC_ALL,"Russian");  double x,y,z;  X *obj = new X;  cout<<"Введите x:";  cin>>x;  cout<<"Введите y:";  cin>>y;  cout<<"Введите z:";  cin>>z;  obj->Set(x,y,z);  obj->Run();  obj->Print();  delete obj;  getch();  return 0; }

 

способ 3

 

 

#include "stdafx.h" #include <math.h> #include <iostream> #include <conio.h> using namespace std; class X {  double z,b;    public:  void Set(double);  void Run();  void Print(); }; void X::Set(double z) {  this->z=z; } void X::Run(void) {  b=1+(pow(z,2)/(3+pow(z,2)/5)); } void X::Print(){  cout<<"b="<<b; } int _tmain(int argc, _TCHAR* argv[]) {  setlocale(LC_ALL,"Russian");  double z;  X *obj = new X;  cout<<"Введите z:";  cin>>z;  obj->Set(z);  obj->Run();  obj->Print();  delete obj;  getch();  return 0; }

22