In: Computer Science
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.
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 MethodsA 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;} |
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.