Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
ПРАКТИКУМ_3.doc
Скачиваний:
1
Добавлен:
01.05.2025
Размер:
266 Кб
Скачать
☆

32. Работа с конструктором и деструктором

// Работа с конструктором и деструктором

#include "stdafx.h"

#include <math.h>

#include <iostream>

using namespace std;

#include "conio.h" // библиотека для функции _getch();

#include <string.h>

#include <stdlib.h>

class strtype

{

char*p;

int len;

public:

strtype(char *ptr);

~strtype();

void show();

};

strtype::strtype(char *ptr)

{

len=strlen(ptr);

p=new(char);

if(!p)

{

cout<<"Mistake\n";

exit(1);

}

strcpy(p,ptr);

}

strtype::~strtype()

{

cout<<"free of p\n";

delete p;

}

void strtype::show()

{

cout<<"length of the string is "<<len;

cout<<"\n";

}

int main()

{

strtype s1("Это проверка");

s1.show();

getch();

return 0;

}

33. Работа с комплексными числами

#include "stdafx.h"

#include <iostream>

using namespace std;

#include "conio.h" // библиотека для функции _getch();

class complex

{

float real,imaginary;

public:

complex();

~complex();

void add(complex x,complex y); //сложение двух комплексных чисел

void enter(float e,float c); // запись комплексного числа

};

complex::complex()

{

real=imaginary=0.;

}

complex::~complex()

{

}

void complex::enter(float e,float c)

{

real=e;

imaginary=c;

}

void complex::add(complex x,complex y)

{

real=x.real+y.real;

imaginary=y.imaginary+x.imaginary;

cout<<"new number is "<<real<<" + "<<imaginary<<"*j\n";

}

int main()

{

complex x,y,z;

x.enter(4.,5.);

y.enter(5.,5.);

z.add(x,y);

_getch();

return 0;

}

34. Работа_1 с комплексными числами через указатели

#include "stdafx.h"

#include <iostream>

using namespace std;

#include "conio.h" // библиотека для функции _getch();

class complex

{

float real,imaginary;

public:

complex();

~complex();

void add(complex *x,complex *y); //сложение двух комплексных чисел

void enter(float e,float c); // запись комплексного числа

};

complex::complex()

{

real=imaginary=0.;

}

complex::~complex()

{

}

void complex::enter(float e,float c)

{

real=e;

imaginary=c;

}

void complex::add(complex *x,complex *y)

{

real=x->real+y->real;

imaginary=y->imaginary+x->imaginary;

cout<<"new number is "<<real<<" + "<<imaginary<<"*j\n";

}

int main()

{

complex x,y,z;

x.enter(4.,5.);

y.enter(5.,5.);

z.add(&x,&y);

_getch();

return 0;

}

35. Работа_2 с комплексными числами через указатели

#include "stdafx.h"

#include <iostream>

using namespace std;

#include "conio.h" // библиотека для функции _getch();

class complex

{

float real,imaginary;

public:

complex();

~complex();

void add(complex *x,complex *y); //сложение двух комплексных чисел

void enter(float e,float c); // запись комплексного числа

};

complex::complex()

{

real=imaginary=0.;

}

complex::~complex()

{

}

void complex::enter(float e,float c)

{

real=e;

imaginary=c;

}

void complex::add(complex *x,complex *y)

{

// this - указатель на текущий объект, который выполняет функцию add

this->real=x->real+y->real;

this->imaginary=y->imaginary+x->imaginary;

cout<<"new number is "<<this->real<<" + "<<this->imaginary<<"*j\n";

}

int main()

{

complex x,y,z;

x.enter(4.,5.);

y.enter(5.,5.);

z.add(&x,&y);

_getch();

return 0;

}