Code:
#ifndef point_h
#define point_h
int gotoxy(int,int);
void clrscr();
class Point {
public:
Point(int,int);
void setPoint(int,int);
int getx() {return x;}
int gety(){return y;}
void printpoint();
protected:
int x,y;
};
class Line :Point{
public:
Line(int,int);
int getlength();
void setlength();
void printline();
protected:
int length;
};
class Rectangle: Point {
public:
Rectangle(int,int);
void setheight();
void setwidth();
int getheight();
int getwidth();
void printrectangle();
protected:
int width,height;
};
class Diomand:Point {
public:
Diomand(int,int);
void set_Diomand_length();
int getsquare() {return square;}
int getlength();
void print_diomand();
int print_diomand_square(int,int,int);
protected:
int length;
int square;
};
//*******************************************************
Diomand::Diomand(int a,int b):Point(a,b)
{
set_Diomand_length();
}
void Diomand::set_Diomand_length()
{
int c,f;
cout<<"\nEnter square length:";
cin>>f;
cout<<"\nEnter length of diomand:";
cin>>c;
if((c%2)==0)
length = c;
else
{
cout<<"Enter even number for length:";
cin>>c;
length = c;
}
square = f;
}
int Diomand::getlength()
{
return length;
}
void Diomand::print_diomand()
{
int k,l,length;
k = getx();
l = gety();
length=getlength();
setPoint(k-length,l);
printpoint();
setPoint(k+length,l);
printpoint();
setPoint(k,l-length);
printpoint();
setPoint(k,l+length);
printpoint();
setPoint(k,l);
}
int Diomand::print_diomand_square(int k,int l,int counter)
{
if(counter>0)
{
setPoint(k,l);
print_diomand();
print_diomand_square(k++,l++,counter--);
setPoint(k,l);
print_diomand();
}
return 0;
}
Point::Point (int a,int b){
setPoint(a,b);
}
void Point::setPoint(int a,int b)
{
x=a;
y=b;
}
void Point::printpoint()
{
int i;
gotoxy(x,y);
cout<<"*";
}
//****************************************
Line::Line(int a,int b):Point(a,b)
{
Point(4,5);
setlength();
}
void Line::printline()
{
int i,new_x,new_y;
clrscr();
new_x =getx();
new_y =gety();
for(i=0;i<length-1;i++)
{
setPoint(new_x+i,new_y);
printpoint();
}
setPoint(new_x-length,new_y);
}
int Line::getlength()
{
return length;
}
void Line::setlength()
{
cout<<"Enter length:";
cin>>length;
}
//************************************************
Rectangle::Rectangle(int a,int b) :Point (a,b)
{
setwidth();
setheight();
}
void Rectangle::printrectangle()
{
int i,j;
int k,l;
clrscr();
k=getx();
l=gety();
for(i=0;i<width;i++)
for(j=0;j<height;j++)
{
setPoint(k+i,l);
printpoint();
setPoint(k,l+j);
printpoint();
setPoint(k+i,l+height-1);
printpoint();
setPoint(k+width-1,l+j);
printpoint();
}
}
void Rectangle::setwidth()
{
cout<<"Enter width:";
cin>>width;
}
void Rectangle::setheight()
{
cout<<"Enter height:";
cin>>height;
}
int Rectangle::getheight()
{
return height;
}
int Rectangle::getwidth()
{
return width;
}
//***************************************************
int gotoxy(int x, int y)
{
char es[100]; //string to hold the escape sequence
char xstr[100]; //need to convert the integers to strings
char ystr[100];
//convert the screen coordinates to strings
sprintf(xstr, "%d", x);
sprintf(ystr, "%d", y);
//build the escape sequence
es[0]='\0'; //truncate es to zero length
strcat(es, "\033["); //\033 is Esc in octal, 3*8 + 3 = 27
strcat(es, ystr); //concatenate the y move
strcat(es, "d"); // d is the code to move the cursor vertically
strcat(es, "\033[");
strcat(es, xstr);
strcat(es, "G"); //G is the code to move the cursor horizontally
//execute the escape sequence
printf("%s", es);
return 0;
}
void clrscr()
{
int i;
for(i=0;i<60;i++)
cout<<"\n";
}
#endif