Leave these fields empty (spam trap):
Name
You can leave this blank to post anonymously, or you can create a Tripcode by using the format Name#Password
Comment
[i]Italic Text[/i]
[b]Bold Text[/b]
[spoiler]Spoiler Text[/spoiler]
>Highlight/Quote Text
[pre]Preformatted & Monospace Text[/pre]
[super]Superset Text[/super]
[sub]Subset Text[/sub]
1. Numbered lists become ordered lists
* Bulleted lists become unordered lists
File

Sandwich


c++

Reply
- Thu, 22 Dec 2016 14:24:32 EST Jf4pElwU No.36392
File: 1482434672770.jpg -(6913B / 6.75KB, 182x251) Thumbnail displayed, click image for full size. c++
Anyone that can explain how to return the value after the change?

#include<iostream>
#include<conio.h>
#include<algorithm>
using namespace std;
class Pyre
{
public:
float width;
float depth;
float height;

float change(float, float);
};

float Pyre::change(float side1, float side2)
{
cout<<side1<<side2<<endl<<endl;
std::swap(side1,side2);
cout<<side1<<side2<<endl<<endl;

}

int main()
{
Pyre pyr;
pyr.width = 50;
pyr.depth = 50;
pyr.height = 100;
cout<<"before :"<<pyr.width<<pyr.depth<<pyr.height<<endl<<endl;
pyr.change((pyr.width, pyr.depth), pyr.height);
cout<<"after :"<<pyr.width<<pyr.depth<<pyr.height<<endl;
cin.get();
}
>>
Thomas Murdfield - Thu, 22 Dec 2016 15:44:55 EST 9QSfnS0r No.36393 Reply
return this;

to make matters worse you probably don't want to to that and you might want to return &this: or return *this;
Assuming by value you mean the instance of Pyre.
Also the call to pyr.change doesn't make sense.

And for gods sake use a proper ide with code inspection so that these kind of errors are visible to you without other people's help.
>>
Thomas Murdfield - Thu, 22 Dec 2016 15:50:48 EST 9QSfnS0r No.36394 Reply
you also would have to change the return type in the definition of change to an instance of your class and create another instance. But then the whole thing still wouldn't make sense. since change doesn't actually use m your instance variables.
>>
Thomas Murdfield - Thu, 22 Dec 2016 16:16:44 EST 9QSfnS0r No.36395 Reply
#include<iostream>
#include<conio.h>
#include<algorithm>

using namespace std;

class Pyre {
public:
float width;
float depth;
float height;

Pyre change(float &, float &);
};

Pyre Pyre::change(float &side1, float &side2) {
cout << side1 << side2 << endl << endl;
std::swap(side1, side2);
cout << side1 << side2 << endl << endl;
return *this;
}

int main() {
Pyre pyr;
pyr.width = 50;
pyr.depth = 50;
pyr.height = 100;
cout << "before :" << pyr.width << pyr.depth << pyr.height << endl << endl;
pyr.change(pyr.depth, pyr.height);
cout << "after :" << pyr.width << pyr.depth << pyr.height << endl;
cin.get();
}

This works for instance.
>>
Dont hack meh - Thu, 22 Dec 2016 17:07:46 EST fjMi/DVw No.36396 Reply
>>36395
>>36394
>>36393

thx alot!

Any way to get the third attribute to change at the same time? It's a pyramid and i want to swap the height value and the base (width & depth) with the change method.

"use a proper ide with code inspection"
suggestions? new to coding.
>>
Thomas Murdfield - Thu, 22 Dec 2016 17:43:46 EST 9QSfnS0r No.36397 Reply
>>36396
I use Clion, there is a 30 day trail for it.
>Any way to get the third attribute to change at the same time? It's a pyramid and i want to swap the height value and the base (width & depth) with the change method.
This is not what the code I posted does.
The argument to change are passed by reference so anything you give it it will call swap on the memory locations. The return is sort of nonsense on it's own but it's useful for method chaining.
in a sense you can do:

pyr.change(pyr.depth, pyr.height).change(pyr.height, pyr.width);

The problem with it is you will still operate on the same memory location and if you do something like
Pyre changed = pyr;

you won't actually have two objects like you would have in other languages, just two names for the same object.

To get to this behavior you need to write a copy constructor and override the assignment operator, which however is something I'm personally not comfortable with. I'll look it up myself though since it's kind of embarrassing I never use it.
I am not a C++ programmer though, I just use it for Arduino stuff and to write small libraries if my python code is too slow.

you could also just write three methods like swapHeightWidth() , swapHeightDepth() and swapWidthDepth() with hard-coded swap in them.
>>
Thomas Murdfield - Thu, 22 Dec 2016 18:10:02 EST 9QSfnS0r No.36398 Reply
Actually discard the copy constructor thing it works as you would expect, it's just that since you often store pointers inside your objects you have to explicitly write a copy constructor and assignment operator to account for that.
With just raw values in your instance variables you're fine.

However if you are using std::swap it doesn't make sense to include it in your class if it's the only thing you're doing in your method.

#include<iostream>
#include<conio.h>
#include<algorithm>

using namespace std;

class Pyre {
public:
float width;
float depth;
float height;
Pyre change(float &, float &);
};

Pyre Pyre::change(float &side1, float &side2) {
cout << side1 << side2 << endl << endl;
std::swap(side1, side2);
cout << side1 << side2 << endl << endl;
return *this;
}


int main() {
Pyre pyr;

pyr.width = 50;
pyr.depth = 50;
pyr.height = 100;
Pyre changeMe = pyr;
cout << "before :" << pyr.width << pyr.depth << pyr.height << endl << endl;
changeMe.change(changeMe.depth, changeMe.height);
cout << "changed :" << changeMe.width << changeMe.depth << changeMe.height << endl;
swap(changeMe.depth, changeMe.height);
cout << "changed :" << changeMe.width << changeMe.depth << changeMe.height << endl;
cin.get();
}
>>
Thomas Murdfield - Thu, 22 Dec 2016 18:30:12 EST 9QSfnS0r No.36399 Reply
// Here is how I would do it.
#include<iostream>


using namespace std;

class Pyre {
public:
float width;
float depth;
float height;
Pyre& swapWidthHeight(); // Method chaining requires method to return reference

Pyre& swapWidthDepth();

Pyre& swapHeightDepth();
};

Pyre& Pyre::swapWidthHeight() {
swap(width, height);
return *this;
}

Pyre& Pyre::swapWidthDepth() {
swap(width, depth);
return *this;
}

Pyre& Pyre::swapHeightDepth() {
swap(height, depth);
return *this;
}


int main() {
Pyre pyr;

pyr.width = 20;
pyr.depth = 50;
pyr.height = 100;
Pyre changeMe = pyr;
cout << "before :" << pyr.width << pyr.depth << pyr.height << endl << endl;
changeMe.swapWidthHeight().swapWidthHeight(); // chaining it two times so it stays the same!
cout << "not really changed :" << changeMe.width << changeMe.depth << changeMe.height << endl;
changeMe.swapWidthDepth();
cout << "changed :" << changeMe.width << changeMe.depth << changeMe.height << endl;
changeMe.swapHeightDepth();
cout << "changed :" << changeMe.width << changeMe.depth << changeMe.height << endl;
cin.get();
}
>>
Trustred - Fri, 23 Dec 2016 04:01:29 EST Jf4pElwU No.36400 Reply
>>36399

what can i use instead of swap then if i need to change the base (50,50) with the height (100) with just calling one method. the base still needs to be a square after the switch and only one called method is allowed.
>>
Trustred - Fri, 23 Dec 2016 04:57:58 EST Jf4pElwU No.36401 Reply
>>36399

thx alot!

i ended up doing this

Pyramid Pyramid::change(float &side1, float &side2)
{
side1 = side2;
return *this;
}

and called three times

pyr.change(pyr.depth, pyr.height);
pyr.change(pyr.height, pyr.width);
pyr.change(pyr.width, pyr.depth);
>>
Hannah Soblingwater - Fri, 23 Dec 2016 10:47:44 EST 9QSfnS0r No.36402 Reply
>>36401
If you just use assignment instead of std::swap you'll override the left hand side but leave the right hand side as it is.
Be aware that using Pyramid Pyramid::change(float &side1, float &side2)
will just use the memory location of whatever you pass into that function, even it doesn't belong to the current instance of Pyramid.
This defeats the purpose of making it a method.

you could as well use
pyr.depth = pyr.height;
pyr.height = pyr.width;
pyr.width = pyr.depth;

for the same result.


>>36400
>what can i use instead of swap then if i need to change the base (50,50) with the height (100) with just calling one method. the base still needs to be a square after the switch and only one called method is allowed.

That's even easier, but what should happen if the base isn't already a square?

You could do this, in case of the area staying constant

float Pyramid::constantAreaSide() {
return (float)pow(2,0.5)*Pyramid().baseArea();
}
void Pyramid::change() {
float oldHeight = height;
height = constantAreaSide();
width = oldHeight;
depth = oldHeight;
}

float Pyramid::baseArea() {
return width*depth;
}
>>
Hannah Soblingwater - Fri, 23 Dec 2016 10:54:35 EST 9QSfnS0r No.36403 Reply
it should be

float Pyramid::constantAreaSide() {
return (float)pow(Pyramid().baseArea(), 0.5);
}

Report Post
Reason
Note
Please be descriptive with report notes,
this helps staff resolve issues quicker.