20
C/C++ 3 Yeting Ge

C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp

Embed Size (px)

Citation preview

Page 1: C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp

C/C++ 3

Yeting Ge

Page 2: C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp

Static variables

Static variables is stored in the static storage.

Static variable will be initialized once.

29.cpp 21.cpp

Page 3: C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp

Static members in class

Static variables Shared by all objects

Static functions Have access to static members only

Static members can be accessed by the class name

Page 4: C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp

Friend functions

Have access to the private members of a class.

Must be declared as friend in that class.

Why friend functions? efficiency

30.cpp 31.cpp 32.cpp

Page 5: C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp

Friend class

A class can be declared as the friend of another class.

Page 6: C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp

Function overloading

Define several functions of the same name, differ by parameters.

void Show()void Show(char *str)Void show(int x) 33.cpp

Page 7: C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp

Function overloading Must have different parameters

int func1(int a, int b); double func1(int a, int b); void func(int value); void func(int &value);

Static binding The compilers determine which function is calle

d. (Often used for the multiple constructors)

Page 8: C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp

Operator overloading

Define new operations for operators (enable them to work with class objects).

+ - * / = < > += -= *= /= << >> <<= >>= == != <= >= ++ -- % & ^ ! | ~ &= ^= |= && || %= [] () new delete

Class date x ,y X+y x-y x>y, x&y

Page 9: C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp

Special member functions

Ret_type class_name::operator<>(arg_list) 34.cpp

The return type can be any type.

Page 10: C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp

Overloading =

this pointer 35.cpp

= & copy constructor

Page 11: C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp

Operator overloading by friend functions

Sometimes it is more convenient to use friend functions to overload a binary operator

36.cpp 37.cpp

Page 12: C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp

Overloading examples

() ++

37-1.cpp 37-2.cpp 37-3.cpp 37-4.cpp

Page 13: C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp

Overloading summary

Same name Different parameters Static binding (compile time) Anywhere

Page 14: C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp

Type conversion between classes

An object of derived class is an object of the base class.

A pointer of the base class can be assigned to an address of the derived class.

38.cpp

Page 15: C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp

Redefine

It is possible to redefine a member of the base class in the derived class

Rule of scoping 40.cpp

Page 16: C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp

What is the result?

41.cpp The way out

43.cpp

Page 17: C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp

Virtual function & overriding

Define a member function to be virtual

Use pointer/reference/member functions to call virtual functions

Dynamic binding (Time consuming) The constructor cannot be virtual Must be a member function

Page 18: C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp

Virtual functions examples

By pointers 42.cpp By reference 43.cpp By member function of the base

class 44.cpp

Page 19: C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp

Overloading & overriding

Polymorphism Static and dynamic

(Compile time and running time) Parameters Anywhere / between the base and

derived class

Page 20: C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp

Pure virtual functions & abstract class Pure virtual functions

A function declared without definition virtual ret_type func_name(arg_list)= 0;

Abstract class A class contains one or more pure functio

ns Can not be instantiated Can be used to define pointers