Implementation of Polygon class - c++

I am writing a program that contains three classes: They are point, line and polygon. I have written the first two, but I am having trouble with the last one.
This class has to have two constructors, one of them builds an object with a point (tip of polygon) and another one builds it with a line. These functions have to be as follows:
polygon(point** arr,int size) and polygon(line** arr,int size).
I don't know why point and line are pointer to pointer? What are the attributes of the polygon class and how can I write polygon's constructor?
class point
{
private:
int first;
int second;
public:
point(void);
point (int x,int y);
point (const point& other);
int getX();
int getY();
int distance(point* other);//distance of two point
line* Line(point*);//build a line with two point
polygon* triangle(point*,point*);//build a triangle with three point
~point(void);
};
point::point(void)
{
first=0;
second=0;
}
point::point (int x,int y)
{
first=x;
second=y;
}
point::point(const point& other)
{
first=other.first;
second=other.second;
}
int point::gha(int a)
{
if(a>=0)
return a;
else
return -a;
}
int point::pow(int a)
{
return a*a;
}
int point::getX(){return first;}
int point::getY(){return second;}
int point::distance(point* other)
{
int d= sqrt((pow(first-other->first))+(pow(second-other->second)));
return d;
}
line* point::Line(point* other)
{
line l(this,other);
return &l;
}
polygon* point::triangle(point*,point*){
}
point::~point(void)
{
}
////////////////////////////
class line
{
private:
int m;
int c;
public:
line(void);
line(point*,point*);
line(int ,point*);
bool isParallel(line*);
bool isPrependicular(line*);
point* intersection(line*);
line* parallel(point*);
polygon* triangle(line*,line*);
~line(void);
};
line::line(void)
{
m=1;
c=0;
}
line::line(point* a,point* b)
{
m=((a->getY())-b->getY())/(a->getX()-b->getX());
c=a->getY()-(m*(a->getX()));
}
line::line(int dip,point* a)
{
m=dip;
c=a->getY()-(dip*(a->getX()));
}
bool line:: isParallel(line* other)
{
if(m==other->m)
return true;
else
return false;
}
bool line::isPrependicular(line* other)
{
if((m*other->m)==1 || (m*other->m)==-1)
return true;
else
return false;
}
point* line::intersection(line* other)
{
int x=(other->c-c)/(m-other->m);
int y=(m*x)+c;
point p (x,y);
return &p;
}
line* line::parallel(point* other)
{
line l(m,other);
return &l;
}
line::~line(void)
{
}
///////////////////////////////////
class polygon
{
private:
int count;
point* tip;
line* l;
public:
polygon(void);
polygon(point** arr,int size);
polygon(line** arr,int size);
bool isTriangle();
bool isSquare();
~polygon(void);
};

Related

Array of arrays of different types of shapes

I want to create a "3D console game" using RayCasting. To begin with, I needed a container with all the information about the world. First of all, I started with just two shapes, a rectangle and a circle. Actually, here's how I implemented them:
enum POINTS
{
POINT_1,
POINT_2
};
enum ASIXS
{
ASIX_X,
ASIX_Y,
};
class Point
{
private:
double properties[2];
public:
Point()
{
properties[ASIX_X] = 0;
properties[ASIX_Y] = 0;
}
Point(double x, double y)
{
properties[ASIX_X] = x;
properties[ASIX_Y] = y;
}
double& operator[] (int index)
{
return properties[index];
}
};
class Rectangle
{
private:
Point base[2];
public:
//Points getter
Point& operator[](int index)
{
return base[index];
}
};
class Circle
{
private:
Point center;
double radius = 0;
public:
Point& getCenter()
{
return center;
}
double& getRadius()
{
return radius;
}
};
Next, I need a generic container. Obviously this should be a pattern:
template<typename T>
class ObjectArr
{
protected:
int qty;
T* objs;
public:
ObjectArr()
{
qty = 0;
objs = nullptr;
}
~ObjectArr()
{
delete[] objs;
}
void add()
{
T* temp = new T[++qty];
for (int i = 0; i < qty - 1; i++)
temp[i] = objs[i];
delete[] objs;
objs = temp;
}
T getObj(int index)
{
return objs[index];
}
};
Here's what it will look like:
#include <iostream>
using namespace std;
int main()
{
ObjectArr<Rectangle> rect;
rect.add();
//rect.getObj(id)[point][asix]
rect.getObj(0)[POINT_1][ASIX_X] = 5;
cout << rect.getObj(0)[POINT_1][ASIX_X]; //Out 5
ObjectArr<Circle> cir;
cir.add();
//cir.getObj(id).getCenter()[asix]//getRadius()
cir.getObj(0).getCenter() = {0, 0};
return 0;
}
And in the end, I would like one common container that would store ObjectArr<>. From the beginning I thought about polymorphism. Something like this:
#include <vector>
class Container
{
//...
};
template<typename T>
class ObjectArr : public Container
{
//...
};
int main()
{
vector<Container*> test;
test.push_back(new ObjectArr<Rectangle>);
test.push_back(new ObjectArr<Circle>);
test[0]->add();
//...
return 0;
}
But with this approach, the function T getObj(int index) cannot be used from such a container. And writing it as a virtual one will not work either because of the template. And I also don’t want to prescribe every possible shape, so that it would be easier to add new shapes. Wrote a class of a new figure and all. Tell me, please, how should I be and what should I do, maybe I need to completely change the approach?

Writing a C++ iterator for a sparse matrix class

I'm attempting to get a basic constant forward-iterator to work in C++.
namespace Rcpp {
class SparseMatrix {
public:
IntegerVector i, p;
NumericVector x;
int begin_col(int j) { return p[j]; };
int end_col(int j) { return p[j + 1]; };
class iterator {
public:
int index;
iterator(SparseMatrix& g) : parent(g) {}
iterator(int ind) { index = ind; }; // ERROR!
bool operator!=(int x) const { return index != x; };
iterator operator++(int) { ++index; return (*this); };
int row() { return parent.i[index]; };
double value() { return parent.x[index]; };
private:
SparseMatrix& parent;
};
};
}
My intention is to use the iterator in contexts similar to the following:
// sum of values in column 7
Rcpp::SparseMatrix A(nrow, ncol, fill::random);
double sum = 0;
for(Rcpp::SparseMatrix::iterator it = A.begin_col(7); it != A.end_col(7); it++)
sum += it.value();
Two questions:
The compiler throws an error on the line indicated above: uninitialized reference member in 'class Rcpp::SparseMatrix&' [-fpermissive]. How can this be fixed?
How might double value() { return parent.x[index]; }; be re-worked to return a pointer to the value rather than a copy of the value?
A little context on the SparseMatrix class: like a dgCMatrix in R, this object of class SparseMatrix consists of three vectors:
i holds row pointers for every element in x
p gives indices in i which correspond to the start of each column
x contains non-zero values
Thanks to #Evg, here's the solution:
namespace Rcpp {
class SparseMatrix {
public:
IntegerVector i, p;
NumericVector x;
class iterator {
public:
int index;
iterator(SparseMatrix& g, int ind) : parent(g) { index = ind; }
bool operator!=(iterator x) const { return index != x.index; };
iterator& operator++() { ++index; return (*this); };
int row() { return parent.i[index]; };
double& value() { return parent.x[index]; };
private:
SparseMatrix& parent;
};
iterator begin_col(int j) { return iterator(*this, p[j]); };
iterator end_col(int j) { return iterator(*this, p[j + 1]); };
};
}
And it can be used as follows, for instance, to calculate colSums:
//[[Rcpp::export]]
Rcpp::NumericVector Rcpp_colSums(Rcpp::SparseMatrix& A) {
Rcpp::NumericVector sums(A.cols());
for (int i = 0; i < A.cols(); ++i)
for (Rcpp::SparseMatrix::iterator it = A.begin_col(i); it != A.end_col(i); it++)
sums(i) += it.value();
return sums;
}
And, the above function is faster than RcppArmadillo, RcppEigen, and R::Matrix equivalents when microbenchmarked from R!
Edit:
The above syntax is inspired by Armadillo. I've come to realize that a slightly different syntax (which involves fewer constructions) gives an iterator similar to Eigen:
class col_iterator {
public:
col_iterator(SparseMatrix& ptr, int col) : ptr(ptr) { indx = ptr.p[col]; max_index = ptr.p[col + 1]; }
operator bool() const { return (indx != max_index); }
col_iterator& operator++() { ++indx; return *this; }
const double& value() const { return ptr.x[indx]; }
int row() const { return ptr.i[indx]; }
private:
SparseMatrix& ptr;
int indx, max_index;
};
Which can then be used like this:
int col = 0;
for (Rcpp::SparseMatrix::col_iterator it(A, col); it; ++it)
Rprintf("row: %3d, value: %10.2e", it.row(), it.value());

Problem with Segmentation Fault with doing operations on class

Sorry for variables not being all in engilsh. I have a problem when i try to do the += operation on class called Uklad3.
It is initialized the same way as previous ones, but with this one the segmentation failure comes up when i try to do any operations on it.
Any sugestions how to fix it? I am not a proffesional programmer.
I sumbited the whole code because I know it might be little hard to read, I am learning and this is for ma class.
The goal of the operation += is to add points from one coordinate system to the other. It is only the portion of the task but this is where I have a problem.
class punkt
{
private:
double x;
double y;
double z;
public:
punkt(){};
string name;
punkt(string,double,double,double);
double getx() const {return x;}
double gety() const {return y;}
double getz() const {return z;}
};
punkt::punkt(string name_,double x_, double y_, double z_)
{
name=name_;
x=x_;
y=y_;
z=z_;
}
class Uklad
{
public:
static const int size = 10;
punkt tablica[size];
string uklad_name;
void add(punkt);
int licznik;
Uklad(){licznik=0;};
Uklad(string);
Uklad & operator+=(const Uklad &var)
{
for(int i=0;i<var.licznik;i++)
{
tablica[licznik]=var.tablica[i];
licznik++;
}
}
Uklad & operator-= (const Uklad &var)
{
for(int i=0;i<licznik;i++)
{
for(int j=0;j<var.licznik;j++)
{
if((tablica[i].getx()==var.tablica[i].getx()) and (tablica[i].gety()==var.tablica[i].gety()) and (tablica[i].getz()==var.tablica[i].getz()))
{
for(int k=i;k<licznik;k++)
{
tablica[k]=tablica[k+1];
}
licznik--;
}
}
}
}
};
Uklad::Uklad(string uklad_name_)
{
uklad_name=uklad_name_;
cout<<"Tworze uklad"<<endl;
}
void Uklad::add(punkt toAdd)
{
if(licznik<size)
{
tablica[licznik]=toAdd;
licznik++;
}
}
}
ostream & operator<<(ostream &s, const punkt &Punkt)
{
cout<<Punkt.name<<" "<<Punkt.getx()<<" "<<Punkt.gety()<<" "<<Punkt.getz();
return s<<" ";
}
ostream & operator<<(ostream &s, const Uklad &uklad)
{
for(int i=0;i<uklad.licznik;i++)
{
if(i==uklad.licznik-1) cout<<uklad.tablica[i]<<" ";
else
cout<<uklad.tablica[i]<<"; ";
}
return s<<" ";
}
int main()
{
//1.
string name1,name2,name3;
cin>>name1;
cin>>name2;
Uklad uklad1(name1);
Uklad uklad2(name2);
//2.
const int M=2;
double xtemp, ytemp, ztemp;
string nametemp;
string xs,ys,zs;
for(int i=0;i<M;i++)
{
cin>>nametemp;
cin>>xtemp;
cin>>ytemp;
cin>>ztemp;
punkt punktT(nametemp,xtemp,ytemp,ztemp);
uklad1.add(punktT);
}
//3.
const int N=2;
double xtemp2, ytemp2, ztemp2;
string nametemp2;
string xs2,ys2,zs2;
for(int i=0;i<N;i++)
{
cin>>nametemp2;
cin>>xtemp2;
cin>>ytemp2;
cin>>ztemp2;
punkt punktT2(nametemp2,xtemp2,ytemp2,ztemp2);
uklad2.add(punktT2);
}
//4.
cin>>name3;
Uklad uklad3(name3);
//5.
uklad3+=uklad1;
cout<<uklad3;
return 0;
}
static const int size = 10;
You make array about 10 size and after all this loops you go out of range of the array. That's why you have this error. You can give it higher size. Or you can use vectors. In case of using array you must be sure you will not go out of range.
I formated the code and add initializer to licznik. This code haven't segmentation fault error
class punkt
{
private:
double x;
double y;
double z;
public:
punkt(){};
string name;
punkt(string,double,double,double);
double getx() const {return x;}
double gety() const {return y;}
double getz() const {return z;}
};
punkt::punkt(string name_,double x_, double y_, double z_)
{
name=name_;
x=x_;
y=y_;
z=z_;
}
class Uklad
{
public:
static const int size = 10;
punkt tablica[size];
string uklad_name = "";
void add(punkt);
int licznik = 0;
Uklad(){licznik=0;};
Uklad(string);
Uklad& operator+=(const Uklad &var)
{
for(int i=0;i<var.licznik;i++)
{
cout << licznik << " += ";
tablica[licznik]=var.tablica[i];
licznik++;
}
return *this;
}
Uklad & operator-= (const Uklad &var)
{
for(int i=0;i<licznik;i++)
{
for(int j=0;j<var.licznik;j++)
{
cout << i << " -=";
if((tablica[i].getx()==var.tablica[i].getx()) and (tablica[i].gety()==var.tablica[i].gety()) and (tablica[i].getz()==var.tablica[i].getz()))
{
for(int k=i;k<licznik;k++)
{
tablica[k]=tablica[k+1];
}
licznik--;
}
}
}
return *this;
}
};
Uklad::Uklad(string uklad_name_)
{
licznik = 0;
uklad_name=uklad_name_;
cout << "Tworze uklad" << endl;
}
void Uklad::add(punkt toAdd)
{
if(licznik<size)
{
tablica[licznik]=toAdd;
licznik++;
}
}
ostream & operator<<(ostream &s, const punkt &Punkt)
{
cout<<Punkt.name<<" "<<Punkt.getx()<<" "<<Punkt.gety()<<" "<<Punkt.getz();
return s<<" ";
}
ostream & operator<<(ostream &s, const Uklad &uklad)
{
for(int i=0;i<uklad.licznik;i++)
{
if(i==uklad.licznik-1) cout<<uklad.tablica[i]<<" ";
else
cout<<uklad.tablica[i]<<"; ";
}
return s<<" ";
}
int main()
{
//1.
string name1,name2,name3;
cin>>name1;
cin>>name2;
Uklad uklad1(name1);
Uklad uklad2(name2);
//2.
const int M=2;
double xtemp, ytemp, ztemp;
string nametemp;
string xs,ys,zs;
for(int i=0;i<M;i++)
{
cin>>nametemp;
cin>>xtemp;
cin>>ytemp;
cin>>ztemp;
punkt punktT(nametemp,xtemp,ytemp,ztemp);
uklad1.add(punktT);
}
//3.
const int N=2;
double xtemp2, ytemp2, ztemp2;
string nametemp2;
string xs2,ys2,zs2;
for(int i=0;i<N;i++)
{
cin>>nametemp2;
cin>>xtemp2;
cin>>ytemp2;
cin>>ztemp2;
punkt punktT2(nametemp2,xtemp2,ytemp2,ztemp2);
uklad2.add(punktT2);
}
//4.
cin>>name3;
Uklad uklad3(name3);
//5.
uklad3+=uklad1;
cout<<uklad3;
return 0;
}

Get "Trigger Breakpoint Error at delete" when using template

I'm using visual studio.
I have 3 class.
It's ok when I try to print Rectangle ABCD, but when i push ABCD into Array [rectangle] A and try to print ABCD again, the "wntdll.pdb not loaded" appear and i continue, it's "triggers breakpoint error!!" at delete in ~Rectangle()
I know it's something up to the pointer in class Rectangle but can't firgure out.
`int main(){
Array<Rectangle>A;
Rectangle a;
cout << a; //It's oke
A.PushBack(a); // problem here
cout<<a;
}`
class Point
{
private:
float _x;
float _y;
public:
float GetX() { return _x; }
float GetY() { return _y; }
public:
Point();
Point(float, float);
Point(const Point&);
~Point() {};
public:
string ToString() const;
public:
friend istream& operator>>(istream&, Point*);
friend ostream& operator<<(ostream&, const Point&);
};
Point::Point() {
_x = 1;
_y = 1;
}
Point::Point(float x, float y) {
_x = x;
_y = y;
}
Point::Point(const Point& a) {
_x = a._x;
_y = a._y;
}
string Point::ToString() const{
stringstream out;
out << "( " << _x << "," << _y << " )";
return out.str();
}
istream& operator>>(istream& in, Point* a) {
cout << "Nhap x: ";
in >> a->_x;
cout << "Nhap y: ";
in >> a->_y;
return in;
}
ostream& operator<<(ostream& out, const Point& a)
{
out << a.ToString();
return out;
}
```
class Rectangle
{
private:
Point* _topleft;
Point* _botright;
public:
void Set_topleft(Point tl) { _topleft = &tl; }
Point Get_topleft() { return *_topleft; }
void Set_botright(Point br) { _botright = &br; }
Point Get_botright() { return *_botright; }
public:
Rectangle();
Rectangle(Point*, Point*);
~Rectangle();
public:
string ToString() const;
public:
friend istream& operator>>(istream&, Rectangle&);
friend ostream& operator<<(ostream&, const Rectangle&);
};
Rectangle::Rectangle()
{
_topleft = new Point(0, 2);
_botright = new Point(3, 0);
}
Rectangle::Rectangle(Point* a, Point* b)
{
_topleft = new Point(*a);
_botright = new Point(*b);
}
Rectangle::~Rectangle()
{
delete _topleft;
delete _botright;
}
string Rectangle::ToString() const
{
stringstream out;
out << "A" << *_topleft << "+" << "D" << *_botright;
return out.str();
}
istream& operator>>(istream& in, Rectangle& a)
{
std::cout << "A( x,y ): ";
in >> a._topleft;
std::cout << "D( x,y ): ";
in >> a._botright;
return in;
}
ostream& operator<<(ostream& out, const Rectangle& a)
{
out << a.ToString();
return out;
}
```
template<class T>
class Array
{
private:
T* _a;
int _len;
public:
Array();
~Array();
public:
int length() { return _len; }
void PushBack(T);
T GetAt(int);
};
template<class T>
Array<T>::Array() {
_a = new T[128];
_len = 0;
}
template<class T>
Array<T>::~Array() {
delete[] _a;
_len = 0;
}
template<class T>
void Array<T>::PushBack(T value) {
if (_len >= 128)
{
std::cout << "Array is over size, which is 128\n";
return;
}
_a[_len] = value;
_len++;
}
template<class T>
T Array<T>::GetAt(int pos) {
return _a[pos];
}
```
The problem in your code is that when you PushBack the Rectangle object a into the array, you create copy of this object. This means that you just copy pointers _topleft and _botright. So in this part of code, you have 2 objects with the same pointers, so you're trying to delete twice the same part of memory.
To fix this you need to define own copy constructor, which will create new pointers in the new object.
Remember also to define own move constructors or make it disable for your class.
Rectangle(const Rectangle &other)
{
if(other._topleft)
_topleft= new Point(*other._topleft);
else
_topleft = nullptr;
if(other._botright)
_botright= new Point(*other._botright);
else
_topleft = nullptr;
}
The next problem is because of definition of void Array<T>::PushBack(T value). Please change it to the void Array<T>::PushBack(const T& value).
Also, try Rectangle(Rectangle&& o) = delete;

What did I do wrong C++ Error

I am trying to do below program and getting following error. please tell me where I am wrong. "main.cpp(289) : error C2535: 'void __thiscall Line::setdefaultvalues(void)' : member function already defined or declare"
#include<iostream>
#define PI 3.14
#include<conio.h>
using namespace std;
class Point
{
int x;
int y;
public:
Point(int=0,int=0);
int getX();
int getY();
void setX(int);
void setY(int);
};
Point::Point(int ax,int ay){
x = ax;
y = ay;
}
int Point::getX(){
return x;
}
int Point::getY(){
return y;
}
void Point::setX(int ax){
x = ax;
}
void Point::setY( int ay){
y =ay;
}
class Shape
{
public:
virtual void draw()
{
cout<<"draw a shape"<<endl;
}
virtual int calcarea()
{
cout<<"area of shape is"<<endl;
return 0;
}
};
class Rectangle:public Shape
{
Point p1;
Point p2;
Point p3;
Point p4;
public:
Rectangle(Point &a ,Point &b ,Point &c ,Point &d);
Point getp1();
Point getp2();
Point getp3();
Point getp4();
virtual void draw();
virtual int calcarea();
int getlength();
int getWidth();
void show()
{
cout<<"P1 ( "<<p1.getX()<<","<<p1.getY()<<" )"<<endl;
cout<<"P2 ( "<<p2.getX()<<","<<p2.getY()<<" )"<<endl;
cout<<"P3( "<<p3.getX()<<","<<p3.getY()<<" )"<<endl;
cout<<"P4( "<<p4.getX()<<","<<p4.getY()<<" )"<<endl;
//p3.setX(8);
// p3.setY(4);
//p4.setX(0);
//p4.setY(4);
}
void defaultvalues (){
p1.setX(0);
p1.setY(0);
p2.setX(8);
p2.setY(0);
p3.setX(8);
p3.setY(4);
p4.setX(0);
p4.setY(4);
}
};
Rectangle::Rectangle(Point &a,Point &b,Point &c,Point &d){
if ((a.getX()<0||a.getX()>20)||(a.getY()<0||a.getY()>20)||
(b.getX()<0||b.getX()>20) ||(b.getY()<0||b.getY()>20)||
( c.getX()<0||c.getX()>20) ||(c.getY()<0||c.getY()>20)||
(d.getX()<0||d.getX()>20) ||(d.getY()<0||d.getY()>20))
{
std::cout << "Incorect Point value for constructor replacing with default values" <<std::endl;
defaultvalues();
}
else
{
p1.setX(a.getX());
p1.setY(a.getY());
p2.setX(b.getX());
p2.setY(b.getY());
p3.setX(c.getX());
p3.setY(c.getY());
p4.setX(d.getX());
p4.setY(d.getY());
}
/* if( a.getX() != c.getX() )
cout<<"error:a and c must have same x values"<<endl;
if( b.getX() != d.getX() )
cout<<"error:b and c must have same x values"<<endl;
*/
}
int Rectangle ::getlength(){
int l;
if(p1.getX()==p2.getX())
{
l=p2.getY()-p1.getY();
}
else if(p2.getX()==p3.getX())
{
l=p3.getY()-p1.getY();
}
else if(p1.getX()==p4.getX())
{
l=p4.getY()-p1.getY();
}
else
{
l=0.0;
}
int w;
if(p1.getY()==p2.getY())
{
w=p2.getX()-p1.getX();
}
else if(p2.getY()==p3.getY())
{
w=p3.getX()-p1.getX();
}
else if(p1.getY()==p4.getY())
{
w=p4.getX()-p1.getX();
}
else
{
w=0.0;
}
if (l>w)
{
return l;
}
else
{
return w;
}
};
int Rectangle::getWidth()
{
int l;
int w;
if(p1.getX()==p2.getX())
{
l=p2.getY()-p1.getY();
}
else if(p2.getX()==p3.getX())
{
l=p3.getY()-p1.getY();
}
else if(p1.getX()==p4.getX())
{
l=p4.getY()-p1.getY();
}
else
{
l=0.0;
}
if(p1.getY()==p2.getY())
{
w=p2.getX()-p1.getX();
}
else if(p2.getY()==p3.getY())
{
w=p3.getX()-p1.getX();
}
else if(p1.getY()==p4.getY())
{
w=p4.getX()-p1.getX();
}
else
{
w=0.0;
}
if (l<w)
{
return l;
}
else
{
return w;
}
}
int Rectangle::calcarea()
{
int l=getlength();
int w=getWidth();
return l*w;
}
void Rectangle:: draw()
{
cout<<"this is a rectangle"<<endl;
}
class Line : public Shape {
Point p1;
Point p2;
public:
void setdefaultvalues();
Line(Point&a,Point&b)
{
if((a.getX()<0||a.getX()>20)
||(a.getY()<0||a.getY()>20)
||(b.getX()<0||b.getX()>20)
||(b.getY()<0||b.getY()>20))
{
setdefaultvalues();
}
else
{
if((a.getX()==b.getX()||a.getY()==b.getY()))
{
setdefaultvalues();
}
else
{
p1.setX(a.getX());
p1.setY(a.getY());
p2.setX(b.getX());
p2.setY(b.getY());
}
}
}
void setdefaultvalues()
{
p1.setX(0);
p1.setY(0);
p2.setX(8);
p2.setY(8);
}
virtual void draw()
{
cout<<"this is a line"<<endl;
}
void show()
{
cout<<"P1 ( "<<p1.getX()<<","<<p1.getY()<<" )"<<endl;
cout<<"P2 ( "<<p2.getX()<<","<<p2.getY()<<" )"<<endl;
}
};
/*class Circle : public Shape {
Point centre;
double radius;
public:
Circle(Point&a, double r)
{
if(r>0&&r<20)
{
radius=r;
centre.setX(a.getX());
centre.setY(a.getY());
}
else
{
radius=3;
centre.setX(0);
centre.setY(0);
}
}
virtual void draw()
{
cout<<"this is a circle"<<endl;
}
virtual int calcarea()
{
cout<<PI*radius*radius<<endl;
}
};*/
class Triangle:public Shape{
Point p1;
Point p2;
Point p3;
public:
Triangle(Point&a,Point&b,Point&c){
if((a.getX()==b.getX())&&(a.getY()==b.getY())
&&(a.getX()==c.getX())&&(a.getY()==c.getY())
&&(b.getX()==c.getX())&&(b.getY()==c.getY()))
{
std::cout << "Incorect values" <<std::endl;
}
else
{ p1.setX(15);
p1.setY(15);
p2.setX(23);
p2.setY(30);
p3.setX(50);
p3.setY(25);
}
virtual int calcarea();
virtual void draw();
}
virtual int calcarea()
{ int area;
area=[p1*getX()(p2*getY()-p3*getY())+p2*getX()(p3*getY()-p1*getY())+p3*getX()(p1*getY()-p2*getY())]/2;
return area;
}
};
void drawshapes();
int main() {
Point p1(0,0),p2(0,4),p3(4,0),p4(4,4);
Shape* _shape[ 4 ];
_shape[0]=new Rectangle(p1,p2,p3,p4);
/*drawshapes(_shape,4);
calarea(_shape,4);
*/
Line l1(p1,p2),l2(p1,p3);
//_shape[0]=new circle(p1,3);
_shape[1]=new Line(p1,p4);
_shape[2]=new Line(p2,p3);
/* drawshapes(_s,4);
calcarea(_s,4);*/
return 0;
}
You have this code:
class Line : public Shape
{
Point p1;
Point p2;
public:
void setdefaultvalues();
...
void setdefaultvalues()
{
p1.setX(0);
p1.setY(0);
p2.setX(8);
p2.setY(8);
}
...
}
Turn that code into this:
class Line : public Shape
{
Point p1;
Point p2;
public:
void setdefaultvalues()
{
p1.setX(0);
p1.setY(0);
p2.setX(8);
p2.setY(8);
}
...
}
In other words take the two declarations of the setdefaultvalues function and reduce it to one.
EDIT: Changed definition to declaration.