Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCE 2100: Computing Foundations 1 Intro to Advanced C++

Similar presentations


Presentation on theme: "CSCE 2100: Computing Foundations 1 Intro to Advanced C++"— 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

3 Templates

4 What We Can Do Already: A Stack of ints
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

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

6 Stack of ints 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

7 Multiple Types of Stack
But what if I also want a stack of floats, 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 ints. Answer: Templates.

8 Stack of <stackelement>s
template <class stackelement> 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

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

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

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

12 Operator Overloading

13 Operator Overloading C++ gives you the power to define operators such as * and + on your own classes. This is called operator overloading. 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 +

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

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

17 Copy Constructors

18 A Vector Class New New Not just friends any more.
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 New New Not just friends any more.

19 Vector Constructor and Destructor
vector::vector(){ //constructor data = new int[VECSIZE]; for(int i=0; i<VECSIZE; i++) data[i] = 0; } //constructor vector::~vector(){ //destructor delete [] data; } //destructor

20 Remember This?

21 Remember This?

22 Remember This?

23 Copy Constructor for Vectors
vector::vector(vector& v){ data = new int[VECSIZE]; for(int i=0; i<VECSIZE; i++) data[i] = v.data[i]; } //copy constructor

24 Overloaded Assignment for Vectors
vector vector::operator=(const vector& v){ if(this != &v) for(int i=0; i<VECSIZE; i++) data[i] = v.data[i]; return *this; } //operator=

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<VECSIZE; i++) data[i] += v.data[i]; return *this; } //operator+=

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

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

28 More Examples

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

30 CImageFileNameList CImageFileNameList::CImageFileNameList(void){
m_lplpImageFileName = NULL; m_nImageFileCount = 0; } //constructor CImageFileNameList::~CImageFileNameList(void){ for(int i=0; i<m_nImageFileCount; i++) delete [] m_lplpImageFileName[i]; delete [] m_lplpImageFileName; } //destructor

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[]

32 Long Integers Note multiple constructors
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 Note multiple constructors

33 Long Integers Note multiple assignment ops
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 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<size; i++) data[i] = l.data[i]; } return *this; longint& longint::operator=(const int i){ reallocate(1); *data = 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; }

36 Using the Long Integer Class
The Windows calculator can’t calculate 52! without resorting to scientific notation.

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.

38 TexCalc, the Texas Sized Calculator

39 Computes 52! Without Scientific Notation

40 They Get Approximately the Same Answer

41 52! is a 226-bit Number

42 That’s 44 Digits in Texadecimal

43 It Can Handle 256! (507 Digits)

44 It Can Handle 8,192! (28,504 Digits)

45

46 Programming with Overloaded Operators
longint g_lngResult, g_lngOperand; switch(g_eOperator){ case ADD_OP: g_lngResult += g_lngOperand; break; case MULTIPLY_OP: g_lngResult *= g_lngOperand; 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; case MOD_OP: g_lngResult = g_lngOperand % g_lngResult; case EXP_OP: g_lngResult = g_lngOperand ^ g_lngResult; How many overloaded operators are used here?

48 Programming with Overloaded Operators
longint temp, result; case FACTORIAL_OP: result = 1; temp = g_lngResult; while(temp > 1){ result *= temp; temp -= 1; } g_lngResult = result; break; How many overloaded operators are used here?


Download ppt "CSCE 2100: Computing Foundations 1 Intro to Advanced C++"

Similar presentations


Ads by Google