classes and constructors

Solutions on MaxInterview for classes and constructors by the best coders in the world

showing results for - "classes and constructors"
Nicola
07 Feb 2017
1using namespace std;
2 
3class Line {
4   public:
5      void setLength( double len );
6      double getLength( void );
7      Line();  // This is the constructor
8   private:
9      double length;
10};
11 
12// Member functions definitions including constructor
13Line::Line(void) {
14   cout << "Object is being created" << endl;
15}
16void Line::setLength( double len ) {
17   length = len;
18}
19double Line::getLength( void ) {
20   return length;
21}
22
23// Main function for the program
24int main() {
25   Line line;
26 
27   // set line length
28   line.setLength(6.0); 
29   cout << "Length of line : " << line.getLength() <<endl;
30 
31   return 0;
32}