Question

In: Computer Science

I need solution for following question. Self-Test Exercise 18 asked you to overload the operator >>...

I need solution for following question.

Self-Test Exercise 18 asked you to overload the operator >> and the operator << for a class Percent. Complete and test this exercise. Implement the default constructor and the constructor with one int parameter. Overload the + and − operators to add and subtract percents. Also, overload the * operator to allow multiplication of a percent by an integer.

Write a program to test all the member functions and overloaded operators in your class definition.

Solutions

Expert Solution

hey the question is not clear but i'll explain what i understood:

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.In object-oriented programming, a constructor (sometimes shortened to ctor) in a class is a special type of subroutine called at the creation of an object. It prepares the new object for use, often accepting parameters which the constructor uses to set any member variables required when the object is first created. It is called a constructor because it constructs the values of data members of the class.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)

    Here the zero-argument constructor method calls the single-argument constructor method, which in turn calls the automatically provided two-argument constructor method. For reasons that will become clear very shortly, additional constructor methods declared as normal methods like this are called outer constructor methods. Outer constructor methods can only ever create a new instance by calling another constructor method, such as the automatically provided default one.

  • This class couples together a pair of values, which may be of different types (T1 and T2). The individual values can be accessed through the public members first and second.

    The class is defined as:

  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
    template <class T1, class T2> struct pair{  typedef T1 first_type;  typedef T2 second_type;  T1 first;  T2 second;  pair() : first(T1()), second(T2()) {}  pair(const T1& x, const T2& y) : first(x), second(y) {}  template <class U, class V>    pair (const pair<U,V> &p) : first(p.first), second(p.second) { }}

    first_type, second_type

    Alises of template parameters T1 and T2 respectively.

    first, second

    Data members containing the first and second values stored in the pair.

    pair()

    Constructs a pair object with each of its members first and second constructed with their respective default constructors.

    pair(const T1& x, const T2& y)

    Constructs a pair object with its members first and second initialized to x and y, respectively.

    template <class U, class V> pair (const pair<U,V> &p)

    Constructs a pair object with its members first and second initialized to the corresponding elements in p, which must be of any couple of implicitly-convertible types (including the same types).
    The header <utility> also overloads the relational operators ==, <, !=, >, >= and <= , so as to be able to compare pair objects of the same type directly:

    Two pair objects are compared equal if the first elements in both objects compare equal to each other and both second elements also compare equal to each other - they all have to match.

    In inequality comparisons (<, >), the first elements are compared first, and only if the inequality comparison is not true for them, the second elements are compared.

    Example

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    #include <iostream>#include <utility>#include <string>using namespace std;int main () {  pair <string,double> product1 ("tomatoes",3.25);  pair <string,double> product2;  pair <string,double> product3;  product2.first = "lightbulbs";     // type of first is string  product2.second = 0.99;            // type of second is double  product3 = make_pair ("shoes",20.0);  cout << "The price of " << product1.first << " is $" << product1.second << "\n";  cout << "The price of " << product2.first << " is $" << product2.second << "\n";  cout << "The price of " << product3.first << " is $" << product3.second << "\n";  return 0;}
    Output:
    The price of tomatoes is $3.25The price of lightbulbs is $0.99The price of shoes is $20
  • This class couples together a pair of values, which may be of different types (T1 and T2). The individual values can be accessed through the public members first and second.

    The class is defined as:


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    template <class T1, class T2> struct pair{ typedef T1 first_type; typedef T2 second_type; T1 first; T2 second; pair() : first(T1()), second(T2()) {} pair(const T1& x, const T2& y) : first(x), second(y) {} template <class U, class V> pair (const pair<U,V> &p) : first(p.first), second(p.second) { }}
    first_type, second_type
    Alises of template parameters T1 and T2 respectively.
    first, second
    Data members containing the first and second values stored in the pair.
    pair()
    Constructs a pair object with each of its members first and second constructed with their respective default constructors.
    pair(const T1& x, const T2& y)
    Constructs a pair object with its members first and second initialized to x and y, respectively.
    template <class U, class V> pair (const pair<U,V> &p)
    Constructs a pair object with its members first and second initialized to the corresponding elements in p, which must be of any couple of implicitly-convertible types (including the same types).
    The header <utility> also overloads the relational operators ==, <, !=, >, >= and <= , so as to be able to compare pair objects of the same type directly:

    Two pair objects are compared equal if the first elements in both objects compare equal to each other and both second elements also compare equal to each other - they all have to match.

    In inequality comparisons (<, >), the first elements are compared first, and only if the inequality comparison is not true for them, the second elements are compared.
    Example
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    #include <iostream>#include <utility>#include <string>using namespace std;int main () { pair <string,double> product1 ("tomatoes",3.25); pair <string,double> product2; pair <string,double> product3; product2.first = "lightbulbs"; // type of first is string product2.second = 0.99; // type of second is double product3 = make_pair ("shoes",20.0); cout << "The price of " << product1.first << " is $" << product1.second << "\n"; cout << "The price of " << product2.first << " is $" << product2.second << "\n"; cout << "The price of " << product3.first << " is $" << product3.second << "\n"; return 0;}
    Output:
    The price of tomatoes is $3.25The price of lightbulbs is $0.99The price of shoes is $20

  • 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.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) .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)

    Accordingly, you can add functionality to a constructor by simply defining new methods.


Related Solutions

Need a long and detailed self explanatory solution for the following question Do you think are...
Need a long and detailed self explanatory solution for the following question Do you think are there any industries that Russia potentially benefits to outweigh the risk? Explain Russia is warming more rapidly than most places in the globe. Could this be an opportunity for green technology in that region? Deforestation is a massive concern for Russia. Is there an opportunity for a company to change and adapt the resources being used, such as minimizing the need for wood? Explain...
Complete the following: Extend the newString class (attached) to include the following: Overload the operator +...
Complete the following: Extend the newString class (attached) to include the following: Overload the operator + to perform string concatenation. Overload the operator += to work as shown here: s1 = "Hello " s2 = "there" s1 += s2 // Should assign "Hello there" to s1 Add a function length to return the length of the string. Write a test program. //myString.h (header file) //Header file myString.h    #ifndef H_myString #define H_myString #include <iostream> using namespace std; class newString {...
What would you say to a colleague or a parent asked you about immune overload. "I...
What would you say to a colleague or a parent asked you about immune overload. "I only received a few shots, but my child will receive dozens of shots! How can his little body handle that?" what are the 10 major talking points related directly to immune overload here?
Please I need answer for This question and it is very important and I need solution...
Please I need answer for This question and it is very important and I need solution for this issue with all the details just nu , and help me with all the details, so that I can read and understand your answer clearly.thanks in advance/Ha 2. Answer whether the following examples of Foreign Direct Investment (FDI) describe horizontal or vertical FDI. Explain your answers. a) Volvo builds a car factory in Austin, Texas, which is similar to Volvo’s factory in...
Please I need answer for This question and it is very important and I need solution...
Please I need answer for This question and it is very important and I need solution for this issue with all the details just nu , and help me with all the details, so that I can read and understand your answer clearly.I need step by step solution to the following this question asap .I have limited time so please do it quickly with detailed explanation.thanks in advance/Ha Q. In a competitive equilibrium, the equilibrium wage clears the market and...
Using C++, identify suitable coding modules for the following (a) Overload the * operator so that...
Using C++, identify suitable coding modules for the following (a) Overload the * operator so that two instances of Quat can be multiplied using the * operator. Given that q1 and q2 are Quaternions. Let q1 = (a1, b1i, c1j, d1k) and q2 = (a2, b2i, c2j, d2k) The product (q1 * q2) is ( a1a2 – b1b2 – c1c2 – d1d2, (a1b2 + b1a2 + c1d2 – d1c2)i, (a1c2 + c1a2 + d1b2 – b1d2)j, (a1d2 + d1a2 +...
I need solution for this issue with all the details,if you can not solve this question,...
I need solution for this issue with all the details,if you can not solve this question, who should and I will send solve this question and help me with all the details? BR/Ha a) Suppose the annual interest rate is 10%. Would you prefer obtaining 1000 $ today or 1150 $ in a year from now? b) Assume that you are 18 years old and deciding whether to go to college or start working. If you work, you will earn...
I need solution for this issue with all the details,if you can not solve this question,...
I need solution for this issue with all the details,if you can not solve this question, who should and I will send solve this question and help me with all the details? BR/Ha Q.) State whether the following statements are true or false. Shortly explain your answer in 1-2 sentences. a) In the basic model of individual labor supply, if leisure is a normal good, an increase in non-labor in-come will reduce hours worked. b) In the basic model of...
I need solution for this issue with all the details,if you can not solve this question,...
I need solution for this issue with all the details,if you can not solve this question, who should and I will send solve this question and help me with all the details? BR/Ha Q. Consider a researcher interested in the causal effect of class size in primary school on educational attainments. The research strategies he is contemplating are (i) regression-control; (ii) randomization; (iii) difference-indifferences and (iv) regression-discontinuity. Explain the workings of each method along with the identification assumptions that enable...
I need step wise hand written solution with formula as I have answer of following question:-...
I need step wise hand written solution with formula as I have answer of following question:- The Provincial Bank lent $20 000 to the owner of the Purple Pelican on April 1, 2012, for commercial improvements. The loan was secured by a demand note subject to a variable rate of interest. This rate was 7% on April 1. The rate of interest was raised to 9% effective August 1 and reduced to 8% effective November 1. Partial payments, applied to the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT