Two ways calling o explicit add sum add23 o implicit

This preview shows page 9 - 18 out of 53 pages.

Two ways Calling: o Explicit add sum = add(2,3); o Implicit add sum(2,3) Shorthand method
Image of page 9

Subscribe to view the full document.

Parameterized Constructors In case of parameterized 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 initialized, 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) { .... }
Image of page 10
Multiple Constructors in a Class C + + permits to use more than one constructors in a single class. Add( ) ; // No arguments Add (int, int) ; // Two arguments
Image of page 11

Subscribe to view the full document.

Multiple Constructors in a Class class add { int m, n ; public : add ( ) {m = 0 ; n = 0 ;} add (int a, int b) {m = a ; n = b ;} add (add & i) {m = i.m ; n = i.n ;} }; The first constructor receives no arguments. The second constructor receives two integer arguments. The third constructor receives one add object as an argument.
Image of page 12
Multiple Constructors in a Class class add { int m, n ; public : add ( ) {m = 0 ; n = 0 ;} add (int a, int b) {m = a ; n = b ;} add (add & i) {m = i.m ; n = i.n ;} }; Add a1; Would automatically invoke the first constructor and set both m and n of a1 to zero. Add a2(10,20); Would call the second constructor which will initialize the data members m and n of a2 to 10 and 20 respectively.
Image of page 13

Subscribe to view the full document.

Multiple Constructors in a Class class add { int m, n ; public : add ( ) {m = 0 ; n = 0 ;} add (int a, int b) {m = a ; n = b ;} add (add & i) {m = i.m ; n = i.n ;} }; Add a3(a2); Would invoke the third constructor which copies the values of a2 into a3. This type of constructor is called the “copy constructor”. Construction Overloading More than one constructor function is defined in a class.
Image of page 14
Multiple Constructors in a Class class complex { float x, y ; public : complex ( ) { } complex (float a) { x = y = a ; } complex (float r, float i) { x = r ; y = i } ------ }; complex ( ) { } This contains the empty body and does not do anything. This is used to create objects without any initial values.
Image of page 15

Subscribe to view the full document.

Multiple Constructors in a Class C + + compiler has an implicit constructor which creates objects, even though it was not defined in the class. This works well as long as we do not use any other constructor in the class. However, once we define a constructor, we must also define the “do- nothing” implicit constructor.
Image of page 16
Constructors with Default Arguments It is possible to define constructors with default arguments. Consider complex (float real, float imag = 0); The default value of the argument imag is zero.
Image of page 17

Subscribe to view the full document.

Image of page 18

{[ snackBarMessage ]}

Get FREE access by uploading your study materials

Upload your study materials now and get free access to over 25 million documents.

Upload now for FREE access Or pay now for instant access
Christopher Reinemann
"Before using Course Hero my grade was at 78%. By the end of the semester my grade was at 90%. I could not have done it without all the class material I found."
— Christopher R., University of Rhode Island '15, Course Hero Intern

Ask a question for free

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern