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

Int main()

{clrscr();

alpha a1(37);

alpha a2;

a2=a1; //Запуск перезавантаженого =

cout<<”\na2=”;a2.display();

alpha a3(a1); //запуск конструктора копіювання

//alpha a3=a1; //еквівалентне визначення а3

cout<<”\na3=”;a3.display();

cout<<endl;

bioskey(0);

return 0;

}

Програма 22.16

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

#include<bios.h>

#include<string.h>

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

class strCount

{private:

int count;

char* str;

friend class String;

strCount(char* s)

{int length=strlen(s);

str=new char[length+1];

strcpy(str,s);

count=1;

}

~strCount()

{delete[] str;}

};

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

class String

{private:

strCount* psc;

public:

String()//конструктор без аргументів

{psc=new strCount("NULL");}

//-------

String(char* s)//конструктор з 1 аргументом

{psc=new strCount(s);}

//-----------

String(String& S) //конструктор копіювання

{psc=S.psc;

(psc->count)++;}

//----------

~String() //деструктор

{if(psc->count==1)

delete psc;

else

(psc->count)--;

}

//----------

void display()

{cout<<psc->str;

cout<<" (addr="<<psc<<")"; //виведення адреси

}

//-----------

void operator=(String& S)

{if(psc->count==1)

delete psc;

else

(psc->count)--;

psc=S.psc;

(psc->count)++;

}

};

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

Int main()

{clrscr();

String s3="Яка-небудь пробна фраза";

cout<<"\ns3=";s3.display();

String s1;

s1=s3;

cout<<"\ns1=";s1.display();

String s2(s3);

cout<<"\ns2=";s2.display();

cout<<endl;

bioskey(0);

return 0;

}

Програма 22.17

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

#include<bios.h>

class where

{private:

char chararray[10];

public:

void reveal()

{cout<<"\nАдреса обєкту "<<this;}

};

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

Int main()

{clrscr();

where w1,w2,w3;

w1.reveal();

w2.reveal();

w3.reveal();

cout<<endl;

bioskey(0);

return 0;

}

Програма 22.18

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

#include<bios.h>

class what

{private:

int alpha;

public:

void tester()

{this->alpha=11;

cout<<this->alpha;

}

};

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

Int main()

{clrscr();

what w;

w.tester();

cout<<endl;

bioskey(0);

return 0;

}

Програма 22.19

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

#include<bios.h>

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

class alpha

{private:

int data;

public:

alpha()

{ }

alpha(int d)

{data=d;}

alpha& operator=(alpha& a)

{data=a.data;

cout<<”\nЗапущений оператор присвоювання”;

return *this;

}

void display()

{cout<<data;}

};

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

Int main()

{clrscr();

alpha a1(37);

alpha a2,a3;

a3=a2=a1; //перезавантажений = двічі

cout<<”\na2=”;a2.display();

cout<<”\na3=”;a3.display();

cout<<endl;

bioskey(0);

return 0;

}

Програма 22.20

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

#include<bios.h>

#include<string.h>

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

class strCount

{private:

int count;

char* str;

friend class String;

strCount(char* s)

{int length=strlen(s);

str=new char[length+1];

strcpy(str,s);

count=1;

}

~strCount()

{delete[] str;}

};

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

class String

{private:

strCount* psc;

public:

String()//конструктор без аргументів

{psc=new strCount(“NULL”);}

//-------

String(char* s)//конструктор з 1 аргументом

{psc=new strCount(s);}

//-----------

String(String& S) //конструктор копіювання

{psc=S.psc;

(psc->count)++;}

//----------

~String() //деструктор

{if(psc->count==1)

delete psc;

else

(psc->count)--;

}

//----------

void display()

{cout<<psc->str;

cout<<” (addr=”<<psc<<”)”; //виведення адреси

}

//-----------

String& operator=(String& S)

{cout<<”\nПрисвоювання”;

if(psc->count==1) //if last

delete psc;

else

(psc->count)--;

psc=S.psc;

(psc->count)++;

return *this;

}

};

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