-4

I previously practiced the Java language and I started learning C++ as of last night. I've briefly tried to look it up and had no luck. Are there "methods" in C++ like there is in java? if so, what are they called and are they used/called just like in java? (link would be helpful, if possible)

In java, for example, I could write

public static void main(String[] args){
gameboard();
}

public static void gameboard(){
//gameboard code
}

I experimented in C++ and couldn't get the compiler to compile error free.

using:

int main()
{
gameBoard();
}

int gameboard(){
//gameboard code
}
  • 7
    Google is your friend. C++ *does* have classes, objects, and methods. – JSON Feb 23 '14 at 04:03
  • 1
    Rule of thumb - all programming languages are similar. They all have the same backbones and similar functions. – Shahar Feb 23 '14 at 04:03
  • This is a huge subject. I'm going to write an answer doing my best to point you in the right direction. – Kyle Marimon Feb 23 '14 at 04:03
  • One difference though is that C++ doesn't have packages, but packages are more or less an abstract concept of linking to an external library which C++ can do. – JSON Feb 23 '14 at 04:06
  • You should really start with a good introductory book or tutorial. – David Rodríguez - dribeas Feb 23 '14 at 04:12
  • Do you have any book preferences or suggestions for me? This would be greatly appreciated. – user3128153 Feb 23 '14 at 04:15
  • @user3128153 look right. There's a link there: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list?rq=1 – Paweł Stawarz Feb 23 '14 at 04:16
  • People really need to put themselves in the shoes of someone new to programming. But I respect you for going to the community for advice. – Kyle Marimon Feb 23 '14 at 04:23
  • @KyleMarimon we do. Which means we expect people to either buy a book or know how to use Google, because all of us certainly did one or the other. – Brian Roach Feb 23 '14 at 04:35

4 Answers4

4

Are there "methods" in C++ like there is in Java? if so, what are they called and are they used/called just like in Java?

Yes, there are methods in C++. They are called member functions. Just like in Java these can be associated with an instance or with a class (i.e. static).

Unlike Java where all methods belong to classes, C++ has free-standing functions. They are similar to static methods of Java, except they are defined outside all classes. main is one example of such free-standing function: it has to be defined outside all classes.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

Yes C++ has methods similar to Java. Also there are some similarities in the way methods are declared in C++ and Java (please note the word some). The code that you have written will compile fine in Java, unfortunately in C++ in order to use a function (e.g. the function gameboard) in any place it must be declared before its used.

The correct way to write the code in c++ would be like this :

int gameBoard(){
    // gameBoard code
}

int main()
{
    gameBoard();
}

or this (using prototypes):

int gameBoard();

int main()
{
    gameBoard();
}

int gameBoard(){
    // gameBoard code
}

This topic needs a very detailed treatment however, it would be the best if you referred some text and try lot of stuff on your own. The more you experiment the more clearer the concept will be.

blalasaadri
  • 5,990
  • 5
  • 38
  • 58
Kakarot
  • 4,252
  • 2
  • 16
  • 18
  • So what you're saying is that The gameboard function would basically have to be placed(defined) above the main function? and also basically meaning that the main function should always be at the bottom of the file? Sorry, i'm so hooked on java, it's kind of hard switching over to the syntax and learning how to properly organize code. – user3128153 Feb 23 '14 at 04:11
  • And... does each "function" have to return 0? or just the main function? – user3128153 Feb 23 '14 at 04:12
  • yes in the context of your question that what you need to do. Say we have 3 functions :func1 , func2, func3 & if I want to use func1 & func2 in the function func3 , then order of declaration should be as follows : void func1(){} void func2(){} void func3(){ func1(); func2(); } – Kakarot Feb 23 '14 at 04:13
  • 3
    @user3128153 you can __define__ a function after the __main__ function, but you have to provide a __declaration__ before you use it. – Paweł Stawarz Feb 23 '14 at 04:13
  • @PawełStawarz +1...I forgot to mention that – Kakarot Feb 23 '14 at 04:14
  • 1
    @user3128153, About the return value, they should return what is sensible, just like in Java. `main` is special. – chris Feb 23 '14 at 04:19
  • @Kakarot you can click the up arrow next to the comment, instead of replying with "+1". – user253751 Feb 23 '14 at 04:23
0
//You need to declare a prototype if you want function after main function.
//So that the compiler knows the function existed.
//Prototype must matches the return type and parameter of the function header.
int gameBoard();
void functionWithParameter(String text); //or just void functionWithParameter(String)

int main()
{
   gameBoard();
}

int gameBoard(){
  //gameboard code
  //needs to return int just like java
}

void functionWithParameter(String text) {
  //code here
}
Elam
  • 19
  • 4
-2

Try this

#include<cstdio>
#include<cstdlib>

int gameBoard(){  // if you return something int type. yous should catch it in main

}


int main(){
    gameBoard();
    return 0;  // Must have if you use int main();
}

The method in c++ is called functions, and the structure / calling method is quite simiiar to java, you may want to look namespace, class of c++ as well.

shole
  • 4,046
  • 2
  • 29
  • 69