Part-3: C++, C#, Java Syntax Differences
· One min read
Comments, Methods, Class, objects Syntax Differences
Comments Same for C++, C#, Java
- Single line comments - //
- Multi line comments
/ This is a a multiline comment /
Method/Function declarations
Same, except that in C# and in Java, function must always be part of a class, and must prefix with access specifier - public/private/protected
check main function for reference
Class declarations
Same but c# and Java does not require a semicolon after closing bracket '}'
_ C++ _
class myMath {
//Methods
public: int Add(int i, int j)
{
return i + j;
}
};
_ C#/Java _
class myMath {
//Methods
public int Add(int i, int j)
{
return i + j;
}
}
Object declaration/creation C++
Object creation on Stack
myMath obj; //on stack
int result = obj.Add(1,2);
Object creation on Heap
myMath \*pobj = new myMath(); //on heap
int result = obj->Add(1,2);
delete pobj;
Java/C#
myMath obj = new myMath();
int result = obj.Add(1,2);