Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to classes

Similar presentations


Presentation on theme: "Introduction to classes"— Presentation transcript:

1 Introduction to classes

2 Outline In this lesson, we will: Describe the insertion sort algorithm
Look at an example Determine how the algorithm work Create a flow chart Implement the algorithm Look at the run times Consider some optimizations

3 Accessing member variables
Suppose we go back to our string class: class String; class String { public: // Constructors String(); String( std::size_t max_length ); private: std::size_t array_capacity; std::size_t string_length; char *character_array; }; We can create them and destroy them, but that’s about it…

4 Accessing member variables
If you tried to access a member variable, you’d get a compile-time error: int main() { String str{10}; std::cout << "The capacity is " << str.array_capacity << std::endl; return 0; } example.cpp: In function 'int main()': example.cpp:15:21: error: 'std::size_t String::array_capacity' is private std::size_t array_capacity; ^ example.cpp:39:44: error: within this context std::cout << "The capacity is " << str.array_capacity

5 Accessing member variables
You may not want the user to directly access the member variables because: They may incorrectly update the location of '\0' They may not update string_length They may go beyond the end of the array

6 Accessing member variables
We can add publicly visible member functions: class String { public: // Constructors and destructor String(); String( std::size_t max_length ); ~String(); // Member functions char get_char( std::size_t n ) const; void set_char( std::size_t n, char ch ); private: std::size_t array_capacity; std::size_t string_length; char *character_array; };

7 Accessing member variables
We must specify their functionality: char get_char( std::size_t n ) const; This function will not change any member variables (const) This member function will either Return the nth character if n < array_length, Otherwise it will return the null character '\0' char set_char( std::size_t n, char ch ); Set the nth character to ch if n < array_length, Update the nth character to ch if n >= array_length && n < (array_capacity - 1) but also update array_length and the position of '\0', filling in all intermediate spaces with the new character, as well

8 Accessing member variables
Getting a character is straight-forward: char String::get_char( std::size_t n ) const { return ( n < string_length ) ? character_array[n] : '\0'; }

9 Accessing member variables
Setting a character is more difficult: void String::set_char( std::size_t n, char ch ) { if ( n < string_length ) { character_array[n] = ch; } else if ( n < (array_capacity - 1) ) { for ( std::size_t k{string_length}; k <= n; ++k ) { character_array[k] = ch; } string_length = n + 1; character_array[string_length] = '\0'; } else { throw n;

10 Calling member functions
When we worked with structures, we implemented functions as follows: string_t str_1, str_2; // Allocate memory for strings of max length 16 and 32 string_init( str_1, 16 ); string_init( str_2, 32 ); The instance was always passed as the first argument by reference void string_init( string_t &str, std::size_t max_len ) { str.capacity = max_len + 1; str.character_array = new char[str.capacity]; str.character_array[0] = '\0'; str.length = 0; }

11 Calling member functions
With classes, we call member functions on an instance the exact same way we access member variables: int main() { String str{11}; str.set_char( 0, 'H' ); str.set_char( 1, 'i' ); str.set_char( 10, '!' ); for ( std::size_t k{0}; k <= 10; ++k ) { std::cout << str.get_char( k ); } std::cout << std::endl; return 0; 1 2 3 4 5 6 7 8 9 10 11 H i ! \0 Output: Hi!!!!!!!!!

12 Calling member functions
If we are dealing with pointers, we use the arrow operator: int main() { String p_str{new String{11}}; p_str->set_char( 0, 'H' ); p_str->set_char( 1, 'i' ); p_str->set_char( 10, '!' ); for ( std::size_t k{0}; k <= 10; ++k ) { std::cout << p_str->get_char( k ); } std::cout << std::endl; delete p_str; p_str = nullptr; return 0; Output: Hi!!!!!!!!!

13 Accessing member variables
There may be other questions we may ask about our string: // Member functions char get_char( std::size_t n ) const; std::size_t length() const; std::size_t max_length() const; char *get_character_array() const; void set_char( std::size_t n, char ch );

14 Accessing member variables
There may be other questions we may ask about our string: std::size_t String::length() const { return string_length; } std::size_t String::max_length() const { return array_capacity - 1;

15 Accessing member variables
If we want to return a character array, we cannot just return the value of the member variable? char *String::get_character_array() const { return character_array; } Why?

16 Accessing member variables
Instead, we must copy everything to a new dynamically-allocated array, and then return the address of that new array char *String::get_character_array() const { char *return_array{new char[string_length + 1]}; for ( std::size_t k{0}; k <= string_length; ++k ) { return_array[k] = character_array[k]; } return return_array;

17 Summary Following this lesson, you now
Understand the insertion sort algorithm You saw an example Watched how to convert the flow chart to an algorithm There is a lot of time spent swapping, which can be reduced significantly The run time is still approximately the same as selection sort

18 References [1] No references?

19 Colophon These slides were prepared using the Georgia typeface. Mathematical equations use Times New Roman, and source code is presented using Consolas. The photographs of lilacs in bloom appearing on the title slide and accenting the top of each other slide were taken at the Royal Botanical Gardens on May 27, 2018 by Douglas Wilhelm Harder. Please see for more information.

20 Disclaimer These slides are provided for the ece 150 Fundamentals of Programming course taught at the University of Waterloo. The material in it reflects the authors’ best judgment in light of the information available to them at the time of preparation. Any reliance on these course slides by any party for any other purpose are the responsibility of such parties. The authors accept no responsibility for damages, if any, suffered by any party as a result of decisions made or actions based on these course slides for any other purpose than that for which it was intended.


Download ppt "Introduction to classes"

Similar presentations


Ads by Google