C ++ shortcut tutorial-Chapter 8-function, Part 2: reference, overload, and default parameters

Source: Internet
Author: User

// -- C ++ shortcut tutorial -- Chapter 8 -- function, Part 2: reference, overload, and default parameters
// -- Chapter 8 -- function, Part 2: reference, overload, and default parameters
// -- 11/15/2005 Tues.
// -- Computer lab
// -- Liwei

// -- Program #1 Value Transfer
# Include <iostream>
Using namespace STD;

Int sqr_it (int x );

Int main ()
{
Int T = 10;
Cout <sqr_it (t) <<'' <t <Endl;

Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}

Int sqr_it (int x)
{
X = x * X;
Return X;
}

// -- Program #2 pointer passing
# Include <iostream>
Using namespace STD;

Void swap (int * X, int * y );

Int main ()
{
Int I, J;
I = 10;
J = 20;

Cout <"initial values of I and J :";
Cout <I <''<j <Endl;

Swap (& J, & I );

Cout <"swapped values of I and J :";
Cout <I <''<j <Endl;

Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}

Void swap (int * X, int * Y)
{
Int temp;
Temp = * X;
* X = * Y;
* Y = temp;
}

// -- Program #3 reference parameters
# Include <iostream>
Using namespace STD;

Void F (Int & I );

Int main ()
{
Int val = 1;
Cout <"old value for Val:" <Val <Endl;
F (VAL );

Cout <"new value for Val:" <Val <Endl;
 
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}

Void F (Int & I)
{
I = 100;
}

// -- Program #4 reference
# Include <iostream>
Using namespace STD;

Void swap (Int & X, Int & Y );

Int main ()
{
Int I, J;
I = 10;
J = 20;

Cout <"initial values of I and J :";
Cout <I <''<j <Endl;

Swap (J, I );

Cout <"swapped values of I and J :";
Cout <I <''<j <Endl;

Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}

Void swap (Int & X, Int & Y)
{
Int temp;
Temp = X;
X = y;
Y = temp;
}

// -- Program #5 returns a reference
# Include <iostream>
Using namespace STD;

Double & F ();
Double val = 100.0;

Int main ()
{
Double newval;
Cout <F () <Endl;
 
Newval = f ();
Cout <newval <Endl;

F () = 99.1;
Cout <F () <Endl;

Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}

Double & F ()
{
Return val;
}

// -- Program #6 RETURN reference
# Include <iostream>
Using namespace STD;

Double & change_it (int I );
Double Vals [] = {1.1, 2.2, 3.3, 4.4, 5.5 };

Int main ()
{
Int I;

Cout <"Here are the original values :";
For (I = 0; I <5; I ++)
Cout <Vals [I] <'';
Cout <Endl;

Change_it (1) = 5298.23;
Change_it (3) =-98.8;

Cout <"Here are the changed values :";
For (I = 0; I <5; I ++)
Cout <Vals [I] <'';
Cout <Endl;

Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}

Double & change_it (int I)
{
Return Vals [I];
}

// -- Program #7 a simple bounded Array
# Include <iostream>
Using namespace STD;

Int & put (int I );
Int get (int I );
Int Vals [10];
Int error =-1;

Int main ()
{
Put (0) = 10;
Put (1) = 20;
Put (9) = 30;

Cout <get (0) <'';
Cout <get (1) <'';
Cout <get (9) <''<Endl;

Put (12) = 12;
Int xxx = get (12 );
Cout <XXX <''<error <Endl;

Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}

Int & put (int I)
{
If (I> = 0 & I <10)
Return Vals [I];
Else {
Cout <"bounds put () error! /N ";
Return Error; // Error
}
}

Int get (int I)
{
If (I> = 0 & I <10)
Return Vals [I];
Else {
Cout <"bounds get () error! /N ";
Return Error;
}
}

// Restrictions on using referenced variables:
//. A referenced variable cannot be referenced.
//. The referenced array cannot be created.
//. The pointer pointing to the reference cannot be created.
// The reference is not used in the in-place domain.

// -- Program #8 independent reference
# Include <iostream>
Using namespace STD;

Int main ()
{
Int J, K;
Int & I = J; // independent reference

J = 10;

Cout <j <"" <I <Endl;

K = 121;
I = K;

Cout <j <''<I <Endl;

Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}

// -- Program #9 function overload
# Include <iostream>
Using namespace STD;

Void F (int I );
Void F (int I, Int J );
Void F (Double K );

Int main ()
{
F (10 );
F (10, 20 );
F (12.23 );
 
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}

Void F (int I)
{
Cout <"in F (INT), I is:" <I <Endl;
}

Void F (int I, Int J)
{
Cout <"in F (INT, INT), I is:" <I;
Cout <", J is:" <j <Endl;
}

Void F (Double K)
{
Cout <"int F (double), k is:" <k <Endl;
}

// -- Program #10 function Overloading
# Include <iostream>
Using namespace STD;

Int myabs (int I );
Double myabs (double D );
Long myabs (long L );

Int main ()
{
Cout <myabs (-10) <Endl;
Cout <myabs (-11.0123) <"/N ";
Cout <myabs (-9l) <'/N ';
 
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}

Int myabs (int I)
{
Cout <"Using Integer myabs ():";

If (I <0)
Return-I;
Else
Return I;
}

Double myabs (double D)
{
Cout <"Using Double myabs ():";

If (d <1, 0.0)
Return-D;
Else
Return D;
}

Long myabs (long l)
{
Cout <"Using Long myabs ():";

If (L <0)
Return-L;
Else
Return L;
}

// -- Program #11 default parameters
# Include <iostream>
Using namespace STD;

Void clrscr (INT size = 25 );

Int main ()
{
Int I;
 
For (I = 0; I <30; I ++)
Cout <I <Endl;
Clrscr ();
 
For (I = 0; I <30; I ++)
Cout <I <Endl;
Clrscr (10 );
 
 
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}

Void clrscr (INT size)
{
For (; size --)
Cout <'/N ';
}

// -- Program #12 custom version of the default Parameter Function strcat ()
# Include <iostream>
# Include <cstring>
Using namespace STD;

Void mystrcat (char * S1, char * S2, int Len =-1 );

Int main ()
{
Char str1 [80] = "this is a test ";
Char str2 [80] = "0123456789 ";

Mystrcat (str1, str2, 5 );
Cout <str1 <Endl;

Strcpy (str1, "this is a test ");

Mystrcat (str1, str2 );
Cout <str1 <Endl;
 
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}

Void mystrcat (char * S1, char * S2, int Len)
{
While (* S1)
S1 ++;
 
If (LEN =-1)
Len = strlen (S2 );

While (* S2 & Len ){
* S1 = * S2;
S1 ++;
S2 ++;
Len --;
}

* S1 = '/0 ';
}

// -- Program #13 Differences Between Heavy Loads
# Include <iostream>
Using namespace STD;

Float myfunc (float I );
Double myfunc (double I );

Int main ()
{
Cout <myfunc (10.1) <"";

Cout <myfunc (10); // Error
 
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}

Float myfunc (float I)
{
Return I;
}

Double myfunc (double I)
{
Return-I;
}

// -- Program #14 another inconsistency Error
# Include <iostream>
Using namespace STD;

Char myfunc (unsigned char ch );
Char myfunc (char ch );

Int main ()
{
Cout <myfunc ('C') <Endl;
Cout <myfunc (-66) <Endl;
 
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}

Char myfunc (unsigned char ch)
{
Return CH-1;
}

Char myfunc (char ch)
{
Return CH + 1;
}

// -- Program #15 inconsistencies
# Include <iostream>
Using namespace STD;

Int myfunc (int I );
Int myfunc (int I, Int J = 1 );

Int main ()
{
Cout <myfunc (4, 5) <"";
Cout <myfunc (10 );
 
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}

Int myfunc (int I)
{
Return I;
}

Int myfunc (int I, Int J)
{
Return I * J;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.