overload
Author |
Message |
alpesh
|
Posted: Thu May 05, 2005 6:47 am Post subject: overload |
|
|
code: |
// OVERLOAD.CPP
// program demonstrates overloaded functions.
#include <iostream.h>
#include <time.h>
#include<conio.h>
void display_time( const struct tm *tim )
{
cout << "1. It is now " << asctime( tim );
}
void display_time( const time_t *tim )
{
cout << "2. It is now " << ctime( tim );
}
void main()
{
clrscr();
time_t tim = time( NULL );
struct tm *ltim = localtime( &tim );
display_time( ltim );
display_time( &tim );
}
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
alpesh
|
Posted: Thu May 05, 2005 6:50 am Post subject: friend() overload |
|
|
code: |
// function overloaded
#include<stdio.h>
#include<conio.h>
class complex
{
float real, imag;
public :
friend complex operator+(const complex &a, const complex &b);
complex() { real = imag = 0,0; }
complex(float a, float b) { real = a; imag = b;}
};
complex operator+(const complex &a,const complex &b)
{
complex z;
z.real = a.real + b.real;
z.imag = a.imag + b.imag;
return z;
}
void main()
{
complex a,b,c;
a = complex(1.5,2.1);
b = complex(1.1,1.4);
c = a+b;
printf("%f",c);
}
|
|
|
|
|
|
|
Andy
|
Posted: Thu May 05, 2005 7:54 am Post subject: (No subject) |
|
|
1, this should be in tutorial/sourcecodes,
2, im sure someone already did a tutorial on this
3, please add in some comments so the newbies can understand it
thank you for contributing |
|
|
|
|
|
jamonathin
|
Posted: Thu May 05, 2005 8:05 am Post subject: (No subject) |
|
|
Andy wrote: 3, please add in some comments so the newbies can understand it
thank you for contributing
<------------- Prime Example |
|
|
|
|
|
wtd
|
Posted: Thu May 05, 2005 12:05 pm Post subject: (No subject) |
|
|
Wow. That's some bad code.
First off, you include "iostream.h", which is deprecated. Instead use:
Second, "time.h" should in C++ be "ctime".
Third, you use "conio.h", limiting this program to Windows. Eliminate this entirely. Clearing the screen is pointless for a terminal-based app.
Fourth, in C++ there's no need to use the "struct" when declaring a variable. All structs are in the namespace properly.
c++: | const struct tm *tim |
Becomes:
Since we use "iostream" rather than "iostream, we have to explicitly tell the compiler cout is in the std namespace.
c++: | std::cout << "It it now " << asctime(tim); |
Lastly (for now), main should be declared as returning int (an int indicates the success or failure of the program to the OS). At the end of it, you should explicitly return zero. |
|
|
|
|
|
|
|