Answer to Question #184410 in C++ for Bilal

Question #184410

Comment this code line by line

include <iostream>
 
 
using namespace std;
 
 
class Line {
 
   public:
 
      void setLength( double len );
 
      double getLength( void );
 
      Line();
 
   private:
 
      double length;
 
};
 
 
 
Line::Line(void) {
 
   cout << "Object is being created" << endl;
 
}
 
void Line::setLength( double len ) {
 
   length = len;
 
}
 
double Line::getLength( void ) {
 
   return length;
 
}
 
 
 
int main() {
 
   Line line;
 
 
 
   line.setLength(6.0);
 
   cout << "Length of line : " << line.getLength() <<endl;
 
 
   return 0;
 
}
 
1
Expert's answer
2021-04-28T07:08:55-0400
#include <iostream>


using namespace std;




class Line { //declare a class named line


   public: //Set class members as public accessible from outside the class


    //Declare setLength method signature that returns void and takes a parameter of type double named len
      void setLength( double len );


      //Declare getLength method signature that returns double and takes no argument
      double getLength( void );


      Line(); //Class Line default constructor


   private: //Set class members as private accessible from outside the class


      double length; //Declare length double data member


};






Line::Line(void) { //Line class default constructor definition done outside the class and takes no arguments


   cout << "Object is being created" << endl; //Display that object is being created


}


//setLength method definition done outside the class Line
void Line::setLength( double len ) {


   length = len; //Assign class member length the len argument passed to the method


}


//getLength method definition done outside the class Line
double Line::getLength( void ) {


   return length; //Return class member length from the function


}






int main() { //main method program entry


   Line line; //Create Line type object line






   line.setLength(6.0); //Call setLength() method using line object and pass a double argument


   cout << "Length of line : " << line.getLength() <<endl; //Display length by calling getLength() method using line object




   return 0; //Return 0 to main to exit the program


}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS