Using the new operator
Thanks guys you have been extremely helpful in problems i have come up with.
My latest problem is implementing the New operator. I was able to compile code wth the new operator implemented but All it would do was print a 7 then the rest would be zeros. I will post the original code below. I'am about to reconstruct the code but i think I i am missing something that would make it simple.
I have class B reference to Class A
Inside the code i'am trying to replace
B(A & x)
{
a = &x;
}
with a new operator.
#include <iostream>
using namespace std;
class A
{
int n ;
public:
A():n(0)
{
}
A(int x):n(x)
{
n = x;
}
void print()
{ cout << n<<"\n\n";
}
A(const A& objectCopy){
n = objectCopy.n; // copy constructor
}
};
class B
{
A * a;
public:
B(A & x)
{
a = &x;
// x.operator=(x);
}
void print ()
{
a->print();
}
B(const B& copy){ // Class B copy constructor
a = copy.a;
}
const B &operator=(const B x){
a = x.a; // Operator
}
B::~B(){
delete a;
}
};
//-------------------------------------- -------------------------------------
int main()
{
A a(5);
A a1(7);
a1.print();
B b(a1);
b.print();
B c(a);
b.print();
b = c;
b.print();
cout << endl;
int trick;
cin >> trick;
return 0;
}
//-------------------------------------- ---------------------------------------- --
using namespace std;
class A
{
int n ;
public:
A():n(0)
{
}
A(int x):n(x)
{
n = x;
}
void print()
{ cout << n<<"\n\n";
}
A(const A& objectCopy){
n = objectCopy.n; // copy constructor
}
};
class B
{
A * a;
public:
B(A & x)
{
a = &x;
// x.operator=(x);
}
void print ()
{
a->print();
}
B(const B& copy){ // Class B copy constructor
a = copy.a;
}
const B &operator=(const B x){
a = x.a; // Operator
}
B::~B(){
delete a;
}
};
//--------------------------------------
int main()
{
A a(5);
A a1(7);
a1.print();
B b(a1);
b.print();
B c(a);
b.print();
b = c;
b.print();
cout << endl;
int trick;
cin >> trick;
return 0;
}
//--------------------------------------
Thanks,
chris
