cs31 lecture 8

There is an exact signature match between the

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: e and the signature of the function definition. and 2. There is a signature match when using integer promotion and/or There floating point promotion floating 3. There is a signature match when using system type conversion 4. There is a signature match when using user-type conversion There (objects) (objects) 5. There is a signature match with a signature containing ellipsis 21 Exact match x Simple: the signature of the call and the signature of the Simple: function fully matches function Ex: Ex: int foo(int x, int y) { return 0; } int int main() { int x = foo(2, 3); int } 22 Match within the same predefined type class x The signature matches up to short-to-int, or float-to-double The conversion conversion Ex: Ex: int foo(int x, int y) { return 0; } int int main() { short z = 3; short int x = foo(2, z); int } 23 Match with promotion on predefined types x The signature matches up to int-to-float, or float-to-int The conversion conversion Ex: Ex: int foo(float x, float y) { return 0; } int int main() { int z = 3; int int x = foo(2, z); int } 24 Forcing a type promotion: type cast x In C and C++, the type of an expression can be changed In explicitly explicitly x This is very useful to avoid confusion / when unsure about This which promotion will occur which x Syntax: (<type>)[expression] Example: float x = (float)(2 / 3) 25 A trick to specialize for a return type x Problem: I want two implementations, one returning an int, Problem: one returning a float. They take the same arguments. I want the compiler to automatically select the implementation based on how I use the returned data implementation Example: int computeDiv (int x, int y) { return x / y; } float computeDiv(int x, int y) { return (float)x / (float)y; } int x = computeDiv(3000, 7); // here, an approximate is enough float x = computeDiv(3, 4); // here, I want an exact result 26 Solution x Solution: pass a reference to the return argument, which is Solution: of the correct type of int computeDiv(int&, int x, int y) { return x / y; return } float computeDiv(float&, int x, int y) { return (float)x / (float)y; return } int main() { int x = computeDiv(x, 3, 4); int float y = computeDiv(y, 3, 4); float } 27 Guidelines for the midterm x Know your C++! x You will be asked: C++ quizzes (~25 points) Understand and analyze a complete C++ progra...
View Full Document

Ask a homework question - tutors are online