A. Complete the following program which display the length of a line: #include <
ID: 3550679 • Letter: A
Question
A. Complete the following program which display the length of a line:
#include <iostream>
using namespace std;
class Line
{
public:
int getLength( void );
Line( int len ); // simple constructor
Line( const Line &obj); // copy constructor
~Line(); // destructor
private:
int *ptr; // pointer
};
// Member functions definitions including constructor
Line::Line(int len)
{
cout << "Normal constructor allocating ptr" << endl;
------------------------// allocate memory for the pointer;
------------------------// complete with necessary statement ;
}
Line::Line(const Line &obj)
{
cout << "Copy constructor allocating ptr." << endl;
----------------------- // allocate memory for the pointer;
----------------------- // copy the value }
Line::~Line(void)
{
cout << "Freeing memory!" << endl;
-----------------------// complete with necessary statement ;}
int Line::getLength( void )
{
------------------// complete with necessary statement ;}
void display(Line obj)
{
cout << "Length of line : " << --------------------- <<endl; // complete
with necessary statement ;
}
// Main function for the program
int main( )
{
Line line(10);
display(line);
return 0;}
Explanation / Answer
// A. Complete the following program which display the length of a line:
#include <iostream>
using namespace std;
class Line
{
public:
int getLength( void );
Line( int len ); // simple constructor
Line( const Line &obj); // copy constructor
~Line(); // destructor
private:
int *ptr; // pointer
int size; // size of line.
};
// Member functions definitions including constructor
Line::Line(int len)
{
cout << "Normal constructor allocating ptr" << endl;
ptr = new int[len];// allocate memory for the pointer;
size = len;// complete with necessary statement ;
}
Line::Line(const Line &obj)
{
cout << "Copy constructor allocating ptr." << endl;
ptr = new int[obj.size]; // allocate memory for the pointer;
size = obj.size; // copy the value
}
Line::~Line(void)
{
cout << "Freeing memory!" << endl;
delete[] ptr;// complete with necessary statement ;
}
int Line::getLength( void )
{
return size;// complete with necessary statement ;
}
void display(Line obj)
{
cout << "Length of line : " <<obj.getLength() <<endl; // complete with necessary statement ;
}
// Main function for the program
int main( )
{
Line line(10);
display(line);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.