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

Практичне заняття 9 Теоретична частина

Базується на матеріалі лекцій 20-21: вказівники

Завдання

Ввести тексти програм, які розглядалися у лекціях 20-21. Прокомпілювати, проаналізувати результат роботи. До тексту звіту включити лістінги програм та результати їх роботи.

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

#include<bios.h>

#include<string.h>

Int main()

{clrscr();

char* str="Пробна фраза";

int len=strlen(str); //довжина рядка

char* ptr;

ptr=new char[len+1]; //виділення памяті

strcpy(ptr,str);

cout<<"ptr="<<ptr<<endl;

delete[]ptr; //вивільнення памяті

bioskey(0);

return 0;

}

Програма 20.15

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

#include<bios.h>

#include<string.h>

class String

{private:

char* str;

public:

String(char* s)

{int length=strlen(s);

str=new char[length+1];

strcpy(str,s);

}

~String()

{cout<<”Видалення рядка\n”;

delete[]str;

}

void display()

{cout<<str<<endl;

}

};

///////////////////

Int main()

{clrscr();

String s1=”Пробна фраза”;

cout<<”s1=”;

s1.display();

return 0;

}

Програма 20.16

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

#include<bios.h>

class Distance

{private:

int feet;

float inches;

public:

void getdist()

{cout<<"\nВведіть фути";cin>>feet;

cout<<"\nВведіть дюйми";cin>>inches;

}

void showdist()

{cout<<feet<<"\'="<<inches<<'\"';}

};

/////////////

Int main()

{clrscr();

Distance dist;

dist.getdist();

dist.showdist();

Distance* distptr;

distptr=new Distance;

distptr->getdist();

distptr->showdist();

cout<<endl;

bioskey(0);

return 0;

}

Програма 20.17

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

#include<bios.h>

class person

{private:

char name[40];

public:

void SetName()

{cout<<"Введіть імя:";cin>>name;

}

void PrintName()

{cout<<"\n Імя:"<<name;

}

};

///////////

Int main()

{clrscr();

person* persPtr[100];

int n=0;

char choice;

do

{persPtr[n]=new person;

persPtr[n]->SetName();

n++;

cout<<"Continue (y/n)?";cin>>choice;

}

while(choice=='y');

for(int j=0;j<n;j++)

{cout<<"\nІнформація про N"<<j+1;

persPtr[j]->PrintName();

}

cout<<endl;

bioskey(0);

return 0;

}

Програма 20.18

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

#include<bios.h>

struct link

{int data;

link* next;

};

class linklist //список

{private:

link* first;

public:

linclist()

{first=NULL;}

void additem(int d);

void display();

};

////////////////////

void linklist::additem(int d)

{link* newlink=new link;

newlink->data=d;

newlink->next=first;

first=newlink;

}

void linklist::display()

{link* current=first;

while(current)

{cout<<current->data<<endl;

current=current->next;

}

}

////////////////

Int main()

{clrscr();

linklist l1;

l1.additem(25);

l1.additem(35);

l1.additem(45);

l1.display();

bioskey(0);

return 0;

}

Програма 20.19

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

#include<bios.h>

#include<string.h>

class person

{protected:

char* name;

public:

void setname()

{cout<<”Name=”;cin>>name;}

void printname()

{cout<<endl<<name;}

char* getname()

{return name;}

};

///////////////