Self-test exercise 17 asked you to overload the operator >> and the operator <<
ID: 3584052 • Letter: S
Question
Self-test exercise 17 asked you to overload the operator >> and the operator << for a class pairs. Complete and test this exercise. Implement the default constructor and the constructors with one and two int parameters. The one parameter constructor should initialize the first member of the pair; the second member of the pair is to be 0. Overload binary operator to add pairs according to the rule (a, b) (c, d) = (a c, b d) Overload operator- analogously. Overload operator* on pairs and int according to the rule (a, b)*c= (a*c, b*c) Write a program to test all the member functions and overloaded operators in your class definition. I Have no idea how to do this.Explanation / Answer
A constructor resembles an instance method, but it differs from a method in that it never has an explicit return-type, it is not inherited (though many languages provide access to the superclass's constructor, for example through the super keyword in Java), and it usually has different rules for scope modifiers. Constructors are often distinguished by having the same name as the declaring class. They have the task of initializing the object's data members and of establishing the invariant of the class, failing if the invariant is invalid. A properly written constructor will leave the object in a valid state. Immutable objects must be initialized in a constructor.
Programmers can also use the term constructor to denote one of the tags that wraps data in an algebraic data type. This is a different usage than in this article.[dubious – discuss]
Most languages allow overloading the constructor in that there can be more than one constructor for a class, each having different parameters. Some languages take consideration of some special types of constructors.
Parameterized constructors
Constructors that can take arguments are termed as parameterized constructors. The number of arguments can be greater or equal to one(1). For example:
class example{ int p, q; public: example(int a, int b); //parameterized constructor};example :: example(int a, int b){ p = a; q = b;}
When an object is declared in a parameterized constructor, the initial values have to be passed as arguments to the constructor function. The normal way of object declaration may not work. The constructors can be called explicitly or implicitly.The method of calling the constructor implicitly is also called the shorthand method.
example e = example(0, 50); //explicit call example e(0, 50); //implicit call
Default constructors
If the programmer does not supply a constructor for an instantiable class, a typical compiler will provide a default constructor. The behavior of the default constructor is language dependent. It may initialize data members to zero or other same values, or it may do nothing at all.
Outer Constructor Methods
A constructor is just like any other function in Julia in that its overall behavior is defined by the combined behavior of its methods. Accordingly, you can add functionality to a constructor by simply defining new methods. For example, let’s say you want to add a constructor method for Foo objects that takes only one argument and uses the given value for both the bar and baz fields. This is simple:
Foo(x) = Foo(x,x)julia> Foo(1)Foo(1,1)
You could also add a zero-argument Foo constructor method that supplies default values for both of the bar and baz fields:
Foo() = Foo(0)julia> Foo()Foo(0,0)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.