C++ Tutorial/Development/typedef

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Use typedef keyword to define new variable type

<source lang="cpp">#include <iostream>

typedef unsigned short int USHORT;       

int main()
{
   USHORT Width = 5;
   USHORT Length;
   Length = 10;
   USHORT Area = Width * Length;
   std::cout << "Width: " << Width << "\n";
   std::cout << "Length: " << Length << std::endl;
   std::cout << "Area: " << Area << std::endl;
   return 0;
}</source>
Width: 5
Length: 10
Area: 50

Use typedef to create new data type and use it as function return type

<source lang="cpp">#include <cmath>

  1. include <iostream>

using namespace std; typedef double NewType; NewType f(double number); NewType f2(double number); NewType f3(double number); int main() {

      int menu;
      double num;
      NewType response;
   response = f(num);
   cout << "The answer is " << response;
   
   response = f2(num);
   cout << "The answer is " << response;
   
   response = f3(num);
   cout << "The answer is " << response;
      
      return 0;

} NewType f(double number) {

  return sqrt(number);

} NewType f2(double number) {

  return pow(number,.3333);

} NewType f3(double number) {

  return pow (number,.25);

}</source>

The answer is 2.29873e-154The answer is 3.8421e-103The answer is 1.51616e-077"