Download presentation
Presentation is loading. Please wait.
Published byCarol Henry Modified over 9 years ago
1
CSC 143F 1 CSC 143 Constructors Revisited
2
CSC 143F 2 Constructors In C++, the constructor is a special function automatically called when a class instance is declared Constructor’s name is class name No explicit return type, not even void...
3
CSC 143F 3 Multiple Constructors May be several reasonable ways to initialize a class instance Multiple constructors All have same name (name of class) Distinguished by number and types of arguments Example of “overloading” (more later)
4
CSC 143F 4 Multiple Constructors (review) //complex.h--simple complex number class class Complex { public: // constructors: Complex( );// = 0+0i Complex(double a, double b); // = a+bi // double -> Complex constructor Complex(double r);// = r+0i... };
5
CSC 143F 5 Default parameters //complex.h--simple complex number class class Complex { public: // combine all previous constructors // in one single constructor // Complex( );// = 0+0i // Complex(double r);// = r+0i Complex(double a=0, double b=0); // Complex() means Complex(0,0) // Complex(3.2) means Complex(3.2,0) };
6
CSC 143F 6 Default Constructor If no explicit constructor is given, a default is supplied by compiler Takes no arguments, does nothing Not guaranteed to perform any initialization Invisible If a class has one or more “non-default” constructor: then NO “default” constructor will be supplied; variable declaration without initialization fails.
7
CSC 143F 7 Guidelines for Constructors A constructor cannot return a value so it must be declared without a return type A class may provide multiple constructors Compiler will choose appropriate one, depending on context. Syntax for invoking a constructor Complex a1; Complex a3 = Complex(123.45); Complex a2(10.0, -1); But not this: Complex a4();
8
CSC 143F 8 CSC 143 Overloading [Chapter 8, pp. 377-381]
9
CSC 143F 9 Name Scope and Visibility Variable and function names may be repeated in different scopes int myVar; void Snork(int myVar) {... while ( a > b){ int myVar = 0;... } } Style issue: using same name in multiple scopes can be confusing
10
CSC 143F 10 Overloading Reusing names Function names can be reused Differentiate based on scope and context Global vs. class vs. local, etc. Different functions in same scope can have same name if argument number and/or types different Name is said to be overloaded Operators (+, =, etc.) can also be overloaded (ex. int+int, double+double, Complex+Complex)
11
CSC 143F 11 Overloaded Function Names Useful if one name has multiple meanings or multiple interfaces Constructors are common example of overloading since all must have same name Compiler determines which function to call at compile-time
12
CSC 143F 12 Resolving Overloaded Functions To "resolve" mean to decide which version of the overloaded function is being called Determined by matching actual arguments against possible formal arguments Compiler gives error if not exactly one matches Complete matching algorithm rather complex If match is not exact, automatic type conversions are used
13
CSC 143F 13 Matching Algorithm Function declarations void Snark(int); void Snark(double); void Snipe(char []); void Snipe(double); void Sneep(char); void Sneep(double); Snark(1);// Integer Snark Snipe(1);// Double Snipe Sneep(1);// Ambiguous
14
CSC 143F 14 Example of Resolving Function declarations void PrintData(int data) { cout << "int = " << data << endl; } void PrintData(char data) { cout << "char = '" << data << "'\n"; } void PrintData(double data) { cout << "double = " << data << endl; } Which calls are valid? PrintData(3); PrintData("Hello"); PrintData(3.14159); PrintData('m');
15
CSC 143F 15 Overloaded Operators For convenience, can define functions named +, -, *, =, /, ==, etc. on classes Gives natural expression to some operations Very confusing if abused Operator functions may be members or friends Function “names” are operator+, operator-,... Several ways of calling them
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.