C++: Classes and objects - Applications solved


1) Describe a class "complex" that allows storing real part and the imaginary in a complex number and calculates its module. The data will not have any protection inside the classroom so that they can have direct access to outside. Make a program that uses this class and read data in a complex number displaying it together with his module.
#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;
 class complex
{
	public:
	float a,b;
	float module()
	{
		return sqrt(pow(a,2)+pow(b,2));
	}
};
void main()
{
	complex z;
	cout<<"Enter no complex: \n";
	cout<<"\tPart real: ";
	cin>>z.a;
	cout<<"\tPart imaginary: ";
	cin>>z.b;
 
	cout<<"\nComplex number is: ("<<z.a<<","<<z.b<<")";
	cout<<"\nThe module is: "<<z.module()<<endl;
 
	int g;
	cin >>g;
}
2) Describe a class "complex" that allows storing real and the imaginary part in a complex number and calculates its module. The data will not have any protection inside the classroom so that they can have direct access to outside. Make a program that reads a string of complex numbers and show those numbers that have the maximum and minimum of the modules together with their modules.
#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;
class complex
{
	public:
	float a,b;
	float module()
	{
		return sqrt(pow(a,2)+pow(b,2));
	}
};
void main()
{
	complex v[20]; //a string of up to 20 elements representing complex numbers
	float max,min; //minimum and maximum modulees
	int n; //real size of the string
	int m,M; //minimum element index, ie, maximum

	cout<<"Enter n: "; cin>>n;
	for(int i=0;i<n;i++)
	{
		cout<<"Complex number "<<i+1<<" is: "<<endl;
		cout<<"\tpart real: "; cin>>v[i].a;
		cout<<"\tpart imaginary: "; cin>>v[i].b;
	}
 
	max=v[0].module();
	min=v[0].module();
	m=0;
	M=0;
 
	for(int i=1;i<n;i++)
	if (v[i].module()<min)
	{
		min=v[i].module();
		m=i;
	}
	else if (v[i].module()>max)
	{
		max=v[i].module();
		M=i;
	}
 
	cout<<"\nComplex number with maximum module: ("<<v[M].a<<","<<v[M].b<<") - module: "<<max;
	cout<<"\nComplex number with minimum module: ("<<v[m].a<<","<<v[m].b<<") - module: "<<min;
 
	int g;
	cin >>g;
}
3) Make an application with an abstract data type (a class) that allows storing data in a complex number, where the real and the imaginary part is considered to be protected for visibility outside the classroom. Class will include the basics, ie, storing data in a complex number and display it.
#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;
class complex
{
	private: //was implicitly
	float a,b; //a - real part, b - imaginary part 
	public:
void read(char* s) //function for entering the data into a complex number
/*this function will be developed within the class being categorized as function in-line*/
	{
cout<<"Enter the data of complex number "<<s<<endl; /*instead of endl would work also '\n' or "\n"*/
		cout<<"\treal part: ";
		cin>>a;
		cout<<"\timaginary part: ";
		cin>>b;
	}
	void display(char*); //function to display data into a complex number
/*<display> function will be developed outside the class being categorized as Off-line*/
};
void complex::display(char* s)
{
	cout<<"No. complex "<<s<<" is: "<<a;
	cout.setf(ios::showpos); //add a sign '+' positive values
	cout<<b<<"*i"<<endl;
	cout.unsetf(ios::showpos); //sign cancels '+' for positive values
}
void main()
{
	//declare an object class defined above
	complex z1;
/*call the read function to the object in case 
(in this particular situation it becomes the current object)*/
	z1.read("z1");
	z1.display("z1");
	cout<<endl;
/*in case above I made a statement of static object
What happens in the event that declared object is dynamic?*/
	complex* z2;
	z2=new complex; //I used operator memory allocation in C + +, namely <new>
	z2->read("z2");
	z2->display("z2");
 
	int g;
	cin >>g;
}
4) Make an application with an abstract data type (a class) that allows storing data in a complex number, where the real and the imaginary part is considered to be protected for visibility outside the classroom. Class will include the basics, ie, storing data in a complex number and display it. Will perform even operations sum, respectively, produced between two complex numbers.
#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;
class complex
{
	private: //was implicitly
	float a,b; //a - real part, b - imaginary part 
	public:
void read(char* s) //function for entering the data into a complex number
/*this function will be developed within the class being categorized as function in-line*/
	{
cout<<"Enter the data of complex number "<<s<<endl; /*instead of endl would work also '\n' or "\n"*/
		cout<<"\treal part: ";
		cin>>a;
		cout<<"\timaginary part: ";
		cin>>b;
	}
 
	void display(char*); //function to display data into a complex number
/*above function will be developed outside the class being categorized as Off-line*/
 
	//More variants for calculating the sum of two complex numbers
 
	//1. sum resulting variant transmitted through variant result
	complex sum1(complex); /*function return a complex number as a result of
expression sum and that participants are expressions of the type no. complex:
one is the current object will call the function, and,
the second is the parameter included in the parameter list of the function*/
 
	//2. sum resulting variant transmitted through line parameters - through type of reference
	void sum2(complex, complex&); /*operator is binary, so the relationship must have two operands:
first operand participant relationship is the subject of current
and the second is the first parameter of the function. The result is transmitted through
the second parameter of the function, namely, the expression of type reference*/
 
	//3. sum resulting variant transmitted through line parameters - through address
	void sum3(complex, complex*);
	//Product of two complex numbers
	complex product(complex);
};
 
void complex::display(char* s)
{
	cout<<"No. complex"<<s<<" is: "<<a;
	cout.setf(ios::showpos); //add a sign '+' positive values
	cout<<b<<"*i"<<endl;
	cout.unsetf(ios::showpos); //cancel the default display option with sign
}
 
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;
}
 
void main()
{
	//declare an object class defined above
	complex z1;
 
	/*call the read function to the object in case 
(in this particular situation it becomes the current object)*/
 
	z1.read("z1");
	z1.display("z1");
	cout<<endl;
	/*in case above I made a statement of static object
What happens in the event that declared object is dynamic?*/
 
	complex* z2;
	z2=new complex; //I used operator memory allocation in C + +, namely <new>
	z2->read("z2");
	z2->display("z2");
	cout<<endl; //pass the newline after case, leaving a blank line

	complex z3;
	z3=z1.sum1(*z2); /*will calculate the sum of complex numbers z1 and z2, the sum will enroll in z3*/
	z3.display("z1+z2");
	complex z4;
	z1.sum2(*z2,z4); 	//z4=z1+z2
	z4.display("z1+z2");
	complex z5;
	z1.sum3(*z2,&z5); 	//z5=z1+z2
	z5.display("z1+z2");
 
	int g;
	cin >>g;
}
Cookies help us deliver our services. By using our services, you agree to our use of cookies.