Uploaded by MadGod

Lab3C-example

advertisement
Ministerul Educatiei din Republica Moldova
Universitatea Tehnica din Moldova
FCIM
Raport
Lucrare de laborator nr.3
Programare in C++
VARIANTA 7
A efectuat: st.gr TI-15X
Student Name
A verificat:
Scrob Sergiu
Chisinau 2016
LUCRARE DE LABORATOR NR. 3
Tema: Supraîncărcarea operatorilor
Scopul lucrării:
 Studierea necesităţii supraîncărcării operatorilor;
 Studierea sintaxei de definire a operatorilor;
 Studierea tipurilor de operatori;
 Studierea formelor de supraîncărcare;
Varianta 7
а) Să se creeze clasa Bool – variabile logice. Să se definească operatorii "+" – SAU logic, "*" –
ŞI logic, "^" – SAU EXCLUSIV, ca metode ale clasei, iar operatorii "==" şi "!=" – ca funcţii
prietene. Operatorii trebuie să permită realizarea operaţiilor atît cu variabilele clasei date, cît şi
cu variabilele de tip predefinit int. (Dacă numărul întreg este diferit de zero, se consideră că
variabila este adevăr, altfel – fals.)
b) Să se creeze clasa String – şir, utilizînd memoria dinamică. Să se definească operatorii "+" –
adunarea şirurilor, "=" şi "+=" – atribuirea ca metode ale clasei. Să se definească operatorii de
comparare: "==", "!=", "<", ">" ca funcţii prietene. Operatorii trebuie să lucreze atît cu String, cît
şi cu char*. Să se definească operatorul "[]" de acces la fiecare simbol în parte. Să se
supraîncarce operatorii "<<" şi ">>" pentru ieşiri/intrări de obiecte.
Codul programului (a) :
#include <iostream>
using namespace std;
class Bool
{
int n;
public:
Bool::Bool() : n(0) {}
Bool::Bool(int z) : n(z) {}
Bool operator+(Bool &obj)
{
Bool res (*this);
return (res.n + obj.n) ? true : false;
}
Bool operator+(int obj)
{
Bool result (*this);
result.n += obj;
2
return (result);
}
Bool operator*(Bool &obj)
{
Bool res (*this);
return (res.n * obj.n) ? true : false;
}
Bool operator*(int obj)
{
Bool result (*this);
result.n *= obj;
return (result);
}
friend bool operator==(Bool &a, Bool &b)
{
return (a.n == b.n) ? true : false;
}
friend bool operator==(int a, Bool &b)
{
return (a == b.n) ? true : false;
}
friend bool operator==(Bool &a, int b)
{
return (a.n == b) ? true : false;
}
friend bool operator!=(Bool &a, Bool &b)
{
return !(a.n == b.n) ? true : false;
}
friend bool operator!=(int a, const Bool &b)
{
return !(a == b.n) ? true : false;
}
friend bool operator!=(Bool &a, int b)
{
return !(a.n == b) ? true : false;
}
friend ostream& operator<<(ostream &out, Bool &obj)
{
out << obj.n << endl;
return out;
}
};
int
{
main()
Bool x(1);
3
Bool y(0);
cout << "X = 1 \nY = 0" << endl;
if (x == 1)
{
cout << "Boolean" <<endl;
cout << " x+x= " << x + x
y+y= " << y + y << endl;
cout << " x*x= " << x * x
y*y= " << y * y << endl;
}
if (x != y)
{
cout <<"\nInteger"<<endl;
cout << " x+5= " << x + 5
y+25= " << y +25 << endl;
cout << " x*5= " << x * 5
y*0= " << y * 25 << endl;
}
system ("pause");
return (0);
}
<< " x+y= " << x + y << " y+x= " << y + x << "
<< " x*y= " << x * y << " y*x= " << y * x << "
<< " x+0= " << x + 0 << " 15+y= " << y + 15 << "
<< " x*0= " << x * 0 << " 5*y= " << y * 5 << "
Codul programului (b) :
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
using namespace std;
class String
{
char *str;
public:
String() : str(NULL) {}
String(char *s)
{
str = new char[strlen(s) + 1];
strcpy(str, s);
}
~String()
{
delete [] str;
}
String& operator+(String &s)
{
4
char *temp;
temp =
strcpy
strcat
strcpy
delete
return
new char[strlen(s.str) + strlen(str) + 1];
(temp, str);
(temp, s.str);
(str, temp);
[] temp;
(*this);
}
String& operator+(char *s)
{
char *temp;
temp =
strcpy
strcat
strcpy
delete
return
new char[strlen(s) + strlen(str) + 1];
(temp, str);
(temp, s);
(str, temp);
[] temp;
(*this);
}
String& operator=(String &s)
{
str = new char[strlen(s.str) + 1];
strcpy(str, s.str);
return (*this);
}
String& operator=(char *s)
{
str = new char[strlen(s) + 1];
strcpy(str, s);
return (*this);
}
String& operator+=(String &s)
{
char *temp;
temp =
strcpy
strcat
strcpy
delete
return
new char[strlen(s.str) + strlen(str) + 1];
(temp, str);
(temp, s.str);
(str, temp);
[] temp;
(*this);
}
String& operator+=(char *s)
{
char *temp;
temp =
strcpy
strcat
strcpy
delete
return
new char[strlen(s) + strlen(str) + 1];
(temp, str);
(temp, s);
(str, temp);
[] temp;
(*this);
5
}
friend bool operator==(String &s, char *s1)
{
return (strcmp(s.str, s1) == 0) ? true : false;
}
friend bool operator==(String &s1, String &s)
{
return (strcmp(s1.str, s.str) == 0) ? true : false;
}
friend int operator!=(String &s, char *s1)
{
return !(s.str == s1);
}
friend int operator!=(String &s1, String &s)
{
return !(s1.str == s.str);
}
friend bool operator>(String &s, char *s1)
{
return (strcmp(s.str, s1) > 0) ? true : false;
}
friend bool operator>(String &s1, String &s)
{
return (strcmp(s1.str, s.str) > 0) ? true : false;
}
friend bool operator<(String &s, char *s1)
{
return (strcmp(s.str, s1) < 0) ? true : false;
}
friend bool operator<(String &s1, String &s)
{
return (strcmp(s1.str, s.str) < 0) ? true : false;
}
char operator[](int i)
{
return (str[i]);
}
void copy(char *s)
{
str = new char[strlen(s) + 1];
strcpy(str, s);
}
friend ostream& operator<<(ostream &out, String &str)
{
out << str.str;
return out;
6
}
friend istream& operator>>(istream &in, String &str)
{
char input[64];
in >> input;
str.copy(input);
return in;
}
};
int
{
main()
String str1;
String str2;
String str3;
int i;
cout << "Input the first string >>
";
cin >> str1;
cout << "\nInput the second string >>
";
cin >> str2;
if (str1 > str2)
cout << "\n*String
if (str1 < str2)
cout << "\n*String
if (str1 == str2)
cout << "\n*String
else
cout << "\n*String
cout << "\nFirst:
1 este mai mare decit String 2 !!!" << endl;
2 este mai mare decit String 1 !!!" << endl;
1 este egal cu String 2 !!!" << endl;
1 nu este egal cu String 2 !!!" << endl;
" << str1 << "\nSecond:
" << str2 << endl;
str3 = str1;
str3 += str2;
cout << "*String 1 + String 2 =
" << str3 << endl;
str1 += str1;
cout << "*String 1 + String 1 =
" << str1 << endl;
str2 += str2;
cout << "*String 2 + String 2 =
" << str2 << endl;
str1 += "<@!Hello_World";
cout << "*String1 + '<@!Hello_World' =
" << str1 << endl;
cout << "\nInput index 'String1[i]'>>
" ;
cin >> i;
cout << "*String1[" << i <<"] = " << str1[i] << endl;
system ("pause"); exit (0);
return (0);
}
7
Demonstratea programului (a):
8
Demonstratea programului (b):
Concluzie:


In rezultatul elaborarii lucrarii date s-a pus baza aplicarii in practica a cunostintelor teoretice
referitoare la utilizarea operatorilor.
Astfel se poate judeca despre posibilitatile largi acordate de limbajul C++ referitor la
manipularea memoriei in directia dorita de utilizator.
9
Download