- #1
yungman
- 5,644
- 227
Hi
I modified the program originally from Jtbell. I don't want to hijack his thread, so I ask my questions in this thread. I change names, mainly I assign names instead of a, b, c. I have question on one part of the program:
This is header file:
1) first question: If you look at the word file, there are garbage when I try to print out the name. Please look at the header file, This is from running line 37 in operator+() jumping to operator+(). I try to print out rhs.name, that gives me the garbage. By if you look at line 23 in Copy Constructor, I assigned "Temp" to the name of the object created. I even verified by printing out the name in line 25 and it works.
When going to operator+(), object Temp is passed to &rhs by reference. I should be able to get "Temp" when I cout << rhs.name; But I got garbage. I double check &rhs = &Temp. Why the name doesn't follow?
2) When I want to pass the c-string name from main to either Constructor, I have to create dynamic memory( name = new char[strlen(desc) + 1]), then using strncpy_s() to copy. I tried declaring a char array[] to copy in, it won't work. Is this the only way when passing c-string?
Thanks
I modified the program originally from Jtbell. I don't want to hijack his thread, so I ask my questions in this thread. I change names, mainly I assign names instead of a, b, c. I have question on one part of the program:
C++:
#include <iostream>
#include "OverLoad.h"
#include <cstring>
using namespace std;
int main()
{ char Ara[] = "First", Arb[] = "Second", Arc[] = "Result";
Vec First(Ara, 1, 2);
cout << " In main, &First = " << &First << "\n\n";
Vec Second(Arb, 3, 4);
cout << " In main, &Second = " << &Second << "\n\n";
Vec Result(Arc);
cout << " In main, &Result = " << &Result << "\n\n";
Result = First + Second;
cout << " &Result = " << &Result << " Result = " << Result << "\n\n";
return 0;
}
This is header file:
C++:
#ifndef OverLoad_H
#define OverLoad_H
#include <iostream>
#include <cstring>
using namespace std;
class Vec
{private: double x, y;
public:
char* name;
Vec(char* desc)
{ x = 0; y = 0; name= new char[strlen(desc)+1];
strncpy_s(name, strlen(desc) + 1, desc, strlen(desc) + 1);
cout <<" [In Constructor(name)], Object created is: "<<name <<
"("<<(*this).x <<", "<<(*this).y << "), address is "<< this << "\n";
}
Vec(char* desc, double x0, double y0)
{ x = x0; y = y0; name = new char[strlen(desc) + 1];
strncpy_s(name, strlen(desc) + 1, desc, strlen(desc) + 1);
cout << " [Constructor(name,x0,y0)] Object created is: "<< name <<
"("<<(*this).x << ", "<<(*this).y << "), address is "<<this <<"\n";
}// this print out name of the object created.
Vec(const Vec& original)
{ char tp[] = "Temp"; x = original.x; y = original.y; name = tp;
cout << "[Copy Constructor], original: " << original.name <<
"\n Object created: "<<(*this).name <<", address: "<< this<<endl;
}// this print out name of the object created.
~Vec()
{cout<<"\n object destroyed: "<<(*this).name <<", address: "<<this<<endl;}
const Vec operator+ (const Vec& rhs) const
{ cout << " [In op+] \n"; char Ars[] = "sum";
Vec sum(Ars);
cout << " \n declare " << sum.name << ", address: " << &sum <<
", rhs is: " << rhs.name << ", address: " << &rhs << endl;
sum.x = x + rhs.x; sum.y = y + rhs.y;
cout <<"\n "<< sum.name << " "<< sum <<" = "<<*this<<" + "<<rhs<<"\n\n\n";
cout << " [To operator+], ";
return sum;
}
Vec& operator= (const Vec& rhs)//cout << rhs.name don't work, but &rhs works why?
{ cout << "\n [in op=] rhs.name:" << rhs.name << ", address: " << &rhs;
x = rhs.x; y = rhs.y;
cout << "\n Address of copied object: this =" << this << *this << endl;
return *this;
}
friend ostream& operator<< (ostream& out, const Vec& v)
{ out << "[In op<<] " << "(" << v.x << "," << v.y << ")";
return out; }
};
#endif
1) first question: If you look at the word file, there are garbage when I try to print out the name. Please look at the header file, This is from running line 37 in operator+() jumping to operator+(). I try to print out rhs.name, that gives me the garbage. By if you look at line 23 in Copy Constructor, I assigned "Temp" to the name of the object created. I even verified by printing out the name in line 25 and it works.
When going to operator+(), object Temp is passed to &rhs by reference. I should be able to get "Temp" when I cout << rhs.name; But I got garbage. I double check &rhs = &Temp. Why the name doesn't follow?
2) When I want to pass the c-string name from main to either Constructor, I have to create dynamic memory( name = new char[strlen(desc) + 1]), then using strncpy_s() to copy. I tried declaring a char array[] to copy in, it won't work. Is this the only way when passing c-string?
Thanks