Good afternoon,
I am having a hard time understanding how to use the "Friend" function prototype and the Friend function to move my rectangle. My programm with just the constructors, the properties and the functions work corectly until I put the "friend" function prototype and function.
I would so much appreciate your help in getting ride of my error messages. The purpose of the "friend" is to move a rectangle from one location to another.
Thank you for your time,
John
My code:
#include "stdafx.h"
using namespace System;
ref class Rectangle // Class definition at global scope
{
//private:
int Left;
int Top;
int Right;
int Bottom;
public:
//1st Constructor
Rectangle() ;
//2nd Constructor
Rectangle(int ,int ,int , int ) ;
//Defining the property for obtaining the LeftValue
property int LeftValue
{
int get ()
{
return Left;
}
void set(int value) { Left = value; }
}
virtual String^ ToStringLeft() override
{ return Left + L"Left";}
//Defining the property for obtaining the TopValue
property int TopValue
{
int get ()
{
return Top;
}
void set(int value) { Top = value; }
}
virtual String^ ToStringTop() override
{ return Top + L"Top";}
//Defining the property for obtaining the RightValue
property int RightValue
{
int get ()
{
return Right;
}
void set(int value) { Right = value; }
}
virtual String^ ToStringRight() override
{ return Right + L"Right";}
//Defining the property for obtaining the BottomValue
property int BottomValue
{
int get ()
{
return Bottom;
}
void set(int value) { Bottom = value; }
}
virtual String^ ToStringBottom() override
{ return Bottom + L"Bottom";}
//Function of Length
long Length()
{
return (Right - Left);
}
//Function of Width
long Width()
{
return (Bottom - Top);
}
//function of area
long area()
{
return (Right - Left)*(Bottom - Top);
}
//Friend function prototype
friend void MoveRect(Rectangle& aRect, int x, int y);
};
Rectangle::Rectangle ()
{
Console::WriteLine(L"Rectangle Constructor with no parameters called.");
Left = 3;
Top = 4;
Right = 7;
Bottom = 8;
}
Rectangle::Rectangle(int lt, int tp, int rt, int bm)
{
Console::WriteLine(L"Rectangle Constructor with four parameters called.");
Left = lt;
Top = tp;
Right = rt;
Bottom = bm;
}
// Friend function to move rectangle
void MoveRect(Rectangle& aRect, int x, int y)
{
int Length = aRect.Right - aRect.Left; // Get length of rectangle
int Width = aRect.Bottom - aRect.Top; // Get width of rectangle
aRect.Left = x; // Set top left point
aRect.Top = y; // to new position
aRect.Right = x + Length; // Get bottom right point as
aRect.Bottom = y + Width; // increment from new position
}
int main(array<System::String ^> ^args)
{
Rectangle Rect(2,8,80,120); //Declaring Rect
Rectangle Rect2;
Rect2 = Rect;
MoveRect(Rect2, 10, 30);
{
//Displaying Rect values
Console::WriteLine(L"Displaying Rect values");
Console::WriteLine(L"Rect left Value: = {0}",Rect.LeftValue);
Console::WriteLine(L"Rect top Value: = {0}",Rect.TopValue);
Console::WriteLine(L"Rect Right Value: = {0}",Rect.RightValue);
Console::WriteLine(L"Rect Bottom Value: = {0}",Rect.BottomValue);
Console::WriteLine(L"Rect area = {0}",Rect.area());
Console::WriteLine(L"Rect Length = {0}",Rect.Length());
Console::WriteLine(L"Rect Width = {0}",Rect.Width());
Console::WriteLine(L" ");
//Display values for Rect2 after the move
Console::WriteLine(L"Displaying the Rect2 values after the move:");
Console::WriteLine(L"Rect2 area after move = {0}",Rect2.area());
Console::WriteLine(L"Rect2 Left after move = {0}",Rect2.LeftValue());
Console::WriteLine(L"Rect2 Top Value after move = {0}",Rect2.TopValue());
Console::WriteLine(L"Rect2 Length after move = {0}",Rect2.Length());
Console::WriteLine(L"Rect2 Width after move = {0}",Rect2.Width());
Console::WriteLine(L" ");
return 0;
}
}