Converting multiple inheritance code into C ++ for D language
Nicholas Wilson via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Feb 17 15:24:57 PST 2017
On Friday, 17 February 2017 at 23:11:25 UTC, Jean Cesar wrote:
> import std.stdio;
> import std.string;
>
> I've been reading a bit about multi-inheritance in D, but I
> have to use interface like C # to use multiple inheritance, but
> I have the code in C ++ that I've been testing to understand
> how it would be possible to implement multi-inheritance
> constructor despite seemingly It does not represent many things
> so I changed the code to use interface but how would I do so I
> could use the constructor in the same way as such a C ++ code?
>
> Test1,2,3 would be the name of the class constructors in C ++
> how to port completely to D so that it works the same way?
>
> import std.stdio;
> import std.string;
>
> class Test1
> {
> protected:
> std::string _msg1;
> public:
> Test1( std::string msg1 ):
> _msg1( msg1 ){}
> };
>
> class Test2
> {
> protected:
> std::string _msg2;
> public:
> Test2( std::string msg2 ):
> _msg2( msg2 ){}
> };
>
> class Test3
> {
> protected:
> std::string _msg3;
> public:
> Test3( std::string msg3 ):
> _msg3( msg3 ){}
> };
>
> class Test4: public Test1, public Test2, public Test3
> {
> std::string _msg4;
> public:
> Test4( std::string msg1, std::string msg2 , std::string msg3,
> std::string msg4 ):
> Test1( msg1 ), Test2( msg2 ), Test3( msg3 ), _msg4( msg4 ){ }
> void show();
> };
>
> void Test4::show()
> {
> std::cout << this->_msg1 << this->_msg2 << this->_msg3 <<
> this->_msg4 << "\n\n";
> }
>
> int main()
> {
> Test4 teste("\n\tTeste1 ","Teste2 ","Teste3 ","Teste4");
> teste.show();
> return 0;
> }
Like in c#, classes on D are reference types and all methods are
virtual unless marked final. Also D only allows single
inheritance for data members, but you can multiplly inherit
methods from interfaces (think abstract classes).
Something like this would be a goods use for struct multiple
alias this, except that we haven't implemented that yet
unfortunately.
More information about the Digitalmars-d-learn
mailing list