January 10, 2008 Re: Translate C++ to D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Luca Lupo | Luca Lupo:
> I would known if also the file include of D have the extension.h
What do you need them for? :-)
Bye,
bearophile
|
January 10, 2008 Re: Translate C++ to D | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | Luca, bearophile's question was rhetorical. D does not require include files. They're just not needed. You only have to write the source files - and they will have an extension of .d
On 1/10/08, bearophile <bearophileHUGS@lycos.com> wrote:
> Luca Lupo:
> > I would known if also the file include of D have the extension.h
>
> What do you need them for? :-)
>
> Bye,
> bearophile
>
|
January 10, 2008 Re: Translate C++ to D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Luca Lupo | Luca Lupo wrote:
> Thanks to everybody for the help, I have converted the other files .h to D I post it now, somebody can said me if it's well?
>
> Thanks
>
> //Observer.h
>
> #include<string>
> using namespace std;
>
> class Subject;
>
> class Observer{
> public:
> virtual ~Observer();
> virtual void Update(Subject* the_change_subject) = 0;
> protected:
> Observer();
> };
>
> class Subscriber : public Observer{
> private:
> string indirizzo_e_mail;
> public:
> void Update(Subject* );
> };
>
> class SubscriberG : public Subscriber{
> private:
> string indirizzo_e_mail_G;
> public:
> SubscriberG(string );
> void Update(Subject* );
> };
>
> class SubscriberS : public Subscriber{
> private:
> string indirizzo_e_mail_S;
> string name_author;
> public:
> SubscriberS(string ,string);
> void Update(Subject* );
> };
>
>
>
>
>
> //Observer_D.h.
>
> import string;
>
>
> class Subject;
>
> class Observer{
> public:
> ~Observer();
> void Update(Subject* the_change_subject) = 0;
> protected:
> this();
> };
>
> class Subscriber : public Observer{
> private:
> string indirizzo_e_mail;
> public:
> void Update(Subject* );
> };
>
> class SubscriberG : public Subscriber{
> private:
> string indirizzo_e_mail_G;
> public:
> SubscriberG(string );
> void Update(Subject* );
> };
>
> class SubscriberS : public Subscriber{
> private:
> string indirizzo_e_mail_S;
> string name_author;
> public:
> SubscriberS(string ,string);
> void Update(Subject* );
> };
>
>
>
>
>
>
> //Post.h
>
> #include<string>
> using namespace std;
>
> class Post{
> private:
> string Title;
> string Text;
> string Author;
> public:
> Post(string ,string ,string);
> void print();
> string print_Title();
> void print_Line();
> string print_Author();
> string print_Text();
> };
>
>
>
>
>
> //Post_d.h
>
> import string;
>
>
> class Post{
> private:
> string Title;
> char *Text;
> string Author;
> public:
> Post(string ,char* ,string);
> void print();
> string print_Title();
> void print_Line();
> string print_Author();
> string print_Text();
> };
>
>
>
>
>
>
> //Subject.h
>
> #include<list>
> #include"Observer.h"
> #include"Post.h"
> using namespace std;
>
> class Subject{
> private:
>
> public:
> list<Observer*> _observer;
> virtual ~Subject();
> virtual void Attach(Observer *);
> virtual void Detach(Observer *);
> virtual void Notify(); //Per tutti gli oggetti nella lista chiama Update()
> protected:
> Subject(); //Puo essere chiamata SOLO dalle classi derivate
> };
>
> class Blog : public Subject{
> private:
> string Blog_name;
> list<Post> l_post;
> public:
> Blog(string );
> void Notify(); //Per tutti gli oggetti nella lista chiama Update()
> void NewPost(Post);
> string GetName();
> void GetText();
> void GetTitle(); //Titolo dell ultimo post
> string GetAuthor();
> void GetLine();
> };
>
>
>
>
> //Subject_d.h
>
> import std.slist;
> import Observer;
> import Post;
>
> class Subject{
> private:
> public:
> // Functions are virtual by default
> Slist!(Observer *) _observer; // is Observer*
> ~Subject();
> void Attach(Observer *); // is Observer*
> void Detach(Observer *); // is Observer*
> void Notify(); protected:
> this();
> }
>
> class Blog : public Subject{
> private:
> string Blog_name;
> Slist!(Post) l_post;
> public:
> // If no overloading by derived classes, // compiler will make non-virtual
> Blog(string );
> void Notify(); void NewPost(Post); // is Post
> string GetName();
> void GetText();
> void GetTitle(); string GetAuthor();
> void GetLine(); };
>
>
>
>
> I would known if also the file include of D have the extension.h
>
> Thanks
D don't need any kind of header files, you just put the implementation in one .d file.
All you .d files need a module declaration in the beginning of the file, "module filename", you can also but them in packages/directories, "module directory.filename"
Change all imports from "string" to "std.string"
Classes in D are by references, therefore you don't need pointers to the classes
Constructors are written as "this()" and destructors "~this()" not the class names
You don't need to write "public" when you extend a class
Why suddenly the "char *" in Post and not "string"
You can't write "void Update(Subject* the_change_subject) = 0;" the " = 0" part. My C++ knowledge is not the best but that class should maybe be an interface or at least the function should be abstract
"std.slist" is D 2 only and D 2 is not stable, if you use D 1 maybe it's enough with D's built in dynamic arrays.
|
January 10, 2008 Re: Translate C++ to D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Janice Caron | Janice Caron:
> Luca, bearophile's question was rhetorical. D does not require include files. They're just not needed. You only have to write the source files - and they will have an extension of .d
Yes, sorry, in the future I'll try to avoid such answers.
Related: the -H compiler flag generates 'header' files, thy may be useful for larger programs, but the compiler generates them for you.
Bye,
bearophile
|
January 10, 2008 Re: Translate C++ to D | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | I would like to thank all the answers, you are very available. Now I have to start converting the files. Cpp and I hope that you can continue to help. This is the first file: observer.cpp, any suggestions? //Observer.cpp #include<iostream> #include<string> #include"Subject.h" using namespace std; Observer::~Observer() {} Observer::Observer() {} SubscriberG::SubscriberG(string a){ indirizzo_e_mail_G = a; } SubscriberS::SubscriberS(string a,string b){ indirizzo_e_mail_S = a; name_author = b; } void Observer::Update(Subject* b) {} void Subscriber::Update(Subject* b) {} void SubscriberS::Update(Subject* b){ if (name_author == ((Blog *)b)->GetAuthor()){ cout<<"Subscriber tipo S->"<<"Name blog: "<<((Blog *)b)->GetName()<<endl; ((Blog *)b)->GetTitle(); ((Blog *)b)->GetText(); cout<<"Autore Ultimo Post :"<<((Blog *)b)->GetAuthor(); cout<<endl<<endl; } } void SubscriberG::Update(Subject* b){ cout<<"Subscriber tipo G->"<<"Name blog: "<<((Blog *)b)->GetName()<<endl; ((Blog *)b)->GetTitle(); ((Blog *)b)->GetLine(); cout<<"Autore Ultimo Post :"<<((Blog *)b)->GetAuthor(); cout<<endl<<endl; } //Observer.d import std.string; import Subject Observer::~this() //distructor for observer {} Observer::this() // costructor for observer {} SubscriberG::this(string a) //contructor for subscriberG {indirizzo_e_mail_G = a;} SubscriberS::this(string a,string b) //contructor for subscriberS { indirizzo_e_mail_S = a; name_author = b; } void Observer::Update(Subject* b) //it's equal? for me yes! {} void Subscriber::Update(Subject* b) //it's equal? for me yes! {} void SubscriberS::Update(Subject* b) //Help?? { if(name_author == ((Blog *)b)->GetAuthor()){ cout<<"Subscriber tipo S->"<<"Name blog: "<<((Blog *)b)->GetName()<<endl; ((Blog *)b)->GetTitle(); ((Blog *)b)->GetText(); cout<<"Autore Ultimo Post :"<<((Blog *)b)->GetAuthor(); } cout<<endl<<endl; } void SubscriberG::Update(Subject* b) //Help?? { cout<<"Subscriber tipo G->"<<"Name blog: "<<((Blog *)b)->GetName()<<endl; ((Blog *)b)->GetTitle(); ((Blog *)b)->GetLine(); cout<<"Autore Ultimo Post :"<<((Blog *)b)->GetAuthor(); cout<<endl<<endl; } |
January 10, 2008 Re: Translate C++ to D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Luca Lupo | Luca Lupo wrote: > I would like to thank all the answers, you are very available. Now I have to start converting the files. Cpp and I hope that you can continue to help. > > This is the first file: observer.cpp, any suggestions? > > //Observer.cpp > > #include<iostream> > #include<string> #include"Subject.h" using namespace std; > > Observer::~Observer() > {} > > Observer::Observer() > {} > > SubscriberG::SubscriberG(string a){ > indirizzo_e_mail_G = a; > } > > SubscriberS::SubscriberS(string a,string b){ > indirizzo_e_mail_S = a; > name_author = b; > } > > > void Observer::Update(Subject* b) > {} > > void Subscriber::Update(Subject* b) > {} > > void SubscriberS::Update(Subject* b){ > if (name_author == ((Blog *)b)->GetAuthor()){ > cout<<"Subscriber tipo S->"<<"Name blog: "<<((Blog *)b)->GetName()<<endl; > ((Blog *)b)->GetTitle(); > ((Blog *)b)->GetText(); > cout<<"Autore Ultimo Post :"<<((Blog *)b)->GetAuthor(); > cout<<endl<<endl; > } > } > > void SubscriberG::Update(Subject* b){ > cout<<"Subscriber tipo G->"<<"Name blog: "<<((Blog *)b)->GetName()<<endl; > ((Blog *)b)->GetTitle(); > ((Blog *)b)->GetLine(); > cout<<"Autore Ultimo Post :"<<((Blog *)b)->GetAuthor(); > cout<<endl<<endl; > } > > > > //Observer.d > > import std.string; > import Subject > > Observer::~this() //distructor for observer > {} > > Observer::this() // costructor for observer > {} > > SubscriberG::this(string a) //contructor for subscriberG > {indirizzo_e_mail_G = a;} > > SubscriberS::this(string a,string b) //contructor for subscriberS > { > indirizzo_e_mail_S = a; > name_author = b; > } > > > void Observer::Update(Subject* b) //it's equal? for me yes! > {} > > void Subscriber::Update(Subject* b) //it's equal? for me yes! > {} > > void SubscriberS::Update(Subject* b) //Help?? > { > if(name_author == ((Blog *)b)->GetAuthor()){ > cout<<"Subscriber tipo S->"<<"Name blog: "<<((Blog *)b)->GetName()<<endl; > ((Blog *)b)->GetTitle(); > ((Blog *)b)->GetText(); > cout<<"Autore Ultimo Post :"<<((Blog *)b)->GetAuthor(); > } > cout<<endl<<endl; > } > > void SubscriberG::Update(Subject* b) //Help?? > { > cout<<"Subscriber tipo G->"<<"Name blog: "<<((Blog *)b)->GetName()<<endl; > ((Blog *)b)->GetTitle(); > ((Blog *)b)->GetLine(); > cout<<"Autore Ultimo Post :"<<((Blog *)b)->GetAuthor(); > cout<<endl<<endl; > } > I think the best would be if read through the language specification (http://www.digitalmars.com/d/1.0/lex.html) and tried to port files own your own and then ask if it's something you don't understand or need help with. Some corrections and tips: Drop all the name spaces All member functions/methods must be in the class not as free functions. Think of it as you write a class declaration in a header file but you also add the implementation. Classes are by reference in D so drop all the pointers ":: -> ." are all written as "." in D If any class has a pure virtual function make it an abstract function (put the keyword "abstract" in front of the function and drop "=0") or if all functions are pure functions then make the class an interface instead(replace the "class" keyword with the "interface" keyword) Don't put a semicolon after a class Tips: put every class in it's own module/file |
January 11, 2008 Re: Translate C++ to D | ||||
---|---|---|---|---|
| ||||
Posted in reply to doob | So, I have converted the -> with the . , but I must make this also for: example: Observer::~this(){} ??? :: no, but .?? And Exit the cout in D?? or I must import some library?? Thanks //Observer.d import std.string; import Subject Observer::~this() //distructor for observer {} Observer::this() // costructor for observer {} SubscriberG::this(string a) //contructor for subscriberG {indirizzo_e_mail_G = a;} SubscriberS::this(string a,string b) //contructor for subscriberS { indirizzo_e_mail_S = a; name_author = b; } void Observer::Update(Subject* b) //it's equal? for me yes! {} void Subscriber::Update(Subject* b) //it's equal? for me yes! {} void SubscriberS::Update(Subject* b) //Help?? { if(name_author == ((Blog *)b).GetAuthor()){ cout<<"Subscriber tipo S->"<<"Name blog: "<<((Blog *)b).GetName()<<endl; ((Blog *)b).GetTitle(); ((Blog *)b).GetText(); cout<<"Autore Ultimo Post :"<<((Blog *)b).GetAuthor(); } cout<<endl<<endl; } void SubscriberG::Update(Subject* b) //Help?? { cout<<"Subscriber tipo G->"<<"Name blog: "<<((Blog *)b).GetName()<<endl; ((Blog *)b).GetTitle(); ((Blog *)b).GetLine(); cout<<"Autore Ultimo Post :"<<((Blog *)b).GetAuthor(); cout<<endl<<endl; } |
January 11, 2008 Re: Translate C++ to D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Luca Lupo | Luca Lupo wrote: > So, I have converted the -> with the . , but I must make this also for: > example: > > Observer::~this(){} ??? :: no, but .?? > > And Exit the cout in D?? or I must import some library?? > > Thanks You don't write classname::function, you put all the functions in the class like this: class Observer { this() { // do something } foo (int i) { // do something else } } But if you want to access a static function you replace the two colons with a dot like: Observer.staticFunction "And Exit the cout in D?? or I must import some library??" what do you mean? You write to the standard output in D like this: import std.stdio; void main () { writefln("Hello D World"); // adds a new line } Again, look at the language specification http://www.digitalmars.com/d/1.0/lex.html and also the phobos (the standard library) link |
January 11, 2008 Re: Translate C++ to D | ||||
---|---|---|---|---|
| ||||
Posted in reply to doob | Thanks, So in this way? //Observer.d import std.stdio; import std.string; import Subject class Observer{ ~this(){} //distructor for observer this(){} // costructor for observer Update(Subject* b){} } class Subscriber{ Update(Subject* b); } class SubscriberG{ this (string a){ indirizzo_e_mail_G = a; } Update (Subject* b){ cout<<"Subscriber tipo G->"<<"Name blog: "<<((Blog *)b).GetName()<<endl; ((Blog *)b).GetTitle(); ((Blog *)b).GetLine(); cout<<"Autore Ultimo Post :"<<((Blog *)b).GetAuthor(); cout<<endl<<endl; } } class SubscriberS{ this (string a, string b){ indirizzo_e_mail_S = a; name_author = b; } Update (Subject* b){ if (name_author == ((Blog *)b).GetAuthor()){ cout<<"Subscriber tipo S->"<<"Name blog: "<<((Blog *)b).GetName()<<endl; ((Blog *)b).GetTitle(); ((Blog *)b).GetText(); cout<<"Autore Ultimo Post :"<<((Blog *)b).GetAuthor(); } cout<<endl<<endl; } } but is possibile import the standard library of cpp so I can use cout? Thanks |
Copyright © 1999-2021 by the D Language Foundation