Top Banner
20

C++ Programming - 4th Study

Jul 23, 2015

Download

Technology

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: C++ Programming - 4th Study
Page 2: C++ Programming - 4th Study
Page 3: C++ Programming - 4th Study

3

Page 4: C++ Programming - 4th Study

int main(){

// int& ref;int i = 10;int& ref = i;

ref += 10;std::cout << "i = " << i

<< std::endl;std::cout << "ref = " << ref

<< std::endl;

return 0;}

4

Page 5: C++ Programming - 4th Study

void swap(int* a, int* b){

int temp = *a; *a = *b; *b = temp;}

int main(){

int a = 10, b = 20;

std::cout << "a = " << a << ", b = " << b << std::endl;swap(&a, &b);std::cout << "a = " << a << ", b = " << b << std::endl;

return 0;}

Pass-by-pointer

5

Page 6: C++ Programming - 4th Study

void swap(int& a, int& b){

int temp = a; a = b; b = temp;}

int main(){

int a = 10, b = 20;

std::cout << "a = " << a << ", b = " << b << std::endl;swap(a, b);std::cout << "a = " << a << ", b = " << b << std::endl;

return 0;}

Pass-by-reference

6

Page 7: C++ Programming - 4th Study
Page 8: C++ Programming - 4th Study

8

Page 9: C++ Programming - 4th Study

9

Page 10: C++ Programming - 4th Study

int computeRectArea1(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4){

return 0;}

int computeRectArea2(int width, int height) { return 0;

}

int main(){

int area1 = computeRectArea1(1, 1, 1, 4, 3, 4, 3, 1);int area2 = computeRectArea2(2, 3);

return 0;}

C

10

Page 11: C++ Programming - 4th Study

int computeRectArea(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4){

return 0;}

int computeRectArea(int width, int height) { return 0;

}

int main(){

int area1 = computeRectArea(1, 1, 1, 4, 3, 4, 3, 1);int area2 = computeRectArea(2, 3);

return 0;}

C++

11

Page 12: C++ Programming - 4th Study
Page 13: C++ Programming - 4th Study

13

Page 14: C++ Programming - 4th Study

14

Page 15: C++ Programming - 4th Study

15

Page 16: C++ Programming - 4th Study

void print(){

std::cout << "A's print" << std::endl;}

void print(){

std::cout << "B's print" << std::endl;}

int main(){

print();

return 0;}

C

16

Page 17: C++ Programming - 4th Study

namespace A {void print() { std::cout << "A's print()" << std::endl; }

}

namespace B {void print() { std::cout << "B's print()" << std::endl; }

}

int main(){

A::print();B::print();

return 0;}

C++

17

Page 18: C++ Programming - 4th Study

C++

18

Page 19: C++ Programming - 4th Study

C++using namespace std;

namespace A {void print() { cout << "A's print()" << endl; }

}

namespace B {void print() { cout << "B's print()" << endl; }

}

int main(){

A::print();B::print();

}

19

Page 20: C++ Programming - 4th Study

namespace A { int i; }namespace B { int i; }

using namespace A;using namespace B;

int main(){

// Don't do thisif (i == i) { }

if (A::i == B::i) { }

return 0;}

20