C++: Constructors and Destructors - Applications solved

1) define a class (an abstract data type) to store and work with complex numbers, which have:
- Member data: elements necessary to store real and the imaginary part of a complex number;
- Member functions to store data in a complex number, calculation module, displaying a complex number and sum, respectively, the product of two complex numbers.
It requires the existence of an explicit constructor initialization and one copy (data transfer).
```#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;

class complex
{
float a,b; //a - real part, b - imaginary part
public:
float& retre();
float& retim();
complex(float,float);
complex(){}
complex(complex&);
void display(char*);
float module();

//More variants for calculating the sum of two complex numbers
//1. sum resulting variant transmitted through variant result
complex sum1(complex); /*a no. complex is transmitted through
current object, the second no. complex is transmitted through
parameter*/

//2. transmitted through line parameters - transfer through address
void sum2(complex,complex*);

/*3. transmitted through line parameters - transfer reference*/
void sum3(complex,complex&);

//Product of two complex numbers
complex product(complex);
};

{
a=x; b=y;
}

{
cout<<"\part real: ";
cin>>a;
cout<<"\part imaginary: ";
cin>>b;
}

float& complex::retre()
{
return a;
}
float& complex::retim()
{
return b;
}

complex::complex(float x, float y)
{
a=x; b=y;
}

complex::complex(complex& z)
{
a=z.a;
b=z.b;
}

void complex::display(char* s)
{
cout<<"Complex number "<<s<<" is: "<<a;
cout.setf(ios::showpos);
cout<<b<<"*i\n";
cout.unsetf(ios::showpos);
}

float complex::module()
{
return sqrt(pow(a,2)+pow(b,2));
}
/*will be made summation between no. complex represented by object
Current, who will call the function, and no. complex transmitted through
object with the name "z" in line parameters*/

complex complex::sum1(complex z)
{
complex r;
r.a=a+z.a;
r.b=b+z.b;
return r;
}

void complex::sum2(complex z,complex* r)
{
r->a=a+z.a;
r->b=b+z.b;
}

void complex::sum3(complex z,complex& r)
{
r.a=a+z.a;
r.b=b+z.b;
}

complex complex::product(complex z)
{
complex r;
r.a=a*z.a-b*z.b;
r.b=a*z.b+b*z.a;
return r;
}

void main()
{
/*1.We will enter data into a complex number using function
complex z1; /*call of the initialization constructor without parameters and defined body*/

int a,b; /*not to be confused: a and b here are not those of
class, because that is protected can not access them from
outside the class*/

cout<<"\tpart real: ";
cin>>a;
cout<<"\tpart imaginary: ";
cin>>b;

//2.We will enter data into a complex number by reading its
complex z2;

//3.Real part directly entered values ??respectively imaginary
complex z3;
cout<<"Entered directly on the real and imaginary, date.\n";
cout<<"\tpart real: ";
cin>>z3.retre();
int c;
cout<<"\tpart imaginary: ";
cin>>c;
z3.retim()=c; /*This transfer is possible because
function returns a reference */

/*4.Introducing dates in complex number by constructor
initialization parameter.*/
cout<<"We entered dates directly through a constructor initialization.\n";
complex z4(3,-2); /*values ??can be given directly, or read previously*/

// display no. complex
z1.display("z1");
z2.display("z2");
z3.display("z3");
z4.display("z4");

//Sum of two NO. COMPLEX
complex s; //can call any of the three functions for calculating the sum
z1.sum3(z2,s); //s=z1+z2
s.display("z1+z2");

//Product of two NO. COMPLEX
complex p;
p=z1.product(z2); //p=z1*z2
p.display("z1*z2");

int g;
cin >>g;
}
```
2) Implement static operations on a stack. For this we declare a class SStiva and methods for each operation: defining class, implementing the methods defined using SStiva class.
```#include <iostream>
#include <fstream>
#include <math.h>
#define dim 20
using namespace std;
class Sstiva
{
int s[dim];
int top; //top of the stack
public:
Sstiva();
~Sstiva();
void Push(int e); //places the 'e' in stack
int Pop(); //extract an element from stack. Technique applies LIFO
int Length() //returns the the stack size
{
}
int Elem(int index); //returns the item on the particular index
void display();
};
Sstiva::Sstiva(void)
{
top=0;
cout<<"Stack initialized."<<endl;
}
Sstiva::~Sstiva(void)
{
if(top==0)
cout<<"Stack destroyed."<<endl;
}
void Sstiva::Push(int e)
{
if(top==dim)
{
cout<<"Stack is full."<<endl;
return; //is forced out from fuction
}
s[top]=e;
top++;
}
int Sstiva::Pop(void)
{
if(top==0)
{
cout<<"Stack overcome"<<endl;
return 0;
}
top--;
return s[top];
}
int Sstiva::Elem(int index)
{
return s[index-1];
}
void Sstiva::display()
{
if(top)
{
for(int i=0;i<top;i++)
cout<<s[i]<<" ";
cout<<endl;
return;
}
}
void main()
{
Sstiva ob;
int e; //variable that holds variables introduced in stack
do
{
cout<<"Enter element (-1=remove, 0=stop): ";
cin>>e;
if(e)
if(e!=(-1))
{
ob.Push(e);
ob.display();
cout<<"The stack size is: "<<ob.Length()<<endl;
}
else
{
ob.Pop();
ob.~Sstiva();
ob.display();
}
} while(e);

int g;
cin >>g;
}```
Cookies help us deliver our services. By using our services, you agree to our use of cookies.