GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-Oct-2004, 22:11
crystalattice's Avatar
crystalattice crystalattice is offline
Flame War Instigator
 
Join Date: Apr 2004
Location: San Diego
Posts: 1,543
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice
Unhappy

Passing multiple objects in functions


I'm confused w/ OOP. In my code below (which isn't complete yet, I know), what I want to do is create an object (e.g. line1) and pass it to the function that lets the user enter the slope and a point on the line, thereby creating a new line, if that makes sense.

What I'm confused about is how to pass the slope and point back to the main function. That is, having passed line1 to the function, how do I get line.slope and line.point back? I thought about using call-by-reference paramenters (create line.slope and line.point and pass them to the function with the '&' ) but I'm not sure I can legally do that.

I'm also attaching my class headers and implementation files.

CPP / C++ / C Code:
/******************************************************
*Purpose:  Read the point and slope information for 2 lines and determine if
*	they intersect or are parallel.  If they intersect, find the point of
*	intersection and determine if they are perpendicular.
**********************************************************/
#include <iostream>
#include "Line.h"
#include "LineSegment.h"
#include "CartesianPoint.h"
using namespace std;

int initLine (Line &line);
int initLineSeg (LineSegment &segment);


int main ()
{
	cout << "Enter two lines or line segments.  Use a point (x,y) and slope (m)\n"
		<< "for a line.  Use two points (x1,y1)(x2,y2) for a line segment.\n"
		<< "Enter 1 to select a line, 2 to select a line segment.\n\n"
		<< "Is the first entry a line or line segment? (Enter 1 or 2)"
		<< endl;
	int entry_num;
	cin >> entry_num;
	int success;

	do
	{
		if (entry_num == 1)			//line
		{
			Line line1;
			int success = initLine(line1);
		}
		else if (entry_num == 2)	//line segment
		{
			LineSegment segment1;
			int success = initLineSeg(segment1);
		}
		else 
		{
			cerr << "***Invalid number entered***" << endl;
			success = 1;
		}
	}while (success == 1);
	
}

/*initLine() gets the slope and point of a Line
*Receives: line, a Line object
*Input: user enters the slope of the line and a point lying on the line
*Send back: line, with the slope and point objects created
*************************************************************/
int initLine (Line &line)
{
	cout << "\nWhat is the slope of the line?" << endl;
	double slope_line;
	cin >> slope_line;
	line.slope = slope_line;
	
	cout << "What is a point on the line?" << endl;
	CartesianPoint pt_line;
	cin >> pt_line;
	line.point = pt_line;

	success = 0;
	return success;
}

int initLineSeg (LineSegment &segment)
{
	cout << "\nWhat is the first point of the segment?" << endl;
	CartesianPoint seg_pt1;
	cin >> seg_pt1;
	segment.pt1 = seg_pt1;

	cout << "\What is the second point of the segment?" << endl;
	CartesianPoint seg_pt2;
	cin >> seg_pt2;
	segment.pt2 = seg_pt2;

	success = 0;
	return success;
}
Attached Files
File Type: zip Lines.zip (3.6 KB, 22 views)
__________________
Common Sense v2.0-Striving to make the world a little bit smarter.
  #2  
Old 07-Oct-2004, 23:31
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
How about using global variables.? They will be available globally throughout your program.

You can extern them to other files if you need to modify their value in multiple source files.

It may not be efficient but not a bad choice for small programs.

Thanks,
  #3  
Old 08-Oct-2004, 01:54
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by crystalattice
I'm confused w/ OOP. In my code below (which isn't complete yet, I know), what I want to do is create an object (e.g. line1) and pass it to the function that lets the user enter the slope and a point on the line, thereby creating a new line, if that makes sense.

What I'm confused about is how to pass the slope and point back to the main function. That is, having passed line1 to the function, how do I get line.slope and line.point back? I thought about using call-by-reference paramenters (create line.slope and line.point and pass them to the function with the '&' ) but I'm not sure I can legally do that.
Sure you can. Just try it and when you get back to main print the values to be sure.
__________________

Age is unimportant -- except in cheese
  #4  
Old 08-Oct-2004, 08:18
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by nkhambal
How about using global variables.? They will be available globally throughout your program.

You can extern them to other files if you need to modify their value in multiple source files.

It may not be efficient but not a bad choice for small programs.

Thanks,

I think that this is really bad advice whatever the size of the program. It is almost never necessary to use global variables. Pass pointers to the variables or structs, or whatever, to any functions where they need to modify the thing.

Some day you may be given a program to debug. If the program has lots (or even a few) global variables, you will surely think of lots of really bad words to say to the person who wrote that program.

Regards,

Dave
 
 

Recent GIDBlogMeeting the populace by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
controling dialog objects & multiple source files omills MS Visual C++ / MFC Forum 0 14-Jul-2004 23:30
Passing strings in functions..HELP please... mgdpetter C Programming Language 2 24-May-2004 22:02
Help on passing in arrays in functions? nusstu C Programming Language 10 02-Apr-2004 10:42
Passing Pointers To Pointers in Functions elumira C Programming Language 8 05-Mar-2004 21:23

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:22.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.