Click here to Skip to main content
15,887,291 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Problem with return value of private variable within class Pin
Md. Ali Naser Khan25-Mar-09 19:01
Md. Ali Naser Khan25-Mar-09 19:01 
GeneralRe: Problem with return value of private variable within class Pin
Michael Randolph25-Mar-09 20:40
Michael Randolph25-Mar-09 20:40 
GeneralRe: Problem with return value of private variable within class Pin
Cedric Moonen25-Mar-09 21:14
Cedric Moonen25-Mar-09 21:14 
GeneralRe: Problem with return value of private variable within class Pin
Niklas L26-Mar-09 1:25
Niklas L26-Mar-09 1:25 
AnswerRe: Problem with return value of private variable within class Pin
Stuart Dootson25-Mar-09 23:41
professionalStuart Dootson25-Mar-09 23:41 
GeneralRe: Problem with return value of private variable within class Pin
Michael Randolph26-Mar-09 19:14
Michael Randolph26-Mar-09 19:14 
GeneralRe: Problem with return value of private variable within class Pin
Stuart Dootson26-Mar-09 21:32
professionalStuart Dootson26-Mar-09 21:32 
GeneralRe: Problem with return value of private variable within class Pin
Michael Randolph27-Mar-09 2:40
Michael Randolph27-Mar-09 2:40 
Thank you so much for your help. Looks like I have a bit of studying to do. I am not totally sure how this works but it does. Here is the fixed source in case someone needs to check it out.
#include <iostream>

using namespace std;

class Point
{
public:
	Point(float f_x = 1.0, float f_y = 1.0, float f_z = 1.0);
	~Point();
	
	void setXYZ(float X, float Y, float Z);
	void setX(float X);
	void setY(float Y);
	void setZ(float Z);

	// return by reference
	void getXYZ(float &X, float &Y, float &Z);
	float getX() const;
	float getY() const;
	float getZ() const;
	
	// overloading = operator to deal with pointers
	Point& operator =(Point const &p);

private: 
	float x, y, z;

protected:

};

// define constructor with args
Point::Point(float f_x, float f_y, float f_z)
{
	cout << "We're in the constructor with arguments " << (int)this /*unique identifier*/ << endl;
	x = f_x;
	y = f_y;
	z = f_z;
}

// define destructor
Point::~Point()
{
	cout << "Destructing now!!! " << (int)this /*unique identifier*/ << endl;
}

// public function to return private value of variable x
float Point::getX() const
{
	return x;
}

// public function to return private value of variable y
float Point::getY() const
{
	return y;
}

// public function to return private value of variable z
float Point::getZ() const
{
	return z;
}

// public function to change value of private variable x
void Point::setX(float X)
{
	x = X;
}

// public function to change value of private variable y
void Point::setY(float Y)
{
	y = Y;
}

// public function to change value of private variable z
void Point::setZ(float Z)
{
	z = Z;
}

// public function to change value of all three private variables
void Point::setXYZ(float X, float Y, float Z)
{
	/* slower
	setX(X);
	setY(Y);
	setZ(Z);*/
	// faster
	x = X;
	y = Y;
	z = Z;
	
}

// public function to return values of x,y,z by reference
void Point::getXYZ(float &X, float &Y, float &Z)
{
	X = getX();
	Y = getY();
	Z = getZ();
}

// overloading = operator
Point& Point::operator =(Point const &p)
{
	setX(p.getX());
	setY(p.getY());
	setZ(p.getZ());
	// passing back value rather than address
	return *this;
}

// overloading operator << (output stream)
ostream &operator <<(ostream &stream, Point &p)
{
	stream << p.getX() << " " << p.getY() << " " << p.getZ();
	return stream;
}

// overloading operator >> (input stream)
istream &operator >>(istream &stream, Point &p)
{
	float x, y, z;
	stream >> x >> y >> z;
	p.setXYZ(x, y, z);
	return stream;
}

//------------------------------------------
// Point Class End
//------------------------------------------

class Vector : public Point
{
public:
	Vector(float X=0, float Y=0, float Z=0);
	Vector operator +(Vector &p);
};

// initiate X, Y, Z from Point Class with : operator
Vector::Vector(float X, float Y, float Z) : Point(X, Y, Z)
{
//anything extra can go here
}

Vector Vector::operator +(Vector &p)
{
	Vector outV;
	outV.setX(getX() + p.getX());
	outV.setY(getY() + p.getY());
	outV.setZ(getZ() + p.getZ());
	return outV;
}

int main()
{
	// instance of the Vector Class
	Vector vect1(1, 2, 3);

	Vector vect2(5, 4, 2);

	Vector vect3;

	vect3 = vect1 + vect2;

	cout << vect3 << endl;
	
}

QuestionHow can I remove vertical scrollbar from Microsoft web browser? Pin
Md. Ali Naser Khan25-Mar-09 17:54
Md. Ali Naser Khan25-Mar-09 17:54 
AnswerRe: How can I remove vertical scrollbar from Microsoft web browser? [modified] Pin
Yajnesh Narayan Behera26-Mar-09 3:35
Yajnesh Narayan Behera26-Mar-09 3:35 
Question"game" fast fingers Pin
Aljaz11125-Mar-09 8:18
Aljaz11125-Mar-09 8:18 
AnswerRe: "game" fast fingers Pin
Cedric Moonen25-Mar-09 8:21
Cedric Moonen25-Mar-09 8:21 
GeneralRe: "game" fast fingers Pin
Aljaz11125-Mar-09 8:26
Aljaz11125-Mar-09 8:26 
JokeRe: "game" fast fingers Pin
CPallini25-Mar-09 8:30
mveCPallini25-Mar-09 8:30 
GeneralRe: "game" fast fingers Pin
Aljaz11125-Mar-09 8:40
Aljaz11125-Mar-09 8:40 
GeneralRe: "game" fast fingers Pin
CPallini25-Mar-09 8:46
mveCPallini25-Mar-09 8:46 
GeneralRe: "game" fast fingers Pin
led mike25-Mar-09 9:35
led mike25-Mar-09 9:35 
GeneralRe: "game" fast fingers Pin
CPallini25-Mar-09 9:50
mveCPallini25-Mar-09 9:50 
GeneralRe: "game" fast fingers Pin
ThatsAlok25-Mar-09 17:44
ThatsAlok25-Mar-09 17:44 
GeneralRe: "game" fast fingers Pin
Aljaz11125-Mar-09 9:45
Aljaz11125-Mar-09 9:45 
GeneralRe: "game" fast fingers Pin
led mike25-Mar-09 10:02
led mike25-Mar-09 10:02 
GeneralRe: "game" fast fingers Pin
Cedric Moonen25-Mar-09 21:07
Cedric Moonen25-Mar-09 21:07 
GeneralRe: "game" fast fingers Pin
led mike26-Mar-09 5:35
led mike26-Mar-09 5:35 
GeneralRe: "game" fast fingers Pin
CPallini25-Mar-09 10:09
mveCPallini25-Mar-09 10:09 
GeneralRe: "game" fast fingers Pin
Aljaz11125-Mar-09 10:25
Aljaz11125-Mar-09 10:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.