#include < iostream >
using namespace std;
class A
{
public:
int x;
A(int i)
{
x= i;
cout<<"Constructor is Called "<<x<<endl;
}
~A()
{
cout<<"destructor is Called "<<x<<endl;
}
A(const A &a)
{
cout<<"in copy constructor a.x = "<<a.x<<endl;
cout<<" x = "<<x<<endl;
}
};
const A &fun(int i)
{
cout<<"in Fun"<<endl;
A t(i+1);
return(t);
}
main()
{
A *gg = new A(5);
A t = fun(2);
}
output of this is :
Constructor is Called 5
in Fun
Constructor is Called 3
destructor is Called 3
in copy constructor a.x = 0
x = 4067272
Why it is a.x=0 and x= 4067272?
Add code to initialize x in the copy constructor.
A(const A &a) : x(a.x)
//^^^^^^^^^^^^^^^ Missing code
{
cout<<"in copy constructor a.x = "<<a.x<<endl;
cout<<" x = "<<x<<endl;
}
Also,
fun returns a reference to a local variable. The reference is invalid after you return from fun. When you use A t = fun(2);, you are entering UB territory. You can't assume anything reasonable from it.
You can fix this by returning an object from fun instead of a const&.
A fun(int i)
{
cout<<"in Fun"<<endl;
A t(i+1);
return(t);
}
Related
Why the following code produces garbage value instead of product of two numbers? While debugging many times destructor is being called. Somebody please explain me what is happening behind the scene. There are two classes A and B. A is inherited by the base class B and the operator "%" is overloaded to produce the product of two numbers of two different classes but the desired output is not obtained when called from the main().
#include <iostream>
using namespace std;
class B;
class A
{
public:
int x;
A()
{
}
~A()
{
}
friend int operator/(A, B);
void acceptValue();
private:
};
class B :public A
{
public:
int y;
B()
{
}
void acceptValueY()
{
cout << "Enter the value of y: ";
cin >> y;
return;
}
~B()
{
}
friend int operator/(A, B);
private:
};
int operator/(A obj1, B obj2)
{
return obj1.x * obj2.y;
}
void A::acceptValue()
{
cout << "Enter the value of x: ";
cin >> x;
return;
}
int main()
{
A ob;
B baseObj;
ob.acceptValue();
baseObj.acceptValueY();
cout<<baseObj / (ob, baseObj);
}
Check inline comments for answer
#include <iostream>
using namespace std;
class B;
class A
{
public:
int x;
A()
{
}
/*A(const A& a)
{
}*/
~A()
{
}
friend int operator/(A, B);
void acceptValue();
private:
};
class B :public A
{
public:
int y;
B() // Will invoke A constructor. But value of member x in A object will be garbage as it is uninitialized.
{
}
void acceptValueY()
{
cout << "Enter the value of y: ";
cin >> y;
return;
}
~B() // Will invoke A destructor
{
}
friend int operator/(A, B);
private:
};
int operator/(A obj1, B obj2)
{
return obj1.x * obj2.y;
} // Destructor of A and B (and hence A again) will get called.
int test(A obj1, B obj2)
{
return obj1.x * obj2.y;
}
void A::acceptValue()
{
cout << "Enter the value of x: ";
cin >> x;
return;
}
int main()
{
A ob;
B baseObj;
ob.acceptValue();
baseObj.acceptValueY();
// This way of invocation is causing garbage value. Firts arument is of type A.
// So copy construction of A type object will happen from baseObj.
// But as B constructor causes default constrcution of A, value of member x is garbage.
//int val = baseObj / (ob, baseObj);
// Correct way of invocation
int val = ob / baseObj; // It will show compute right val value. Will invoke copy constructor of A and B as they are being passed by value.
cout << val;
} // Destructor of A and B (hence again A) will get called.
First, the comma-operator will evaluate the expression (ob, baseObj) and discard all but the last statement, so it evaluates to baseObj.
You end up with cout << baseObj / baseObj and the operator int operator/(A obj1, B obj2) effectively calculates baseObj.x * baseObj.y.
The randome values appear, because baseObj.x was never initialized and thus contains garbage.
I've seen the following code snippet: test() function output is 6
#include <iostream>
using namespace std;
int z = 0;
class A{
public:
int a = 2;
A(){
a = 1;
z++;
}
A(const A& aa){
a = 3;
z += 2;
}
A& f(){return *this;}
};
void test(){
{A a, b(a), c(A().f());}
cout << z;
}
int main()
{
test();
return 0;
}
I understand what happens in all lines except object c initialization: c(A().f())? I'd would be glad, if you explain me this line?
The lines breaks down like this:
A a; // Calls A::A(); (Default constructor. Has `z++;`, so `z` is now 1)
A b(a); // Calls A::A(const A&); (Copy constructor. `z += 2;`, so `z` is now 3)
A c(
A() // Calls A::A(); (`z++;`, `z` is now 4)
// This creates a temporary `A` object
.f() // Just returns `*this`, the temporary. No copy made, nothing happens to `z`
); // Calls A::A(const A&); (`z += 2;`, `z` is now 6)
The below is the code, but I dont know how to debuy it. Can someone help me?
enter image description here
#include <iostream>
using namespace std;
class CSample
{
int *x;
int N;
public:
//dafualt constructor
CSample(): x(NULL)
{}
void AllocateX(int N)
{
this->N = N;
x = new int[this->N];
}
int GetX()
{
return x;
}
~CSample()
{
delete []x;
}
};
int main()
{
CSample ob1; //Default constructor is called.
ob1.AllocateX(10);
//problem with this line
CSample ob2 = ob1; //default copy constructor called.
CSample ob3; //Default constructor called.
//problem with this line
ob3 = ob1; //default overloaded = operator function called.
}
This method has the wrong signature
int GetX()
{
return x;
}
it should be
int* GetX()
{
return x;
}
As far as your assignment, you'd need a copy assignment operator to say ob3 = ob1 which would look like
CSample& operator=(CSample& other)
{
N = other.N;
x = new int[N];
std::copy(other.x, other.x + other.N, x);
return *this;
}
I am attempting to add a copy constructor to each in order to run the main function. What I have implemented right now currenrtly prints out "b2.valuea = -858993460 b2.valueb = 10" So it reads valueb correctly but obviously something is going wrong with valuea. Advice?
#include "stdafx.h"
#include <iostream>
using namespace std;
class A
{
int valuea;
public:
int getValuea() const { return valuea; }
void setValuea(int x) { valuea = x; }
// copy constructor
A() {}
A(const A& original)
{
valuea = original.valuea;
}
};
class B : public A
{
int valueb;
public:
int getValueb() const { return valueb; }
void setValueb(int x) { valueb = x; }
// copy constructor
B(){}
B(const B& original)
{
valueb = original.valueb;
}
};
int main()
{
B b1;
b1.setValuea(5);
b1.setValueb(10);
B b2(b1);
cout << "b2.valuea = " << b2.getValuea() << "b2.valueb = " <<
b2.getValueb() << endl;
}
You are not calling the copy constructor of your base-class in your derived copy constructor.
Change it like:
B(const B& original) : A(original){
valueb = original.valueb;
}
And it will output
b2.valuea = 5
b2.valueb = 10
Only first pair of values output on running this program seem correct, the others don't. What is going on?
#include <iostream>
#include <vector>
class a
{
public:
class b
{
public:
a* parent;
void test()
{
std::cout<<parent->value<<std::endl;
}
} b1;
unsigned long value;
a()
{
b1.parent = this;
value = 2;
}
void go()
{
value++;
b1.test();
}
};
int main()
{
{
a a1;
a1.go();
std::cout<<a1.value<<std::endl;
}
std::cout<<std::endl;
{
a a1; a1 = a();
a1.go();
std::cout<<a1.value<<std::endl;
}
std::cout<<std::endl;
{
std::vector<a> a1; a1.push_back(a());
a1.at(0).go();
std::cout<<a1.at(0).value<<std::endl;
}
return 0;
}
You are missing a copy ctor and assignment operator for type 'a'. When copying or assigning objects, you consequently don't properly update their b1.parent. Instead, the b1.parent values point to a different 'a' object than their real parent.
To see this problem in action, use this in your existing code:
void go() {
value++;
std::cout << (this == b1.parent ? "as expected\n" : "uh-oh\n");
b1.test();
}
To fix it, modify class a:
a() : b1 (this), value (2) {} // a change from your default ctor
a(a const &x) : b1 (this), value (x.value) {}
a& operator=(a const &x) {
value = x.value;
return *this;
}
And modify class b (necessarily to use the ctor initializer as I do above):
b(a *parent) : parent (parent) {}