Presentation is loading. Please wait.

Presentation is loading. Please wait.

Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.

Similar presentations


Presentation on theme: "Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty."— Presentation transcript:

1 Prepared by Andrew Jung

2 Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty Prameter Lists Default Argument Unary Scope Resolution Operator Function Overloading Function Templates

3 Introduction C++ improves Object-Oriented Programming capability(OOP) – increase software productibility, quality, resusability. C++ developed by Bjarne Stroustrup at Bell Lab C++ originally called “C with classes”. File extension.cpp not.c

4 Adding two numbers

5 Simple program 1#include 2 3int main() 4{ 5 int number1; 6 int number2; 7 int sum; 8 9 std::cout << "Enter first integer: "; 10 std::cin >> number1; 11 12 std::cout << "Enter second integer: "; 13 std::cin >> number2; 14 sum = number1 + number2; 15 16 std::cout << "sum is " <<sum << std::endl; 17 18}

6 Simple program Line 1: standard C++ header file is included from C++ standard library. iostream: allows program to perform input and output Line 3: keyword “int” indicate return type. Line 5: variable declaration. Declaration can be placed anywhere in a c++ program However, it must be declared before their corresponding variables are used.

7 Simple program Line 9: std::cout - standard output stream object << - standard output stream operator Send the stream of characters ‘Enter first integer: “ to std::cout which is normally connected to screen. Line 10: std::cin - standard input stream object >> - standard input stream operator std::cin takes character input from standard input stream which is usually the keyboard.

8 Simple program Line 16: displays the character string sum is followed by the numerical value of variable sum followed by std::endl – called stream manipulator endl – end of line The std::endl stream manipulator outputs a newline, then “flushes the output buffer.” Std:: before cin, cout, endl. Required when we use standard C++ header files. It specifies that we are using a name, in this case cout that belongs to “namespace” std.

9 Simple program The program doesn’t have return statement(return 0). According to C++ standard, if program execution reaches the end of main without encountering a return statement, it is assumed that the program terminated successfully.

10 Simple program How to eliminate std:: before each mention of cout, cin, and endl in a program? Use using stament

11 Simple program #include using std::cout; using std::cin; using std::endl; int main() { int number1; int number2; int sum; cout << "Enter first integer: "; cin >> number1; cout << "Enter second integer: "; cin >> number2; sum = number1 + number2; cout << "sum is " <<sum << endl; }

12 Simple program #include using namespace std; int main() { int number1; int number2; int sum; cout << "Enter first integer: "; cin >> number1; cout << "Enter second integer: "; cin >> number2; sum = number1 + number2; cout << "sum is " <<sum << endl; }

13 In-Class-Activity 1 (Ungraded) On Moodle…

14

15 C++ standard library and header files We can use C++ standard library which has rich collections of existing classes and functions C++ standard library is divided into many portions, each with its own header file. The header file contain the function prototypes for the related functions that form each portion of the library. The header file also contain definition of various class types and functions, as well as constants needed by those functions.

16 C++ standard library and header files References: http://en.wikipedia.org/wiki/C%2B%2B_Standard_Library http://www.cplusplus.com/reference/

17 Avoid function overhead

18 Inline Function Reduce function call overhead Copy of function’s codes and place copied codes to the place when a program needs. Advantage: Avoid function call overhead Disadvantage: Multiple copies of function codes are inserted into the program – program size are larger.

19 Inline Function #include using namespace std; inline double cube(const double side) { int a; a = side * side * side; return a; } int main() { double sideValue; for(int i = 1; i <= 3; i++) { cout << "\nEnter the side length of tour cube: "; cin >> sideValue; } cout << "Volume of cube with side " << sideValue << " is " << cube(sideValue) << endl; }

20 In-Class-Activity 2 On Moodle…

21

22 References and Reference parameters References as Alias within a Function References can be used as alias for other variables within a function All operations performed on the alias are actually performed in the original value The alias is simply another name for the original value Example: int count = 1; int &cRef = count; cRef++;

23 References and Reference parameters #include using namespace std; int main() { int x = 3; int &y = x; cout << "x = " << x << endl << "y = " << y <<endl; y = 7; cout << "x = " << x << endl << "y = " << y << endl; }

24 References and Reference parameters Two ways to pass arguments Pass by value Pass by reference

25 References and Reference parameters #include using namespace std; int squareByValue(int); void squareByReference(int &); int main() { int x = 2; int z = 4; //demonstrate squareByValue cout << "x = " << x << " before squareByValue\n "; cout << "Value returned by squareByValue: " << squareByValue(x) << endl; cout << "x = " << x << "\tafter squareByValue\n" << endl; //demonstrate squareByReference cout << "z = " << z << "\tbefore squareByReference\n" << endl; squareByReference(z); cout << "z = " << z << "\tafter squareByReference" << endl; } int squareByValue(int number) { return number *= number; } void squareByReference(int &numberRef) { numberRef *= numberRef; }

26

27 Empty Parameter Lists Allows to define functions with no parameters. An empty parameter list is specified by writing either void or nothing at all in parentheses. Example: void print() void print(void);

28 Default Arguments Can a program invoke a function repeatedly with the same argument value for a particular parameter? Yes, we can In such a case, the programmer can specify that such a parameter has a default argument. – default value to be passed to that parameter. When a program omits an argument for a parameter with a default argument in a function call, the compiler rewrites the function call and inserts the default value of that argument to be passed as an argument in the function call. Default arguments are defined in function prototype.

29 Default Arguments #include using namespace std; int boxVolume(int length = 1, int width = 1, int height = 1); int main() { cout << "The default box volume is: " << boxVolume(); cout << "\n\n The volume of a box with length 10,\n" << " width 1 and height 1 is: " << boxVolume(10); cout << "\n\n The volume of a box with length 10,\n" << " width 5 and height 1 is: " << boxVolume(10, 5); cout << "\n\n The volume of a box with length 10,\n" << " width 5 and height 2 is: " << boxVolume(10, 5, 2) << endl; } int boxVolume(int length, int width, int height) { return length * width * height; }

30 Global and local variable has the same name?

31 Unary Scope Resolution Operator It is possible to declare local and global variable of the same name. This causes the global variable to be hidden by the local variable in the local scope. C++ provides the unary scope resolution operator, ::, to access a global variable when a local variable of the same name in scope. A global variable can be accessed directly without the unary scope resolution operator if the name of the global variable is not the same as that of a local variable.

32 Unary Scope Resolution Operator #include using namespace std; int number = 7; //global variable named number int main() { double number = 10.5; //display values of local and global variables cout << "Local double value of number = " << number << "\n\n Global int value of number = " << ::number << endl; }

33 Functions have the same name?

34 Function Overloading C++ enables several functions of the same name to be defined as long as these functions have different sets of parameters. This is called function overloading. When an overloaded function is called, the C++ compiler selects the proper function by examining the number, types and order of the arguments in the call. Function overloading is commonly used to create several functions of the same name that perform similar tasks.

35 Function Overloading #include using namespace std; int square(int x) { cout << "square of integer " << x << "is "; return x * x; } double square(double y) { cout << "square of double " << y << "is "; return y *y; } int main() { cout << square(7); cout << endl; cout << square(7.5); cout << endl; return 0; }

36 Same function name with different data type?

37 Function Template If the program logic and operations are identical for each data type, overloading may be performed more compactly and conveniently by using function template. The program writes a single function template definition Given the argument types provided in calls to this function, C++ automatically generates separate function template specializations to handle each type of call appropriately. Defining a single function template essentially defines a whole family of overloaded functions.

38 Function Template Create.h file Use keyword template and Define parameter type template or template Specify function type, function name, and parameter lists T maximum (T value1, T value2, T value3) { }

39 Function Template maximum.h file template //or template T maximum(T value1, T value2, T value3) { T maximumValue = value1; if(value2 > maximumValue) maximumValue = value2; if(value3 > maximumValue) maximumValue = value3; return maximumValue; }

40 Function Template #include using namespace std; #include "maximum.h" int main() { int int1, int2, int3; cout << "Input three integer values: "; cin >> int1 >> int2 >> int3; cout << "The maximum integer value is: " << maximum(int1, int2, int3); double double1, double2, double3; cout << "\n\nInput three double values: "; cin >> double1 >> double2 >> double3; cout << "The maximum double value is: " << maximum(double1, double2, double3); char char1, char2, char3; cout << "\n\nInput three characters: "; cin >> char1 >> char2 >> char3; cout << "The maximum character value is: " << maximum(char1, char2, char3); }

41 In-Class-Activity 3 On Moodle…

42 Thank you…

43 C++ Array…


Download ppt "Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty."

Similar presentations


Ads by Google