This question already has answers here:
C++: Argument Passing "passed by reference"
(4 answers)
Why is the copy constructor called when we pass an object as an argument by value to a method?
(3 answers)
Closed 6 years ago.
The program should resolve grade 1 equations in this specific manner. I had to use output messages for the constructors and destructors for better understanding of the code.
#include "stdafx.h"
#include <iostream>
using namespace std;
class Ec {
public: float a, b; //equation's parameters
public:
Ec() {float x, y; cout <<"a= "; cin >> x; cout << "b= ";
cin >> y; a = x; b = y; cout << "void constr\n";};
Ec(float x, float y) { a = x; b = y; cout << "param constr\n"; }
~Ec() { cout << "destr\n"; }
Ec(Ec &z) { a = z.a; b = z.b; cout << "cpy constr\n"; }
friend float half1(Ec); //function to return a/2
friend float half2(Ec); //function to return b/2
friend float sol1(Ec); //function to return the solution for the standard eq
friend float sol2(Ec); //function to return the sol for the /2 param eq
};
float half1(Ec ec1) { return (ec1.a / 2);}
float half2(Ec ec1) { return (ec1.b / 2); }
float sol1(Ec ec1) { float x; return x = -ec1.b / ec1.a; }
float sol2(Ec ec1) { float x2; return x2 = -half2(ec1) / half1(ec1); }
int main()
{
int x, y;
cout << "a= ";
cin >> x;
cout << "b= ";
cin >> y;
Ec ec1;
Ec ec2(x, y);
Ec ec3 = ec1;
//the couts display for ex:" ec 2x+1=0 has sol -0.5"
cout << "ec " << ec1.a << "x+ " << ec1.b << "=0 has sol " << sol1(ec1) << endl;
cout << "ec " << ec2.a << "x+ " << ec2.b << "=0 has sol " << sol1(ec2) << endl;
cout << "ec " << ec3.a << "x+ " << ec3.b << "=0 has sol " << sol1(ec3) << endl;
cout << "ec halved " << half1(ec1) << "x+ " << half2(ec1) << "=0 has sol " << sol2(ec1) << endl;
cout << "ec halved " << half1(ec2) << "x+ " << half2(ec2) << "=0 has sol " << sol2(ec2) << endl;
cout << "ec halved " << half1(ec3) << "x+ " << half2(ec3) << "=0 has sol " << sol2(ec3) << endl;
}
return 0;
}
Now, I have troubles understanding why after the first cpy constructor(for ec3) another cpy constructor is called then a destructor. What is it doing?
Your functions take Ec objects by value
float half1(Ec ec1) { return (ec1.a / 2);}
^
These will make function local copies that are then destroyed at the end of each function.
If you want to avoid making these copies, pass the arguments by const reference
float half1(Ec const& ec1) { return (ec1.a / 2);}
^
Related
#include<iostream>
using namespace std;
class Point {
private:
int x;
int y;
static int numCreatedObjects;
public:
Point() :x(0), y(0) {
numCreatedObjects++;
}
Point(int _x, int _y) {
x = _x;
y = _y;
numCreatedObjects++;
}
~Point() {
cout << "Destructed..." << endl;
}
void setXY(int _x, int _y) {
this->x= _x;
this->y = _y;
}
int getX() const { return x; }
int getY() const { return y; }
Point operator+(Point& pt2) {
Point result(this->x + pt2.x, this->y + pt2.y);
return result;
}
Point& operator = (Point& pt) {
this->x = pt.x;
this->y = pt.y;
}
static int getNumCreatedObject() { return numCreatedObjects; }
friend void print(const Point& pt);
friend ostream& operator<<(ostream& cout, Point& pt);
friend class SpyPoint;
};
int Point::numCreatedObjects = 0;
void print(const Point& pt) {
cout << pt.x << "," << pt.y << endl;
}
ostream& operator<<(ostream& cout, Point& pt) {
cout << pt.x << "," << pt.y;
return cout;
}
class SpyPoint{
public:
void hack_point_info(const Point& pt) {
cout << "Hacked by SpyPoint" << endl;
cout <<"x: "<<pt.x <<endl<<"y: "<<pt.y << endl;
cout << "numCreatedObj:" << pt.getNumCreatedObject() << endl;
}
};
int main() {
Point pt1(1, 2);
cout << "pt1 : ";
print(pt1);
cout << endl;
Point* pPt1 = &pt1;
cout << "pt1 : ";
cout << pPt1->getX() << ", " << pPt1->getY() << endl;
cout << "pt1 : ";
cout << pPt1->getX() << ", " << pPt1->getY() << endl;
cout << endl;
Point* pPt2 = new Point(10, 20);
cout << "pt2 : ";
cout << pPt2->getX() << ", " << pPt2->getY() << endl;
cout << endl;
delete pPt2;
cout << "pt1 NumCreatedObject : ";
cout << pt1.getNumCreatedObject() << endl;
Point pt2(10, 20);
Point pt3(30, 40);
Point pt4 = pt2 + pt3;
cout << "pt2 : ";
cout << pt2 << endl;
cout << "pt3 : ";
cout << pt3 << endl;
cout << "pt4 : ";
cout << pt4 << endl;
cout << "pt1 NumCreatedObject : ";
cout << pt1.getNumCreatedObject() << endl << endl;
Point* ptAry = new Point[5];
cout << "pt2 NumCreatedObject : ";
cout << pt2.getNumCreatedObject() << endl;
cout << endl;
delete[] ptAry;
cout << endl;
SpyPoint spy;
cout << "pt1 info" << endl;
spy.hack_point_info(pt1);
cout << endl;
cout << "pt4 info" << endl;
spy.hack_point_info(pt4);
cout << endl;
return 0;
}
I don't know why the yellow underlined part prints out. I understand that the destructor is called when returning, unless it is dynamically allocated.
I guess pt1 is going to disappear, but I don't know why.
Also, I would like to know if there is a method that can be forcibly destroyed even if it is not dynamically allocated.
I am going through the book "Beginning C++ Through Game Programming". I have typed this supplied script perfectly (I even pasted over it with the script supplied from online download- and used undo/redo to compare every character- to find no difference- except line endings and actually capitalizing the first character of function names). Despite no change, the supplied script compiles perfectly fine, but mine does not (unless I copy/paste supplied script). I am given the error in the second function: GoodSwap()
#include<iostream>
using namespace std;
void BadSwap(int x, int y);
void GoodSwap(int* const pX, int* const pY);
int main(){
int myScore = 150;
int yourScore = 1000;
cout << "Original Values" << endl;
cout << "myScore : " << myScore << endl;
cout << "yourScore: " << yourScore << endl;
cout << "Calling BadSwap()" << endl;
BadSwap(myScore, yourScore);
cout << "myScore : " << myScore << endl;
cout << "yourScore: " << yourScore << endl;
cout << "Calling GoodSwap()" << endl;
GoodSwap(&myScore, &yourScore);
cout << "myScore : " << myScore << endl;
cout << "yourScore: " << yourScore << endl;
return 0;
}
void BadSwap(int x, int y){
int temp = x;
x = y;
y = temp;
}
void GoodSwap(int* const pX, int* const pY){
int temp = *pX
*pX = *pY;
*pY = temp;
}
You missed a ; at first line of GoodSwap().
int temp = *pX // There should be a `;`
After this change, your program can compile in VS2015.
I'm trying to execute the following code:
#include <iostream>
using namespace std;
class ABC {
private:
int x, y;
public:
ABC(){
cout << "Default constructor called!" << endl;
ABC(2, 3);
cout << x << " " << y << endl;
}
ABC(int i, int j){
cout << "Parameterized constructor called with parameters "<< i << " " << j << "!" << endl;
x = i;
y = j;
cout << x << " " << y << endl;
}
};
int main(){
ABC a;
return 0;
}
I am getting the following output:
Default constructor called!
Parameterized constructor called with parameters 2 3!
2 3
-858993460 -858993460
Shouldn't the member variables be initialized with values 2 and 3?
ABC(2, 3); doesn't call the constructor to initialize the members, it just create a temporary variable which will be destroyed immediately.
If you meant delegating constructor you should:
ABC() : ABC(2, 3) {
cout << "Default constructor called!" << endl;
cout << x << " " << y << endl;
}
Note this is a C++11 feature. You can add a member function if you can't use C++11.
class ABC {
private:
int x, y;
init(int i, int j) {
x = i;
y = j;
}
public:
ABC(){
cout << "Default constructor called!" << endl;
init(2, 3);
cout << x << " " << y << endl;
}
ABC(int i, int j){
cout << "Parameterized constructor called with parameters "<< i << " " << j << "!" << endl;
init(i, j);
cout << x << " " << y << endl;
}
};
You create a temporary variable in ABC() body. You can use this syntax to overcome this:
class ABC
{
private:
int x, y;
public:
ABC() : ABC(2,3)
{
std::cout << "Default constructor called!" << std::endl;
}
ABC(int i, int j)
{
std::cout << "Parameterized constructor called with parameters "<< i << " " << j << "!" << std::endl;
x = i;
y = j;
std::cout << x << " " << y << std::endl;
}
};
This question already has answers here:
Why does flowing off the end of a non-void function without returning a value not produce a compiler error?
(11 answers)
Closed 6 months ago.
This is my assignment : " Write a function that finds the larger of two integers input (in the main program) but allows you to change the value of the integers in the function using pass by reference." I been trying to find out whats wrong with my code but I cant find out what it is. Someone please help me !! this assignment is due tonight!!
Here is my code as of right now :
#include <iostream>
using namespace std;
int change(int& x, int& y)
{
int temp=0;
cout << "Function(before change): " << x << " " << y << endl;
temp = x;
x = y;
y = temp;
cout << "Function(after change): " << x << " " << y << endl;
}
int main()
{
int x,y;
cout << " Please Enter your first number: ";
cin >> x;
cout << " Please Enter your second number: ";
cin >> y;
if (x > y)
cout << x << " is greater than " << y << endl;
if (x < y)
cout << y << " is greater than " << x << endl;
if (x == y)
cout << x << " is equal to " <<y << endl;
cout << "Main (before change): " << x << " " << y << endl;
change(x, y);
cout << "Main (after change): " << x << " " << y << endl;
system("Pause");
return 0;
}
change is declared as returning an int, but it never returns anything. It doesn't look like your function should return anything, so just declare it as void:
void change(int& x, int& y)
#include<iostream>
using namespace std;
class a
{
private:
int x;
int y;
public:
int getx()
{
return x;
}
int gety()
{
return y;
}
a()
{
x = 100;
y = 100;
}
void xmin()
{
x--;
}
void ab(a x)
{
x.xmin(); x.xmin(); x.xmin(); x.xmin();
}
};
void main()
{
a xx;
a yy;
cout << "xx" << endl;
cout << "x : " << xx.getx() << "y : " << xx.gety()<<endl;
cout << "yy" << endl;
cout << "x : " << yy.getx() << "y : " << yy.gety()<<endl;
xx.ab(yy);
cout << "xx" << endl;
cout << "x : " << xx.getx() << "y : " << xx.gety() << endl;
cout << "yy" << endl;
cout << "x : " << yy.getx() << "y : " << yy.gety() << endl;
}
Why the function x.xmin() in void ab(a x) cannot be executed properly? (The value of x didn't change as the function of xmin() decrease the value of x by 1.
This is the simple version of my code so that will be easier to understand :)
void ab(a x)
That takes its argument by value. The function modifies a local copy of the argument, so the caller won't see any changes. If you want the function to modify the caller's object, then pass by reference:
void ab(a & x)
^