In case of paramterized constructor, you can use following syntax to initialize the fields:Line::Line( double len): length(len){ cout << "Object is being created, length = " << len << endl;}Above syntax is equal to the following syntax:Line::Line( double len){ cout << "Object is being created, length = " << len << endl; length = len;}If for a class C, you have multiple fields X, Y, Z etc to be initialize then use can use same syntax and separate the fields by comma as follows:C::C( double a, double b, double c): X(a), Y(b), Z(c){ ....}
C++
Topic: Constructors and destructors
What are using Initialization Lists to Initialize Fields ?
Browse random answers:
What are Class Constructors and destructors in C++ ?
What is the use of Constructor ?
What is the use of Destructors
What are Constructor and Destructor Order ?
What is virtual Destructors ?
Explain about overview of constructors and destructors (C++ only) ?
What are the restrictions apply to constructors and destructors?
What is virtual constructors/destructors?
What is a default constructor?
What is a conversion constructor?
Difference between a copy constructor and an assignment operator.
What is copy constructor?
How should a constructor handle a failure?
What are shallow and deep copy?
What are the restrictions apply to constructors and destructors?
Explain the order in which constructors are called when an object of a derived class is created.
Explain about parameterized Constructor ?
What are using Initialization Lists to Initialize Fields ?
what’s a virtual destructor and when is it needed?
Explain about overloading Constructors ?
Explain interfaces in C++ (Abstract Classes) ?
Explain about Designing Strategy ?
What are types of Constructor ?