Re: User imput string int and float[DOUBT]
On Thursday, 16 February 2017 at 22:44:58 UTC, Ali Çehreli wrote: On 02/16/2017 02:05 PM, Jean Cesar wrote: > So I used get methods and sets only as initial pattern to netender the > functioning of the language in relation to some concepts of the same Makes sense... > how to leave a very small code with the largest number of > Possible functionality type I think D is very suitable for that. > I still do not know very well or use > constructors in C ++ Understandable: Many coding guidelines eschew doing non-trivial work in constructors. They require a member function like obj.initialize(/* ... */) to be called in order to get a functioning object. > but I have very high potential in a code with > multiple inheritance That's going to be a problem because D does not allow multiple inheritance. > I think of compilers in the case of the code that > favors me in reading so I would do something like: > > void main () > { >minhaclasse c = new minhaclasse(string text); >minhaclasse d = new minhaclasse(int number); > > write("Enter your name: ") > c.set(); So, your minhaclasse is basically ValorLegível (ReadableValue), which would not scale because likely it's also writable and movable, etc. And that explains why you're looking for multiple inheritance. :) // NOT valid D (and no, I don't speak Portuguese) class MinhaValor : ValorLegível, ValorEscrita, ValorMóvel /*, ... */ { // ... } > void main () > { > string txt; >Int num; > write("Enter your name: ") > minhaclasse(text).set(); > > write("Enter your age: ") > minhaclasse(num).set(); > > writeln >( > "\n\tString:", minhaclasse(text).print() ;, > "\n\tInt:", minhaclasse(num).print(); >); > } > > I think of object orientation this way to avoid getting rewritten many > things so I would only define what the set or get would return by > initializing the constructor only but I have no idea how to do that .. You make it sound as if OOP is for code reuse or for reducing code repetition. I think regular functions provide that already. Unless polymorphism is really beneficial, functional style is preferable. Additionally, D has this very useful universal function call syntax (UFCS), which makes your use case easy to implement, and which my earlier code could have benefited from as well. import std.stdio; import std.traits; auto read(T)(ref T t, string message) if (!isSomeString!T) { writef("%s: ", message); readf(" %s", ); return t; } auto read(S)(ref S s, string message) if (isSomeString!S) { import std.string : strip; writef("%s: ", message); s = readln().strip(); return s; } class person { private: string name, address; int age; float height; public: static person fromConsole() { auto p = new person(); /* UFCS in action: Note how these are not written as *read(p.name, /* ... */) */ p.name.read("Enter Your Name"); p.age.read("Enter Your Age"); p.height.read("Enter Your Height"); return p; } float getHeight() { return height; } int getIty() { return age; } string getNome() { return name; } } void main () { person p = person.fromConsole(); writeln(p.getNome()); writeln(p.getIty()); writeln(p.getHeight()); } > My goal in learning to use languages like Java, C ++, D is with the > intention of learning the best way to reuse code and orienation to > objects and also development cross-platform codes that will run in > standard ansi for, Unix, Linux, Windows, android etc. .. Ali I tried to define a method to read vectors of chars but this is giving error auto read(C)(ref C c, char[] message) if (!isSomeChar!C) { writef("\n\t%s: ", message); read(" %s", ); return c; }
Re: User imput string int and float[DOUBT]
On 02/16/2017 02:05 PM, Jean Cesar wrote: > So I used get methods and sets only as initial pattern to netender the > functioning of the language in relation to some concepts of the same Makes sense... > how to leave a very small code with the largest number of > Possible functionality type I think D is very suitable for that. > I still do not know very well or use > constructors in C ++ Understandable: Many coding guidelines eschew doing non-trivial work in constructors. They require a member function like obj.initialize(/* ... */) to be called in order to get a functioning object. > but I have very high potential in a code with > multiple inheritance That's going to be a problem because D does not allow multiple inheritance. > I think of compilers in the case of the code that > favors me in reading so I would do something like: > > void main () > { >minhaclasse c = new minhaclasse(string text); >minhaclasse d = new minhaclasse(int number); > > write("Enter your name: ") > c.set(); So, your minhaclasse is basically ValorLegível (ReadableValue), which would not scale because likely it's also writable and movable, etc. And that explains why you're looking for multiple inheritance. :) // NOT valid D (and no, I don't speak Portuguese) class MinhaValor : ValorLegível, ValorEscrita, ValorMóvel /*, ... */ { // ... } > void main () > { > string txt; >Int num; > write("Enter your name: ") > minhaclasse(text).set(); > > write("Enter your age: ") > minhaclasse(num).set(); > > writeln >( > "\n\tString:", minhaclasse(text).print() ;, > "\n\tInt:", minhaclasse(num).print(); >); > } > > I think of object orientation this way to avoid getting rewritten many > things so I would only define what the set or get would return by > initializing the constructor only but I have no idea how to do that .. You make it sound as if OOP is for code reuse or for reducing code repetition. I think regular functions provide that already. Unless polymorphism is really beneficial, functional style is preferable. Additionally, D has this very useful universal function call syntax (UFCS), which makes your use case easy to implement, and which my earlier code could have benefited from as well. import std.stdio; import std.traits; auto read(T)(ref T t, string message) if (!isSomeString!T) { writef("%s: ", message); readf(" %s", ); return t; } auto read(S)(ref S s, string message) if (isSomeString!S) { import std.string : strip; writef("%s: ", message); s = readln().strip(); return s; } class person { private: string name, address; int age; float height; public: static person fromConsole() { auto p = new person(); /* UFCS in action: Note how these are not written as *read(p.name, /* ... */) */ p.name.read("Enter Your Name"); p.age.read("Enter Your Age"); p.height.read("Enter Your Height"); return p; } float getHeight() { return height; } int getIty() { return age; } string getNome() { return name; } } void main () { person p = person.fromConsole(); writeln(p.getNome()); writeln(p.getIty()); writeln(p.getHeight()); } > My goal in learning to use languages like Java, C ++, D is with the > intention of learning the best way to reuse code and orienation to > objects and also development cross-platform codes that will run in > standard ansi for, Unix, Linux, Windows, android etc. .. Ali
Re: User imput string int and float[DOUBT]
On Thursday, 16 February 2017 at 02:17:49 UTC, Ali Çehreli wrote: On 02/15/2017 05:49 PM, Jean Cesar wrote: > So I'm a beginner in this language and have very little time I started > I'm interested in apprehending concepts of object orientation > polymorphism inheritance, multiple inheritance as in c ++ D is similar to C++ but also very different. > but I did not > understand how to use constructor in it > Because I simply did. > > Class person > { >person(){} >~ Person () {} > } > > And error ... In D, constructor is always called this(): class Person { this(){} ~this() {} } void main() { auto p = new Person(); } Ali So I used get methods and sets only as initial pattern to netender the functioning of the language in relation to some concepts of the same I intend to learn it not because it is a new language, but I want to understand how to leave a very small code with the largest number of Possible functionality type I still do not know very well or use constructors in C ++ but I have very high potential in a code with multiple inheritance, I think of compilers in the case of the code that favors me in reading so I would do something like: void main () { minhaclasse c = new minhaclasse(string text); minhaclasse d = new minhaclasse(int number); write("Enter your name: ") c.set(); write("Enter your age: ") d.set(); /* the set method would already fetch user i imput by mistake for the information automatically */ Writeln ( "\n\tString:", c.get (), "\n\tInt:", d.get () ); } Or something like: void main () { string txt; Int num; write("Enter your name: ") minhaclasse(text).set(); write("Enter your age: ") minhaclasse(num).set(); writeln ( "\n\tString:", minhaclasse(text).print() ;, "\n\tInt:", minhaclasse(num).print(); ); } I think of object orientation this way to avoid getting rewritten many things so I would only define what the set or get would return by initializing the constructor only but I have no idea how to do that .. My goal in learning to use languages like Java, C ++, D is with the intention of learning the best way to reuse code and orienation to objects and also development cross-platform codes that will run in standard ansi for, Unix, Linux, Windows, android etc. ..
Re: User imput string int and float[DOUBT]
On 02/15/2017 05:49 PM, Jean Cesar wrote: > So I'm a beginner in this language and have very little time I started > I'm interested in apprehending concepts of object orientation > polymorphism inheritance, multiple inheritance as in c ++ D is similar to C++ but also very different. > but I did not > understand how to use constructor in it > Because I simply did. > > Class person > { >person(){} >~ Person () {} > } > > And error ... In D, constructor is always called this(): class Person { this(){} ~this() {} } void main() { auto p = new Person(); } Ali
Re: User imput string int and float[DOUBT]
On Wednesday, 15 February 2017 at 23:40:41 UTC, Ali Çehreli wrote: On 02/15/2017 03:20 PM, Jean Cesar wrote: How do I make a class person where I use set and get methods to imput the user type: I have some information here: http://ddili.org/ders/d.en/input.html You should also know how to read strings: http://ddili.org/ders/d.en/strings.html And this section about refactoring has the concept of a readInt() function template: http://ddili.org/ders/d.en/functions.html#ix_functions.refactor Combining all three: import std.stdio; import std.traits; auto read(T)(ref T t, string message) if (!isSomeString!T) { writef("%s: ", message); readf(" %s", ); return t; } auto read(S)(ref S s, string message) if (isSomeString!S) { import std.string : strip; writef("%s: ", message); s = readln().strip(); return s; } class person { private: string name, address; int age; float height; public: void setNome() { read(name, "Enter Your Name"); } void setIty() { read(age, "Enter Your Age"); } void setHeight() { read(height, "Enter Your Height"); } float getHeight() { return height; } int getIty() { return age; } string getNome() { return name; } } void main () { person p = new person(); p.setNome(); p.setIty(); p.setHeight(); writeln(p.getNome()); writeln(p.getIty()); writeln(p.getHeight()); } Unrelated, a bunch of get/set methods is commonly seen as inferior to a design where another piece of code does the reading and makes the object after the fact: person readPerson(File input) { // ... parse the input ... // Potentially, use the constructor: auto p = new person(name, age, /* ... */); return p; } One reason is the fact that the person may be seen as incomplete and unusable unless all fields are set. Again, it's beside the point... :) Ali So I'm a beginner in this language and have very little time I started I'm interested in apprehending concepts of object orientation polymorphism inheritance, multiple inheritance as in c ++, but I did not understand how to use constructor in it Because I simply did. Class person { person(){} ~ Person () {} } And error ...
Re: User imput string int and float[DOUBT]
On 02/15/2017 03:20 PM, Jean Cesar wrote: How do I make a class person where I use set and get methods to imput the user type: I have some information here: http://ddili.org/ders/d.en/input.html You should also know how to read strings: http://ddili.org/ders/d.en/strings.html And this section about refactoring has the concept of a readInt() function template: http://ddili.org/ders/d.en/functions.html#ix_functions.refactor Combining all three: import std.stdio; import std.traits; auto read(T)(ref T t, string message) if (!isSomeString!T) { writef("%s: ", message); readf(" %s", ); return t; } auto read(S)(ref S s, string message) if (isSomeString!S) { import std.string : strip; writef("%s: ", message); s = readln().strip(); return s; } class person { private: string name, address; int age; float height; public: void setNome() { read(name, "Enter Your Name"); } void setIty() { read(age, "Enter Your Age"); } void setHeight() { read(height, "Enter Your Height"); } float getHeight() { return height; } int getIty() { return age; } string getNome() { return name; } } void main () { person p = new person(); p.setNome(); p.setIty(); p.setHeight(); writeln(p.getNome()); writeln(p.getIty()); writeln(p.getHeight()); } Unrelated, a bunch of get/set methods is commonly seen as inferior to a design where another piece of code does the reading and makes the object after the fact: person readPerson(File input) { // ... parse the input ... // Potentially, use the constructor: auto p = new person(name, age, /* ... */); return p; } One reason is the fact that the person may be seen as incomplete and unusable unless all fields are set. Again, it's beside the point... :) Ali
User imput string int and float[DOUBT]
How do I make a class person where I use set and get methods to imput the user type: Import std.stdio; class person { private: string name, address; int age; float height; public: void setNome() { write("Enter Your Name:"); // the problem is here how am I going to read the imput of a string typed by the user? } void setIty() { write("Enter Your Age:"); // Another problem here also to read integer values like I would? } void setHeight() { write("Enter Your Height:"); // Another problem here also to read floats or double values like I would? } float getHeight() { return height; } int getIty() { return age; } string getNome() { return name; } } void main () { person p = new person(); p.setName(); p.setIdade(); p.setHeight(); p.getName(); p.getIdade(); p.getHeight(); }
Re: Int to float?
Ola Fosheim Grøstad: D claims to follow C, so using unions for type punning is ultimately implementation defined. I am not sure if D is the same as C regarding this. Bye, bearophile
Re: Int to float?
On Friday, 6 March 2015 at 00:57:16 UTC, Ola Fosheim Grøstad wrote: On Thursday, 5 March 2015 at 23:50:28 UTC, Jesse Phillips wrote: I think I read somewhere you don't want to use unions like this, but I think it is more because you generally don't want to reinterpret bits. It is non-portable, since some hardware architectures may use different representations (e.g. different byte order on int and float). Then maybe use std.bitmanip? import std.bitmanip; int i = 5; float f = bigEndianToNative!float(nativeToBigEndian(i)); // or float f = littleEndianToNative!float(nativeToLittleEndian(i));
Re: Int to float?
On Thursday, 5 March 2015 at 20:32:20 UTC, anonymous wrote: That's not really simpler, though. Maybe, but I think the union is a bit nicer because then the compiler is responsible for more of the details. For example, it should work with class objects without the complication of dealing with the fact that they are already pointers under the hood. Either way works though and should compile to the same instructions, just if I was doing it as a generic library, I think I'd use the union method.
Re: Int to float?
On Thursday, 5 March 2015 at 20:03:09 UTC, Benjamin Thaut wrote: int someValue = 5; float sameBinary = *(cast(float*)cast(void*)someValue); The cast(void*) isn't necessary.
Re: Int to float?
On Thursday, 5 March 2015 at 20:03:09 UTC, Benjamin Thaut wrote: Am 05.03.2015 um 21:00 schrieb Taylor Hillegeist: How to I cast a Int to float without changing its binary representation? int someValue = 5; float sameBinary = *(cast(float*)cast(void*)someValue); ahh of course! lol :)
Re: Int to float?
On Thursday, 5 March 2015 at 20:16:55 UTC, anonymous wrote: On Thursday, 5 March 2015 at 20:03:09 UTC, Benjamin Thaut wrote: int someValue = 5; float sameBinary = *(cast(float*)cast(void*)someValue); The cast(void*) isn't necessary. Actually even the cast is unecessary, just use a uniform. union N { int i; float f; } http://dpaste.dzfl.pl/58b6eddcf725
Int to float?
How to I cast a Int to float without changing its binary representation?
Re: Int to float?
Am 05.03.2015 um 21:00 schrieb Taylor Hillegeist: How to I cast a Int to float without changing its binary representation? int someValue = 5; float sameBinary = *(cast(float*)cast(void*)someValue);
Re: Int to float?
On Thursday, 5 March 2015 at 20:21:18 UTC, badlink wrote: On Thursday, 5 March 2015 at 20:16:55 UTC, anonymous wrote: On Thursday, 5 March 2015 at 20:03:09 UTC, Benjamin Thaut wrote: int someValue = 5; float sameBinary = *(cast(float*)cast(void*)someValue); The cast(void*) isn't necessary. Actually even the cast is unecessary, just use a uniform. union N { int i; float f; } http://dpaste.dzfl.pl/58b6eddcf725 That's not really simpler, though.
Re: Int to float?
On Thursday, 5 March 2015 at 20:06:55 UTC, Taylor Hillegeist wrote: On Thursday, 5 March 2015 at 20:03:09 UTC, Benjamin Thaut wrote: Am 05.03.2015 um 21:00 schrieb Taylor Hillegeist: How to I cast a Int to float without changing its binary representation? int someValue = 5; float sameBinary = *(cast(float*)cast(void*)someValue); ahh of course! lol :) I think I read somewhere you don't want to use unions like this, but I think it is more because you generally don't want to reinterpret bits. import std.stdio; void main() { union Fi { float f; int i; } Fi fi; fi.i = 65; writeln(fi.f); }
Re: Int to float?
On Thursday, 5 March 2015 at 23:50:28 UTC, Jesse Phillips wrote: I think I read somewhere you don't want to use unions like this, but I think it is more because you generally don't want to reinterpret bits. It is non-portable, since some hardware architectures may use different representations (e.g. different byte order on int and float). D claims to follow C, so using unions for type punning is ultimately implementation defined. In C++ using unions for type punning is illegal/undefined behaviour, so in C++ you should use memcpy. Memcpy also has the advantage of explicitly copying thus avoiding some aliasing issues.