User imput string int and float[DOUBT]
Jean Cesar via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Feb 16 19:55:30 PST 2017
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", &t);
> 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", &c);
return c;
}
More information about the Digitalmars-d-learn
mailing list