Reputation: 1
I'm wondering what a call to an object is supposed to display. I have a class called big_number that has a few different constructors. In another method, I'm declaring an object 'a' using class big_number as follows:
big_number a;
cout << "Default constructor gives " << a << endl;
And my constructor is:
big_number::big_number()
{
head_ptr = 0;
tail_ptr = 0;
positive = false;
digits = 0;
base = 10;
}
(Although I'm sure that this constructor is wrong).
The full code of the testing file:
int main()
{
int n1, n2;
unsigned int base;
string s;
char choice;
do
{
cout << "Type 'd' to test default constructor" << endl;
cout << "Type 'i' to test int constructor" << endl;
cout << "Type 's' to test string constructor" << endl;
cout << "Type 'a' to test assignment" << endl;
cout << "Type '>' to test input operator" << endl;
cout << "Type '=' to test comparison operators" << endl;
cout << "Type 'q' to quit" << endl;
cin >> choice;
if (toupper(choice) == 'D')
{
big_number a;
cout << "Default constructor gives " << a << endl;
}
//More Code
Upvotes: 0
Views: 66
Reputation: 1137
If by "call to an object" you mean your call of the operator<<
on the object a
with cout
as the stream argument: It displays whatever you define it to display (in a member function of big_number
or a free function). There is no "default" operator<<
for user-defined classes. So, if you define it like
#include <iostream>
struct big_number
{
template<typename T>
friend T& operator<< (T& stream, const big_number& bn);
};
template<typename T>
T& operator<<(T& stream, const big_number& bn) {
return (stream << "Hello World");
}
int main()
{
big_number a;
std::cout << a << std::endl;
}
... it will just display "Hello world".
The purpose of making it a friend
function is so it can access the private data members of big_number
(since you usually want it to display something that depends on the data stored in the object, and not a constant "Hello world"). So, within the operator<<
definition, you would probably iterate through the digits in your linked list and push them to the stream (if I understand correctly what you are trying to do).
Upvotes: 1