Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCE 2100: Computing Foundations 1 Intro to Advanced C++ Ian Parberry Summer 2013.

Similar presentations


Presentation on theme: "CSCE 2100: Computing Foundations 1 Intro to Advanced C++ Ian Parberry Summer 2013."— Presentation transcript:

1 CSCE 2100: Computing Foundations 1 Intro to Advanced C++ Ian Parberry Summer 2013

2 Some Advanced C++ Topics Templates – Example: Templated stack Operator overloading – Example: Complex numbers Copy constructors – Example: Multidimensional vectors More examples – CImageFileNameList – Arbitrary precision integers 2

3 TEMPLATES 3

4 What We Can Do Already: A Stack of int s class StackClass{ private: int *value; //data in stack int count; //number of elements int size; //maximum number of elements allowed public: StackClass(int s); //constructor StackClass(); //destructor void reset(); //clear the stack void push(int v); //push v onto stack int pop(); //delete and return top }; //StackClass 4

5 Stack of int s StackClass::StackClass(int s){ value = new int[s]; size = s; count = 0; } //constructor StackClass::~StackClass(){ delete [] value; } //destructor void StackClass::reset(){ count = 0; } //reset 5

6 Stack of int s void StackClass::push(int v){ if(count < size) value[count++] = v; } //push int StackClass::pop(){ if(count > 0) return(value[--count]); else return 0; } //pop 6

7 Multiple Types of Stack But what if I also want a stack of float s, or a stack of strings ? Do I really want to make three different stack classes? Tedious to build, difficult to maintain. All we want to do is change the red int s. Answer: Templates. 7

8 Stack of s template class StackClass{ private: stackelement *value; //data in stack int count; //number of elements int size; //maximum number of elements allowed public: StackClass(int s); //constructor StackClass(); //destructor void reset(); //clear the stack void push(stackelement v); //push v onto stack stackelement pop(); //delete & return top }; //StackClass 8

9 Stack of s template StackClass ::StackClass(int s){ value = new stackelement[s]; size = s; count = 0; } //constructor template StackClass ::StackClass::~StackClass(){ delete [] value; } //destructor template void StackClass ::reset(){ count = 0; } //reset 9

10 Stack of s template void StackClass ::push(stackelement v){ if(count0) return(value[--count]); else return 0; } //pop 10

11 Declaration of Templated Stack const int STACKSIZE = 42; StackClass stack0(STACKSIZE); StackClass stack1(STACKSIZE); StackClass stack2(STACKSIZE); StackClass stack3(STACKSIZE); class longint{ //my own long integer class … }; StackClass stack4(STACKSIZE); 11

12 OPERATOR OVERLOADING 12

13 Operator Overloading C++ gives you the power to define operators such as * and + on your own classes. This is called operator overloading. 13 class complex{ //complex number private: double imaginary, real; public: complex(double r=0.0, double i=0.0); friend complex operator+(complex, complex); friend complex& operator+=(complex&, complex); friend ostream& operator<<(ostream&,complex x); }; //complex

14 Complex Numbers complex::complex(double r, double i){ imaginary = i; real = r; } //constructor complex operator+(complex first, complex second){ return complex(first.real + second.real, first.imaginary + second.imaginary); } //overloaded + 14

15 Complex Numbers complex& operator+=(complex& x, complex y){ x.imaginary += y.imaginary; x.real += y.real; return x; } //overloaded += ostream& operator<<(ostream& output_stream, complex x){ output_stream << "(" << x.real << "," << x.imaginary << ")"; return output_stream; } //overloaded output 15

16 Complex Numbers int main(){ complex a(1,2), b(3,4), c; c = a + b; cout << c; //outputs (4,6) c += a; cout << c; //outputs (5,8) return 0; } //main 16

17 COPY CONSTRUCTORS 17

18 A Vector Class const int VECSIZE = 4; //vector dimension class vector{ private: int *data; public: vector(); //constructor ~vector(); //destructor vector(vector&); //copy constructor friend ostream& operator<<(ostream&, vector); friend istream& operator>>(istream&, vector&); vector operator=(const vector&); vector operator+=(vector); friend vector operator+(vector, vector); }; //vector 18 New Not just friends any more.

19 Vector Constructor and Destructor vector::vector(){ //constructor data = new int[VECSIZE]; for(int i=0; i

20 Remember This? 20

21 Remember This? 21

22 Remember This? 22

23 Copy Constructor for Vectors vector::vector(vector& v){ data = new int[VECSIZE]; for(int i=0; i

24 Overloaded Assignment for Vectors 24 vector vector::operator=(const vector& v){ if(this != &v) for(int i=0; i

25 Overloaded Addition for Vectors vector operator+(vector x, vector y){ //addition vector result=x; result += y; return result; } // operator+ vector vector::operator+=(vector v){ //overloaded += for(int i=0; i

26 Overloaded I/O for Vectors ostream& operator<<(ostream& output_stream, vector v){ output_stream<<'('; for(int i=0; i>(istream& input_stream, vector &v){ for(int i=0; i>v.data[i]; return input_stream; } //overloaded input 26

27 How to Use Our Vector Class int main(){ vector a, b, c; cout "; cin >> a; //overloaded input cout "; cin >> b; //overloaded input c = a + b; //overloaded operator+ cout << "a+b= " << c << endl; //overloaded output return 0; } //main 27

28 MORE EXAMPLES 28

29 CImageFileNameList class CImageFileNameList{ private: char** m_lplpImageFileName; int m_nImageFileCount; public: CImageFileNameList(void); ~CImageFileNameList(void); void GetImageFileNames(); char* operator[](const int); }; //CImageFileNameList 29

30 CImageFileNameList CImageFileNameList::CImageFileNameList(void){ m_lplpImageFileName = NULL; m_nImageFileCount = 0; } //constructor CImageFileNameList::~CImageFileNameList(void){ for(int i=0; i

31 CImageFileNameList //safe index into name list char* CImageFileNameList::operator[](const int ix){ if(ix >= 0 && ix < m_nImageFileCount) return m_lplpImageFileName[ix]; else return "NotAValidFileName.bmp"; } //operator[] 31

32 Long Integers class longint{ //long integer class private: unsigned int* data; unsigned int size; void loadstring(const char* string); void reallocate(int s); void grow(int s); public: longint(); //constructor longint(unsigned int); //constructor longint(char*); //constructor longint(const longint&); //copy constructor ~longint(); //destructor 32 Note multiple constructors

33 Long Integers friend ostream& operator<<(ostream&, longint); friend istream& operator>>(istream&, longint&); longint& operator=(const longint&); longint& operator=(const int); longint& operator=(const char*); longint& operator+=(const longint&); friend longint operator+(longint, longint); }; //longint 33 Note multiple assignment ops

34 Long Integers //overloaded assignment operators longint& longint::operator=(const longint& l){ if(this != &l){ //protect against self assignment reallocate(l.size); //grab enough space for(int i=0; i

35 Long Integers longint& longint::operator=(const char* string){ int digitcount = strlen(string); int s = digitcount/HalfBytesInWord + (digitcount%HalfBytesInWord>0?1:0); reallocate(s); loadstring(string); return *this; } 35

36 Using the Long Integer Class 36

37 TexCalc, the Texas Sized Calculator Just for fun, I decided to code a calculator that can handle not just long integers, but loooooo- ooooooong integers. 37

38 TexCalc, the Texas Sized Calculator 38

39 39

40 They Get Approximately the Same Answer 40

41 41

42 42

43 43

44 44

45

46 Programming with Overloaded Operators 46 How many overloaded operators are used here?

47 Programming with Overloaded Operators case SUBTRACT_OP: g_lngResult = g_lngOperand - g_lngResult; break; case DIVIDE_OP: g_lngResult = g_lngOperand / g_lngResult; break; case MOD_OP: g_lngResult = g_lngOperand % g_lngResult; break; case EXP_OP: g_lngResult = g_lngOperand ^ g_lngResult; break; 47 How many overloaded operators are used here?

48 Programming with Overloaded Operators 48 How many overloaded operators are used here?


Download ppt "CSCE 2100: Computing Foundations 1 Intro to Advanced C++ Ian Parberry Summer 2013."

Similar presentations


Ads by Google