Question

In: Computer Science

#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int,...

#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?

Solutions

Expert Solution

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;

}


Related Solutions

#include<vector> #include<iostream> using namespace std; void println(const vector<int>& v) {    for (int x : v)...
#include<vector> #include<iostream> using namespace std; void println(const vector<int>& v) {    for (int x : v)        cout << x << " ";    cout << endl; } void println(const vector<string>& v) {    for (const string& x : v)        cout << " "<< x << " ";    cout << endl; } int main() {    vector<int> v0;    cout << "An empty vector of integers: ";    println(v0);    vector<int> v1(3);    cout << "A...
#include <iostream> #include <fstream> #include <vector> using namespace std; struct Point{ int x, y; bool operator==(const...
#include <iostream> #include <fstream> #include <vector> using namespace std; struct Point{ int x, y; bool operator==(const Point& p2) { return this->x == p2.x and this->y == p2.y; } bool operator!=(const Point& p2) { return this->x != p2.x or this->y != p2.y; } friend ostream &operator<<( ostream &out, const Point &P ) { out << "(" << P.x << ", " << P.y << ")"; return out; } friend istream &operator>>( istream &in, Point &P ) { char d1, d2, d3;...
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int&...
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int& numberUsed) { cout << "Enter up to " << size << " nonnegative whole numbers.\n" << "Mark the end of the list with a negative number.\n"; int next, index = 0; cin >> next; while ((next >= 0) && (index < size)) { a[index] = next; index++; cin >> next; } numberUsed = index; } int search(const int a[], int numberUsed, int target) {...
#include <iostream> #include <string> #include <array> #include <vector> using namespace std; void f(int x, int &y);...
#include <iostream> #include <string> #include <array> #include <vector> using namespace std; void f(int x, int &y); int main(){ char s1[] = "zoey"; char s2[] = "zoey"; string s3 = s1; string s4 = s2; cout << "1. sizeof(s1) = " << sizeof(s1) << endl; if(s1 == s2) cout << "2. s1 == s2 is true" << endl; else cout << "2. s1 == s2 is false" << endl; if(s3 == s4) cout << "3. s3 == s4 is true" <<...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int LABSIZE = 10; const int PROJSIZE = 3; const int EXAMSIZE = 3; float getAverage(float arr[], int size) { float total = 0; for (int i = 0; i < size; i++) { total += arr[i]; } return total/size; } // the following main function do.... int main() { ifstream dataIn; string headingLine; string firstName, lastName; float quiz[QUIZSIZE]; float lab[LABSIZE]; float project[PROJSIZE]; float midExam[EXAMSIZE];...
Write the pseudocodes for these programs: a) #include <iostream> using namespace std; void print(int arr[],int n){...
Write the pseudocodes for these programs: a) #include <iostream> using namespace std; void print(int arr[],int n){ if(n==0) return; cout<<arr[n-1]<<" "; print(arr,n-1); } int main() { int n; cout<<"Enter the size of the array:"; cin>>n; int arr[n]; int i; cout<<"Enter the elements of the array:"<<endl; for(i=0;i<n;i++){ cin >> arr[i]; } cout<<"Displaying the elements of the array in reverse order:"<<endl; print(arr,n); return 0; } b) #include<iostream> #include<fstream> using namespace std; //student structure struct student { int id; float gpa; }; //node structure...
#include <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() {        const int...
#include <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() {        const int SIZE = 20;     char str[SIZE];     char str1[SIZE];     int n;     int k =1;        printf("Enter a word: \n");     fgets(str,SIZE,stdin);     printf("Enter another word: \n");     fgets(str1,SIZE,stdin);        if (str1[strlen(str1) - 1] == '\n')     {         str1[strlen(str1)-1] = '\0';     }     if (str[strlen(str) - 1] == '\n')     {         str[strlen(str)-1] = '\0';     }      ...
#include<iostream> using namespace std; void calcSumAndDiff(int ,int, int &, int &); void calcSumAndDiff(int n1,int n2, int...
#include<iostream> using namespace std; void calcSumAndDiff(int ,int, int &, int &); void calcSumAndDiff(int n1,int n2, int &sum, int &diff){ sum = n1 + n2; diff = n1 - n2; } int main() { int n1,n2,sum,diff; n1=30;n2=10; calcSumAndDiff(n1,n2,sum,diff); cout<<"Sum is :"<<sum<<endl; cout<<"Diff is:"<<diff<<endl; system("pause"); }
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int end) { for (int i = start; i <= end; i++) cout << items[i] << " "; cout << endl; } //The legendary "Blaze Sort" algorithm. //Sorts the specified portion of the array between index start and end (inclusive) //Hmmm... how fast is it? /* void blazeSort(double * items, int start, int end) { if (end - start > 0) { int p =...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int end) { for (int i = start; i <= end; i++) cout << items[i] << " "; cout << endl; } //The legendary "Blaze Sort" algorithm. //Sorts the specified portion of the array between index start and end (inclusive) //Hmmm... how fast is it? /* void blazeSort(double * items, int start, int end) { if (end - start > 0) { int p =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT