https://kotlinlang.org logo
#getting-started
Title
# getting-started
a

anlex N

12/10/2023, 2:58 PM
Copy code
C++
// deriv_VirtualFunctions2.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;

class Base {
public:
  virtual void NameOf();  // Virtual function.
  void InvokingClass();  // Nonvirtual function.
};

// Implement the two functions.
void Base::NameOf() {
  cout << "Base::NameOf\n";
}

void Base::InvokingClass() {
  cout << "Invoked by Base\n";
}

class Derived : public Base {
public:
  void NameOf();  // Virtual function.
  void InvokingClass();  // Nonvirtual function.
};

// Implement the two functions.
void Derived::NameOf() {
  cout << "Derived::NameOf\n";
}

void Derived::InvokingClass() {
  cout << "Invoked by Derived\n";
}

int main() {
  // Declare an object of type Derived.
  Derived aDerived;

  // Declare two pointers, one of type Derived * and the other
  // of type Base *, and initialize them to point to aDerived.
  Derived *pDerived = &aDerived;
  Base  *pBase  = &aDerived;

  // Call the functions.
  pBase->NameOf();      // Call virtual function.
  pBase->InvokingClass();  // Call nonvirtual function.
  pDerived->NameOf();    // Call virtual function.
  pDerived->InvokingClass(); // Call nonvirtual function.
}
how to convert this c++ to kotlin?
y

Youssef Shoaib [MOD]

12/10/2023, 3:43 PM
Probably a good idea to try giving this to ChatGpt or similar first before posting it here. It's unlikely that someone will do a translation for you, especially when there isn't a clear attempt that you've made at it or a very specific part that you're unable to translate
1
d

Daniel Pitts

12/10/2023, 4:13 PM
Plenty of helpful people here, but none are going to do you the disservice of completing your homework for you. Try answering these questions first: Why do you want to convert this to Kotlin? Have you tried to convert it yourself? What stops you from converting it?
1
a

anlex N

12/11/2023, 2:06 AM
1. I am summarizing the similarities to C++ and Kotlin
2. I have tried to convert it myself, but result is wrong
d

Daniel Pitts

12/11/2023, 2:07 AM
How was it wrong?
a

anlex N

12/11/2023, 2:09 AM
3. I can not use Chatgpt, because my phone number do not support to sign in. can you help me with your Chatgpt account? or tell me your password to try?
d

Daniel Pitts

12/11/2023, 2:09 AM
If you need ChatGPT to do this right, you're definitely not qualified to summarize the similarities between C++ and Kotlin.
a

anlex N

12/11/2023, 2:09 AM
4. I use google bard and other AI, the result is wrong.
d

Daniel Pitts

12/11/2023, 2:10 AM
Do you understand the C++ code?
a

anlex N

12/11/2023, 2:12 AM
I know, my C++ code result is:
Derived::NameOf
Invoked by Base
Derived::NameOf
Invoked by Derived
d

Daniel Pitts

12/11/2023, 2:12 AM
So you don't understand how it works?
a

anlex N

12/11/2023, 2:13 AM
I don't know how to convert it to Kotlin.
d

Daniel Pitts

12/11/2023, 2:14 AM
If you understood C++ and Kotlin, this honestly would be trivial. If you don't understand one or the other, then I can see why you're struggling.
Is this for coursework? If so, I strongly suggest talking to your teacher, a tutor, or a TA to get direct one-on-one help with understanding it.
a

anlex N

12/11/2023, 2:15 AM
google bard gives me these converted code:
// Base.kt
open class Base {
// Abstract function
open fun nameOf() {
println("Base::nameOf")
}
// Non-abstract function
fun invokingClass() {
println("Invoked by Base")
}
}
// Derived.kt
class Derived : Base() {
override fun nameOf() {
println("Derived::nameOf")
}
override fun invokingClass() {
println("Invoked by Derived")
}
}
fun main() {
val aDerived = Derived()
val pDerived = aDerived
val pBase: Base = aDerived
// Call functions
pBase.nameOf() // Output: Derived::nameOf
pBase.invokingClass() // Output: Invoked by Base
pDerived.nameOf() // Output: Derived::nameOf
pDerived.invokingClass() // Output: Invoked by Derived
}
But kotlin playground print Error:
d

Daniel Pitts

12/11/2023, 2:18 AM
Think about what override means, and what the error message is telling you. It looks almost correct. Think about what
a

anlex N

12/11/2023, 2:24 AM
how not to be final in
Base
and override invokingClass in
Derived
I do not deep understand kotlin, ok?
d

Daniel Pitts

12/11/2023, 2:26 AM
Take a little time to read over the documentation. This section in particular should help you get a deeper understanding of how it works.
a

anlex N

12/11/2023, 3:04 AM
after read hard, I can not still solve. please help me. @Daniel Pitts
d

Daniel Pitts

12/11/2023, 3:07 AM
Final clue: The code is missing an
open
, on one of the functions.
a

anlex N

12/11/2023, 3:26 AM
I have tried before, the result is wrong. Have you tried by yourself? please, @Daniel Pitts
y

Youssef Shoaib [MOD]

12/11/2023, 4:39 AM
Okay, this actually is an interesting difference between C++ and Kotlin. Basically, if you just try to do:
Copy code
open class Base {
  fun notOverrideable() { }
}
class Derived : Base {
  fun notOverrideable() { }
}
then you get an error because `Derived`'s method shadows
Base
's method without overriding it. The way to actually do that same behaviour in Kotlin is through extension methods. So hence, your example would look like:
Copy code
open class Base {
    // Abstract function
    open fun nameOf() {
        println("Base::nameOf")
    }
}
// Non-abstract function
fun Base.invokingClass() {
    println("Invoked by Base")
}

class Derived : Base() {
    override fun nameOf() {
        println("Derived::nameOf")
    }
}

fun Derived.invokingClass() {
    println("Invoked by Derived")
}

fun main() {
    val aDerived = Derived()
    val pDerived = aDerived
    val pBase: Base = aDerived
    // Call functions
    pBase.nameOf() // Output: Derived::nameOf
    pBase.invokingClass() // Output: Invoked by Base
    pDerived.nameOf() // Output: Derived::nameOf
    pDerived.invokingClass() // Output: Invoked by Derived
}
a

anlex N

12/11/2023, 8:04 AM
do you know how to convert to typescript too?
@Daniel Pitts @Youssef Shoaib [MOD] please help me. I have tried to convert to typescript, but fail.....
d

Daniel Pitts

12/11/2023, 3:09 PM
Do your own homework.
j

Jose Antonio Campillo

12/12/2023, 10:39 AM
@anlex N recently ChatGpt does not require registration by phone
a

anlex N

12/12/2023, 2:52 PM
@Jose Antonio Campillo Can you help me to try?
Now I have tried with chatgpt, but result is wrong,
d

Daniel Pitts

12/13/2023, 3:30 AM
No one here is going to do your homework for you. I strongly recommend seeking help from your teacher or tutors. Please do not
@
me again, as I no longer wish to engage with you on this topic. I hope you're able to get the help you need.