How to explain function overloading in C

王林
Release: 2024-01-12 11:36:04
forward
1174 people have browsed it

How to explain the overloading of functions in C

Suppose we define two functions in the program: String test(String str1) and then int test1(int i). Their contents are very simple functions

String test(String str1){

Response.Write("Function overload test, this is a String function");

Return "123232";

}

int test(int i){

Response.Write("Function overload test, this is an int function");

Return

1;

}

When we call test ("Tofu") and test (888), the compiler will not only not report an error, but also execute different results, like this.

Function overloading brings us great convenience in the programming process. We can let the compiler decide to execute different codes according to changes in our parameters. And this is also

One of the main features of object-oriented programming. I believe MS really wants ASP.Net to become the standard for future programming on the Web.

c function overloading

You can change the initialization as you like. Be careful to change the global variables as well. You can change the array length by yourself. I don’t know if the string size is the longest one. This is what I did. If it is to compare the ASCII code size, just change the function. The idea is similar.

#include

#include "string.h"

#define n 3

class Vector

{

public:

Vector(){}

int max(int ​​z[])

{

int maxNumber=z[0];

for(int i=0;i

{

if (maxNumber

}

return maxNumber;

}

double max(double z[])

{

double maxNumber=z[0];

for(int i=0;i

{

if (maxNumber

}

return maxNumber;

}

long max(long z[])

{

long maxNumber=z[0];

for(int i=0;i

{

if (maxNumber

}

return maxNumber;

}

float max(float z[])

{

float maxNumber=z[0];

for(int i=0;i

{

if (maxNumber

}

return maxNumber;

}

char* max(char z[3][10])

{

char maxNumber[10];

strcpy(maxNumber,z[0]);

for(int i=0;i

{

if(strlen(maxNumber)

}

return maxNumber;

}

};

void main()

{

Vector vt;

int a1[3]={1,3,2};

double a2[3]={2.5,7.8,9.3};

long a3[3]={1000,5677,3456};

float a4[3]={1.5f,5.7f,3.8f};

char a5[3][10]={"I","love","china"};

char maxVector[10]={0};

cout

cout

cout

cout

int i=0;

strcpy(maxVector,vt.max(a5));

while(maxVector[i]!='\0')

{

cout

}

cout

}

c’s question about overloaded functions in class templates

/*

In your code

T_counter& operator =(T_counter x)

{

this->data=x.data;

return *this;

}

T_counter& operator (T_counter x)

{

this->data =x.data;

return *this;

};

If you run c=a b; then c is the value of a b, and a is also the value of a b!

*/

//Change the code to the following and try it

#include

using namespace std;

template

class T_counter

{

public:

T_counter(t a=0)

{

data=a;

}

T_counter& operator =(T_counter x)

{

this->data=x.data;

return *this;

}

T_counter operator (T_counter x)

{

T_counter y;

y.data=this->data x.data;

return y;

}

T_counter operator -(T_counter x)

{

T_counter y;

y.data=this->data-x.data;

return y;

}

T_counter operator *(T_counter x)

{

T_counter y;

y.data=this->data*x.data;

return y;

}

friend ostream & operator{

wocareturn woca;

}

friend istream & operator >>(istream &input,T_counter & x)

{

cin>>x.data;

return input;

}

t getdata()

{

return this->data;

}

private:

t data;

};

int main()

{

T_counter a(5.9),b(6.6),c(0);

coutc=a b;

coutc=(a-b);

coutc=a*b;

coutcoutcin>>c;

coutsystem("pause");

return 0;

}

The above is the detailed content of How to explain function overloading in C. For more information, please follow other related articles on the PHP Chinese website!

source:docexcel.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template