סדנא תרגיל 6 שאלה 2

benny++C, נכתב ע"י Benny, סדנא ב++C, תרגילים, תרגילים בסדנא ב++CLeave a Comment

תרגיל בירושה, מחלקה כללית של מצולע היא מחלקת האב, ומחלקות משולש ומלבן, הם יורשות / תת מחלקות של המלחקה הראשית מצולע

תכנית ראשית

 /******************************
  

  targil: 6/2
  lecturer: david Cohen
  discription: the program get info about tow polygons, and return if thay are equal (if the scope is the same)
  
  *******************************/
#include <iostream>
using namespace std;
#include "Triangle.h"
#include "Rectangle.h"
#include "Polygon.h"

int main()
{
	cout<<"enter number of sides for polygon 1:\n";
	int side_1;                      
	cin>>side_1;
	Polygon Metsula_1(side_1);          //create Polygon with array (array size is 'side_1' ) to put the sides value 
	
	if (side_1==3||side_1==4)           //in case: the Polygon is Triangle or Rectangle (malben / meshoolash)
	{
		if (side_1==3)                 // if is Triangle
		{
			cout<<"enter sides for polygon:\n";
			int a,b,c;
			cin>>a>>b>>c;

			Triangle Mshulash_1(a,b,c);       // insetrt the sides value to the polygon 1 sidse array

			cout<<"enter number of sides for polygon 2:\n";
			int size_2;
			cin>>size_2;
			if (size_2==side_1)               // if polygon 2 is allso Triangle
			{
				int a,b,c;
				cout<<"enter sides for polygon:\n";
				cin>>a>>b>>c;
				Triangle Mshulash_2(a,b,c);       // insetrt the sides value to the polygon 2 sidse array

				if (Mshulash_2==Mshulash_1)      // if the tow Triangles scope is equal
				{
					cout<<"equal\n";
				}
				else
				{
					cout<<"not equal\n";
				}

			}
			else
			{
				cout<<"not equal\n";
			}

		}
		if (side_1==4)                                  // if is Rectangle
		{
			cout<<"enter sides for polygon:\n";
			int a,b;
			cin>>a>>b;              // get only to sides (the Rectangle c-tor add tow mor sidse becauze in Rectangle tow side are even )

			Rectangle Malben_1(a,b);    // insetrt the sides value to the Rectangle 1 sidse array (4 side (2*2))

			cout<<"enter number of sides for polygon 2:\n";
			int size_2;
			cin>>size_2;
			if (size_2==side_1)          //  if is allso a Rectangle
			{
				int a,b;
				cout<<"enter sides for polygon:\n";
				cin>>a>>b;
				Rectangle Malben_2(a,b);   // insetrt the sides value to the Rectangle 2 sidse array (4 side (2*2))

				if (Malben_2==Malben_1)    // if the tow Rectangles scope is equal
				{
					cout<<"equal\n";
				}
				else
				{
					cout<<"not equal\n";
				}

			}
			else
			{
				cout<<"not equal\n";
			}

		} 
	}
	else   //in case: is not Polygon and is not Triangle (mor then 4 sidse)
	{
		cout<<"enter number of sides for polygon 2:\n";
		int size_2;
		cin>>size_2;
		Polygon Metsula_2(size_2);   // insetrt the sides value to the polygon 2 sidse array 
	
		if (Metsula_2==Metsula_1)    // if the tow polygons scope is equal
		{
			cout<<"equal\n";
		}
		else
		{
			cout<<"not equal\n";
		}

	}

//	system("pause");
	return 0;
}

 
 /*********OUTPUT***************

 enter number of sides for polygon 1:
4
enter sides for polygon:
33 22
enter number of sides for polygon 2:
4
enter sides for polygon:
33 66
not equal


=====================================

enter number of sides for polygon 1:
6
enter sides for polygon:
1 2 3 4 5 6
enter number of sides for polygon 2:
6
enter sides for polygon:
1 2 3 4 5 6
equal

*************************************/

המלחקה Polygon

#pragma once
class Polygon
{
	int numSides;
	int* sides;
public:

	int get_numSides();
	void set_numSides(int);
	void set_Sides(int,int);
	Polygon(const Polygon&temp);
	Polygon(int);
	Polygon();
    void print_sides();
	~Polygon(void);
	int Scope();

	bool operator==(Polygon &a);
};


מימוש המחלקה Polygon


#include "Polygon.h"
#include <iostream>

using namespace std;

//constructor
Polygon::Polygon(int numSides)
{
	
	this->numSides=numSides;
	sides = new int[numSides];    // create array in size = numSides
	if (numSides>4)
	{
		cout<<"enter sides for polygon:\n";
		int value;
		for (int i = 0; i < numSides; i++)            
		{
			cin>>value;              // get input (sidse), and insert the sidse values to the array
			sides[i]=value;
		} 
	}


}

Polygon::Polygon(const Polygon&Temp)        //copy c-tor
{
	
	this->numSides=Temp.numSides;

	for (int i = 0; i < numSides; i++)
	{
		sides[i]=Temp.sides[i];
	}
}
  
int Polygon::get_numSides()
{
	return numSides;
}
void Polygon::set_numSides(int a)
{
	this->numSides=a;

}

void Polygon::set_Sides(int value,int index)  // get index and value and put the value in the array[index]
{
	sides[index]=value;

}
void Polygon:: print_sides()    // not nessesry for this specific program
{
	cout<<"the side are: ";
	for (int i = 0; i < numSides; i++)
	{
		cout<<sides[i]<<" ";
	}
	cout<<endl;
}

int Polygon:: Scope()          // the function clculate the polygon scope
{
	int sum_sides=0;
	for (int i = 0; i < numSides; i++)
	{
		sum_sides+=sides[i];
	}

	return sum_sides;
}

bool Polygon::operator==(Polygon& a)
 {
	 if( numSides!= a.numSides)
		return false;
	 return(this->Scope()==a.Scope());           // return if the scope is equal
 }


Polygon::~Polygon(void)
{
	if (sides)
   	{
   		delete[]sides;
   	}
}

המחלקה Rectangle

#pragma once
#include "Polygon.h"
class Rectangle:public Polygon
{
public:
	Rectangle(int,int);
	~Rectangle(void);
};


מימוש המחלקה Rectangle

#include "Rectangle.h"


Rectangle::Rectangle(int a,int b):Polygon(4) // use base class constructor
{
	this->set_Sides(a,0);
	this->set_Sides(b,1);
	this->set_Sides(a,2);
	this->set_Sides(b,3);
}


Rectangle::~Rectangle(void)
{
}

המחלקה Triangle

#pragma once
#include "Polygon.h"
class Triangle:public Polygon
{
public:
	Triangle(int,int,int);
	~Triangle(void);
};


מימוש המחלקה Triangle

#include "Triangle.h"


Triangle::Triangle(int a ,int b ,int c):Polygon(3)  // use base class constructor
{
	this->set_Sides(a,0);
	this->set_Sides(b,1);
	this->set_Sides(c,2);
}


Triangle::~Triangle(void)
{
}

כתיבת תגובה

האימייל לא יוצג באתר. שדות החובה מסומנים *