```C++ // deriv_VirtualFunctions2.cpp // compile w...
# getting-started
a
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
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
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
1. I am summarizing the similarities to C++ and Kotlin
2. I have tried to convert it myself, but result is wrong
d
How was it wrong?
a
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
If you need ChatGPT to do this right, you're definitely not qualified to summarize the similarities between C++ and Kotlin.
a
4. I use google bard and other AI, the result is wrong.
d
Do you understand the C++ code?
a
I know, my C++ code result is:
Derived::NameOf
Invoked by Base
Derived::NameOf
Invoked by Derived
d
So you don't understand how it works?
a
I don't know how to convert it to Kotlin.
d
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
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
Think about what override means, and what the error message is telling you. It looks almost correct. Think about what
a
how not to be final in
Base
and override invokingClass in
Derived
I do not deep understand kotlin, ok?
d
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
after read hard, I can not still solve. please help me. @Daniel Pitts
d
Final clue: The code is missing an
open
, on one of the functions.
a
I have tried before, the result is wrong. Have you tried by yourself? please, @Daniel Pitts
y
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
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
Do your own homework.
j
@anlex N recently ChatGpt does not require registration by phone
a
@Jose Antonio Campillo Can you help me to try?
Now I have tried with chatgpt, but result is wrong,
d
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.