SEARCH

The How-To Geek Forums Have Migrated to Discourse

How-To Geek Forums / Geek Stuff

C++ Programming Help Required

(5 posts)
  • Started 2 years ago by cas34
  • Latest reply from raphoenix
  • Topic Viewed 383 times

cas34
cas34
Posts: 361

Hi,

I am currently trying to teach myself C++ and have reached a brick wall. The problem I am having relates to a class using another class. The programme I am trying to understand is in two parts which are posted bellow:

PART 1:-

//Begin "Rect.hpp"
//To call class use #include "Rect.hpp"
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

class Point //hold x, y coordinates
{
//no constructor, using default
public:
void SetX(int x) {ItsX = x;} //defined
void SetY(int y) {ItsY = y;} //defined
int GetX() const {return ItsX;} //defined
int GetY() const {return ItsY;} //defined
private:
int ItsX;
int ItsY;
}; //end of Point declaration

class Rectangle
{
public:
Rectangle(int Top, int Left, int Bottom, int Right); //constructor
~Rectangle(){} //deconstructor

int GetTop()const {return ItsTop;} //defined
int GetLeft()const {return ItsLeft;} //defined
int GetBottom()const {return ItsBottom;} //defined
int GetRight()const {return ItsRight;} //defined

Point GetUpperLeft()const {return ItsUpperLeft;} //defined
Point GetLowerLeft()const {return ItsLowerLeft;} //defined
Point GetUpperRight()const {return ItsUpperRight;} //defined
Point GetLowerRight()const {return ItsLowerRight;} //defined

void SetUpperLeft (Point Location) {ItsUpperLeft = Location;} //defined
void SetLowerLeft (Point Location) {ItsLowerLeft = Location;} //defined
void SetUpperRight (Point Location) {ItsUpperRight = Location;} //defined
void SetLowerRight (Point Location) {ItsLowerRight = Location;} //defined

void SetTop (int Top) {ItsTop = Top;} //defined
void SetLeft (int Left) {ItsLeft = Left;} //defined
void SetBottom (int Bottom) {ItsBottom = Bottom;} //defined
void SetRight (int Right) {ItsRight = Right;} //defined

int GetArea()const;
int GetPerimeter()const;

private:
Point ItsUpperLeft;
Point ItsLowerLeft;
Point ItsUpperRight;
Point ItsLowerRight;
int ItsTop;
int ItsLeft;
int ItsBottom;
int ItsRight;
}; //end of Rectangle Declaration

PART 2:-
//6.9 - Begin Rect.cpp
#include "Rect.hpp"

Rectangle::Rectangle(int Top, int Left, int Bottom, int Right) //defining constructor
{
ItsTop = Top;
ItsLeft = Left;
ItsBottom = Bottom;
ItsRight = Right;

ItsUpperLeft.SetX(Left);
ItsUpperLeft.SetY(Top);

ItsUpperRight.SetX(Right);
ItsUpperRight.SetY(Top);

ItsLowerLeft.SetX(Left);
ItsLowerLeft.SetY(Bottom);

ItsLowerRight.SetX(Right);
ItsLowerRight.SetY(Bottom);
}

//compute area of the rectangle by finding cornersides, establish width and
//height and then multiply
int Rectangle::GetArea() const //defining GetArea
{
int Width = ItsRight - ItsLeft;
int Height = ItsTop - ItsBottom;
return (Width * Height);
}

int Rectangle::GetPerimeter()const //defining GetPerimeter
{
int Width_Top = ItsRight - ItsLeft;
int Width_Bottom = Width_Top;
int Height_Left = ItsTop - ItsBottom;
int Height_Right = Height_Left;
return (Width_Top + Width_Bottom + Height_Left + Height_Right);
}

int main()
{
//initialise a local Rectangle variable
Rectangle MyRectangle (100, 20, 50, 80);

int m;
int Area = MyRectangle.GetArea();
int Perimeter = MyRectangle.GetPerimeter();

cout << "Area: " << Area << " square metres." <<endl;
cout << "Perimeter: " << Perimeter << " metres." <<endl;
cout << "\nLower Right X Coordinate: ";
cout << MyRectangle.GetLowerRight().GetX() << endl;
cout << "Lower Right Y Coordinate: ";
cout << MyRectangle.GetLowerRight().GetY() << endl;

cin >> m;
return 0;
}

The programme is doing what I want it to do and is working but I am just unsure of how it is doing it. Here are the things I am unsure of.

1. Would I be correct in thinking that the 4 functions - GetTop(), GetLeft(), GetBottom() and GetRight() are not actually doing anything here? The only reason they are included is because it is good programming practice?

2. Would I be correct in thinking that the 4 functions - SetUpperLeft(Point Location),SetUpperRight(Point Location),SetLowerLeft(Point Location) and SetLowerRight(Point Location) are not actually doing anything here and again are only included because it is good programming practice? Also if I wanted to use them then I would need to pass in values in the main body of the programme?

3. Would I be correct in thinking that the 4 functions - SetTop(int top), SetLeft(int left), SetBottom(int bottom) and SetRight(int right) are not actually doing anything here and again are only included because it is good programming practice? Also if I wanted to use them then I would need to pass in values in the main body of the programme?

4. Are the 4 functions - GetUpperLeft,GetLowerLeft,GetUpperRight and GetLowerRight being used so they can access private data in the point class?

5. ItsUpperLeft.SetX(left) and ItsUpperLeft.SetY(top) are they just setting the x and y coordinates of each vertex of the rectangle?

Please excuse my ignorance if I have not explained myself well and thanks in advance if anyone can help me.

Posted 2 years ago
Top
 
raphoenix
raphoenix
Posts: 14920

Probably Best Continuing Answers can be had a C++ Forum.

Some Forum as this might be helpful for you.
http://www.cplusplus.com/forum/beginner/
http://www.cplusplus.com/forum/general/

There are many more Forums plus tons of books available on the net. :) :)

Rick P.

Posted 2 years ago
Top
 
cas34
cas34
Posts: 361

Thanks Raphoenix, I shall take a look at those forums later and let you know how I get on.

Posted 2 years ago
Top
 
cas34
cas34
Posts: 361

Cheers mate, I am now getting some help but not yet satisfied. You can consider this thread closed.

Posted 2 years ago
Top
 
raphoenix
raphoenix
Posts: 14920

GREAT !!! cas34

Been a lonnnng time since I programmed C so I thought that another Forum would probably be more helpful. :)

Rick P.

Posted 2 years ago
Top
 



Topic Closed

This topic has been closed to new replies.