Question

1. Write a virtual member function named length , which specifies that the function is constant...

1. Write a virtual member function named length , which specifies that the function is constant (that is does not modify the member data) and returns the square root of x squared plus y squared.

2.write the operator << function to have a member of the ostream class as its left hand operand and a member of the Two Dimensions class as its right hand operand. This function shall take the left hand operand by reference, This function shall return a reference to the left hand operand by reference, and the right hand operand by constant reference. This function shall return a reference to the left hand operand by reference. It shall print the values of X,Y and length to the ostream object that is the left hand operand.

3.Write the operator<, function for the Three Dimensions class. It shall print the values of x y z and length to the ostream object that is the left hand operand.

4. Why cant the operator << function that works with the TwoDimensions class be written as a member function.

5. write a length function that returns the square root x squared plus y square plus z square

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Written class for TwoDimentions,

This is the base class from which ThreeDimentions class inherits. In main , creating pointer to object of class TwoDimentions , hence providing virtual function for setting value of third point z and getting third point z.

#include<iostream>

#include<cmath>

using namespace std;

class TwoDimensions

{

int x,y;

public:

TwoDimensions()

{

x=0;

y=0;

}

void setX(int v)

{

x=v;

}

void setY(int v)

{

y=v;

}

virtual void setZ(int v)

{

}

virtual void getZ() const{

}

int getX() const

{

return x;

}

int getY() const

{

return y;

}

friend ostream& operator<<(ostream &out,TwoDimensions &obj)

{

out<<"x= "<<obj.x<<", y = "<<obj.y<<endl;

return out;

}

virtual double length() const

{

return sqrt(getX()*getX()+ getY()*getY());

}

};

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

//Written class for ThreeDimentions

#include"TwoDimensions.h"

class ThreeDimentions: public TwoDimensions

{

int z;

public:

ThreeDimentions():TwoDimensions()

{

z=0;

}

void setZ(int v)

{

z=v;

}

friend ostream& operator<<(ostream &out,ThreeDimentions &obj)

{

out<<"x= "<<obj.getX()<<", y= "<<obj.getY()<<", z= "<<obj.z<<endl;

return out;

}

double length() const

{

return sqrt(getX()*getX()+getY()*getY()+z*z);

}

};

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

//Written Main.cpp two show the two objects TwoDimentions and ThreeDimentions

#include <iostream>

#include"ThreeDimensions.h"

using namespace std;

int main() {

//std::cout << "Hello World!\n";

//declare 2-dimentinal pointer

TwoDimensions *obj1,*obj2;

//set x and y of obj1

obj1=new TwoDimensions;

obj1->setX(10);

obj1->setY(20);

//set x,y and z of 3-d object obj2

obj2=new ThreeDimentions;

obj2->setX(10);

obj2->setY(20);

obj2->setZ(-5);

//print obj1 and obj2

cout<<"2-d object is : "<<endl<<*obj1<<endl;

cout<<"3-d object is : "<<endl<<*obj2<<endl;

cout<<"Length of 2-d object: "<<obj1->length()<<endl;

cout<<"Length of 3-d object: "<<obj2->length()<<endl;

}

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

//Output

2-d object is :
x= 10, y = 20

3-d object is :
x= 10, y= 20, z= -5

Length of 2-d object: 22.3607
Length of 3-d object: 22.9129

Add a comment
Know the answer?
Add Answer to:
1. Write a virtual member function named length , which specifies that the function is constant...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 12. Declare the operator< as a friend function. }: II This is the end of the...

    12. Declare the operator< as a friend function. }: II This is the end of the ThreeDimensions class 13. Write the operator< function for the ThreeDimensions class. It shall print the values of x, y, z, and length to the ostream object that is the left hand operand. 14. Write a main funcdon that has a TwoDimension object with dimensions 3 and 4 ThreeDimension object with dimensions 12, 16, and 15. Print these two objects. and a 15a. Could the...

  • c++)Which situation would require the operator to be overloaded as a non-member function? provide an example...

    c++)Which situation would require the operator to be overloaded as a non-member function? provide an example of your answer choice of when this would occur. a. The overloaded operator is =. b. The left operand is an int. c. The operator returns a reference. d. The left operand must not be a class object (or a reference to a class object).

  • public: /this makes the functions that are coded later public 8. Write a default constructor for...

    public: /this makes the functions that are coded later public 8. Write a default constructor for the class ThreeDimensions. 9. Write a copy constructor for the class ThreeDimensions. . e a srctor that has three parameters and sets the values of the private data the values of the parameters. 11. Write a length function that returns the square root of x squared plus y squared plus z squared.

  • Code in c++ please. Does not have to be complete, just write code for the ones you know. Thank y...

    Code in c++ please. Does not have to be complete, just write code for the ones you know. Thank you. template <typename T> class smart_ptr { public: smart_ptr(); // Create a smart_ptr that is initialized to nullptr. The reference count // should be initialized to nullptr. explicit smart_ptr(T* raw_ptr); // Create a smart_ptr that is initialized to raw_ptr. The reference count // should be one. smart_ptr(const smart_ptr& rhs); // Copy construct a pointer from rhs. The reference count should be...

  • Q1. Write a recursive function in C++ void printNumberedChars(string str, int i=0) { ... } which...

    Q1. Write a recursive function in C++ void printNumberedChars(string str, int i=0) { ... } which prints each character of the given string str on a new line, with each line numbered 1, 2, 3, …. For example calling the function with printNumberedChars("hello"); should output the following: 1. h 2. e 3. l 4. l 5. o Q2. Write a recursive function in C++ int sumArray(const int* arr, unsigned int size) { ... } which takes an array of integers,...

  • 17. Write a non-member function called centroid(param) that takes a static array of points and the...

    17. Write a non-member function called centroid(param) that takes a static array of points and the size of the array and return the centroid of the array of points. If there is no center, return the origins private: double x, y, z public: /Constructors Point(); Point(double inX, double inY, double inZ = 0); Point(const Point& inPt); / Get Functions double getX() const; double getY) const; double getZ) const; Set Functions void setX(double inX); void setY(double inY); void setZ(double inZ); void...

  • OUTCOMES After you finish this assignment, you will be able to do the following: Define an...

    OUTCOMES After you finish this assignment, you will be able to do the following: Define an abstract class Create concrete classes from an abstract class Overload an operator Split classes into .h and .cpp files Open files for reading Write to files Use output manipulators such as setw, fixed, and setprecision DESCRIPTION A binary arithmetic operation takes two double operands (left and right) to perform addition, subtraction, multiplication, or division on. For example, 10 + 11 is an addition (+)...

  • Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the c...

    C++ Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the class point that should have the following Private data members x and y (of type int), with default values of 0 A constant ID of type int A private static integer data member named numOfPoints This data member should be o Incremented whenever a new point object is created. o Decremented whenever a point object is destructed. A default constructor An...

  • Please use C++. Write a class named Circle that has a double data member named radius...

    Please use C++. Write a class named Circle that has a double data member named radius and a static double data member named maxRadius. It should have a default constructor that initializes the radius to 1.0. It should have a constructor that takes a double and uses it to initialize the radius. It should have a method called calcArea that returns the area of the Circle (use 3.14159 for pi). It should have a static set method for the maxRadius....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT