4.1. Implement default constructor: 4.2. Implement copy constructor: 4.3. Implem
ID: 670676 • Letter: 4
Question
4.1. Implement default constructor:
4.2. Implement copy constructor:
4.3. Implement operator =:
4.4. Implement destructor:
4.5. Implement getLength():
4.6. Implement operator []:
4.7. Implement clear():
4.8. Implement showStructure():
#include "Text.h"
Text::Text ( const char *charSeq )
{
}
Text::Text ( const Text &other )
{
}
void Text::operator = ( const Text &other )
{
}
Text::~Text ()
{
}
int Text::getLength () const
{
return -1;
}
char Text::operator [] ( int n ) const
{
return 0;
}
void Text::clear ()
{
}
void Text::showStructure () const
{
}
Text Text::toUpper( ) const
{
Text temp;
return temp;
}
Text Text::toLower( ) const
{
Text temp;
return temp;
}
bool Text::operator == ( const Text& other ) const
{
return false;
}
bool Text::operator < ( const Text& other ) const
{
return false;
}
bool Text::operator > ( const Text& other ) const
{
return false;
}
Explanation / Answer
4.1: Implement default constructor:
The default constructor is the no-argument constructor automatically generated unless you define another constructor. It initialises any uninitialised fields to their default values. For your example, it would look like this assuming that the types are String, int and int:
This is exactly the same as
And exactly the same as having no constructors at all. However, if you define at least one constructor, the default constructor is not generated.
4.2. Implement copy constructor:
Copy constructors:
Example
Example run of this class:
>java -cp . Galaxy
M101 mass: 15.0
M101Copy mass: 25.0
M101 mass: 15.0
M101CopyTwo mass: 35.0
4.3. Implement operator =:
The copy assignment operator, often just called the "assignment operator", is a special case of assignment operator where the source (right-hand side) and destination (left-hand side) are of the same class type. It is one of the special member functions, which means that a default version of it is generated automatically by the compiler if the programmer does not declare one.
4.4. Implement destructor:
4.5. Implement getLength():
If you have an array of a known type or is a subclass of Object[] you can cast the array first.
or
However if you have no idea what type of array it is you can use Array.getLength(Object);
4.6. Implement operator []:
I have a class with a few numeric fields such as:
A simplistic implementation:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.