C++/Function/Basics

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

A C++ program with several functions

<source lang="cpp">

  1. include <iostream>

using namespace std; void line(), message(); int main() {

  cout << "The program starts in main()." << endl;
  line();
  message();
  line();
  cout << "At the end of main()." << endl;
  return 0;

} void line() {

  cout << "line();" << endl;

} void message() {

  cout << "In function message()." << endl;

}

      </source>


Another example of a conversion function

<source lang="cpp">

  1. include <iostream>

using namespace std; class Power {

 double b;
 int e;
 double val;

public:

 Power(double base, int exp);
 Power operator+(Power o) {
   double base;
   int exp;
   base = b + o.b;
   exp = e + o.e;
   Power temp(base, exp);
   return temp;
 }
 operator double() { return val; } // convert to double

}; Power::Power(double base, int exp) {

 b = base;
 e = exp;
 val = 1;
 if(exp==0) 
    return;
 for( ; exp>0; exp--) 
    val = val * b;

} int main() {

 Power x(4.0, 2);
 double a;
 a = x;              // convert to double
 cout << x + 100.2;  // convert x to double and add 100.2
 cout << "\n";
 Power y(3.3, 3), z(0, 0);
 z = x + y;          // no conversion
 a = z;              // convert to double
 cout << a;
 return 0;

}


      </source>


Class as a return type

<source lang="cpp">

  1. include <iostream>

using namespace std; class myclass { public:

 myclass();
 myclass(const myclass &o);
 myclass f();

}; myclass::myclass() {

 cout << "Constructing normally\n";

} myclass::myclass(const myclass &o) {

 cout << "Constructing copy\n";

} myclass myclass::f() {

 myclass temp;
 return temp;

} int main() {

 myclass obj;
 obj = obj.f();
 return 0;

}


      </source>


Computes the factorial of an integer iteratively: a loop, and recursively

<source lang="cpp">

  1. include <iostream>
  2. include <iomanip>

using namespace std;

  1. define LENGTH 20

long double iterativeFunction(unsigned int n); // Iterative solution long double recursiveFunction(unsigned int n); // Recursive solution int main() {

  unsigned int n;
  cout << fixed << setprecision(0);
  cout << setw(10) << "n" << setw(30) << "Factorial of n"
       << "           (Iterative solution)\n" << endl;
  for( n = 0; n <= LENGTH;  ++n)
     cout << setw(10) << n << setw(30) << iterativeFunction(n)
          << endl;
  cout << "Go on with <return>";
  cin.get();
  cout << setw(10) << "n" << setw(30) << "Factorial of n"
       << "           (Recursive solution)\n"  << endl;
  for( n = 0; n <= LENGTH;  ++n)
     cout << setw(10) << n << setw(30) << recursiveFunction(n)
          << endl;
  cout << endl;
  return 0;

} long double iterativeFunction(unsigned int n) // Iterative solution. {

  long double result = 1.0;
  for( unsigned int i = 2; i <= n; ++i)
      result *= i;
  return result;

} long double recursiveFunction(unsigned int n) // Recursive solution. {

  if( n <= 1)
     return 1.0;
  else
     return recursiveFunction(n-1) * n;

}

      </source>


Throwing an exception from a function

<source lang="cpp">


  1. include <iostream>

using namespace std;

void myFunction(int test) {

 cout << "Inside myFunction, test is: " << test << endl; 
 if(test) throw test; 

}

int main() {

 cout << "start\n"; 

 try {                           // start a try block 
   cout << "Inside try block\n"; 
   myFunction(0); 
   myFunction(1); 
   myFunction(2); 
 } catch (int i) {              // catch an error 
   cout << "Caught an exception -- value is: "; 
   cout << i << endl; 
 } 

 cout << "end"; 

 return 0; 

}

      </source>