I am trying to learn a bit of c++ by following C+ How to program book.
I have got to a chapter on overloading and classes. I have written a very basic prgram and class with std::cout statements to try and get a better understanding of how things happen in c++.
I have a basic class with 2 constructors, 1 is the 'standard' constructor and the other uses an initiliser list. Each has a std::cout statement to show its call.
I have also written a destructor with does has a std::cout to show its call.
I overloaded the + operator and stream << as part of the exercise.
The bit where i need clarification is the line where the overloaded + is. Am I correct in thinking that the object is created inside the function and the variables are added then the object is returned as a paramter to the overloaded << ostream. Once displated it is then destroyed?
1. Your constructor needs to be set.. Everything in it is empty
object::object(){
length = 0;
breadth = 0;
// you also want to create your new object in here.
std::cout << "constructor" << std::endl;
}
2. The purpose of the destructor is to clear all data when the program ends.
object::~object(){
delete [] array;
std::cout << "destructor" << std::endl;
}
3. Yes you are correct about the operator overload. You have not stored the return value.
std::cout << "addition of 2 objects using overloaded +\n" << test + test2 << std::endl;
You are saying that :
std::cout << "Add two numbers " << 3 + 5 << std::endl;
if you don't store the sum then it is deleted after it is called.
*** instead of calling "std" all the time. make it global **
"using namespace std;"