Write a C Program This problem concerns function return by r

Write a C++ Program

This problem concerns \'function return by reference\'. Write a class called Vec2 that has two private members x, and y. Two of the public functions are called x_value(), and y_value(). They make use of \'return by reference\' to assign values to the the private members x, and y. Test your class with a main() function that asks for two floats as inputs. You main() function may look like:

Solution

#include <iostream>
#include<conio.h>
using namespace std;

class Vec2
{
   private:
   float x;
   float y;

   public:
  
   int& x_value()
   {
       cout<<\"Value of x: \"<<x;
       return x;
   }

   int& y_value()
   {
       cout<<\"Value of y: \"<<y;
       return y;
   }

};

int main()
{
   float xx,yy;
   cout<<\"Enter 1st value :\";
   cin>>xx;
   cout<<\"Enter 2nd value :\";
   cin>>yy;
  
Vec2 v;

    v.x_value()=xx;
   v.y_value()=yy;

   cout<<\"Number 1: \"<<xx;
   cout<<\"Number 2: \"<<yy;
   return 0;
}

Write a C++ Program This problem concerns \'function return by reference\'. Write a class called Vec2 that has two private members x, and y. Two of the public f

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site