using setprecision without fixed or scientific format. – Computer Sir Ki Class
  

Login
[lwa]



Code Learning #CPP#4341    siteicon   siteicon  

using setprecision without fixed or scientific format.

typedef as replacement for using a friendlier name.

Learning Objectives

  • Use of typedef as replacement of a friendlier name for a primitive datatype..

Source Code

TC++ #4341

#include <iostream.h>
#include <conio.h>
typedef double amount;
int main()
{
clrscr();
amount principal=100.50;
amount term=2.5;
amount rate=5.5;
amount simple_interest=principal*term*rate/100.0;
cout<<"Simple Interest = "<<simple_interest<<endl;
getch();
return 0;
}

Source Code

#include <iostream>
typedef double amount;
using namespace std;
int main()
{
amount principal=100.50;
amount term=2.5;
amount rate=5.5;
amount simple_interest=principal*term*rate/100.0;
cout<<"Simple Interest = "<<simple_interest<<endl;
return 0;
}

Test it !

Run Output

Simple Interest = 13.8187

Code Understanding

typedef double amount;
Here the name amount can be used as a replacement of primitive datatype double.

amount principal=100.50;
amount term=2.5;
amount rate=5.5;
All these are declaration and initialization based on a friendlier replacement name.

amount simple_interest=principal*term*rate/100.0;
Here we use the friendlier name for output of the expression as well.

Notes

  • When typedef is used, the standard primitive type can still be used. For e.g. double can still be used for definitions.

Common Errors

  • Sometime student write primitive data type after the friendly name. The standard syntax is –
    typedef <primitive datatype> <friendly name>


Suggested Filename(s): typedef1.cpp



Share

sunmitra| Created: 23-May-2018 | Updated: 23-May-2018|






Back