anlex N
12/10/2023, 2:58 PMC++
// 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?Youssef Shoaib [MOD]
12/10/2023, 3:43 PMDaniel Pitts
12/10/2023, 4:13 PManlex N
12/11/2023, 2:06 AManlex N
12/11/2023, 2:07 AMDaniel Pitts
12/11/2023, 2:07 AManlex N
12/11/2023, 2:09 AMDaniel Pitts
12/11/2023, 2:09 AManlex N
12/11/2023, 2:09 AMDaniel Pitts
12/11/2023, 2:10 AManlex N
12/11/2023, 2:12 AMDerived::NameOf
Invoked by Base
Derived::NameOf
Invoked by Derived
Daniel Pitts
12/11/2023, 2:12 AManlex N
12/11/2023, 2:13 AMDaniel Pitts
12/11/2023, 2:14 AMDaniel Pitts
12/11/2023, 2:15 AManlex N
12/11/2023, 2:15 AM// 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
}
anlex N
12/11/2023, 2:16 AMDaniel Pitts
12/11/2023, 2:18 AManlex N
12/11/2023, 2:24 AMBase
and override invokingClass in Derived
I do not deep understand kotlin, ok?Daniel Pitts
12/11/2023, 2:26 AManlex N
12/11/2023, 3:04 AMDaniel Pitts
12/11/2023, 3:07 AMopen
, on one of the functions.anlex N
12/11/2023, 3:26 AMYoussef Shoaib [MOD]
12/11/2023, 4:39 AMopen 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:
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
}
anlex N
12/11/2023, 8:04 AManlex N
12/11/2023, 10:21 AMDaniel Pitts
12/11/2023, 3:09 PMJose Antonio Campillo
12/12/2023, 10:39 AManlex N
12/12/2023, 2:52 PManlex N
12/13/2023, 3:25 AMDaniel Pitts
12/13/2023, 3:30 AM@
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.