www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/03/09/00:42:53

Message-ID: <3AA86A78.8CCCAB79@earthlink.net>
From: Martin Ambuhl <mambuhl AT earthlink DOT net>
X-Mailer: Mozilla 4.76 [en] (Win95; U)
X-Accept-Language: en,zh-CN,fr,de-CH,ru
MIME-Version: 1.0
Newsgroups: comp.os.msdos.djgpp
Subject: Re: integer???
References: <ACVp6.202$nq3 DOT 45130 AT sapphire DOT mtt DOT net>
Lines: 35
Date: Fri, 09 Mar 2001 05:28:04 GMT
NNTP-Posting-Host: 64.152.169.215
X-Complaints-To: abuse AT earthlink DOT net
X-Trace: newsread1.prod.itd.earthlink.net 984115684 64.152.169.215 (Thu, 08 Mar 2001 21:28:04 PST)
NNTP-Posting-Date: Thu, 08 Mar 2001 21:28:04 PST
Organization: EarthLink Inc. -- http://www.EarthLink.net
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Debbie wrote:
> 
> Can someone help me write a program that uses overloaded functions to
> calculate the cube of an integer, a float, and a double.

I'll do your homework this time, but not again.  Even once is probably a
disservice to you:

#include <iostream>
using namespace std;

template < class T > T cube(T x)
{
    return x * x * x;
}

int main(void)
{
    int ix = 3;
    float fx = 3.76f;
    double dx = 7.3;
    long double ldx = -24.31L;
    cout << "int example.  " << ix << " cubed is " << cube(ix) << endl;
    cout << "float example.  " << fx << " cubed is " << cube(fx) << endl;
    cout << "double example.  " << dx << " cubed is " << cube(dx) << endl;
    cout << "long double example.  " << ldx << " cubed is " << cube(ldx) <<
        endl;
    return 0;
}


int example.  3 cubed is 27
float example.  3.76 cubed is 53.1574
double example.  7.3 cubed is 389.017
long double example.  -24.31 cubed is -14366.6

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019