├── README.md └── demo ├── lecture01 └── version.cc ├── lecture02 ├── auto.cc ├── bool.c ├── ptr-ref.cc ├── sizeof.cc ├── string_R.cc ├── variable.c └── variable.cc ├── lecture03 ├── casting.cc ├── dma.cc ├── enum.cc ├── for.cc ├── nullptr.cc ├── strong_enum.cc ├── structured_binding.cc └── test ├── lecture04 ├── default_arg.cc ├── functions.cc ├── overloading_resolution.cc ├── reference_return.cc ├── swap.cc └── test ├── lecture05 ├── 10_formatting_output.cc ├── 11_double_interpretation.cc ├── 1_char_overflow.cc ├── 2_char.cc ├── 3_char_initialization.cc ├── 4_short_string.cc ├── 5_display9_4.cc ├── 6_Obj_conversion.cc ├── 7_io_example1.cc ├── 8_cin_lecture.cc ├── 9_io_example1_with_getline.cc └── test ├── lecture06 ├── 1_struct_usage1.cc ├── 2_struct_usage2.cc ├── 3_struct_as_arg.cc ├── 4_struct_initialization.cc ├── 5_class_DayOfYear.cc ├── 6_public_vs_private.cc ├── 7_const.cc └── test ├── lecture07 ├── 11_double_interpretation.cc ├── 1_string_example.cc ├── 3_word_manipulator.cc └── test ├── lecture08 ├── 1_ctors.cc ├── 2_ctors_init_order.cc ├── 3_ctors_private.cc ├── 4_ctors_default.cc ├── 5_ctors_copy.cc ├── 6_server.cc ├── 7_destructor.cc ├── 8_counter.cc └── test ├── lecture09 ├── 1_inheritance.cc ├── 2_inheritance_redefining.cc ├── 3_inheritance_ctors.cc ├── 4_inheritance_protected.cc └── test ├── lecture10 ├── 0_class_basic_1.cc ├── 0_class_basic_2.cc ├── 0_class_basic_3.cc ├── 0_class_basic_4.cc ├── 1_student_addmark.cc ├── 1_student_addmark_reference.cc ├── 1_student_redefine_setName.cc ├── 1_student_redefine_setName_protected.cc ├── 2_call_sq_dummy.cc ├── 2_call_sq_initializing_ctor.cc ├── 2_call_sq_initializing_protected.cc ├── 2_call_sq_undefined_default.cc └── test ├── lecture11 ├── 1_pet.cc ├── 2_pet_override_final.cc ├── 3_pet_pure_virtual.cc ├── 4_1_pet_slicing_prob.cc ├── 4_2_pet_slicing_prob.cc └── test ├── lecture12 ├── 1_money.cc ├── 2_int.cc ├── 3_money_member.cc ├── 4_money_member.cc ├── 5_money_all.cc ├── 5_x_money_ostream_member.cc ├── 6_money_assign.cc ├── 7_inc.cc └── test ├── lecture13 ├── 0_private_inheritance.cc ├── 1_1_swap.cc ├── 1_2_swap.cc ├── 2_showStuff.cc ├── 3_showStuff_error.cc ├── 4_1_swap_error.cc ├── 4_2_passing_arrays.cc ├── 4_3_swap_correct.cc ├── 5_class_template.cc ├── 6_typedef.cc └── test └── lecture14 ├── 1_vector_basic.cc ├── 2_vector_cycling.cc ├── 3_vector_random_access.cc ├── 4_vector_reverse_order.cc ├── 5_list.cc ├── 6_stack.cc ├── 7_set.cc ├── 8_map.cc └── test /README.md: -------------------------------------------------------------------------------- 1 | # 2019FALL_CPE 2 | 3 | Repository for computer programming for engineers, Fall, 2019. 4 | It will be a main repository for demo source codes. 5 | -------------------------------------------------------------------------------- /demo/lecture01/version.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | if (__cplusplus == 201703L) std::cout << "C++17\n"; 5 | else if (__cplusplus == 201402L) std::cout << "C++14\n"; 6 | else if (__cplusplus == 201103L) std::cout << "C++11\n"; 7 | else if (__cplusplus == 199711L) std::cout << "C++98\n"; 8 | else std::cout << "pre-standard C++\n"; 9 | } 10 | -------------------------------------------------------------------------------- /demo/lecture02/auto.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | auto x = 10; 8 | auto y = "Ten"; 9 | cout << x << " is " << y << endl; 10 | } 11 | -------------------------------------------------------------------------------- /demo/lecture02/bool.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | bool flag = true; 6 | 7 | if(flag) 8 | printf("true\n"); 9 | else 10 | printf("false\n"); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /demo/lecture02/ptr-ref.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | #if 1 8 | int a = 0 , b = 1; 9 | int* pa = &a, *pb = &b; 10 | int ca = a, cb = b; 11 | 12 | cout << "a:" << a << ", b:" << b << endl; 13 | // Check the addresses 14 | cout << "pa:" << pa << " pb:" << pb << endl; 15 | cout << "*pa:" << *pa << " *pb:" << *pb << endl; 16 | cout << "ca:" << ca << " cb:" << cb << endl; 17 | 18 | cout << endl << "Increasing a,b" << endl; 19 | 20 | a++; 21 | b++; 22 | 23 | cout << "a:" << a << ", b:" << b << endl; 24 | cout << "pa:" << pa << " pb:" << pb << endl; 25 | cout << "*pa:" << *pa << " *pb:" << *pb << endl; 26 | cout << "ca:" << ca << " cb:" << cb << endl; 27 | #else 28 | int a = 0 , b = 1; 29 | int& ra = a, &rb = b; 30 | int ca = a, cb = b; 31 | 32 | cout << "a:" << a << ", b:" << b << endl; 33 | // Check the addresses 34 | cout << "ra:" << ra << " rb:" << rb << endl; 35 | cout << "ca:" << ca << " cb:" << cb << endl; 36 | 37 | cout << endl << "Increasing a,b" << endl; 38 | 39 | a++; 40 | b++; 41 | 42 | cout << "a:" << a << ", b:" << b << endl; 43 | cout << "ra:" << ra << " rb:" << rb << endl; 44 | cout << "ca:" << ca << " cb:" << cb << endl; 45 | #endif 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /demo/lecture02/sizeof.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | size_t int_size = sizeof(int); 6 | size_t double_size = sizeof(double); 7 | 8 | printf("%zd %zd\n", int_size, double_size ); 9 | 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /demo/lecture02/string_R.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | string s = "(\t\\t\n)"; 8 | //string s = R"(\t\\t\n)"; 9 | 10 | cout << s << endl; 11 | 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /demo/lecture02/variable.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | for(int i=0; i<10; i++) 6 | printf("%d\n", i); 7 | 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /demo/lecture02/variable.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | for(int i=0; i<10; i++) 8 | cout << i << endl; 9 | 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /demo/lecture03/casting.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | unsigned int get_bit(unsigned int num, int pos) 7 | { 8 | if(pos > 32) 9 | return -1; 10 | return (num >> pos) & 1; 11 | } 12 | 13 | void print_bits(unsigned int num) 14 | { 15 | printf("%u is ", num); 16 | for(int i=0; i<32; i++) { 17 | if(i % 4 == 0) 18 | printf(" "); 19 | printf("%u", get_bit(num, 31-i)); 20 | } 21 | printf(".\n"); 22 | } 23 | 24 | 25 | int main() 26 | { 27 | int intVar1 = 1, intVar2 = 2; 28 | cout << 1 / 2 << endl; 29 | cout << intVar1 / intVar1 << endl; 30 | cout << 1.0 / 2 << endl; 31 | cout << 1 / 2.0 << endl; 32 | 33 | // following line doesn't work 34 | //cout << intVar1.0 / intVar1 << endl; 35 | // ~~ 36 | 37 | // different styles of casting 38 | 39 | cout << endl << "diffent styles of casting" << endl; 40 | int myInt = 1; 41 | double myDouble = 2; 42 | cout << (double) 1/2 << endl; 43 | cout << (double) (1/2) << endl; 44 | cout << 1/(double)2 << endl; 45 | cout << double(1)/2 << endl; 46 | cout << myInt/myDouble << endl; 47 | cout << double(myInt)/myDouble << endl; 48 | 49 | // static_cast 50 | cout << endl << "const_cast example" << endl; 51 | double doubleVar = static_cast(intVar1) / intVar2; 52 | cout << doubleVar << endl;; 53 | doubleVar = intVar1 / static_cast(intVar2); 54 | cout << doubleVar << endl;; 55 | doubleVar = static_cast(intVar1 / intVar2); 56 | cout << doubleVar << endl;; 57 | 58 | // const_cast 59 | cout << endl << "const_cast example" << endl; 60 | // Check what happens with the below line!! 61 | //double var2 = const_cast(var1); 62 | double var1 = 1.1; 63 | // Beware! It is undefined behavior to modify a value which is initially declared as const. 64 | //const double var1 = 1.1; 65 | const double& var2 = var1; 66 | // Check what happens with the below line!! 67 | //var2 = 1.2; 68 | double& var3 = const_cast(var2); 69 | cout << var1 << '\t' << var2 << '\t' << var3 << endl; 70 | var3 = 1.3; 71 | cout << var1 << '\t' << var2 << '\t' << var3 << endl; 72 | //cout << &var1 << '\t' << &var2 << '\t' << &var3 << endl; 73 | 74 | // reinterpret_cast 75 | cout << endl << "reinterpret_cast example" << endl; 76 | int var4 = 1; 77 | float var5 = 1; 78 | print_bits(var4); 79 | print_bits(*((int*)&var5)); 80 | print_bits(reinterpret_cast(var5)); 81 | } 82 | -------------------------------------------------------------------------------- /demo/lecture03/dma.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | #if 0 // C style 8 | int* ptr; 9 | cout << ptr << endl; 10 | ptr = (int*) malloc( sizeof(int)*1 ); 11 | cout << ptr << endl; 12 | *ptr = 1; 13 | cout << *ptr << endl; 14 | 15 | if(ptr1) free( ptr1 ); 16 | ptr1 = NULL; 17 | #else // C++ style 18 | // c++ style 19 | int *ptr = new int; 20 | *ptr = 1; 21 | cout << *ptr << endl; 22 | 23 | // guess results 24 | //cout << ptr << endl; 25 | //cout << sizeof(ptr) << endl; 26 | //cout << sizeof(*ptr) << endl; 27 | 28 | if(ptr != nullptr) delete ptr; 29 | ptr = nullptr; 30 | #endif 31 | 32 | return 1; 33 | } 34 | -------------------------------------------------------------------------------- /demo/lecture03/enum.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | enum MODE { WEAPON, EQUIPMENT, GEM = 10, DEFENSE}; 8 | 9 | int mode; 10 | cout << "Enter mode(0:Weapon, 1:Equipment, 2:Gem, 3:Defence): "; 11 | cin >> mode; 12 | 13 | switch(mode) { 14 | case WEAPON: 15 | cout << "Weapon" << endl; 16 | break; 17 | case EQUIPMENT: 18 | cout << "Equipment" << endl; 19 | break; 20 | case GEM: 21 | cout << "Gem" << endl; 22 | break; 23 | case DEFENSE: 24 | cout << "Defence" << endl; 25 | break; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /demo/lecture03/for.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int arr[] = {20, 30, 40, 50}; 8 | 9 | for(int i=0; i 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int* p_int = nullptr; 8 | printf("%s\n", typeid(nullptr).name() ); 9 | 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /demo/lecture03/strong_enum.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | #if 1 8 | // Check this code. Does it work?? 9 | enum IOResult { 10 | Error, 11 | Ok 12 | }; 13 | 14 | enum ParseResult { 15 | Error, 16 | Ok 17 | }; 18 | #else 19 | enum class IOResult { 20 | Error, 21 | Ok 22 | }; 23 | 24 | enum class ParseResult { 25 | Error, 26 | Ok 27 | }; 28 | #endif 29 | 30 | IOResult io_return_code = IOResult::Ok; 31 | 32 | switch(io_return_code) { 33 | case IOResult::Ok: 34 | cout << "IO done" << endl; 35 | break; 36 | case IOResult::Error: 37 | cout << "IO Error" << endl; 38 | } 39 | 40 | ParseResult parse_return_code = ParseResult::Error; 41 | switch(parse_return_code) { 42 | case ParseResult::Ok: 43 | cout << "Parse done" << endl; 44 | break; 45 | case ParseResult::Error: 46 | cout << "Parse Error" << endl; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /demo/lecture03/structured_binding.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int a[2] = {1,2}; 8 | auto [x,y] = a; 9 | auto& [xr, yr] = a; 10 | cout << x << "," << y << endl; 11 | 12 | xr = 3; 13 | yr = 4; 14 | 15 | // what will be the result? 16 | //cout << x << "," << y << endl; 17 | //cout << a[0] << "," << a[1] << endl; 18 | //cout << xr << "," << yr << endl; 19 | 20 | struct { 21 | int i=1; 22 | double d=2; 23 | } f; 24 | auto [i,d] = f; 25 | cout << i << " " << d << endl; 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /demo/lecture03/test: -------------------------------------------------------------------------------- 1 | for lecture 03 2 | -------------------------------------------------------------------------------- /demo/lecture04/default_arg.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // The code has errors 6 | 7 | void show_volume( int length, int width=1, int height=1 ); 8 | 9 | int main() 10 | { 11 | show_volume(2, 4, 6); // all arguments supplied 12 | show_volume(3, 5); // height defaulted to 1 13 | show_volume(7); // width & height defaulted to 1 14 | } 15 | 16 | void show_volume( int length, int width=1, int height=1 ) 17 | { 18 | cout << length * width * height << endl; 19 | 20 | } 21 | -------------------------------------------------------------------------------- /demo/lecture04/functions.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int AddOneRef(int& i) 6 | { 7 | return i++; 8 | //return ++i; 9 | } 10 | 11 | 12 | int AddOne(int i) 13 | { 14 | return i++; 15 | //return ++i; 16 | } 17 | 18 | double Half(double d) 19 | //double Half(double& d) 20 | { 21 | d /= 2; 22 | return d; 23 | } 24 | 25 | int main() 26 | { 27 | int i = 1; 28 | double d = 3.14; 29 | 30 | cout << "before: " << i << endl; 31 | cout << AddOne(i) << endl; 32 | //cout << AddOneRef(i) << endl; 33 | cout << "after : " << i << endl; 34 | 35 | cout << "before: " << d << endl; 36 | cout << Half(d) << endl; 37 | cout << "after : " << d << endl; 38 | } 39 | -------------------------------------------------------------------------------- /demo/lecture04/overloading_resolution.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | double mpg( double miles, double gallons ){ 6 | cout << "dd" << endl; 7 | return (miles/gallons); 8 | } 9 | 10 | #if 0 11 | double mpg( int miles, int gallons ){ 12 | cout << "ii" << endl; 13 | return (double(miles)/gallons); 14 | } 15 | 16 | double mpg( int miles, double gallons ){ 17 | cout << "id" << endl; 18 | return (miles/gallons); 19 | } 20 | #endif 21 | 22 | int main() 23 | { 24 | cout << mpg(5, 20) << endl; 25 | cout << mpg(5, 24.9) << endl; 26 | } 27 | -------------------------------------------------------------------------------- /demo/lecture04/reference_return.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | double& func( double& variable ) 6 | { 7 | return variable; 8 | } 9 | 10 | double& mal_func( double variable) 11 | { 12 | return variable; 13 | } 14 | 15 | int main() 16 | { 17 | double dVar = 3.14; 18 | 19 | cout << dVar << endl; 20 | cout << func(dVar) << endl; 21 | cout << dVar << endl; 22 | 23 | // Assign the returned variable to a normal variable 24 | 25 | double dVar2 = func(dVar); 26 | // See the difference 27 | //double& dVar2 = func(dVar); 28 | cout << "&dVar:" << &dVar << endl; 29 | cout << "&dVar2:" << &dVar2 << endl; 30 | 31 | 32 | // What happens? 33 | dVar2 = mal_func(dVar); 34 | //double& dVar2 = func(dVar); 35 | cout << "&dVar:" << &dVar << endl; 36 | cout << "&dVar2:" << &dVar2 << endl; 37 | 38 | 39 | } 40 | -------------------------------------------------------------------------------- /demo/lecture04/swap.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void swapValuesCpy( int variable1, int variable2 ) 6 | { 7 | int temp; 8 | temp = variable1; 9 | variable1 = variable2; 10 | variable2 = temp; 11 | } 12 | 13 | 14 | void swapValuesPtr( int* variable1, int* variable2 ) 15 | { 16 | int temp; 17 | temp = *variable1; 18 | *variable1 = *variable2; 19 | *variable2 = temp; 20 | } 21 | 22 | void swapValuesRef( int& variable1, int& variable2 ) 23 | { 24 | int temp; 25 | temp = variable1; 26 | variable1 = variable2; 27 | variable2 = temp; 28 | } 29 | 30 | int main() 31 | { 32 | int i1 = 1, i2 = 2; 33 | 34 | cout << i1 << ", " << i2 << endl; 35 | 36 | swapValuesCpy(i1, i2); 37 | cout << i1 << ", " << i2 << endl; 38 | //swapValuesPtr(&i1, &i2); 39 | //cout << i1 << ", " << i2 << endl; 40 | //swapValuesRef(i1, i2); 41 | //cout << i1 << ", " << i2 << endl; 42 | 43 | } 44 | -------------------------------------------------------------------------------- /demo/lecture04/test: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /demo/lecture05/10_formatting_output.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | #if 0 8 | cout.setf(ios::fixed); 9 | cout.setf(ios::showpoint); 10 | cout.precision(2); 11 | #endif 12 | 13 | double price = 78.5; 14 | cout << "The price is $" << price << endl; 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /demo/lecture05/11_double_interpretation.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | #if 1 8 | cout.setf(ios::fixed); 9 | cout.setf(ios::showpoint); 10 | cout.precision(100); 11 | #endif 12 | double num1 = 0.000899999999999999975366926641; 13 | double num2 = 0.0009; 14 | cout << "num1:\t" << num1 << endl; 15 | cout << "num2:\t" << num2 << endl; 16 | cout << "diff:\t" << num1 - num2 << endl; 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /demo/lecture05/1_char_overflow.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | // Following code has errors 8 | char name1[5] = "Tzuyu"; 9 | char name2[4] = {'S', 'a', 'n', 'a'}; 10 | char name3[6] = {'D', 'a', 'h', 'y', 'u', 'n'}; 11 | 12 | cout << name1 << endl; 13 | cout << name2 << endl; 14 | cout << name3 << endl; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /demo/lecture05/2_char.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | // Following code has errors 8 | char s[10] = "Hi Mom!"; 9 | 10 | cout << s << endl; 11 | 12 | // Checking items of s 13 | for(char c : s) 14 | cout << int(c) << endl; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /demo/lecture05/3_char_initialization.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | // Following code has errors 8 | char s1[10]; 9 | char s2[10] = ""; 10 | char s3[10] = {}; 11 | char s4[10] = {'\0'}; 12 | 13 | // Checking items of s 14 | cout << "s1" << endl; 15 | for(char c : s1) 16 | cout << int(c) << " "; 17 | cout << endl; 18 | 19 | cout << "s2" << endl; 20 | for(char c : s2) 21 | cout << int(c) << " "; 22 | cout << endl; 23 | 24 | cout << "s3" << endl; 25 | for(char c : s3) 26 | cout << int(c) << " "; 27 | cout << endl; 28 | 29 | cout << "s4" << endl; 30 | for(char c : s4) 31 | cout << int(c) << " "; 32 | cout << endl; 33 | 34 | 35 | // What result can we conclude from the above results? 36 | } 37 | 38 | -------------------------------------------------------------------------------- /demo/lecture05/4_short_string.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | char short_string1[] = "abcdefg"; 8 | cout << "String1: " << short_string1 << endl; 9 | cout << "Size: " << sizeof(short_string1) << endl; 10 | 11 | char short_string2[] = {"k", "l", "m"}; 12 | #if 0 13 | // What happens with the below code? 14 | char short_string2[] = {'k', 'l', 'm'}; 15 | cout << "String2: " << short_string2 << endl; 16 | cout << "Size: " << sizeof(short_string2) << endl; 17 | #endif 18 | } 19 | -------------------------------------------------------------------------------- /demo/lecture05/5_display9_4.cc: -------------------------------------------------------------------------------- 1 | // demonstrates the standard class string. 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main( ) 7 | { 8 | string phrase; // initialized to the empty string 9 | 10 | // two equivalent ways of initializing a string variable 11 | string adjective("fried"), noun("ants"); 12 | string newWord(adjective + " " + noun); 13 | 14 | string wish = "Bon appetite!"; 15 | 16 | phrase = "I love " + newWord + "!"; 17 | phrase = "I love " + adjective + " " + noun + "!"; 18 | cout << phrase << endl << wish << endl; 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /demo/lecture05/6_Obj_conversion.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | char aCString[] = "My C-string"; 9 | string stringVar = aCString; 10 | 11 | cout << "C String: " << aCString << endl; 12 | cout << "string: " << stringVar << endl; 13 | 14 | strcpy( aCString, stringVar.c_str() ); 15 | //aCString = stringVar; // illegal! 16 | //aCString = stringVar.c_str(); // What happens with this code? 17 | 18 | cout << endl; 19 | cout << "C String: " << aCString << endl; 20 | cout << "string: " << stringVar << endl; 21 | } 22 | -------------------------------------------------------------------------------- /demo/lecture05/7_io_example1.cc: -------------------------------------------------------------------------------- 1 | //Program to demonstrate cin and cout with strings 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main( ) 7 | { 8 | string dogName; 9 | int actualAge; 10 | int humanAge; 11 | cout << "How many years old is your dog?" << endl; 12 | cin >> actualAge; 13 | humanAge = actualAge * 7; 14 | cout << "What is your dog's name?" << endl; 15 | cin >> dogName; 16 | cout << dogName << "'s age is approximately " << 17 | "equivalent to a " << humanAge << " year old human." 18 | << endl; 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /demo/lecture05/8_cin_lecture.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int num; 8 | // See the initial value of num 9 | cout << "Initial value of num: " << num << endl; 10 | cout << "Enter a new number for num: "; 11 | cin >> num; 12 | // What happens when we enter a value 13 | // with a different data type rather than int? 14 | cout << "Entered value of num: " << num << endl; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /demo/lecture05/9_io_example1_with_getline.cc: -------------------------------------------------------------------------------- 1 | //Program to demonstrate cin and cout with strings 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main( ) 7 | { 8 | string dogName; 9 | int actualAge; 10 | int humanAge; 11 | cout << "How many years old is your dog?" << endl; 12 | cin >> actualAge; 13 | humanAge = actualAge * 7; 14 | cout << "What is your dog's name?" << endl; 15 | // Is it enough? 16 | getline(cin, dogName); 17 | cout << dogName << "'s age is approximately " << 18 | "equivalent to a " << humanAge << " year old human." 19 | << endl; 20 | return 0; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /demo/lecture05/test: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /demo/lecture06/1_struct_usage1.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | struct CDAccountV1 // name of new struct "type" 7 | { 8 | double balance; // member names 9 | double interestRate; 10 | int term; 11 | }; 12 | 13 | struct CDAccountV2 // name of new struct "type" 14 | { 15 | int balance; // member names 16 | int interestRate; 17 | int term; 18 | }; 19 | 20 | 21 | int main() 22 | { 23 | #if 1 24 | // C++ 25 | CDAccountV1 account1; 26 | // C 27 | //struct CDAccountV1 account1; 28 | 29 | account1.balance = 1000; 30 | account1.interestRate = 0.02; 31 | account1.term = 2; 32 | 33 | cout << "I have $" << account1.balance << " in my account." << endl; 34 | double rate = pow(1+account1.interestRate, account1.term); 35 | cout << "After " << account1.term << " years it will become $" << account1.balance * rate << "." << endl; 36 | #else 37 | // We can use the same names for member vars of different structs 38 | CDAccountV2 account2; 39 | 40 | account2.balance = 1000; 41 | account2.interestRate = 0.02; // CHECK TYPE! 42 | account2.term = 2; 43 | 44 | cout << "I have $" << account2.balance << " in my account." << endl; 45 | double rate = pow(1+account2.interestRate, account2.term); 46 | cout << "After " << account2.term << " years it will become $" << account2.balance * rate << "." << endl; 47 | #endif 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /demo/lecture06/2_struct_usage2.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | struct CDAccountV1 // name of new struct "type" 7 | { 8 | double balance; // member names 9 | double interestRate; 10 | int term; 11 | } account; 12 | //~~~~~~~ 13 | 14 | int main() 15 | { 16 | account.balance = 1000; 17 | account.interestRate = 0.02; 18 | account.term = 2; 19 | 20 | cout << "I have $" << account.balance << " in my account." << endl; 21 | double rate = pow(1+account.interestRate, account.term); 22 | cout << "After " << account.term << " years it will become $" << account.balance * rate << "." << endl; 23 | 24 | CDAccountV1 acc; 25 | acc.balance = 2000; 26 | acc.interestRate = 0.02; 27 | acc.term = 3; 28 | 29 | cout << "I have $" << acc.balance << " in my account." << endl; 30 | rate = pow(1+acc.interestRate, acc.term); 31 | cout << "After " << acc.term << " years it will become $" << acc.balance * rate << "." << endl; 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /demo/lecture06/3_struct_as_arg.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | struct CDAccountV1 // name of new struct "type" 7 | { 8 | double balance; // member names 9 | double interestRate; 10 | int term; 11 | }; 12 | 13 | void printAccountInfo(CDAccountV1 myAccount) 14 | { 15 | cout << "I have $" << myAccount.balance << " in my account." << endl; 16 | double rate = pow(1+myAccount.interestRate, myAccount.term); 17 | cout << "After " << myAccount.term << " years it will become $" << myAccount.balance * rate << "." << endl; 18 | // What happens when we modify the value of myAccount's member variables? 19 | // What does it imply? 20 | } 21 | 22 | int main() 23 | { 24 | CDAccountV1 acc; 25 | acc.balance = 2000; 26 | acc.interestRate = 0.02; 27 | acc.term = 3; 28 | 29 | printAccountInfo(acc); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /demo/lecture06/4_struct_initialization.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // Only from C++11 6 | // g++ -std=c++98 ... shows an error or a warning 7 | struct Date { 8 | int month = 12; 9 | int day = 31; 10 | int year = 2003; 11 | }; 12 | 13 | int main() 14 | { 15 | Date dueDate; 16 | cout << dueDate.month << endl; 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /demo/lecture06/5_class_DayOfYear.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class DayOfYear // name of new class type 6 | { 7 | public: 8 | void output(); // member function! implementation elsewhere 9 | void assign1(int m, int d); 10 | void assign2(int month, int day); 11 | void assign3(int, int); 12 | int month; 13 | int day; 14 | }; 15 | 16 | void DayOfYear::assign1(int m, int d) 17 | { 18 | month = m; 19 | day = d; 20 | } 21 | 22 | // Resolving scope confliction by using this pointer 23 | void DayOfYear::assign2(int month, int day) 24 | { 25 | this->month = month; 26 | this->day = day; 27 | } 28 | 29 | 30 | void DayOfYear::assign3(int month, int day) 31 | { 32 | this->month = month; 33 | this->day = day; 34 | } 35 | 36 | void DayOfYear::output() 37 | { 38 | cout << month << "/" << day << endl; 39 | } 40 | 41 | int main() 42 | { 43 | DayOfYear birthday; 44 | 45 | birthday.month = 5; 46 | birthday.day = 11; 47 | birthday.output(); // invokes member function 48 | 49 | birthday.assign1(9,6); 50 | birthday.output(); 51 | 52 | birthday.assign2(1,22); 53 | birthday.output(); 54 | 55 | birthday.assign3(12,23); 56 | birthday.output(); 57 | } 58 | 59 | -------------------------------------------------------------------------------- /demo/lecture06/6_public_vs_private.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class DayOfYear // name of new class type 6 | { 7 | public: 8 | void output(); // member function! implementation elsewhere 9 | void assign(int month, int day); 10 | private: 11 | int month; 12 | int day; 13 | }; 14 | 15 | void DayOfYear::output() 16 | { 17 | cout << month << "/" << day << endl; 18 | } 19 | 20 | void DayOfYear::assign(int month, int day) 21 | { 22 | this->month = month; 23 | this->day = day; 24 | } 25 | 26 | int main() 27 | { 28 | DayOfYear birthday; 29 | 30 | // Illegal accesses to private member variables 31 | //birthday.month = 5; 32 | //birthday.day = 11; 33 | birthday.assign(5, 11); 34 | birthday.output(); // invokes member function 35 | } 36 | 37 | -------------------------------------------------------------------------------- /demo/lecture06/7_const.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int number7 = 7; 8 | int number8 = 8; 9 | int* const addr_const = &number7; 10 | const int* const_addr = &number7; 11 | 12 | cout << number7 << " has an address of " << addr_const << endl; 13 | 14 | // To check the address of number8 15 | // ERROR 16 | //addr_const = &number8; 17 | //cout << number8 << " has an address of " << addr_const << endl; 18 | const_addr = &number8; 19 | cout << number8 << " has an address of " << const_addr << endl; 20 | 21 | // Assigning a new value through pointers 22 | // addr_const points to number7 23 | *addr_const = 77; 24 | cout << "number7 is now " << *addr_const << endl; 25 | 26 | // const_addr points to number8 27 | // ERROR 28 | //*const_addr = 88; 29 | //cout << "number8 is now " << *const_addr << endl; 30 | } 31 | -------------------------------------------------------------------------------- /demo/lecture06/test: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /demo/lecture07/11_double_interpretation.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | #if 1 8 | cout.setf(ios::fixed); 9 | cout.setf(ios::showpoint); 10 | cout.precision(100); 11 | #endif 12 | double num1 = 0.000899999999999999975366926641; 13 | double num2 = 0.0009; 14 | cout << "num1:\t" << num1 << endl; 15 | cout << "num2:\t" << num2 << endl; 16 | cout << "diff:\t" << num1 - num2 << endl; 17 | if(num1 == num2) 18 | cout << "1: num1 and num2 are same" << endl; 19 | cout << endl; 20 | 21 | num1 = 0.09; 22 | num2 = num1 * 10 - 0.81; 23 | cout << "num1:\t" << num1 << endl; 24 | cout << "num2:\t" << num2 << endl; 25 | cout << "diff:\t" << num1 - num2 << endl; 26 | if(num1 == num2) 27 | cout << "2: num1 and num2 are same" << endl; 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /demo/lecture07/1_string_example.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | int main() 7 | { 8 | string str(""); 9 | 10 | // empty 11 | if(str.empty()) 12 | str = "This is my string"; 13 | 14 | // iterating 15 | for(char c : str) 16 | cout << c; 17 | cout << endl; 18 | 19 | for(int i=0; i 2 | 3 | using namespace std; 4 | 5 | class myWordMani 6 | { 7 | public: 8 | string getWord() { 9 | return str; 10 | } 11 | void setWord(string str) { 12 | this->str = str; 13 | } 14 | void printWord() { 15 | cout << str << endl; 16 | } 17 | void sortWord(); 18 | void reverseWord(); 19 | int countChar(char); 20 | int removeChar(char); 21 | private: 22 | string str; 23 | }; 24 | 25 | void swap(string& str, int i, int j) 26 | { 27 | char tmp = str[i]; 28 | str[i] = str[j]; 29 | str[j] = tmp; 30 | } 31 | 32 | int myWordMani::countChar(char c) 33 | { 34 | int count = 0; 35 | for(char ch : str) { 36 | if(ch == c) 37 | count++; 38 | } 39 | return count; 40 | } 41 | 42 | int myWordMani::removeChar(char c) 43 | { 44 | int count = 0; 45 | for(int i=0; i 2 | 3 | using namespace std; 4 | 5 | class DayOfYear 6 | { 7 | public: 8 | DayOfYear(int, int); 9 | 10 | void output(); 11 | private: 12 | int month; 13 | int day; 14 | }; 15 | 16 | DayOfYear::DayOfYear(int monthValue, int dayValue) 17 | { 18 | cout << "In the constructor DayOfYear(" << monthValue; 19 | cout << "," << dayValue << ")" << endl; 20 | month = monthValue; 21 | day = dayValue; 22 | } 23 | 24 | void DayOfYear::output() 25 | { 26 | cout << month << "/" << day << endl; 27 | } 28 | 29 | int main() 30 | { 31 | DayOfYear christmas(12,25); 32 | christmas.output(); 33 | 34 | // We are calling constructors for intergers 35 | int month(5); 36 | int* day = new int(11); 37 | 38 | DayOfYear* birthday = new DayOfYear(month, *day); 39 | birthday->output(); 40 | } 41 | 42 | -------------------------------------------------------------------------------- /demo/lecture08/2_ctors_init_order.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class DayOfYear 6 | { 7 | public: 8 | DayOfYear(int, int); 9 | 10 | void output(); 11 | private: 12 | int month = 1; 13 | int day = 1; 14 | }; 15 | 16 | DayOfYear::DayOfYear(int monthValue, int dayValue) 17 | : month(monthValue), day(dayValue) 18 | { 19 | cout << "In the constructor DayOfYear(" << monthValue; 20 | cout << "," << dayValue << ")" << endl; 21 | month = monthValue; 22 | day = dayValue; 23 | } 24 | 25 | void DayOfYear::output() 26 | { 27 | cout << month << "/" << day << endl; 28 | } 29 | 30 | int main() 31 | { 32 | DayOfYear christmas(12,25); 33 | // What is the result? What does it mean? 34 | christmas.output(); 35 | } 36 | 37 | -------------------------------------------------------------------------------- /demo/lecture08/3_ctors_private.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class DayOfYear 6 | { 7 | public: 8 | DayOfYear(int, int); 9 | 10 | void output(); 11 | private: 12 | // We cannot call private constructors 13 | DayOfYear(int=0, double=0) {} 14 | DayOfYear(double=0, int=0) {} 15 | DayOfYear(double=0, double=0) {} 16 | 17 | int month; 18 | int day; 19 | }; 20 | 21 | DayOfYear::DayOfYear(int monthValue, int dayValue) 22 | : month(monthValue), day(dayValue) 23 | { 24 | cout << "In the constructor DayOfYear" << endl; 25 | } 26 | 27 | void DayOfYear::output() 28 | { 29 | cout << month << "/" << day << endl; 30 | } 31 | 32 | int main() 33 | { 34 | DayOfYear christmas(12, 25.0); 35 | christmas.output(); 36 | } 37 | 38 | -------------------------------------------------------------------------------- /demo/lecture08/4_ctors_default.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class DayOfYear 6 | { 7 | public: 8 | DayOfYear(int, int); 9 | 10 | void output(); 11 | private: 12 | int month; 13 | int day; 14 | }; 15 | 16 | DayOfYear::DayOfYear(int monthValue, int dayValue) 17 | : month(monthValue), day(dayValue) 18 | { 19 | cout << "In the constructor DayOfYear" << endl; 20 | } 21 | 22 | void DayOfYear::output() 23 | { 24 | cout << month << "/" << day << endl; 25 | } 26 | 27 | int main() 28 | { 29 | // Declaring an object without arguments causes an error 30 | // when we have constructors but the default one. 31 | DayOfYear christmas; 32 | christmas.output(); 33 | } 34 | 35 | -------------------------------------------------------------------------------- /demo/lecture08/5_ctors_copy.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class DayOfYear 6 | { 7 | public: 8 | DayOfYear(); 9 | DayOfYear(int, int); 10 | 11 | DayOfYear( const DayOfYear& other ) 12 | { 13 | cout << "In the copy constructor " ; 14 | other.output(); 15 | this->month = other.month; 16 | this->day = other.day; 17 | } 18 | 19 | void output() const; 20 | private: 21 | int month; 22 | int day; 23 | }; 24 | 25 | DayOfYear::DayOfYear() 26 | : month(1), day(1) 27 | { 28 | cout << "In the default constructor" << endl; 29 | } 30 | 31 | 32 | DayOfYear::DayOfYear(int monthValue, int dayValue) 33 | : month(monthValue), day(dayValue) 34 | { 35 | cout << "In the constructor DayOfYear(" << monthValue; 36 | cout << "," << dayValue << ")" << endl; 37 | month = monthValue; 38 | day = dayValue; 39 | } 40 | 41 | void DayOfYear::output() const 42 | { 43 | cout << month << "/" << day << endl; 44 | } 45 | 46 | void print_day( DayOfYear day ) 47 | { 48 | cout << "In print_day" << endl; 49 | day.output(); 50 | } 51 | 52 | int main() 53 | { 54 | DayOfYear newYear(1,1); 55 | DayOfYear holiday = newYear; 56 | holiday.output(); 57 | 58 | holiday = DayOfYear(9,25); 59 | print_day(holiday); 60 | } 61 | 62 | -------------------------------------------------------------------------------- /demo/lecture08/6_server.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Server 4 | { 5 | public: 6 | Server(char letterName); 7 | static int getTurn( ); 8 | void serveOne( ); 9 | static bool stillOpen( ); 10 | private: 11 | static int turn; 12 | static int lastServed; 13 | static bool nowOpen; 14 | char name; 15 | }; 16 | int Server::turn = 0; 17 | int Server::lastServed = 0; 18 | bool Server::nowOpen = true; 19 | int main( ) 20 | { 21 | Server s1('A'), s2('B'); 22 | int number, count; 23 | do 24 | { 25 | cout << "How many in your group? "; 26 | cin >> number; 27 | cout << "Your turns are: "; 28 | for (count = 0; count < number; count++) 29 | cout << Server::getTurn( ) << ' '; 30 | cout << endl; 31 | s1.serveOne( ); 32 | s2.serveOne( ); 33 | } while (Server::stillOpen( )); 34 | cout << "Now closing service.\n"; 35 | return 0; 36 | } 37 | 38 | Server::Server(char letterName) : name(letterName) 39 | {/*Intentionally empty*/} 40 | 41 | int Server::getTurn( ) { turn++; return turn; } 42 | //Since getTurn is static, only static members 43 | //can be referenced in here. 44 | 45 | bool Server::stillOpen( ) { return nowOpen; } 46 | 47 | void Server::serveOne( ) 48 | { 49 | if (nowOpen && lastServed < turn) 50 | { 51 | lastServed++; 52 | cout << "Server " << name 53 | << " now serving " << lastServed << endl; 54 | } 55 | if (lastServed >= turn) //Everyone served 56 | nowOpen = false; 57 | } 58 | 59 | -------------------------------------------------------------------------------- /demo/lecture08/7_destructor.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class DayOfYear 6 | { 7 | public: 8 | DayOfYear() {month = 1; day = 1;} 9 | DayOfYear(int, int); 10 | ~DayOfYear(); 11 | 12 | void output(); 13 | private: 14 | int month; 15 | int day; 16 | }; 17 | 18 | DayOfYear::~DayOfYear() 19 | { 20 | cout << "Destructing "; 21 | output(); 22 | } 23 | 24 | DayOfYear::DayOfYear(int monthValue, int dayValue) 25 | : month(monthValue), day(dayValue) 26 | { 27 | cout << "In the constructor "; 28 | output(); 29 | } 30 | 31 | void DayOfYear::output() 32 | { 33 | cout << month << "/" << day << endl; 34 | } 35 | 36 | int main() 37 | { 38 | DayOfYear day(10,31); 39 | { 40 | DayOfYear day(11,1); 41 | day.output(); 42 | cout << "We are going out of scope" << endl; 43 | } 44 | day.output(); 45 | cout << "Program ends" << endl; 46 | } 47 | 48 | -------------------------------------------------------------------------------- /demo/lecture08/8_counter.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Obj 6 | { 7 | public: 8 | Obj():name("not set") {count++;} 9 | Obj(string name):name(name) {count++;} 10 | static void printCounter(); 11 | void printInfo(); 12 | private: 13 | string name; 14 | static int count; 15 | }; 16 | int Obj::count = 0; 17 | 18 | void Obj::printInfo() 19 | { 20 | cout << name << ": " << count << endl; 21 | } 22 | 23 | void Obj::printCounter() 24 | { 25 | //cout << name << ": " << count << endl; 26 | cout << count << endl; 27 | } 28 | 29 | int main() 30 | { 31 | Obj obj1("first"); 32 | obj1.printCounter(); 33 | obj1.printInfo(); 34 | Obj obj2("second"); 35 | obj2.printCounter(); 36 | obj2.printInfo(); 37 | 38 | Obj obj[10]; 39 | obj1.printCounter(); 40 | obj1.printInfo(); 41 | obj2.printCounter(); 42 | obj2.printInfo(); 43 | 44 | return 0; 45 | } 46 | 47 | -------------------------------------------------------------------------------- /demo/lecture08/test: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /demo/lecture09/1_inheritance.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Person 6 | { 7 | public: 8 | Person() : name("not set") {} 9 | Person(string name) : name(name) {} 10 | string getName() const {return name;} 11 | void setName(string name) {this->name=name;} 12 | void printInfo() const; 13 | private: 14 | string name; 15 | }; 16 | 17 | void Person::printInfo() const 18 | { 19 | cout << "Name: " << name << endl; 20 | } 21 | 22 | class Student : public Person 23 | { 24 | public: 25 | void setSid(int sid) {this->sid = sid;} 26 | int getSid() const {return sid;} 27 | private: 28 | int sid; 29 | }; 30 | 31 | int main() 32 | { 33 | Student kim; 34 | kim.setName("Kim"); 35 | kim.setSid(100); 36 | kim.printInfo(); 37 | cout << kim.getSid() << endl; 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /demo/lecture09/2_inheritance_redefining.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Person 6 | { 7 | public: 8 | Person() : name("not set") {} 9 | Person(string name) : name(name) {} 10 | string getName() const {return name;} 11 | void setName(string name) {this->name=name;} 12 | void printInfo() const; 13 | private: 14 | string name; 15 | }; 16 | 17 | void Person::printInfo() const 18 | { 19 | cout << "Name: " << name << endl; 20 | } 21 | 22 | class Student : public Person 23 | { 24 | public: 25 | void setSid(int sid) {this->sid = sid;} 26 | int getSid() const {return sid;} 27 | void printInfo(); 28 | private: 29 | int sid; 30 | }; 31 | 32 | void Student::printInfo() 33 | { 34 | Person::printInfo(); 35 | cout << "Student ID: " << sid << endl; 36 | } 37 | 38 | int main() 39 | { 40 | Student kim; 41 | kim.setName("Kim"); 42 | kim.setSid(100); 43 | kim.printInfo(); 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /demo/lecture09/3_inheritance_ctors.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Person 6 | { 7 | public: 8 | Person() : name("not set") {} 9 | Person(string name) : name(name) {} 10 | string getName() const {return name;} 11 | void setName(string name) {this->name=name;} 12 | void printInfo() const; 13 | private: 14 | string name; 15 | }; 16 | 17 | void Person::printInfo() const 18 | { 19 | cout << "Name: " << name << endl; 20 | } 21 | 22 | class Student : public Person 23 | { 24 | public: 25 | Student() : Person(), sid(0) {} 26 | Student(int sid) : Person(), sid(sid) {} 27 | Student(string name, int sid) : Person(name), sid(sid) {} 28 | void setSid(int sid) {this->sid = sid;} 29 | int getSid() const {return sid;} 30 | void printInfo(); 31 | private: 32 | int sid; 33 | }; 34 | 35 | void Student::printInfo() 36 | { 37 | Person::printInfo(); 38 | cout << "Student ID: " << sid << endl; 39 | } 40 | 41 | int main() 42 | { 43 | Student kim; 44 | kim.printInfo(); 45 | Student lee(100); 46 | lee.printInfo(); 47 | Student park("PSJ", 200); 48 | park.printInfo(); 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /demo/lecture09/4_inheritance_protected.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Person 6 | { 7 | public: 8 | Person() : name("not set") {} 9 | Person(string name) : name(name) {} 10 | string getName() const {return name;} 11 | void setName(string name) {this->name=name;} 12 | void printInfo() const; 13 | protected: 14 | string name; 15 | }; 16 | 17 | void Person::printInfo() const 18 | { 19 | cout << "Name: " << name << endl; 20 | } 21 | 22 | class Student : public Person 23 | { 24 | public: 25 | Student() : Person(), sid(0) {} 26 | Student(int sid) : Person(), sid(sid) {} 27 | Student(string name, int sid) : Person(name), sid(sid) {} 28 | void setSid(int sid) {this->sid = sid;} 29 | int getSid() const {return sid;} 30 | void printInfo(); 31 | private: 32 | int sid; 33 | }; 34 | 35 | void Student::printInfo() 36 | { 37 | cout << "Name: " << name << endl; 38 | cout << "Student ID: " << sid << endl; 39 | } 40 | 41 | int main() 42 | { 43 | Student kim; 44 | kim.printInfo(); 45 | Student lee(100); 46 | lee.printInfo(); 47 | Student park("PSJ", 200); 48 | park.printInfo(); 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /demo/lecture09/test: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /demo/lecture10/0_class_basic_1.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Person 6 | { 7 | public: 8 | Person(); 9 | Person(string name); 10 | ~Person(); 11 | string getName() const {return name;} 12 | void setName(string name) {this->name=name;} 13 | void printInfo() const; 14 | private: 15 | string name; 16 | }; 17 | 18 | Person::Person() 19 | :name("Not set") 20 | { 21 | cout << "in Person()" << endl; 22 | } 23 | 24 | Person::Person(string name) 25 | : name(name) 26 | { 27 | cout << "in Person(string name)" << endl; 28 | } 29 | 30 | Person::~Person() 31 | { 32 | cout << "in ~Person()" << endl; 33 | } 34 | 35 | void Person::printInfo() const 36 | { 37 | cout << "Name: " << name << endl; 38 | } 39 | 40 | int main() 41 | { 42 | Person personA; 43 | personA.printInfo(); 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /demo/lecture10/0_class_basic_2.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Person 6 | { 7 | public: 8 | Person(); 9 | Person(string name); 10 | ~Person(); 11 | string getName() const {return name;} 12 | void setName(string name) {this->name=name;} 13 | void printInfo() const; 14 | private: 15 | string name; 16 | }; 17 | 18 | Person::Person() 19 | :name("Not set") 20 | { 21 | cout << "in Person()" << endl; 22 | } 23 | 24 | Person::Person(string name) 25 | : name(name) 26 | { 27 | cout << "in Person(string name)" << endl; 28 | } 29 | 30 | Person::~Person() 31 | { 32 | cout << "in ~Person()" << endl; 33 | } 34 | 35 | void Person::printInfo() const 36 | { 37 | cout << "Name: " << name << endl; 38 | } 39 | 40 | int main() 41 | { 42 | Person personA("Mike"); 43 | personA.printInfo(); 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /demo/lecture10/0_class_basic_3.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Person 6 | { 7 | public: 8 | Person(); 9 | Person(string name); 10 | ~Person(); 11 | string getName() const {return name;} 12 | void setName(string name) {this->name=name;} 13 | void printInfo() const; 14 | private: 15 | string name; 16 | int myCount; 17 | static int personCount; 18 | }; 19 | 20 | int Person::personCount = 0; 21 | 22 | Person::Person() 23 | :name("Not set") 24 | { 25 | cout << "in Person()" << endl; 26 | myCount = personCount++; 27 | } 28 | 29 | Person::Person(string name) 30 | : name(name) 31 | { 32 | cout << "in Person(string name)" << endl; 33 | myCount = personCount++; 34 | } 35 | 36 | Person::~Person() 37 | { 38 | cout << "in ~Person() #" << myCount << endl; 39 | } 40 | 41 | void Person::printInfo() const 42 | { 43 | cout << "Name: " << name << endl; 44 | cout << "Count: " << myCount << endl; 45 | } 46 | 47 | int main() 48 | { 49 | Person persons[10]; 50 | for(int i=0; i<10; i++) 51 | persons[i].printInfo(); 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /demo/lecture10/0_class_basic_4.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Person 6 | { 7 | public: 8 | Person(); 9 | Person(string name); 10 | ~Person(); 11 | string getName() const {return name;} 12 | void setName(string name) {this->name=name;} 13 | void printInfo() const; 14 | private: 15 | string name; 16 | }; 17 | 18 | Person::Person() 19 | :name("Not set") 20 | { 21 | cout << "in Person()" << endl; 22 | } 23 | 24 | Person::Person(string name) 25 | : name(name) 26 | { 27 | cout << "in Person(string name)" << endl; 28 | } 29 | 30 | Person::~Person() 31 | { 32 | cout << "in ~Person()" << endl; 33 | } 34 | 35 | void Person::printInfo() const 36 | { 37 | cout << "Name: " << name << endl; 38 | } 39 | 40 | int main() 41 | { 42 | Person personA; 43 | Person& personB = personA; 44 | personA.printInfo(); 45 | personB.printInfo(); 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /demo/lecture10/1_student_addmark.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Person 6 | { 7 | public: 8 | Person() : name("John Doe") {} 9 | Person(string name) : name(name) {} 10 | string getName() const {return name;} 11 | void setName(string name) {this->name=name;} 12 | void printInfo() const; 13 | private: 14 | string name; 15 | }; 16 | 17 | void Person::printInfo() const 18 | { 19 | cout << "Name: " << name << endl; 20 | } 21 | 22 | class Student : public Person 23 | { 24 | public: 25 | void setSid(int sid) {this->sid = sid;} 26 | int getSid() const {return sid;} 27 | void printInfo(); 28 | 29 | void addMark(); 30 | private: 31 | int sid; 32 | }; 33 | 34 | void Student::addMark() 35 | { 36 | string name = getName(); 37 | name += "(student)"; 38 | setName(name); 39 | } 40 | 41 | void Student::printInfo() 42 | { 43 | cout << "Name:\t" << getName() << endl; 44 | cout << "Student ID: " << sid << endl; 45 | } 46 | int main() 47 | { 48 | Student kim; 49 | kim.setName("Kim"); 50 | kim.setSid(100); 51 | kim.addMark(); 52 | kim.printInfo(); 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /demo/lecture10/1_student_addmark_reference.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Person 6 | { 7 | public: 8 | Person() : name("John Doe") {} 9 | Person(string name) : name(name) {} 10 | string& getName() {return name;} 11 | void setName(string name) {this->name=name;} 12 | void printInfo() const; 13 | private: 14 | string name; 15 | }; 16 | 17 | void Person::printInfo() const 18 | { 19 | cout << "Name: " << name << endl; 20 | } 21 | 22 | class Student : public Person 23 | { 24 | public: 25 | void setSid(int sid) {this->sid = sid;} 26 | int getSid() const {return sid;} 27 | void addMark(); 28 | void printInfo(); 29 | private: 30 | int sid; 31 | }; 32 | 33 | void Student::addMark() 34 | { 35 | string& name = getName(); 36 | name += "(student)"; 37 | } 38 | 39 | void Student::printInfo() 40 | { 41 | cout << "Name:\t" << getName() << endl; 42 | cout << "Student ID: " << sid << endl; 43 | } 44 | int main() 45 | { 46 | Student kim; 47 | kim.setName("Kim"); 48 | kim.setSid(100); 49 | kim.addMark(); 50 | kim.printInfo(); 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /demo/lecture10/1_student_redefine_setName.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Person 6 | { 7 | public: 8 | Person() : name("John Doe") {} 9 | Person(string name) : name(name) {} 10 | string getName() {return name;} 11 | void setName(string name) {this->name=name;} 12 | void printInfo() const; 13 | private: 14 | string name; 15 | }; 16 | 17 | void Person::printInfo() const 18 | { 19 | cout << "Name: " << name << endl; 20 | } 21 | 22 | class Student : public Person 23 | { 24 | public: 25 | void setSid(int sid) {this->sid = sid;} 26 | int getSid() const {return sid;} 27 | void setName(string name); 28 | void printInfo(); 29 | private: 30 | int sid; 31 | }; 32 | 33 | void Student::setName(string name) 34 | { 35 | Person::setName(name+"(student)"); 36 | } 37 | 38 | void Student::printInfo() 39 | { 40 | cout << "Name:\t" << getName() << endl; 41 | cout << "Student ID: " << sid << endl; 42 | } 43 | 44 | int main() 45 | { 46 | Student kim; 47 | kim.setName("Kim"); 48 | kim.setSid(100); 49 | kim.printInfo(); 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /demo/lecture10/1_student_redefine_setName_protected.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Person 6 | { 7 | public: 8 | Person() : name("John Doe") {} 9 | Person(string name) : name(name) {} 10 | string getName() {return name;} 11 | void setName(string name) {this->name=name;} 12 | void printInfo() const; 13 | protected: 14 | string name; 15 | }; 16 | 17 | void Person::printInfo() const 18 | { 19 | cout << "Name: " << name << endl; 20 | } 21 | 22 | class Student : public Person 23 | { 24 | public: 25 | void setSid(int sid) {this->sid = sid;} 26 | int getSid() const {return sid;} 27 | void setName(string name); 28 | void printInfo(); 29 | private: 30 | int sid; 31 | }; 32 | 33 | void Student::setName(string name) 34 | { 35 | this->name = name+"(student)"; 36 | } 37 | 38 | void Student::printInfo() 39 | { 40 | cout << "Name:\t" << getName() << endl; 41 | cout << "Student ID: " << sid << endl; 42 | } 43 | 44 | int main() 45 | { 46 | Student kim; 47 | kim.setName("Kim"); 48 | kim.setSid(100); 49 | kim.printInfo(); 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /demo/lecture10/2_call_sq_dummy.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class GrandParent 6 | { 7 | public: 8 | GrandParent() { cout << "in GrandParent()" << endl; } 9 | ~GrandParent() { cout << "in ~GrandParent()" << endl; } 10 | }; 11 | 12 | class Parent : public GrandParent 13 | { 14 | public: 15 | Parent() { cout << "in Parent(int age)" << endl; } 16 | ~Parent() { cout << "in ~Parent()" << endl; } 17 | }; 18 | 19 | class Child : public Parent 20 | { 21 | public: 22 | Child() { cout << "in Child()" << endl; } 23 | ~Child() { cout << "in ~Child()" << endl; } 24 | }; 25 | 26 | int main() 27 | { 28 | Child child; 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /demo/lecture10/2_call_sq_initializing_ctor.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class GrandParent 6 | { 7 | public: 8 | GrandParent():age(1000) { cout << "in GrandParent()" << endl; } 9 | GrandParent(int age) : age(age) { cout << "in GrandParent(int age)" << endl; } 10 | void printAge() { cout << age << endl; } 11 | ~GrandParent() { cout << "in ~GrandParent()" << endl; } 12 | private: 13 | int age; 14 | }; 15 | 16 | class Parent : public GrandParent 17 | { 18 | public: 19 | Parent(int age) : GrandParent(age){ 20 | cout << "in Parent(int age)" << endl; 21 | } 22 | ~Parent() { cout << "in ~Parent()" << endl; } 23 | // We cannot initialize parents' member variable. 24 | //Parent(int age) : age(age) { cout << "in Parent(int age)" << endl; } 25 | }; 26 | 27 | int main() 28 | { 29 | Parent parent(100); 30 | parent.printAge(); 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /demo/lecture10/2_call_sq_initializing_protected.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class GrandParent 6 | { 7 | public: 8 | GrandParent():age(1000) { cout << "in GrandParent()" << endl; } 9 | GrandParent(int age) : age(age) { cout << "in GrandParent(int age)" << endl; } 10 | void printAge() { cout << age << endl; } 11 | ~GrandParent() { cout << "in ~GrandParent()" << endl; } 12 | protected: 13 | int age; 14 | }; 15 | 16 | class Parent : public GrandParent 17 | { 18 | public: 19 | Parent(int age) { 20 | this->age = age; 21 | cout << "in Parent(int age)" << endl; 22 | } 23 | ~Parent() { cout << "in ~Parent()" << endl; } 24 | // We cannot initialize parents' member variable. 25 | //Parent(int age) : age(age) { cout << "in Parent(int age)" << endl; } 26 | }; 27 | 28 | int main() 29 | { 30 | Parent parent(100); 31 | parent.printAge(); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /demo/lecture10/2_call_sq_undefined_default.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class GrandParent 6 | { 7 | public: 8 | GrandParent():age(1000) { cout << "in GrandParent()" << endl; } 9 | GrandParent(int age) : age(age) { cout << "in GrandParent(int age)" << endl; } 10 | void printAge() { cout << age << endl; } 11 | ~GrandParent() { cout << "in ~GrandParent()" << endl; } 12 | protected: 13 | int age; 14 | }; 15 | 16 | class Parent : public GrandParent 17 | { 18 | public: 19 | Parent(int age) { 20 | this->age = age; 21 | cout << "in Parent(int age)" << endl; 22 | } 23 | ~Parent() { cout << "in ~Parent()" << endl; } 24 | // We cannot initialize parents' member variable. 25 | //Parent(int age) : age(age) { cout << "in Parent(int age)" << endl; } 26 | }; 27 | 28 | class Child : public Parent 29 | { 30 | public: 31 | Child() : Parent(0) { cout << "in Child()" << endl; } 32 | // The following line causes an error 33 | // because there is no default ctor in Parent class. 34 | //Child() { } 35 | ~Child() { cout << "in ~Child()" << endl; } 36 | }; 37 | 38 | int main() 39 | { 40 | Child child; 41 | child.printAge(); 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /demo/lecture10/test: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /demo/lecture11/1_pet.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Pet 6 | { 7 | public: 8 | string name; 9 | virtual void print() const; 10 | }; 11 | 12 | class Dog : public Pet 13 | { 14 | public: 15 | string breed; 16 | virtual void print() const; 17 | }; 18 | 19 | void Pet::print() const 20 | { 21 | cout << "name: " << name << endl; 22 | } 23 | 24 | void Dog::print() const 25 | { 26 | Pet::print(); 27 | cout << "breed: " << breed << endl; 28 | } 29 | 30 | int main() 31 | { 32 | Dog dog; 33 | 34 | dog.name = "Tiny"; 35 | dog.breed = "Great Dane"; 36 | dog.print(); 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /demo/lecture11/2_pet_override_final.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Pet 6 | { 7 | public: 8 | string name; 9 | virtual void print() const; 10 | }; 11 | 12 | class Dog : public Pet 13 | { 14 | public: 15 | string breed; 16 | void print() const override; 17 | }; 18 | 19 | class MyDog : public Dog 20 | { 21 | public: 22 | string address; 23 | void print() const override; 24 | }; 25 | 26 | void Pet::print() const 27 | { 28 | cout << "name: " << name << endl; 29 | } 30 | 31 | void Dog::print() const 32 | { 33 | Pet::print(); 34 | cout << "breed: " << breed << endl; 35 | } 36 | 37 | void MyDog::print() const 38 | { 39 | Dog::print(); 40 | cout << "address: " << address << endl; 41 | } 42 | 43 | 44 | int main() 45 | { 46 | MyDog myDog; 47 | 48 | myDog.name = "Tiny"; 49 | myDog.breed = "Great Dane"; 50 | myDog.address = "2066"; 51 | 52 | myDog.print(); 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /demo/lecture11/3_pet_pure_virtual.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Pet 6 | { 7 | public: 8 | string name; 9 | virtual void print() const = 0; 10 | }; 11 | 12 | class Dog : public Pet 13 | { 14 | public: 15 | string breed; 16 | void print() const override final; 17 | }; 18 | 19 | void Pet::print() const 20 | { 21 | cout << "name: " << name << endl; 22 | } 23 | 24 | void Dog::print() const 25 | { 26 | Pet::print(); 27 | cout << "breed: " << breed << endl; 28 | } 29 | 30 | int main() 31 | { 32 | Dog dog; 33 | 34 | dog.name = "Tiny"; 35 | dog.breed = "Great Dane"; 36 | 37 | dog.print(); 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /demo/lecture11/4_1_pet_slicing_prob.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Pet 6 | { 7 | public: 8 | string name; 9 | virtual void print() const; 10 | Pet() { 11 | } 12 | Pet(const Pet& pet) { 13 | cout << "in copy ctor(Pet)" << endl; 14 | name = pet.name; 15 | } 16 | }; 17 | 18 | class Dog : public Pet 19 | { 20 | public: 21 | string breed; 22 | void print() const; 23 | Dog() { 24 | } 25 | Dog(const Dog& dog):Pet(dog), breed(dog.breed) { 26 | cout << "in copy ctor(Pet)" << endl; 27 | } 28 | }; 29 | 30 | void Pet::print() const 31 | { 32 | cout << "name: " << name << endl; 33 | } 34 | 35 | void Dog::print() const 36 | { 37 | Pet::print(); 38 | cout << "breed: " << breed << endl; 39 | } 40 | 41 | int main() 42 | { 43 | Dog dog; 44 | 45 | dog.name = "Tiny"; 46 | dog.breed = "Great Dane"; 47 | 48 | Pet pet; 49 | pet = dog; 50 | // We can have deeper observation with the following line 51 | //Pet pet = dog; 52 | 53 | // Following line is illegal 54 | //cout << pet.breed << endl; 55 | pet.print(); 56 | 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /demo/lecture11/4_2_pet_slicing_prob.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Pet 6 | { 7 | public: 8 | string name; 9 | virtual void print() const; 10 | Pet() { 11 | } 12 | }; 13 | 14 | class Dog : public Pet 15 | { 16 | public: 17 | string breed; 18 | void print() const; 19 | Dog() { 20 | } 21 | }; 22 | 23 | void Pet::print() const 24 | { 25 | cout << "name: " << name << endl; 26 | } 27 | 28 | void Dog::print() const 29 | { 30 | Pet::print(); 31 | cout << "breed: " << breed << endl; 32 | } 33 | 34 | int main() 35 | { 36 | Dog* dog; 37 | Pet* pet; 38 | dog = new Dog; 39 | 40 | dog->name = "Tiny"; 41 | dog->breed = "Great Dane"; 42 | pet = dog; 43 | // Following line is still illegal 44 | //cout << pet->breed << endl; 45 | pet->print(); 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /demo/lecture11/test: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /demo/lecture12/1_money.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | //Class for amounts of money in U.S. currency. 7 | class Money 8 | { 9 | public: 10 | Money(); 11 | Money(int theDollars, int theCents); 12 | Money(int theDollars); 13 | double getAmount() const; 14 | int getDollars() const; 15 | int getCents() const; 16 | void output() const; 17 | private: 18 | int dollars; //A negative amount is represented as negative dollars and 19 | int cents; //negative cents. Negative $4.50 is represented as -4 and -50 20 | }; 21 | 22 | const Money operator+(const Money& amount1, const Money& amount2); 23 | const Money operator-(const Money& amount1, const Money& amount2); 24 | bool operator==(const Money& amount1, const Money& amount2); 25 | const Money operator-(const Money& amount); 26 | 27 | int main() 28 | { 29 | Money amount1(10), amount2(6), amount3; 30 | amount3 = amount1 - amount2; 31 | //Calls binary "-" overload 32 | amount3.output(); //Displays $4.00 33 | amount3 = -amount1; 34 | //Calls unary "-" overload 35 | amount3.output(); //Displays -$10.00 36 | 37 | return 0; 38 | } 39 | 40 | const Money operator+(const Money& amount1, const Money& amount2) 41 | { 42 | int allCents1 = amount1.getCents() + amount1.getDollars()*100; 43 | int allCents2 = amount2.getCents() + amount2.getDollars()*100; 44 | int sumAllCents = allCents1 + allCents2; 45 | int absAllCents = abs(sumAllCents); //Money can be negative. 46 | int finalDollars = absAllCents/100; 47 | int finalCents = absAllCents%100; 48 | 49 | if (sumAllCents < 0) { 50 | finalDollars = -finalDollars; 51 | finalCents = -finalCents; 52 | } 53 | 54 | return Money(finalDollars, finalCents); 55 | } 56 | 57 | //Uses cstdlib: 58 | const Money operator-(const Money& amount1, const Money& amount2) 59 | { 60 | int allCents1 = amount1.getCents() + amount1.getDollars()*100; 61 | int allCents2 = amount2.getCents() + amount2.getDollars()*100; 62 | int diffAllCents = allCents1 - allCents2; 63 | int absAllCents = abs(diffAllCents); 64 | int finalDollars = absAllCents/100; 65 | int finalCents = absAllCents%100; 66 | 67 | if (diffAllCents < 0) { 68 | finalDollars = -finalDollars; 69 | finalCents = -finalCents; 70 | } 71 | 72 | return Money(finalDollars, finalCents); 73 | } 74 | 75 | bool operator==(const Money& amount1, const Money& amount2) 76 | { 77 | return ((amount1.getDollars() == amount2.getDollars()) 78 | && (amount1.getCents() == amount2.getCents())); 79 | } 80 | 81 | const Money operator-(const Money& amount) 82 | { 83 | return Money(-amount.getDollars(), -amount.getCents()); 84 | } 85 | 86 | Money::Money(): dollars(0), cents(0) 87 | {/*Body intentionally empty.*/} 88 | 89 | Money::Money(int theDollars) 90 | : dollars(theDollars), cents(0) 91 | {/*Body intentionally empty*/} 92 | 93 | //Uses cstdlib: 94 | Money::Money(int theDollars, int theCents) 95 | { 96 | if ((theDollars < 0 && theCents > 0) || (theDollars > 0 && theCents < 0)) { 97 | cout << "Inconsistent money data.\n"; 98 | exit(1); 99 | } 100 | dollars = theDollars; 101 | cents = theCents; 102 | } 103 | 104 | double Money::getAmount() const 105 | { 106 | return (dollars + cents*0.01); 107 | } 108 | 109 | int Money::getDollars() const 110 | { 111 | return dollars; 112 | } 113 | 114 | int Money::getCents() const 115 | { 116 | return cents; 117 | } 118 | 119 | //Uses iostream and cstdlib: 120 | void Money::output() const 121 | { 122 | int absDollars = abs(dollars); 123 | int absCents = abs(cents); 124 | if (dollars < 0 || cents < 0)//accounts for dollars == 0 or cents == 0 125 | cout << "$-"; 126 | else 127 | cout << '$'; 128 | cout << absDollars; 129 | 130 | if (absCents >= 10) 131 | cout << '.' << absCents; 132 | else 133 | cout << '.' << '0' << absCents; 134 | cout << "\n"; 135 | } 136 | -------------------------------------------------------------------------------- /demo/lecture12/2_int.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int operator+(const int num1, const int num2) 6 | { 7 | return num1 * num2; 8 | } 9 | int main() 10 | { 11 | cout << 11+11 << endl; 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /demo/lecture12/3_money_member.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | //Class for amounts of money in U.S. currency. 7 | class Money 8 | { 9 | public: 10 | Money(); 11 | Money(int dollars, int cents); 12 | Money(int dollars); 13 | double getAmount() const; 14 | int getDollars() const; 15 | int getCents() const; 16 | void input(); //Reads the dollar sign as well as the amount number. 17 | void output() const; 18 | const Money operator+(const Money& amount2) const; 19 | const Money operator-(const Money& amount2) const; 20 | bool operator==(const Money& amount2) const; 21 | const Money operator-() const; 22 | private: 23 | int dollars; //A negative amount is represented as negative dollars and 24 | int cents; //negative cents. Negative $4.50 is represented as -4 and -50 25 | }; 26 | 27 | int main() 28 | { 29 | Money amount1(10), amount2(6), amount3; 30 | amount3 = amount1 - amount2; 31 | //Calls binary "-" overload 32 | amount3.output(); //Displays $4.00 33 | amount3 = -amount1; 34 | //Calls unary "-" overload 35 | amount3.output(); //Displays -$10.00 36 | 37 | return 0; 38 | } 39 | 40 | const Money Money::operator+(const Money& secondOperand) const 41 | { 42 | int allCents1 = cents + dollars*100; 43 | int allCents2 = secondOperand.cents + secondOperand.dollars*100; 44 | int sumAllCents = allCents1 + allCents2; 45 | int absAllCents = abs(sumAllCents); //Money can be negative. 46 | int finalDollars = absAllCents/100; 47 | int finalCents = absAllCents%100; 48 | 49 | if (sumAllCents < 0) { 50 | finalDollars = -finalDollars; 51 | finalCents = -finalCents; 52 | } 53 | 54 | return Money(finalDollars, finalCents); 55 | } 56 | 57 | const Money Money::operator-(const Money& secondOperand) const 58 | { 59 | int allCents1 = cents + dollars*100; 60 | int allCents2 = secondOperand.cents 61 | + secondOperand.dollars*100; 62 | int diffAllCents = allCents1 - allCents2; 63 | int absAllCents = abs(diffAllCents); 64 | int finalDollars = absAllCents/100; 65 | int finalCents = absAllCents%100; 66 | 67 | if (diffAllCents < 0) { 68 | finalDollars = -finalDollars; 69 | finalCents = -finalCents; 70 | } 71 | 72 | return Money(finalDollars, finalCents); 73 | } 74 | bool Money::operator==(const Money& secondOperand) const 75 | { 76 | return ((dollars == secondOperand.dollars) 77 | && (cents == secondOperand.cents)); 78 | } 79 | 80 | const Money Money::operator-() const 81 | { 82 | return Money(-dollars, -cents); 83 | } 84 | 85 | Money::Money(): dollars(0), cents(0) 86 | {/*Body intentionally empty.*/} 87 | 88 | Money::Money(int theDollars) 89 | : dollars(theDollars), cents(0) 90 | {/*Body intentionally empty*/} 91 | 92 | //Uses cstdlib: 93 | Money::Money(int theDollars, int theCents) 94 | { 95 | if ((theDollars < 0 && theCents > 0) || (theDollars > 0 && theCents < 0)) { 96 | cout << "Inconsistent money data.\n"; 97 | exit(1); 98 | } 99 | dollars = theDollars; 100 | cents = theCents; 101 | } 102 | 103 | double Money::getAmount() const 104 | { 105 | return (dollars + cents*0.01); 106 | } 107 | 108 | int Money::getDollars() const 109 | { 110 | return dollars; 111 | } 112 | 113 | int Money::getCents() const 114 | { 115 | return cents; 116 | } 117 | 118 | //Uses iostream and cstdlib: 119 | void Money::output() const 120 | { 121 | int absDollars = abs(dollars); 122 | int absCents = abs(cents); 123 | if (dollars < 0 || cents < 0)//accounts for dollars == 0 or cents == 0 124 | cout << "$-"; 125 | else 126 | cout << '$'; 127 | cout << absDollars; 128 | 129 | if (absCents >= 10) 130 | cout << '.' << absCents; 131 | else 132 | cout << '.' << '0' << absCents; 133 | cout << endl; 134 | } 135 | -------------------------------------------------------------------------------- /demo/lecture12/4_money_member.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | //Class for amounts of money in U.S. currency. 7 | class Money 8 | { 9 | public: 10 | Money(); 11 | Money(int theDollars, int theCents); 12 | Money(int theDollars); 13 | double getAmount() const; 14 | int getDollars() const; 15 | int getCents() const; 16 | void output() const; 17 | 18 | const Money operator-(); 19 | void operator()(int theDollars); 20 | void operator()(int theDollars, int theCents); 21 | private: 22 | int dollars; //A negative amount is represented as negative dollars and 23 | int cents; //negative cents. Negative $4.50 is represented as -4 and -50 24 | }; 25 | 26 | const Money Money::operator-() 27 | { 28 | return Money(-getDollars(), -getCents()); 29 | } 30 | 31 | void Money::operator()(int theDollars) 32 | { 33 | dollars = theDollars; 34 | } 35 | 36 | void Money::operator()(int theDollars, int theCents) 37 | { 38 | dollars = theDollars; 39 | cents = theCents; 40 | } 41 | 42 | const Money operator+(const Money& amount1, const Money& amount2); 43 | const Money operator-(const Money& amount1, const Money& amount2); 44 | bool operator==(const Money& amount1, const Money& amount2); 45 | 46 | int main() 47 | { 48 | Money amount1(10), amount2(6), amount3; 49 | amount3 = amount1 - amount2; 50 | //Calls binary "-" overload 51 | amount3.output(); //Displays $4.00 52 | amount3 = -amount1; 53 | //Calls unary "-" overload 54 | amount3.output(); //Displays -$10.00 55 | 56 | amount3(100,99); 57 | amount3.output(); //Displays -$10.00 58 | 59 | return 0; 60 | } 61 | 62 | const Money operator+(const Money& amount1, const Money& amount2) 63 | { 64 | int allCents1 = amount1.getCents() + amount1.getDollars()*100; 65 | int allCents2 = amount2.getCents() + amount2.getDollars()*100; 66 | int sumAllCents = allCents1 + allCents2; 67 | int absAllCents = abs(sumAllCents); //Money can be negative. 68 | int finalDollars = absAllCents/100; 69 | int finalCents = absAllCents%100; 70 | 71 | if (sumAllCents < 0) { 72 | finalDollars = -finalDollars; 73 | finalCents = -finalCents; 74 | } 75 | 76 | return Money(finalDollars, finalCents); 77 | } 78 | 79 | //Uses cstdlib: 80 | const Money operator-(const Money& amount1, const Money& amount2) 81 | { 82 | int allCents1 = amount1.getCents() + amount1.getDollars()*100; 83 | int allCents2 = amount2.getCents() + amount2.getDollars()*100; 84 | int diffAllCents = allCents1 - allCents2; 85 | int absAllCents = abs(diffAllCents); 86 | int finalDollars = absAllCents/100; 87 | int finalCents = absAllCents%100; 88 | 89 | if (diffAllCents < 0) { 90 | finalDollars = -finalDollars; 91 | finalCents = -finalCents; 92 | } 93 | 94 | return Money(finalDollars, finalCents); 95 | } 96 | 97 | bool operator==(const Money& amount1, const Money& amount2) 98 | { 99 | return ((amount1.getDollars() == amount2.getDollars()) 100 | && (amount1.getCents() == amount2.getCents())); 101 | } 102 | 103 | Money::Money(): dollars(0), cents(0) 104 | {/*Body intentionally empty.*/} 105 | 106 | Money::Money(int theDollars) 107 | : dollars(theDollars), cents(0) 108 | {/*Body intentionally empty*/} 109 | 110 | //Uses cstdlib: 111 | Money::Money(int theDollars, int theCents) 112 | { 113 | if ((theDollars < 0 && theCents > 0) || (theDollars > 0 && theCents < 0)) { 114 | cout << "Inconsistent money data.\n"; 115 | exit(1); 116 | } 117 | dollars = theDollars; 118 | cents = theCents; 119 | } 120 | 121 | double Money::getAmount() const 122 | { 123 | return (dollars + cents*0.01); 124 | } 125 | 126 | int Money::getDollars() const 127 | { 128 | return dollars; 129 | } 130 | 131 | int Money::getCents() const 132 | { 133 | return cents; 134 | } 135 | 136 | //Uses iostream and cstdlib: 137 | void Money::output() const 138 | { 139 | int absDollars = abs(dollars); 140 | int absCents = abs(cents); 141 | if (dollars < 0 || cents < 0)//accounts for dollars == 0 or cents == 0 142 | cout << "$-"; 143 | else 144 | cout << '$'; 145 | cout << absDollars; 146 | 147 | if (absCents >= 10) 148 | cout << '.' << absCents; 149 | else 150 | cout << '.' << '0' << absCents; 151 | cout << "\n"; 152 | } 153 | -------------------------------------------------------------------------------- /demo/lecture12/5_money_all.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | //Class for amounts of money in U.S. currency. 7 | class Money 8 | { 9 | public: 10 | Money(); 11 | Money(double amount); 12 | Money(int theDollars, int theCents); 13 | Money(int theDollars); 14 | double getAmount() const; 15 | int getDollars() const; 16 | int getCents() const; 17 | friend const Money operator+(const Money& amount1, const Money& amount2); 18 | friend const Money operator-(const Money& amount1, const Money& amount2); 19 | friend bool operator==(const Money& amount1, const Money& amount2); 20 | friend const Money operator-(const Money& amount); 21 | friend ostream& operator<<(ostream& outputStream, const Money& amount); 22 | friend istream& operator>>(istream& inputStream, Money& amount); 23 | private: 24 | int dollars; //A negative amount is represented as negative dollars and 25 | int cents; //negative cents. Negative $4.50 is represented as -4 and -50 26 | 27 | int dollarsPart(double amount) const; 28 | int centsPart(double amount) const; 29 | int round(double number) const; 30 | }; 31 | 32 | int main() 33 | { 34 | Money yourAmount, myAmount(10, 9); 35 | cout << "Enter an amount of money: "; 36 | cin >> yourAmount; 37 | cout << "Your amount is " << yourAmount << endl; 38 | cout << "My amount is " << myAmount << endl; 39 | 40 | if (yourAmount == myAmount) 41 | cout << "We have the same amounts.\n"; 42 | else 43 | cout << "One of us is richer.\n"; 44 | 45 | Money ourAmount = yourAmount + myAmount; 46 | cout << yourAmount << " + " << myAmount 47 | << " equals " << ourAmount << endl; 48 | 49 | Money diffAmount = yourAmount - myAmount; 50 | cout << yourAmount << " - " << myAmount 51 | << " equals " << diffAmount << endl; 52 | 53 | return 0; 54 | } 55 | 56 | ostream& operator<<(ostream& outputStream, const Money& amount) 57 | { 58 | int absDollars = abs(amount.dollars); 59 | int absCents = abs(amount.cents); 60 | if (amount.dollars < 0 || amount.cents < 0) 61 | //accounts for dollars == 0 or cents == 0 62 | outputStream << "$-"; 63 | else 64 | outputStream << '$'; 65 | outputStream << absDollars; 66 | 67 | if (absCents >= 10) 68 | outputStream << '.' << absCents; 69 | else 70 | outputStream << '.' << '0' << absCents; 71 | 72 | return outputStream; 73 | } 74 | 75 | //Uses iostream and cstdlib: 76 | istream& operator>>(istream& inputStream, Money& amount) 77 | { 78 | char dollarSign; 79 | inputStream >> dollarSign; //hopefully 80 | if (dollarSign != '$') { 81 | cout << "No dollar sign in Money input.\n"; 82 | exit(1); 83 | } 84 | 85 | double amountAsDouble; 86 | inputStream >> amountAsDouble; 87 | amount.dollars = amount.dollarsPart(amountAsDouble); 88 | amount.cents = amount.centsPart(amountAsDouble); 89 | 90 | return inputStream; 91 | } 92 | 93 | const Money operator+(const Money& amount1, const Money& amount2) 94 | { 95 | int allCents1 = amount1.cents + amount1.dollars*100; 96 | int allCents2 = amount2.cents + amount2.dollars*100; 97 | int sumAllCents = allCents1 + allCents2; 98 | int absAllCents = abs(sumAllCents); //Money can be negative. 99 | int finalDollars = absAllCents/100; 100 | int finalCents = absAllCents%100; 101 | 102 | if (sumAllCents < 0) { 103 | finalDollars = -finalDollars; 104 | finalCents = -finalCents; 105 | } 106 | 107 | return Money(finalDollars, finalCents); 108 | } 109 | 110 | //Uses cstdlib: 111 | const Money operator-(const Money& amount1, const Money& amount2) 112 | { 113 | int allCents1 = amount1.cents + amount1.dollars*100; 114 | int allCents2 = amount2.cents + amount2.dollars*100; 115 | int diffAllCents = allCents1 - allCents2; 116 | int absAllCents = abs(diffAllCents); 117 | 118 | int finalDollars = absAllCents/100; 119 | int finalCents = absAllCents%100; 120 | 121 | if (diffAllCents < 0) { 122 | finalDollars = -finalDollars; 123 | finalCents = -finalCents; 124 | } 125 | 126 | return Money(finalDollars, finalCents); 127 | } 128 | 129 | bool operator==(const Money& amount1, const Money& amount2) 130 | { 131 | return ((amount1.dollars == amount2.dollars) 132 | && (amount1.cents == amount2.cents)); 133 | } 134 | 135 | const Money operator-(const Money& amount) 136 | { 137 | return Money(-amount.dollars, -amount.cents); 138 | } 139 | 140 | 141 | Money::Money(): dollars(0), cents(0) 142 | {/*Body intentionally empty.*/} 143 | 144 | Money::Money(double amount) 145 | : dollars(dollarsPart(amount)), cents(centsPart(amount)) 146 | {/*Body intentionally empty*/} 147 | 148 | Money::Money(int theDollars) 149 | : dollars(theDollars), cents(0) 150 | {/*Body intentionally empty*/} 151 | 152 | //Uses cstdlib: 153 | Money::Money(int theDollars, int theCents) 154 | { 155 | if ((theDollars < 0 && theCents > 0) || (theDollars > 0 && theCents < 0)) { 156 | cout << "Inconsistent money data.\n"; 157 | exit(1); 158 | } 159 | dollars = theDollars; 160 | cents = theCents; 161 | } 162 | 163 | double Money::getAmount() const 164 | { 165 | return (dollars + cents*0.01); 166 | } 167 | 168 | int Money::getDollars() const 169 | { 170 | return dollars; 171 | } 172 | 173 | int Money::getCents() const 174 | { 175 | return cents; 176 | } 177 | 178 | int Money::dollarsPart(double amount) const 179 | { 180 | return static_cast(amount); 181 | } 182 | 183 | int Money::centsPart(double amount) const 184 | { 185 | double doubleCents = amount*100; 186 | int intCents = (round(fabs(doubleCents)))%100;//% can misbehave on negatives 187 | if (amount < 0) 188 | intCents = -intCents; 189 | return intCents; 190 | } 191 | 192 | int Money::round(double number) const 193 | { 194 | return static_cast(floor(number + 0.5)); 195 | } 196 | -------------------------------------------------------------------------------- /demo/lecture12/5_x_money_ostream_member.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | //Class for amounts of money in U.S. currency. 7 | class Money 8 | { 9 | public: 10 | Money(); 11 | Money(double amount); 12 | Money(int theDollars, int theCents); 13 | Money(int theDollars); 14 | double getAmount() const; 15 | int getDollars() const; 16 | int getCents() const; 17 | friend const Money operator+(const Money& amount1, const Money& amount2); 18 | friend const Money operator-(const Money& amount1, const Money& amount2); 19 | friend bool operator==(const Money& amount1, const Money& amount2); 20 | friend const Money operator-(const Money& amount); 21 | ostream& operator<<(ostream& outputStream) const; 22 | friend istream& operator>>(istream& inputStream, Money& amount); 23 | private: 24 | int dollars; //A negative amount is represented as negative dollars and 25 | int cents; //negative cents. Negative $4.50 is represented as -4 and -50 26 | 27 | int dollarsPart(double amount) const; 28 | int centsPart(double amount) const; 29 | int round(double number) const; 30 | }; 31 | 32 | ostream& Money::operator<<(ostream& outputStream) const 33 | { 34 | int absDollars = abs(dollars); 35 | int absCents = abs(cents); 36 | if (dollars < 0 || cents < 0) 37 | //accounts for dollars == 0 or cents == 0 38 | outputStream << "$-"; 39 | else 40 | outputStream << '$'; 41 | outputStream << absDollars; 42 | 43 | if (absCents >= 10) 44 | outputStream << '.' << absCents; 45 | else 46 | outputStream << '.' << '0' << absCents; 47 | 48 | return outputStream; 49 | } 50 | 51 | 52 | int main() 53 | { 54 | Money yourAmount, myAmount(10, 9); 55 | cout << "Enter an amount of money: "; 56 | cin >> yourAmount; 57 | cout << "Your amount is " << yourAmount << endl; 58 | cout << "My amount is " << myAmount << endl; 59 | 60 | if (yourAmount == myAmount) 61 | cout << "We have the same amounts.\n"; 62 | else 63 | cout << "One of us is richer.\n"; 64 | 65 | Money ourAmount = yourAmount + myAmount; 66 | cout << yourAmount << " + " << myAmount 67 | << " equals " << ourAmount << endl; 68 | 69 | Money diffAmount = yourAmount - myAmount; 70 | cout << yourAmount << " - " << myAmount 71 | << " equals " << diffAmount << endl; 72 | 73 | return 0; 74 | } 75 | //Uses iostream and cstdlib: 76 | istream& operator>>(istream& inputStream, Money& amount) 77 | { 78 | char dollarSign; 79 | inputStream >> dollarSign; //hopefully 80 | if (dollarSign != '$') { 81 | cout << "No dollar sign in Money input.\n"; 82 | exit(1); 83 | } 84 | 85 | double amountAsDouble; 86 | inputStream >> amountAsDouble; 87 | amount.dollars = amount.dollarsPart(amountAsDouble); 88 | amount.cents = amount.centsPart(amountAsDouble); 89 | 90 | return inputStream; 91 | } 92 | 93 | const Money operator+(const Money& amount1, const Money& amount2) 94 | { 95 | int allCents1 = amount1.cents + amount1.dollars*100; 96 | int allCents2 = amount2.cents + amount2.dollars*100; 97 | int sumAllCents = allCents1 + allCents2; 98 | int absAllCents = abs(sumAllCents); //Money can be negative. 99 | int finalDollars = absAllCents/100; 100 | int finalCents = absAllCents%100; 101 | 102 | if (sumAllCents < 0) { 103 | finalDollars = -finalDollars; 104 | finalCents = -finalCents; 105 | } 106 | 107 | return Money(finalDollars, finalCents); 108 | } 109 | 110 | //Uses cstdlib: 111 | const Money operator-(const Money& amount1, const Money& amount2) 112 | { 113 | int allCents1 = amount1.cents + amount1.dollars*100; 114 | int allCents2 = amount2.cents + amount2.dollars*100; 115 | int diffAllCents = allCents1 - allCents2; 116 | int absAllCents = abs(diffAllCents); 117 | 118 | int finalDollars = absAllCents/100; 119 | int finalCents = absAllCents%100; 120 | 121 | if (diffAllCents < 0) { 122 | finalDollars = -finalDollars; 123 | finalCents = -finalCents; 124 | } 125 | 126 | return Money(finalDollars, finalCents); 127 | } 128 | 129 | bool operator==(const Money& amount1, const Money& amount2) 130 | { 131 | return ((amount1.dollars == amount2.dollars) 132 | && (amount1.cents == amount2.cents)); 133 | } 134 | 135 | const Money operator-(const Money& amount) 136 | { 137 | return Money(-amount.dollars, -amount.cents); 138 | } 139 | 140 | 141 | Money::Money(): dollars(0), cents(0) 142 | {/*Body intentionally empty.*/} 143 | 144 | Money::Money(double amount) 145 | : dollars(dollarsPart(amount)), cents(centsPart(amount)) 146 | {/*Body intentionally empty*/} 147 | 148 | Money::Money(int theDollars) 149 | : dollars(theDollars), cents(0) 150 | {/*Body intentionally empty*/} 151 | 152 | //Uses cstdlib: 153 | Money::Money(int theDollars, int theCents) 154 | { 155 | if ((theDollars < 0 && theCents > 0) || (theDollars > 0 && theCents < 0)) { 156 | cout << "Inconsistent money data.\n"; 157 | exit(1); 158 | } 159 | dollars = theDollars; 160 | cents = theCents; 161 | } 162 | 163 | double Money::getAmount() const 164 | { 165 | return (dollars + cents*0.01); 166 | } 167 | 168 | int Money::getDollars() const 169 | { 170 | return dollars; 171 | } 172 | 173 | int Money::getCents() const 174 | { 175 | return cents; 176 | } 177 | 178 | int Money::dollarsPart(double amount) const 179 | { 180 | return static_cast(amount); 181 | } 182 | 183 | int Money::centsPart(double amount) const 184 | { 185 | double doubleCents = amount*100; 186 | int intCents = (round(fabs(doubleCents)))%100;//% can misbehave on negatives 187 | if (amount < 0) 188 | intCents = -intCents; 189 | return intCents; 190 | } 191 | 192 | int Money::round(double number) const 193 | { 194 | return static_cast(floor(number + 0.5)); 195 | } 196 | -------------------------------------------------------------------------------- /demo/lecture12/6_money_assign.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | //Class for amounts of money in U.S. currency. 7 | class Money 8 | { 9 | public: 10 | Money(); 11 | Money(int theDollars, int theCents); 12 | Money(int theDollars); 13 | double getAmount() const; 14 | int getDollars() const; 15 | int getCents() const; 16 | void output() const; 17 | 18 | const Money& operator=(const Money& theMoney); 19 | private: 20 | int dollars; //A negative amount is represented as negative dollars and 21 | int cents; //negative cents. Negative $4.50 is represented as -4 and -50 22 | }; 23 | 24 | const Money& Money::operator=(const Money& theMoney) 25 | { 26 | // Just for example 27 | dollars = theMoney.getDollars() - 1; 28 | return *this; // Why should we return *this? 29 | } 30 | 31 | int main() 32 | { 33 | Money amount1(10), amount2, amount3; 34 | amount3 = amount2 = amount1; 35 | amount1.output(); 36 | amount2.output(); 37 | amount3.output(); 38 | 39 | return 0; 40 | } 41 | 42 | Money::Money(): dollars(0), cents(0) 43 | {/*Body intentionally empty.*/} 44 | 45 | Money::Money(int theDollars) 46 | : dollars(theDollars), cents(0) 47 | {/*Body intentionally empty*/} 48 | 49 | //Uses cstdlib: 50 | Money::Money(int theDollars, int theCents) 51 | { 52 | if ((theDollars < 0 && theCents > 0) || (theDollars > 0 && theCents < 0)) { 53 | cout << "Inconsistent money data.\n"; 54 | exit(1); 55 | } 56 | dollars = theDollars; 57 | cents = theCents; 58 | } 59 | 60 | double Money::getAmount() const 61 | { 62 | return (dollars + cents*0.01); 63 | } 64 | 65 | int Money::getDollars() const 66 | { 67 | return dollars; 68 | } 69 | 70 | int Money::getCents() const 71 | { 72 | return cents; 73 | } 74 | 75 | //Uses iostream and cstdlib: 76 | void Money::output() const 77 | { 78 | int absDollars = abs(dollars); 79 | int absCents = abs(cents); 80 | if (dollars < 0 || cents < 0)//accounts for dollars == 0 or cents == 0 81 | cout << "$-"; 82 | else 83 | cout << '$'; 84 | cout << absDollars; 85 | 86 | if (absCents >= 10) 87 | cout << '.' << absCents; 88 | else 89 | cout << '.' << '0' << absCents; 90 | cout << "\n"; 91 | } 92 | -------------------------------------------------------------------------------- /demo/lecture12/7_inc.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | //Class for amounts of money in U.S. currency. 7 | class Money 8 | { 9 | public: 10 | Money(); 11 | Money(int theDollars, int theCents); 12 | Money(int theDollars); 13 | double getAmount() const; 14 | int getDollars() const; 15 | int getCents() const; 16 | void output() const; 17 | 18 | Money operator++(); // preifx 19 | // Make members public just for the example 20 | //private: 21 | int dollars; //A negative amount is represented as negative dollars and 22 | int cents; //negative cents. Negative $4.50 is represented as -4 and -50 23 | }; 24 | 25 | // Postfix version, not a member 26 | Money operator++(Money& theMoney, int ignoreMe) 27 | { 28 | // We need range checks for cents. 29 | // This is just for an example. 30 | int dollars = theMoney.dollars++; 31 | int cents = theMoney.cents++; 32 | return Money(dollars, cents); 33 | } 34 | 35 | 36 | Money Money::operator++() 37 | { 38 | // We need range checks for cents. 39 | // This is just for an example. 40 | dollars++; 41 | cents++; 42 | return Money(dollars, cents); 43 | } 44 | 45 | int main() 46 | { 47 | Money amount(10); 48 | Money a = amount++; 49 | a.output(); 50 | amount.output(); 51 | 52 | amount = Money(10); 53 | a = ++amount; 54 | a.output(); 55 | amount.output(); 56 | 57 | return 0; 58 | } 59 | 60 | Money::Money(): dollars(0), cents(0) 61 | {/*Body intentionally empty.*/} 62 | 63 | Money::Money(int theDollars) 64 | : dollars(theDollars), cents(0) 65 | {/*Body intentionally empty*/} 66 | 67 | //Uses cstdlib: 68 | Money::Money(int theDollars, int theCents) 69 | { 70 | if ((theDollars < 0 && theCents > 0) || (theDollars > 0 && theCents < 0)) { 71 | cout << "Inconsistent money data.\n"; 72 | exit(1); 73 | } 74 | dollars = theDollars; 75 | cents = theCents; 76 | } 77 | 78 | double Money::getAmount() const 79 | { 80 | return (dollars + cents*0.01); 81 | } 82 | 83 | int Money::getDollars() const 84 | { 85 | return dollars; 86 | } 87 | 88 | int Money::getCents() const 89 | { 90 | return cents; 91 | } 92 | 93 | //Uses iostream and cstdlib: 94 | void Money::output() const 95 | { 96 | int absDollars = abs(dollars); 97 | int absCents = abs(cents); 98 | if (dollars < 0 || cents < 0)//accounts for dollars == 0 or cents == 0 99 | cout << "$-"; 100 | else 101 | cout << '$'; 102 | cout << absDollars; 103 | 104 | if (absCents >= 10) 105 | cout << '.' << absCents; 106 | else 107 | cout << '.' << '0' << absCents; 108 | cout << "\n"; 109 | } 110 | -------------------------------------------------------------------------------- /demo/lecture12/test: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /demo/lecture13/0_private_inheritance.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Person 6 | { 7 | public: 8 | Person() : name("not set") {} 9 | Person(string name) : name(name) {} 10 | string getName() const {return name;} 11 | void setName(string name) {this->name=name;} 12 | void printInfo() const; 13 | private: 14 | string name; 15 | }; 16 | 17 | void Person::printInfo() const 18 | { 19 | cout << "Name: " << name << endl; 20 | } 21 | 22 | class Student : private Person 23 | { 24 | public: 25 | void setSid(int sid) {this->sid = sid;} 26 | void setName(string name); 27 | int getSid() const {return sid;} 28 | void printInfo() const; 29 | private: 30 | int sid; 31 | }; 32 | 33 | void Student::setName(string name) 34 | { 35 | // It causes an error with private member name 36 | this->name = name; 37 | } 38 | 39 | void Student::printInfo() const 40 | { 41 | cout << "Name: " << getName()<< endl; 42 | cout << "Student ID: " << sid << endl; 43 | } 44 | 45 | int main() 46 | { 47 | Student kim; 48 | kim.setName("Kim"); 49 | kim.setSid(100); 50 | kim.printInfo(); 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /demo/lecture13/1_1_swap.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void swapValues(int& var1, int& var2 ) 6 | { 7 | cout << "swapValues i" << endl; 8 | int temp = var1; 9 | var1 = var2; 10 | var2 = temp; 11 | } 12 | 13 | void swapValues(char& var1, char& var2 ) 14 | { 15 | cout << "swapValues c" << endl; 16 | char temp = var1; 17 | var1 = var2; 18 | var2 = temp; 19 | } 20 | 21 | int main() 22 | { 23 | int i_A = 1, i_B = 2; 24 | cout << i_A << ", " << i_B << endl; 25 | swapValues(i_A, i_B); 26 | cout << i_A << ", " << i_B << endl; 27 | 28 | char c_A = 'a', c_B = 'b'; 29 | cout << c_A << ", " << c_B << endl; 30 | swapValues(c_A, c_B); 31 | cout << c_A << ", " << c_B << endl; 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /demo/lecture13/1_2_swap.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | template 6 | void swapValues( T& var1, T& var2 ) 7 | { 8 | cout << "swapValues template" << endl; 9 | T temp = var1; 10 | var1 = var2; 11 | var2 = temp; 12 | } 13 | 14 | int main() 15 | { 16 | int i_A = 1, i_B = 2; 17 | cout << i_A << ", " << i_B << endl; 18 | swapValues(i_A, i_B); 19 | cout << i_A << ", " << i_B << endl; 20 | 21 | char c_A = 'a', c_B = 'b'; 22 | cout << c_A << ", " << c_B << endl; 23 | swapValues(c_A, c_B); 24 | cout << c_A << ", " << c_B << endl; 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /demo/lecture13/2_showStuff.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | template 6 | void showStuff(int stuff1, T stuff2, T stuff3) 7 | { 8 | cout << stuff1 << endl 9 | << stuff2 << endl 10 | << stuff3 << endl; 11 | } 12 | 13 | int main() 14 | { 15 | showStuff(1, 1.1, 2.2); 16 | 17 | return 0; 18 | 19 | } 20 | 21 | -------------------------------------------------------------------------------- /demo/lecture13/3_showStuff_error.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | template 6 | void showStuff(int stuff1, T1 stuff2, T1 stuff3) 7 | { 8 | cout << stuff1 << endl 9 | << stuff2 << endl 10 | << stuff3 << endl; 11 | } 12 | 13 | int main() 14 | { 15 | showStuff(1, 1.1, 2.2); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /demo/lecture13/4_1_swap_error.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | template 6 | void swapValues( T& var1, T& var2 ) 7 | { 8 | cout << "swapValues template" << endl; 9 | T temp = var1; 10 | var1 = var2; 11 | var2 = temp; 12 | } 13 | 14 | int main() 15 | { 16 | int a[3], b[3]; 17 | swapValues(a, b); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /demo/lecture13/4_2_passing_arrays.cc: -------------------------------------------------------------------------------- 1 | // For demo, passing arrays to functions 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | #if 1 8 | // Error case 9 | void foo(int a[3]) 10 | { 11 | for(auto &i : a) 12 | cout << &i << endl; 13 | } 14 | 15 | void print(int a[3]) 16 | { 17 | for(auto i : a) 18 | cout << i << endl; 19 | } 20 | #endif 21 | 22 | #if 0 23 | // for one array size 24 | void foo(int (&a)[3]) 25 | { 26 | for(auto &i : a) 27 | cout << &i << endl; 28 | } 29 | 30 | void print(int (&a)[3]) 31 | { 32 | for(auto i : a) 33 | cout << i << endl; 34 | } 35 | #endif 36 | 37 | #if 0 38 | // for variable array sizes 39 | template 40 | void foo(T (&a)[N]) 41 | { 42 | for(auto &i : a) 43 | cout << &i << endl; 44 | } 45 | 46 | template 47 | void print(T (&a)[N]) 48 | { 49 | for(auto i : a) 50 | cout << i << endl; 51 | } 52 | 53 | #endif 54 | 55 | int main() 56 | { 57 | int a[3] = {0,1,2}; 58 | 59 | for(auto &i : a) 60 | cout << &i << endl; 61 | foo(a); 62 | 63 | print(a); 64 | 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /demo/lecture13/4_3_swap_correct.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | template 6 | void swapValues( T& var1, T& var2 ) 7 | { 8 | cout << "swapValues template" << endl; 9 | T temp = var1; 10 | var1 = var2; 11 | var2 = temp; 12 | } 13 | 14 | template 15 | void swapValues( T (&var1)[N], T (&var2)[N] ) 16 | { 17 | cout << "swapValues template for arrays" << endl; 18 | for(int i=0; i 26 | void print(T (&a)[N]) 27 | { 28 | for(auto i : a) 29 | cout << i << endl; 30 | } 31 | 32 | int main() 33 | { 34 | int a[3] = {0,1,2}, b[3] = {3,4,5}; 35 | print(a); 36 | swapValues(a, b); 37 | print(a); 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /demo/lecture13/5_class_template.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | template 6 | class Pair{ 7 | public: 8 | Pair() {}; 9 | Pair(T firstVal, T secondVal); 10 | void setFirst(T newVal); 11 | void setSecond(T newVal); 12 | T getFirst() const { return first; }; 13 | T getSecond() const { return second; }; 14 | void print() const { cout << "(" << first << ", " << second << ")" << endl; } 15 | private: 16 | T first; 17 | T second; 18 | }; 19 | 20 | template 21 | Pair::Pair(T firstVal, T secondVal) 22 | { 23 | first = firstVal; 24 | second = secondVal; 25 | } 26 | 27 | template 28 | void Pair::setFirst(T newVal) 29 | { 30 | first = newVal; 31 | } 32 | 33 | 34 | template 35 | void Pair::setSecond(T newVal) 36 | { 37 | second = newVal; 38 | } 39 | 40 | template 41 | T addUp(const Pair& thePair) 42 | { 43 | return thePair.getFirst() + thePair.getSecond(); 44 | } 45 | 46 | template 47 | T diff(const Pair& thePair) 48 | { 49 | return thePair.getFirst() - thePair.getSecond(); 50 | } 51 | 52 | 53 | int main() 54 | { 55 | Pair score; 56 | 57 | score.setFirst(3); 58 | score.setSecond(1); 59 | 60 | score.print(); 61 | 62 | cout << addUp(score) << endl; 63 | cout << diff(score) << endl; 64 | 65 | Pair *tmp = new Pair(); 66 | 67 | tmp->setFirst(3); 68 | tmp->setSecond(1); 69 | 70 | tmp->print(); 71 | 72 | cout << addUp(*tmp) << endl; 73 | cout << diff(*tmp) << endl; 74 | 75 | return 0; 76 | } 77 | -------------------------------------------------------------------------------- /demo/lecture13/6_typedef.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | template 7 | class Pair{ 8 | public: 9 | Pair() {}; 10 | Pair(T firstVal, T secondVal); 11 | void setFirst(T newVal); 12 | void setSecond(T newVal); 13 | T getFirst() const { return first; }; 14 | T getSecond() const { return second; }; 15 | void print() const { cout << "(" << first << ", " << second << ")" << endl; } 16 | private: 17 | T first; 18 | T second; 19 | }; 20 | 21 | template 22 | Pair::Pair(T firstVal, T secondVal) 23 | { 24 | first = firstVal; 25 | second = secondVal; 26 | } 27 | 28 | template 29 | void Pair::setFirst(T newVal) 30 | { 31 | first = newVal; 32 | } 33 | 34 | 35 | template 36 | void Pair::setSecond(T newVal) 37 | { 38 | second = newVal; 39 | } 40 | 41 | template 42 | T addUp(const Pair& thePair) 43 | { 44 | return thePair.getFirst() + thePair.getSecond(); 45 | } 46 | 47 | #if 1 48 | typedef Pair PairOfInt; 49 | #else 50 | using PairOfInt = Pair; 51 | #endif 52 | 53 | int main() 54 | { 55 | PairOfInt score; 56 | 57 | score.setFirst(3); 58 | score.setSecond(0); 59 | 60 | score.print(); 61 | 62 | cout << addUp(score) << endl; 63 | 64 | PairOfInt *tmp = new PairOfInt(); 65 | 66 | tmp->setFirst(3); 67 | tmp->setSecond(0); 68 | 69 | tmp->print(); 70 | 71 | cout << addUp(*tmp) << endl; 72 | 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /demo/lecture13/test: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /demo/lecture14/1_vector_basic.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | vector v; 9 | 10 | cout << "capacity: " << v.capacity() << endl; 11 | 12 | int num[] = {0,1,2,3,4,5}; 13 | 14 | for(auto i : num) { 15 | v.push_back(i); 16 | cout << "after insert " << i << ", capacity: " << v.capacity() << endl; 17 | } 18 | 19 | for(auto i = 0; i < v.size(); i++) { 20 | cout << v[i] << endl; 21 | } 22 | 23 | cout << "capacity: " << v.capacity() << endl; 24 | 25 | v.reserve(100); 26 | 27 | cout << "capacity: " << v.capacity() << endl; 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /demo/lecture14/2_vector_cycling.cc: -------------------------------------------------------------------------------- 1 | //Program to demonstrate STL iterators. 2 | #include 3 | #include 4 | 5 | using std::cout; // Using only a part of std 6 | using std::endl; 7 | using std::vector; 8 | 9 | int main( ) 10 | { 11 | vector container; 12 | 13 | for (int i = 1; i <= 4; i++) 14 | container.push_back(i); 15 | 16 | for (auto it = container.begin(); it != container.end( ); it++) 17 | cout << *it << " "; 18 | cout << endl; 19 | 20 | for (auto it = container.begin(); it != container.end( ); it++) 21 | *it = 0; 22 | 23 | for (auto it = container.begin( ); it != container.end( ); it++) 24 | cout << *it << " "; 25 | cout << endl; 26 | 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /demo/lecture14/3_vector_random_access.cc: -------------------------------------------------------------------------------- 1 | //Program to demonstrate STL iterators. 2 | #include 3 | #include 4 | 5 | using std::cout; // Using only a part of std 6 | using std::endl; 7 | using std::vector; 8 | 9 | int main( ) 10 | { 11 | vector container; 12 | 13 | for (int i = 1; i <= 4; i++) 14 | container.push_back(i); 15 | 16 | auto it = container.begin(); 17 | for (; it != container.end( ); it++) 18 | cout << *it << " "; 19 | cout << endl; 20 | 21 | // Setting the last item with zero 22 | // Why should we use --? 23 | *--it = 0; 24 | cout << *it << endl; 25 | 26 | // Setting the second item with zero 27 | // Why -2 is used here? 28 | it[-2] = 0; 29 | cout << *it << endl; 30 | 31 | for (it = container.begin( ); it != container.end( ); it++) 32 | cout << *it << " "; 33 | cout << endl; 34 | 35 | return 0; 36 | } 37 | 38 | -------------------------------------------------------------------------------- /demo/lecture14/4_vector_reverse_order.cc: -------------------------------------------------------------------------------- 1 | //Program to demonstrate STL iterators. 2 | #include 3 | #include 4 | using std::cout; // Using only a part of std 5 | using std::endl; 6 | using std::vector; 7 | 8 | int main( ) 9 | { 10 | vector container; 11 | 12 | for (int i = 1; i <= 4; i++) 13 | container.push_back(i); 14 | 15 | cout << "Here is what is in the container:\n"; 16 | for (auto it = container.begin(); it != container.end(); it++) 17 | cout << *it << " "; 18 | cout << endl; 19 | 20 | cout << "Print in a reverse order\n"; 21 | // What happens with the below line? 22 | for (auto it = container.end(); it != container.begin(); it--) 23 | //for (vector::reverse_iterator it = container.rbegin(); it != container.rend(); it++) 24 | cout << *it << " "; 25 | cout << endl; 26 | 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /demo/lecture14/5_list.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::endl; 5 | using std::list; 6 | 7 | int main() 8 | { 9 | list listObject; 10 | for (int i = 1; i <= 3; i++) 11 | listObject.push_back(i); 12 | 13 | cout << "List contains:\n"; 14 | list::iterator iter; 15 | for (iter = listObject.begin(); iter != listObject.end(); iter++) 16 | cout << *iter << " "; 17 | cout << endl; 18 | 19 | cout << "Setting all entries to 0:\n"; 20 | for (iter = listObject.begin(); iter != listObject.end(); iter++) 21 | *iter = 0; 22 | 23 | cout << "List now contains:\n"; 24 | for (iter = listObject.begin(); iter != listObject.end(); iter++) 25 | cout << *iter << " "; 26 | cout << endl; 27 | 28 | // Random access is not defined. 29 | //iter = listObject.begin(); 30 | //cout << iter[2] << endl; 31 | 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /demo/lecture14/6_stack.cc: -------------------------------------------------------------------------------- 1 | //Program to demonstrate use of the stack template class from the STL. 2 | #include 3 | #include 4 | #include 5 | 6 | using std::cin; 7 | using std::cout; 8 | using std::endl; 9 | using std::stack; 10 | using std::vector; 11 | 12 | int main( ) 13 | { 14 | stack> s; // We change the default container from deque to vector. 15 | 16 | cout << "Enter a line of text:\n"; 17 | char next; 18 | cin.get(next); // cin.get is used for accepting spaces and new line. 19 | while (next != '\n') 20 | { 21 | s.push(next); 22 | cin.get(next); 23 | } 24 | 25 | cout << "Written backward that is:\n"; 26 | while ( ! s.empty( ) ) 27 | { 28 | cout << s.top( ); 29 | s.pop( ); 30 | } 31 | cout << endl; 32 | 33 | // Stack has no iterator 34 | //auto it = s.begin(); 35 | //cout << *it << endl; 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /demo/lecture14/7_set.cc: -------------------------------------------------------------------------------- 1 | //Program to demonstrate use of the set template class. 2 | #include 3 | #include 4 | using std::cout; 5 | using std::endl; 6 | using std::set; 7 | 8 | int main() 9 | { 10 | set s; 11 | 12 | s.insert('A'); 13 | s.insert('D'); 14 | s.insert('D'); 15 | s.insert('C'); 16 | s.insert('C'); 17 | s.insert('B'); 18 | 19 | cout << "The set contains:\n"; 20 | set::const_iterator p; 21 | for (p = s.begin(); p != s.end(); p++) 22 | cout << *p << " "; 23 | cout << endl; 24 | 25 | cout << "Set contains 'C': "; 26 | if (s.find('C') == s.end()) 27 | cout << " no " << endl; 28 | else 29 | cout << " yes " << endl; 30 | 31 | cout << "Removing C.\n"; 32 | s.erase('C'); 33 | for (p = s.begin(); p != s.end(); p++) 34 | cout << *p << " "; 35 | cout << endl; 36 | 37 | cout << "Set contains 'C': "; 38 | if (s.find('C')==s.end()) 39 | cout << " no " << endl; 40 | else 41 | cout << " yes " << endl; 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /demo/lecture14/8_map.cc: -------------------------------------------------------------------------------- 1 | //Program to demonstrate use of the map template class. 2 | #include 3 | #include 4 | #include 5 | using std::cout; 6 | using std::endl; 7 | using std::map; 8 | using std::string; 9 | 10 | int main() 11 | { 12 | map planets; 13 | 14 | planets["Mercury"] = "Hot planet"; 15 | planets["Venus"] = "Atmosphere of sulfuric acid"; 16 | planets["Earth"] = "Home"; 17 | planets["Mars"] = "The Red Planet"; 18 | planets["Jupiter"] = "Largest planet in our solar system"; 19 | planets["Saturn"] = "Has rings"; 20 | planets["Uranus"] = "Tilts on its side"; 21 | planets["Neptune"] = "1500 mile per hour winds"; 22 | planets["Pluto"] = "Dwarf planet"; 23 | 24 | cout << "Entry for Mercury - " << planets["Mercury"] << endl; 25 | cout << endl; 26 | 27 | if (planets.find("Mercury") != planets.end()) 28 | cout << "Mercury is in the map." << endl; 29 | if (planets.find("Ceres") == planets.end()) 30 | cout << "Ceres is not in the map." << endl << endl; 31 | 32 | // Iterator outputs planets in order sorted by key 33 | cout << "Iterating through all planets: " << endl; 34 | map::const_iterator iter; 35 | for (iter = planets.begin(); iter != planets.end(); iter++) 36 | { 37 | cout << iter->first << " - " << iter->second << endl; 38 | } 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /demo/lecture14/test: -------------------------------------------------------------------------------- 1 | 2 | --------------------------------------------------------------------------------