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;
}
#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
}
Comments
Leave a comment