Revision include iostream using namespace std class base
Revision
#include <iostream> using namespace std; class base { public: virtual void who() {cout << "Basen"; }}; class first_d : public base { public: void who() {cout << "First derivationn"; }}; class second_d : public base {}; // who() not defined int main() {base_obj, *p; first_d first_obj; second_d second_obj; p = &base_obj; p->who(); p = &first_obj; p->who(); p = &second_obj; p->who(); return 0; }
#include <iostream> using namespace std; class base { public: virtual void who() {cout << "Basen"; }}; class first_d : public base { public: void who() {cout << "First derivationn"; }}; // second_d now inherited first_d -- not base. class second_d : public first_d {}; // who() not defined void main() {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 << "Basen"; }}; class first_d : public base { public: void who() {cout << "First derivationn"; }}; // second_d now inherited first_d -- not base. class second_d : public first_d { Public: void who() {cout << "second derivationn"; }}; void main() {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 [parameter. Name]]. . . ); Function Definition Syntax return_type function_name ( [type parameter. Name]. . . ) { statements; //function body }
#include <iostream> using namespace std; double Find. Area(double length, double width); //function prototype void main() { double length. Of. Yard; double width. Of. Yard; double area. Of. Yard; cout << "n. How wide is your yard? "; cin >> width. Of. Yard; cout << "n. How long is your yard? "; cin >> length. Of. Yard; area. Of. Yard= Find. Area(length. Of. Yard, width. Of. Yard); cout<< "n. Your yard is " <<area. Of. Yard<< " square meternn"; } double Find. Area(double l, double w) { return l * w; }
#include <iostream> using namespace std ; void my. Func(); int x = 6; void main() { cout << "n In main x is: " << x; { int x = 5; cout << "n In main x is: " << x; my. Func(); cout << "n Back in main, x is: " << x; } cout << "n In main x is: " << x; } void my. Func() { int x = 8; cout << "n In my. Func, local x: " << x << endl; { cout << "n In block in my. Func, x is: " << x; int x = 9; cout << "n. Very local x: " << x; } cout << "n. Out of block, in my. Func, 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 my. Function (int x, int y); int my. Function (long x, long y); int my. Function (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. int my. Function (int x, int y); long my. Function (int x, int y);
Exercises What is wrong with the following code? #include <iostream> using namespace std ; void my. Func(int x); int main() { int x, y; y = my. Func(6); cout << "x: " << x << " y: " << y << "n"; } void my. Func(int x) { return (4*x); }
Exercises What is wrong with the following code? #include <iostream> using namespace std ; int my. Func( int x); int main() { int x, y; y = my. Func(x); cout << "x: " << x << " y: " << y << "n"; } int my. Func(int x); { return (4*x); }
- Slides: 17