开发者

Examples of PHP In C++

Well I want to learn C++ and at the moment I'm only familiar with PHP and Javascript. And I thought a good way to start learning would be to transfer methods in PHP to C++.

So basically I want the code snippets below in C++

The post with the best comments will get a big green tick.

Also, if you know of a good beginners tutorial please leave a comment.

So here are the bits of code I want in C++

First

$array = array('I\'m', 'learning', 'C++');
foreach($array as $word){
    echo $word.' ';
}

Second

function foo($num,$ber, $add = true){
    if(is_numeric($num) && is_numeric($ber)){
        if(!$add){
            echo $num*$ber;
        }
        else{
            echo $num + $ber;
        }
    }
    else{
        echo 'They aren\'t numbers!';
    }
}
开发者_如何学JAVAfoo(2,4, false);


I'm skeptical about the pedagogical usefulness of translating this into C++. Just translating the above code may not be too useful. Take your first example, where you loop over an array of strings and print out each word - sure, I could translate this into C++ using an std::vector<std::string>, iterate over the vector and output each string to stdout. But is that really going to teach you anything? I could also use a C array of const char* pointers, iterate over that and call printf on each one. But is that really going to teach you anything?

Since you already know how to code in PHP and Javascript, you're obviously aware of basic programming concepts like variables, loops, conditionals, etc. But C++ is a dramatically different language than either PHP or Javascript. For one thing, it's statically typed. For another thing, it requires manual memory management. So I think rather than trying to translate PHP code to C++, you'd be better off reading a good introductory book to C++.


never try to learn any complex subject by 'translating' from another one, no matter how well you know the old one.

You'd only get inconsistent concepts, with the limitations of both and the advantages of none.


I think you'd be much better off if you tried to figure it out and asked questions you had about it along the way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜