We are learning about parameterized constructor in C++
What is Parameterized Constructor ?
In Object oriented programming constructor is a special type of function . but what is Parameterized constructor ? Parameterized , Constructor that can take at least one argument are termed as Parameterized constructor .
When an object is declared in a Parameterized constructor , the initial value have to be passed as the argument to the constructor function . The normal way of object deceleration may not be worked . The constructor is also invoked implicit and explicit .
Some Important characteristic of constructor are -
- Constructor is automatically called when it's object is created .
- Constructor does not have return type .
- Constructor has same name type itself .
Examples :-
class Line { public: void setLength( double len ); double getLength( void ); Line(double len); // This is the constructor private: double length; }; // Member functions definitions including constructor Line::Line( double len) { cout << "Object is being created, length = " << len << endl; length = len; } void Line::setLength( double len ) { length = len; } double Line::getLength( void ) { return length; } // Main function for the program void main( ) { Line line(10.0); // get initially set length. cout << "Length of line : " << line.getLength() <<endl; // set line length again line.setLength(6.0); cout << "Length of line : " << line.getLength() <<endl; getch(); }
Output :
Object is being created, length = 10
Length of line : 10
Length of line : 6
RELATED ARTICLES :
- Constructor in C++
- Inline function in C++
- Encapsulation in C++
- Data abstraction in C++
- Object and Classes in C++
Please ! write comment if you find anything incorrect in this blog .
This article is contributed by Mohd Abuzar . If you like code with abuzar or want to contribute with us so please mail at - abuzar.abuzar.mohd325@gmail.com
0 Comments
Do not Comment and any spam text