Presentation is loading. Please wait.

Presentation is loading. Please wait.

Extra.

Similar presentations


Presentation on theme: "Extra."— Presentation transcript:

1 Extra

2 Functions Function Prototype Syntax return_type function_name ( [type [parameterName]]...); Function Definition Syntax return_type function_name ( [type parameterName]...) { statements; //function body }

3 #include <iostream> using namespace std; double FindArea(double length, double width); //function prototype void main() { double lengthOfYard; double widthOfYard; double areaOfYard; cout << "\nHow wide is your yard? "; cin >> widthOfYard; cout << "\nHow long is your yard? "; cin >> lengthOfYard; areaOfYard= FindArea(lengthOfYard,widthOfYard); cout<< "\nYour yard is " <<areaOfYard<< " square meter\n\n"; } double FindArea(double l, double w) { return l * w; }

4

5 #include <iostream> using namespace std ; void myFunc(); int x = 6; void main() { cout << "\n In main x is: " << x; { int x = 5; cout << "\n In main x is: " << x; myFunc(); cout << "\n Back in main, x is: " << x; } cout << "\n In main x is: " << x; } void myFunc() { int x = 8; cout << "\n In myFunc, local x: " << x << endl; { cout << "\n In block in myFunc, x is: " << x; int x = 9; cout << "\nVery local x: " << x; cout << "\nOut of block, in myFunc, x: " << x << endl;

6

7 Exercises Write the prototype for a function named Perimeter(), which returns int and that takes two parameters, both ints. Write the definition of the function Perimeter() The two parameters represent the length and width of a rectangle. Have the function return the perimeter (twice the length plus twice the width).

8 Overloading Functions
C++ enables you to create more than one function with the same name. This is called function overloading. The functions must differ in their parameter list, with a different type of parameter, a different number of parameters, or both. Here's an example: int myFunction (int x, int y); int myFunction (long x, long y); int myFunction (long z); The return types can be the same or different on overloaded functions. You should note that two functions with the same name and parameter list, but different return types, generate a compiler error. long myFunction (int x, int y);

9 Exercises What is wrong with the following code?
#include <iostream> using namespace std ; void myFunc(int x); int main() { int x, y; y = myFunc(6); cout << "x: " << x << " y: " << y << "\n"; } void myFunc(int x) return (4*x);

10 Exercises What is wrong with the following code?
#include <iostream> using namespace std ; int myFunc( int x); int main() { int x, y; y = myFunc(x); cout << "x: " << x << " y: " << y << "\n"; } int myFunc(int x); return (4*x);

11

12 Exercises Write a programme contain a function that takes two integer arguments and returns the result of dividing the first by the second. Do not do the division if the second number is zero, but do return -1. In main asks the user for two numbers and calls the function Print the answer, or print an error message if you get -1.

13 call by reference The call by reference method of passing arguments to a function, copies the reference of an argument into the formal parameter. Inside the function, the reference is used to access the actual argument used in the call. This means that changes made to the parameter affect the passed argument. void AddOne(int &y) { y = y + 1; }

14 #include <iostream> using namespace std; // function declaration void swap(int &x, int &y); void main () { // local variable declaration: int a = 100; int b = 200; cout << "Before swap, value of a :" << a << endl; cout << "Before swap, value of b :" << b << endl; swap(a, b); cout << "After swap, value of a :" << a << endl; cout << "After swap, value of b :" << b << endl; } void swap(int &x, int &y) { int temp; temp = x; x = y; y = temp;

15

16 void chorus(Pet pt, Pet *petPtr, Pet &petRef) { pt.speak();
Trace the program. class Pet{public:void speak(){cout << "Growl" << endl;}}; class Rat: public Pet {public:void speak(){cout << "Rat noise" << endl;}}; class Cat: public Pet {Public:void speak(){cout << "Meow" << endl;}}; void chorus(Pet pt, Pet *petPtr, Pet &petRef) { pt.speak(); petPtr->speak(); petRef.speak();} void main() { Pet *ptr; //Pointer to base class ptr = new Pet; chorus(*ptr,ptr,*ptr); delete ptr; /////////////////////////////////////// ptr = new Rat; chorus(*ptr,ptr,*ptr); delete ptr; ////////////////////////////////////////// ptr = new Cat; chorus(*ptr,ptr,*ptr); delete ptr; }

17 Convert Speak() function to virtual function then trace the program again.
class Pet {public: virtual void speak(){cout << "Growl" << endl;}};

18 Change class pet to be an abstract pet.
class Pet{Public: virtual void speak()=0;}; What will you change in the main ??


Download ppt "Extra."

Similar presentations


Ads by Google