In: Computer Science
#include<iostream>
using namespace std;
class point{
private:
int x;
int y;
public:
void print()const;
void setf(int, int);
};
class line{
private:
point ps;
point pe;
public:
void print()const;
void setf(int, int, int, int);
};
class rectangle{
private:
line length[2];
line breadth[2];
public:
void print()const;
void setf(int, int, int, int, int, int, int, int);
};
int main(){
rectangle r1;
r1.setf(3,4,5,6, 7, 8, 9, 10);
r1.print();
system("pause");
return 0;
}
a. Write function implementation of rectangle, line and
point.
b. What is the call of constructor of point, line and rectangle
when r1 is created? What is the call of
destructor(if any) of point, line and rectangle when r1 goes out of
scope?
If you have any doubts, please give me comment...
#include <iostream>
using namespace std;
class point
{
private:
int x;
int y;
public:
point() {}
point(int, int);
void print() const;
void setf(int, int);
~point();
};
point::point(int x, int y)
{
setf(x, y);
}
void point::print() const { cout << "x: " << x << ", y: " << y << endl; }
void point::setf(int x, int y)
{
this->x = x;
this->y = y;
}
point::~point()
{
x = 0;
y = 0;
}
class line
{
private:
point ps;
point pe;
public:
line() {}
line(int, int, int, int);
void print() const;
void setf(int, int, int, int);
~line();
};
line::line(int x1, int y1, int x2, int y2)
{
setf(x1, y1, x2, y2);
}
void line::print() const
{
cout << "Point Starts: ";
ps.print();
cout << "Ends:";
pe.print();
}
void line::setf(int x1, int y1, int x2, int y2)
{
ps.setf(x1, y1);
pe.setf(x2, y2);
}
line::~line()
{
}
class rectangle
{
private:
line length[2];
line breadth[2];
public:
rectangle() {}
rectangle(int, int, int, int, int, int, int, int);
void print() const;
void setf(int, int, int, int, int, int, int, int);
~rectangle();
};
rectangle::rectangle(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
{
setf(x1, y1, x2, y2, x3, y3, x4, y4);
}
void rectangle::print() const
{
cout << "Length: ";
cout << "1 : ";
length[0].print();
cout << "2: ";
length[1].print();
cout << endl
<< "Breadth: ";
cout << "1 : ";
breadth[0].print();
cout << "2: ";
breadth[1].print();
}
void rectangle::setf(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
{
length[0].setf(x1, y1, x2, y2);
length[1].setf(x3, y3, x4, y4);
breadth[0].setf(x1, y1, x3, y3);
breadth[0].setf(x2, y2, x4, y4);
}
rectangle::~rectangle()
{
}
// x2, y2 x4, y4
// x1,y1 x3, y3
int main()
{
rectangle r1;
r1.setf(3, 4, 5, 6, 7, 8, 9, 10);
r1.print();
system("pause");
return 0;
}