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

31. Работа с библиотечными карточками

#include "stdafx.h"

#include <string.h>

#include <iostream>

using namespace std;

class card {

char title[8];

char author[40];

int number;

public:

void store(char *t,char *name,int num);

void show();

};

void card::store(char *t,char *name,int num)

{

strcpy(title,t);

strcpy(author,name);

number=num;

}

void card::show()

{

cout<<"Title :"<<title<<"\n";

cout<<"Author :"<<author<<"\n";

cout<<" Number:"<<number<<"\n";

}

int main()

{

card book1,book2;

int n;

book1.store("Dune","Frank",2);

book2.store("Trilogy","Asimov",1);

book1.show();

book2.show();

cin>>n;

return 0;

}

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

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

#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. Работа 2 с конструктором и деструктором

#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)

{

strcpy(&p,ptr);

len=strlen(&p);

}

strtype::~strtype()

{

}

void strtype::show()

{

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

cout<<"\n";

}

int main()

{

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

s1.show();

getch();

return 0;

}

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

#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. Работа_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;

}