Revision
#include <iostream> using namespace std; class base { public:virtual void who() {cout << "Base\n";}}; class first_d : public base { public:void who() {cout << "First derivation\n";}}; class second_d : public base {};// who() not defined int main() {base base_obj, *p; first_d first_obj; second_d second_obj; p = &base_obj; p->who(); p = &first_obj; p = &second_obj; return 0; }
#include <iostream> using namespace std; class base { public:virtual void who() {cout << "Base\n";}}; class first_d : public base { public:void who() {cout << "First derivation\n";}}; // second_d now inherited first_d -- not base. class second_d : public first_d {};// who() not defined void main() {base base_obj,*p; first_d first_obj;second_d second_obj; p = &base_obj; p->who(); // access base's who() p = &first_obj; p->who(); // access first_d's who() p = &second_obj; p->who(); }
#include <iostream> using namespace std; class base { public:virtual void who() {cout << "Base\n";}}; class first_d : public base { public:void who() {cout << "First derivation\n";}}; // second_d now inherited first_d -- not base. class second_d : public first_d { Public: void who() {cout << "second derivation\n";}}; void main() {base base_obj,*p; first_d first_obj;second_d second_obj; p = &base_obj; p->who(); // access base's who() p = &first_obj; p->who(); // access first_d's who() p = &second_obj; p->who(); }
Functions Function Prototype Syntax return_type function_name ( [type [parameterName]]...); Function Definition Syntax return_type function_name ( [type parameterName]...) { statements; //function body }
#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; }
#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;
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).
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);
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);
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);