473,414 Members | 1,688 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

help with classes

moi
hi,

im fairly new to c++ and as part of a module at uni im learning it. i know
the basics i suppose, but as our final hand-in we have to alter code we
wrote for an earlier assignment to use classes as opposed to structures. the
program basically modelled a simple polygon using point, line and polyogon
structures. and had various functions to find the properties of the shape
such as area, perimeter, etc.

the question reads along the lines of:-
rewrite the data structures as classes and assign private access to all data
members with appropriate public access member functions. all previous global
functions must now be redesigned as member functions of the corresponding
classes, thus the following member functions must be supported:
point: distance(), display()
line: length(), display()
polygon: area(), centroid(), perimeter(), display().

i have copied and pasted my code below. obviously im not looking for someone
to do my work for me (man, that would be nice) but i could really use a
nudge in the right direction, and a bit of a foothold to get started from.

thanks,

M.o'N

-----------------------------------
#include<iostream.h> //c++ I/O
#include<conio.h> //getche()
#include<math.h> // sqrt(),...

const int MAX_NO_VERTICES = 25; //max no of polygon vertices and edges.
const int MAX_NO_EDGES = 25;

//Data Structures:-

//Cartesian point
struct Point
{
double x, y, z; //triple coordinates of a point.
};

//Straight line segment
struct Line
{
Point p1, p2; //two end points of a line
};

//Polygon with Point vertices and Line straight-line edges.
struct Polygon
{
int num_vertices, num_edges; //number of vertices and edges.
Point verts[MAX_NO_VERTICES]; //polygon vertices
Line edges[MAX_NO_EDGES]; //polygon edges
};

//3D vector
struct Vector3D
{
double a, b, c; //vector components
};

//adds two Vector3D objects
Vector3D Add(const Vector3D& u, const Vector3D& v)
{
Vector3D add;
add.a = u.a + v.a;
add.b = u.b + v.b;
add.c = u.c + v.c;
return add;
}

//returns the cross product of two vectors (right-hand screw rule)
// UxV = [(UyVz-UzVy), (UzVx-UxVz), (UxVy-UyVx)]
Vector3D CrossProduct(const Vector3D& u, const Vector3D& v)
{
Vector3D cross;
cross.a = u.b*v.c - u.c*v.b;
cross.b = u.c*v.a - u.a*v.c;
cross.c = u.a*v.b - u.b*v.a;
return cross;
}

//returns the norm or magnitued (i.e. lenght) of a vector
double Norm (const Vector3D& v)
{return sqrt(v.a*v.a + v.b*v.b + v.c*v.c);}

//Point functions:

//returns the distance between 2 points
double Distance(const Point& p1, const Point& p2)
{
double x = p2.x-p1.x;
double y = p2.y-p1.y;
double z = p2.z-p1.z;
return sqrt(x*x + y*y + z*z);
}

//retunrs v=p-q, ie a Vector3D object
Vector3D Subtract(const Point& p, const Point& q)
{
Vector3D v;
v.a = p.x - q.x;
v.b = p.y - q.y;
v.c = p.z - q.z;
return v;
}

//o/p a point
void Display(const Point& p)
{ cout << "(" << p.x << ", " << p.y << ", " << p.z << ")" ; }

//Line functions:

//returns the length of a Line
double Length(const Line& l)
{ return Distance(l.p1, l.p2);}

//o/p a line
void Display(const Line& l)
{cout << "["; Display(l.p1) ; cout << ", " ; Display(l.p2) ; cout << "]" ;}

//polygon functions

//returns the perimeter of a polygon
double Perimeter(const Polygon& p)
{
double perimeter(0.0);
for (int i = 0; i<p.num_edges; i++)
perimeter += Length(p.edges[i]);
return perimeter;
}

//returns the centroid of a polygon (uses averaging of vertices)
Point Centroid(const Polygon& p)
{
Point centroid = {0.0,0.0,0.0};
for (int i=0; i<p.num_vertices; i++)
{
centroid.x += p.verts[i].x;
centroid.y += p.verts[i].y;
centroid.z += p.verts[i].z;
}
centroid.x /= p.num_vertices;
centroid.y /= p.num_vertices;
centroid.z /= p.num_vertices;
return centroid;
}

//returns the area of a polygon (uses the cross-product rule)
double Area(const Polygon& p)
{
Vector3D vi1v0, vi2v0, sum_vector;
for (int i=0; i<p.num_vertices-2; i++)
{
//use vertex 0 as 'local' origin
vi1v0 = Subtract(p.verts[i+1], p.verts[0]);
vi2v0 = Subtract(p.verts[i+2], p.verts[0]);
sum_vector = Add(sum_vector, CrossProduct(vi1v0, vi2v0));
}
return Norm(sum_vector)/2.0;
}

//o/p a polygon
void Display(const Polygon& p)
{
cout << "polygon vertices: ";
for (int i=0; i<p.num_vertices; i++)
{
Display(p.verts[i]);
if (i != p.num_vertices-1)
cout << ",";
}
cout << endl;
cout << "polygon edges: ";
for (int i=0; i<p.num_edges; i++)
{
Display(p.edges[i]);
if (i != p.num_edges-1)
cout << ",";
}
}

void main()
{
//define a polygon [triangle in this instance]
Polygon poly;

//set poly's number of vertices and edges
poly.num_vertices = 3;
poly.num_edges = 3;

//vertices
poly.verts[0].x = 1.0 ; poly.verts[0].y = 1.0 ; poly.verts[0].z = 0.0;
poly.verts[1].x = 4.0 ; poly.verts[1].y = 1.0 ; poly.verts[1].z = 0.0;
poly.verts[2].x = 2.0 ; poly.verts[2].y = 3.0 ; poly.verts[2].z = 0.0;

//edges
poly.edges[0].p1 = poly.verts[0] ; poly.edges[0].p2 = poly.verts[1];
poly.edges[1].p1 = poly.verts[1] ; poly.edges[1].p2 = poly.verts[2];
poly.edges[2].p1 = poly.verts[2] ; poly.edges[2].p2 = poly.verts[0];

//properties
double perimeter = Perimeter(poly);
Point centroid = Centroid(poly);
double area = Area(poly);

//o/p
cout << "polygon: " <<endl;
Display(poly) ; cout << endl;
cout << "perimeter of polygon: " << perimeter << endl;
cout << "centroid of polygon: ";
Display(centroid) ; cout << endl;
cout <<"area of polygon: " << area << endl;

cout << "press any key to continue...";
getche();
}

Jul 22 '05 #1
6 2624

"moi" <ra****************@blueyonder.co.uk> wrote in message
news:Zs*****************@news-binary.blueyonder.co.uk...
hi,

im fairly new to c++ and as part of a module at uni im learning it. i know the basics i suppose, but as our final hand-in we have to alter code we
wrote for an earlier assignment to use classes as opposed to structures. the program basically modelled a simple polygon using point, line and polyogon
structures. and had various functions to find the properties of the shape
such as area, perimeter, etc.

the question reads along the lines of:-
rewrite the data structures as classes and assign private access to all data members with appropriate public access member functions. all previous global functions must now be redesigned as member functions of the corresponding
classes, thus the following member functions must be supported:
point: distance(), display()
line: length(), display()
polygon: area(), centroid(), perimeter(), display().

i have copied and pasted my code below. obviously im not looking for someone to do my work for me (man, that would be nice) but i could really use a
nudge in the right direction, and a bit of a foothold to get started from.

thanks,

M.o'N

-----------------------------------
#include<iostream.h> //c++ I/O
should be iostream - note no .h
#include<conio.h> //getche()
non-standard header
#include<math.h> // sqrt(),...
should be cmath

const int MAX_NO_VERTICES = 25; //max no of polygon vertices and edges.
const int MAX_NO_EDGES = 25;
why dont you #define these?

//Data Structures:-

//Cartesian point
struct Point
{
double x, y, z; //triple coordinates of a point.
};


this would be simply
class Point
{
public:
Point();
~Point();

void SetPoint(double xiX, double xiY, double xiZ) // i use xiXXX to
denote a valid input var
{mX = xiX; mY = xiY; mZ = xiZ;} // inline member function

void GetPoint(double *xoX, double *xoY, double *xoZ) // i use xoXXX to
denote an input I must set
{*xoX = mX; *xoY = mY; *xoZ = mZ;}

virtual void Display()
{std::cout << "(" << p.x << ", " << p.y << ", "
<< p.z << ")" << std::endl; } // virtual so can be over-ridden
protected: // same as private but inherited classes may access directly
double mX, mY, mZ; // i iuse mXXX to specify that this is a member
variable
};

This is an example of a very basic class for a point, you can now do some
work to implement the other classes, using this as a guide.
Jul 22 '05 #2

"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message
news:bq**********@news.freedom2surf.net...
[SNIP]
-----------------------------------
#include<iostream.h> //c++ I/O


should be iostream - note no .h
#include<conio.h> //getche()


non-standard header
#include<math.h> // sqrt(),...


should be cmath


If you use the standard headers (which is of course recommended) don't
forget a using statement or add the std:: to the classes from standard
namespace.

const int MAX_NO_VERTICES = 25; //max no of polygon vertices and edges.
const int MAX_NO_EDGES = 25;


why dont you #define these?


Because #defines are a bad thing. If you use #define MAX_NO_VERTICES then
the preprocssor will go ahead and remove MAX_NO_VERTICES from the code
replacing it with the defined value. Therefore any problems that might arise
from using this name will give you a very cryptic error because the compiler
does not see the symbol MAX_NO_VERTICES anymore but a constant number!

//Data Structures:-

//Cartesian point
struct Point
{
double x, y, z; //triple coordinates of a point.
};


this would be simply
class Point
{
public:
Point();


Don't forget to initialize your member variables in the constructor.
~Point();
I'd suggest to make the dtor virtual as this class might be derived from.

void SetPoint(double xiX, double xiY, double xiZ) // i use xiXXX to
denote a valid input var
{mX = xiX; mY = xiY; mZ = xiZ;} // inline member function

void GetPoint(double *xoX, double *xoY, double *xoZ) // i use xoXXX to
denote an input I must set
{*xoX = mX; *xoY = mY; *xoZ = mZ;}
For access functions it's quite a good and common practice to declare the
function as const:

void GetPoint(double& xoX, double& xoY, double& xoZ) const {
xoX = mX; xoY = mY; xoZ = mZ;
};

virtual void Display()
{std::cout << "(" << p.x << ", " << p.y << ", "
<< p.z << ")" << std::endl; } // virtual so can be over-ridden
Where does p.x, p.y and p.z come from? There is neither an object p nor are
there member variables x, y, z in your class. I'd say you should use

std::cout << mX << "," << mY << "," << mZ << std::endl;

In general (but for the OP this might be more than he asks for) I'd suggest
to implement a virtual display function which receives a reference to a
ostream and is called by overloaded version of operator <<.

For example:

virtual ostream& Display( ostream& os ) {
return os << mX << "," << mY << "," << mZ;
}

friend ostream& operator<<( ostream& os, Point& rhs ) {
return rhs.Display( os );
}

protected: // same as private but inherited classes may access directly
double mX, mY, mZ; // i iuse mXXX to specify that this is a member
variable
};

This is an example of a very basic class for a point, you can now do some
work to implement the other classes, using this as a guide.


Regards
Chris
Jul 22 '05 #3

"Chris Theis" <Ch*************@nospam.cern.ch> wrote in message
news:bq**********@sunnews.cern.ch...

"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message
news:bq**********@news.freedom2surf.net...
[SNIP]
-----------------------------------
#include<iostream.h> //c++ I/O


should be iostream - note no .h
#include<conio.h> //getche()


non-standard header
#include<math.h> // sqrt(),...


should be cmath


If you use the standard headers (which is of course recommended) don't
forget a using statement or add the std:: to the classes from standard
namespace.

const int MAX_NO_VERTICES = 25; //max no of polygon vertices and edges. const int MAX_NO_EDGES = 25;


why dont you #define these?


Because #defines are a bad thing. If you use #define MAX_NO_VERTICES then
the preprocssor will go ahead and remove MAX_NO_VERTICES from the code
replacing it with the defined value. Therefore any problems that might

arise from using this name will give you a very cryptic error because the compiler does not see the symbol MAX_NO_VERTICES anymore but a constant number!
ok, this will probably be my C side showing through.

//Data Structures:-

//Cartesian point
struct Point
{
double x, y, z; //triple coordinates of a point.
};

this would be simply
class Point
{
public:
Point();


Don't forget to initialize your member variables in the constructor.
~Point();


I'd suggest to make the dtor virtual as this class might be derived from.


Of course, sorry OP, i forgot (I am lazy and let VS do this bit for me!)

void SetPoint(double xiX, double xiY, double xiZ) // i use xiXXX to
denote a valid input var
{mX = xiX; mY = xiY; mZ = xiZ;} // inline member function

void GetPoint(double *xoX, double *xoY, double *xoZ) // i use xoXXX to denote an input I must set
{*xoX = mX; *xoY = mY; *xoZ = mZ;}
For access functions it's quite a good and common practice to declare the
function as const:

void GetPoint(double& xoX, double& xoY, double& xoZ) const {
xoX = mX; xoY = mY; xoZ = mZ;
};


I suppose, similarly for SetPoint (but not identical obviously)

virtual void Display()
{std::cout << "(" << p.x << ", " << p.y << ", "
<< p.z << ")" << std::endl; } // virtual so can be over-ridden
Where does p.x, p.y and p.z come from? There is neither an object p nor

are there member variables x, y, z in your class. I'd say you should use

std::cout << mX << "," << mY << "," << mZ << std::endl;
I was being lazy (again) and copied the OP display code (and shoved an
std::endl on the end). That will serve me right

In general (but for the OP this might be more than he asks for) I'd suggest to implement a virtual display function which receives a reference to a
ostream and is called by overloaded version of operator <<.

For example:

virtual ostream& Display( ostream& os ) {
return os << mX << "," << mY << "," << mZ;
}

friend ostream& operator<<( ostream& os, Point& rhs ) {
return rhs.Display( os );
}

protected: // same as private but inherited classes may access directly
double mX, mY, mZ; // i iuse mXXX to specify that this is a member variable
};

This is an example of a very basic class for a point, you can now do some work to implement the other classes, using this as a guide.


Regards
Chris

Jul 22 '05 #4

"moi" <ra****************@blueyonder.co.uk> wrote in message
news:Zs*****************@news-binary.blueyonder.co.uk...
struct Point
{
double x, y, z; //triple coordinates of a point.
};
With structs the default access is public. I suggest you make that explicit
in your code. For example,
struct Point
{
public:
double x, y, z;
};

And with classes
class Point
{
private:
double x, y, z;
};
//3D vector
struct Vector3D
{
double a, b, c; //vector components
};

//adds two Vector3D objects
Vector3D Add(const Vector3D& u, const Vector3D& v)
{
Vector3D add;
add.a = u.a + v.a;
add.b = u.b + v.b;
add.c = u.c + v.c;
return add;
}
This function needs to get moved inside the brackets of the Vector3D
struct/class, and it should be in a public section, not private. The trick
then will be to figure out the parameters. You will no longer need 2
parameters, because you will already be INSIDE the object that is one of the
parameters. Alternatively, this could be a static function that still
requires the use of 2 parameters. You will have to ask your instructor this
question.
//Point functions:

//returns the distance between 2 points
double Distance(const Point& p1, const Point& p2)
{
double x = p2.x-p1.x;
double y = p2.y-p1.y;
double z = p2.z-p1.z;
return sqrt(x*x + y*y + z*z);
}


This goes inside the Point class, with the same comments.
Jul 22 '05 #5

"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message
news:bq**********@news.freedom2surf.net...

-----------------------------------
#include<iostream.h> //c++ I/O


should be iostream - note no .h


This depends on his compiler. I'd say this is most likely already working
code for him, since he has to change it for this assignment.
const int MAX_NO_VERTICES = 25; //max no of polygon vertices and edges.
const int MAX_NO_EDGES = 25;


why dont you #define these?


Because const is better.
Jul 22 '05 #6
moi wrote:
the question reads along the lines of:-
rewrite the data structures as classes and assign private access to all
data members with appropriate public access member functions. all previous
global functions must now be redesigned as member functions of the
corresponding classes, thus the following member functions must be
supported: point: distance(), display()
line: length(), display()
polygon: area(), centroid(), perimeter(), display().
You basically do what they ask you to here. Make all your structs to
classes, make all the freestanding functions to public member functions,
make all the variables private.
#include<iostream.h> //c++ I/O
#include <iostream>
#include<math.h> // sqrt(),...
#include <cmath>
void main()


int main()

--
John L. Fjellstad

A: Top posting!
Q: What is the most irritating thing on Usenet?
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Vera | last post by:
Hi, I have a very annoying problem, with which I NEED HELP DESPERATELY!! It smells like a bug to me, but I'm not sure. SITUATION This description is a very much simplified version of the real...
1
by: Robert | last post by:
I have two tables (classes and students). Right now, I'm generating a report in asp that shows a list of classes, the enrollment for each class, and how many seats are available (see query below)....
2
by: cm500 | last post by:
I'm very new to databases so bear with me. What I need is a way to track the training for the employees at my firm. I have 40 classes that I will be teaching on various subjects and various...
2
by: Andrew S. Giles | last post by:
OK, Ive run my head into this wall for too long. I need help. I am developing an applicaiton in C# to present a user with a GUI to specify a configurable list of machines that he wants to listen...
9
by: Jordan Tiona | last post by:
I can't get this code to work right. It seems to be skipping some of the cin functions. Can someone help me with this? ClassTrack.cpp: #include <iostream> #include "ClassTrack.h" using...
4
by: Tarun Mistry | last post by:
Hi all, I have posted this in both the c# and asp.net groups as it applies to both (apologies if it breaks some group rules). I am making a web app in asp.net using c#. This is the first fully OO...
5
by: QbProg | last post by:
Hello, I did some experiments with VC++ 2005, and some ILDasm. Please tell me if I have understood the concepts of C++ programming under .NET, since I'm a bit confused! -- There are 4 types of...
9
by: Chrissy | last post by:
I took a C# class as an elective and received an incomplete in it and am desparate for help. I have two assignments left (arrays and inheritance) and would gladly pay anyone that can assist me with...
6
by: Minor | last post by:
I am a beginning student in java and really need some help. I am totally lost and don't know where to begin. This is my last assignment and I just need to submit something. I have decided computer...
2
by: hcaptech | last post by:
This is my Test.can you help me ? 1.Which of the following statement about C# varialble is incorrect ? A.A variable is a computer memory location identified by a unique name B.A variable's name...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.