Can Main Be Overloaded in C++ - GeeksforGeeks

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

1/8/2015

Canmain()beoverloadedinC++?GeeksforGeeks

GeeksforGeeks
Acomputerscienceportalforgeeks

GeeksQuiz
Login
Home
Algorithms
DS
GATE
InterviewCorner
Q&A
C
C++
Java
Books
Contribute
AskaQ
About
Array
BitMagic
C/C++
Articles
GFacts
LinkedList
MCQ
Misc
Output
String
Tree
Graph

Canmain()beoverloadedinC++?
PredicttheoutputoffollowingC++program.
#include<iostream>
usingnamespacestd;
intmain(inta)
{
cout<<a<<"\n";
return0;
}
intmain(char*a)
http://www.geeksforgeeks.org/canmainoverloadedc/

1/9

1/8/2015

Canmain()beoverloadedinC++?GeeksforGeeks

{
cout<<a<<endl;
return0;
}
intmain(inta,intb)
{
cout<<a<<""<<b;
return0;
}
intmain()
{
main(3);
main("C++");
main(9,6);
return0;
}
Theaboveprogramfailsincompilationandproduceswarningsanderrors(Seethisforproduced
warningsanderrors).Youmaygetdifferenterrorsondifferentcompilers.
Tooverloadmain()functioninC++,itisnecessarytouseclassanddeclarethemainasmember
function.NotethatmainisnotreservedwordinprogramminglanguageslikeC,C++,JavaandC#.For
example,wecandeclareavariablewhosenameismain,trybelowexample:
#include<iostream>
intmain()
{
intmain=10;
std::cout<<main;
return0;
}
Ouput:
10

Thefollowingprogramshowsoverloadingofmain()functioninaclass.
#include<iostream>
usingnamespacestd;
classTest
{
public:
intmain(ints)
{
cout<<s<<"\n";
return0;
}
intmain(char*s)
{
cout<<s<<endl;
return0;
}
http://www.geeksforgeeks.org/canmainoverloadedc/

2/9

1/8/2015

Canmain()beoverloadedinC++?GeeksforGeeks

intmain(ints,intm)
{
cout<<s<<""<<m;
return0;
}
};
intmain()
{
Testobj;
obj.main(3);
obj.main("IloveC++");
obj.main(9,6);
return0;
}
Theoutcomeofprogramis:
3
IloveC++
96

ThisarticleiscontributedbyPravasiMeet.Pleasewritecommentsifyoufindanythingincorrect,oryou
wanttosharemoreinformationaboutthetopicdiscussedabove

RelatedTopics:
ComparisonofafloatwithavalueinC
PurevirtualdestructorinC++
C++mutablekeyword
Isitpossibletocallconstructoranddestructorexplicitly?
MultithreadinginC
HowtoprintrangeofbasicdatatypeswithoutanylibraryfunctionandconstantinC?
C++finalspecifier
PrintsubstringofagivenstringwithoutusinganystringfunctionandloopinC
http://www.geeksforgeeks.org/canmainoverloadedc/

3/9

1/8/2015

Canmain()beoverloadedinC++?GeeksforGeeks

2
Tweet 5

Writingcodeincomment?Pleaseuseideone.comandsharethelinkhere.
Like

18

16Comments

GeeksforGeeks

Login

Share Favorite

SortbyNewest

Jointhediscussion
dhiraj 21hoursago

whatistheoutputincaseofc??
#include<stdio.h>
intmain()
{
intmain=10
printf("%d",main)
return0
}
ifmainisakeywordincitshouldshowsomeerror.
butitcompilingandoutputis10

Reply Share

khan 3daysago

Whatincaseofembeddedprogramming,canmain()beoverloaded??Basedoncertainvalues/
oroncertainconditionsthenitgetsstartingpoint(main).Isitpossible?????
(sorryforrepeatingquestion)

Reply Share

Khan 3daysago

Whatincaseofembeddedprogramming,canmain()beoverloaded.Basedoncertainvalues
thenitgetsstartingpoint.Isitpossible

Reply Share

PiyushJohri 4monthsago

IamusingTurboCon32bitwindows.
Andherethecompilergivestheerror.
ERROR:class_name::Main(int)isnotaccessible.

Reply Share

Tharun 5monthsago

pravsiMeet...ilikeit(y)

Reply Share

http://www.geeksforgeeks.org/canmainoverloadedc/

4/9

1/8/2015

Canmain()beoverloadedinC++?GeeksforGeeks

newspecies 7monthsago

ismain()andmain(int,char**)functionoveloading?

Reply Share

Saurabh>newspecies 6monthsago

Nothisnotfunctionoverloadingsinceyoucannotusebothatthesametime.Thedriver
functionmain()cannotbeoverloaded.Thetitleofthistopicismisleading.Herethe
memberfunctionmain()isoverloaded.
4

Reply Share

Meet ayearago

main()functioncannotbeoverloadedbecausetherecanbeonlyentrypointintheprogram,
multiplemain()functionsconfusesthecompilerthat'swhycompilerwillshowerrormessages.
Thisarticleshowstheoverloadingofmemberfunctionmainnotthedriverfunctionmain().
1

Reply Share

AnandSwaroop>Meet ayearago

thenwhatisspecialaboutmemberfunctionmain()overloading?
itisjustaboutfunctionoverloadinginc++.
alsowhatissignificanceof"Canmain()beoverloadedinC++?"?itssimplyNO.can
youexplainwhatareyoutryingtocommunicate?ifiamnotwrongOverloadingisto
namingidenticalnameofmorethanonefunctionsinsamescope(withsomerulesand
restrictions).
2

Reply Share

AnandSwaroop ayearago

Youarenotoverloadingmain.Justoverloadingmemberfunctionhavingnamemain,Anditcan't
be.ifoverloadingofmainispossiblethenhowwillOSidentifywhereithavetostart?
[VisualStudio2010]
errorC2731:'main':functioncannotbeoverloaded
althoughmainhavetwoprototypes
intmain(charargc,char*argv)
and
intmain()
butyoucan'tusethemtogether.Youcanuseoneofthematatime.
1

Reply Share

Jack ayearago

Himeet,whyisitmandatorythatmain()tobeamemberfunctionofaclass?
whatistheproblemifwedeclarethemgloballyoutsidetheclass.isitc/c++conventiondoesn't
http://www.geeksforgeeks.org/canmainoverloadedc/

5/9

1/8/2015

Canmain()beoverloadedinC++?GeeksforGeeks

whatistheproblemifwedeclarethemgloballyoutsidetheclass.isitc/c++conventiondoesn't
allowstodothis?

Reply Share

BikramHanzra ayearago

HeyMeet,
Thetutorialismisleading.Youarenotoverloadingthemain()function.Thekindofoverloading
youaredoingareformemberfunctionofyourclassTest.main()functionshouldreturnits
valuetoOSwhichyouarenotdoing.Moreover,main()isthefirstfunctionthatiscalledandin
theexampleabove,itsnotpossibleunlessyouexplicitlymakeaobjectofclassTestandthen
makeasystemcall.
ThankYou
21

Reply Share

Srinivas>BikramHanzra ayearago

Ihavethesamequery.Heisnotoverloadingintmain.Heisoverloadingmember
functionmain.Sincemainisnotakeyword,compilerallowstheaboveusage.Please
correctmeifIamwrong.
2

Reply Share

nikinjain ayearago

ThanksMeet,makessense.NiceArticleIndeed.
1

Reply Share

Meet ayearago

itisbettertowritereturn0inthememberfunctionsdefinedinsidetheclassbecausetheir
returntypeisint.otherwisekeeptheirreturntypevoidsothatthese3functionsdon'treturn
anythingtocallerfunction.
3

Reply Share

GeeksforGeeks

Mod >Meet

ayearago

Meet,thanksforpointingthisout.Wehaveaddedreturnstatementsinallmains
functuins.
1

Subscribe

Reply Share

AddDisqustoyoursite

http://www.geeksforgeeks.org/canmainoverloadedc/

Privacy

6/9

1/8/2015

Canmain()beoverloadedinC++?GeeksforGeeks

GeeksforGeeks
Like

85,426peoplelikeGeeksforGeeks.

Facebooksocialplugin

InterviewExperiences
AdvancedDataStructures
DynamicProgramming
GreedyAlgorithms
Backtracking
PatternSearching
Divide&Conquer
MathematicalAlgorithms
Recursion
GeometricAlgorithms

http://www.geeksforgeeks.org/canmainoverloadedc/

7/9

1/8/2015

Canmain()beoverloadedinC++?GeeksforGeeks

PopularPosts
Allpermutationsofagivenstring
MemoryLayoutofCPrograms
UnderstandingexternkeywordinC
Medianoftwosortedarrays
Treetraversalwithoutrecursionandwithoutstack!
StructureMemberAlignment,PaddingandDataPacking
IntersectionpointoftwoLinkedLists
LowestCommonAncestorinaBST.
CheckifabinarytreeisBSTornot
SortedLinkedListtoBalancedBST
Follow@GeeksforGeeks

http://www.geeksforgeeks.org/canmainoverloadedc/

Subscribe
8/9

1/8/2015

Canmain()beoverloadedinC++?GeeksforGeeks

RecentComments
Dev
Input={1,2,3,4,5,6}output=3Whichis...
FindthemissingnumberinArithmeticProgression3minutesago
Abhishek
#include<iostream>usingnamespacestdint...
FindthemissingnumberinArithmeticProgression11minutesago
Gauravpruthi
WontittakeO(length)toconvertthatstring...
AmazonInterviewExperience|Set165(ForSDEI)15minutesago
Gauravpruthi
Preprocessingwasdonebeforeyouwe'regivend...
AmazonInterviewExperience|Set165(ForSDEI)18minutesago
dev
Hey!whymycommenthasbeendeleted?Input=...
FindthemissingnumberinArithmeticProgression26minutesago
Kartik
Theta(Logn)issolutionofT(n)=T(n/a)+n,...
DataStructuresandAlgorithms|Set1129minutesago

C++Example
C++Programming
C++Program

C++Error
C++Code
GNUC++Compiler C++Problem
C++HowTo
C++Information

@geeksforgeeks,SomerightsreservedContactUs!
PoweredbyWordPress&MooTools,customizedbygeeksforgeeksteam

http://www.geeksforgeeks.org/canmainoverloadedc/

9/9

You might also like