Skip to main content

Posts

Showing posts from October, 2021

ADTs

  ADTs 1. Queue -FIFO We can implement in this way:  template<typename T> class Queue { public:     void enqueue(const T & item);     T dequeue();     T & peek()     const T & peek() const;     int numbebrOfItem() const; }; - In the actual C++ STL library, it has the following functions: push() pop() peek() 2. Stack - LIFO We can implement in this way: template<typename T> class Stack { public:     void push(const T & item);     T pop();     T & peek();     const T & peek() const;     int numberOfItem() const; }; In the C++ STL library, it has similar functions as queue. 3. Sets - Add - Test if an item is in the set - Check if the set is empty - Take the union of two set - intersect two sets A variant of this ADT is a multiset. (Also called a bag) It allows the same element to appear multiple times, whereas a true set either has an object or n...

Error Handling and Exceptions

  Error Handling and Exceptions - The worst possible way to deal with any problem is for the program to produce the wrong answer without informing the user - a silent failure.  - When the program deals with an error by aborting, it should do so with the explicit intention of the programmer. (i.e. the programmer should call assert or check a condition then call abort) And the program should give an error message to the user. Simply segmentation fault due to an invalid memory access is never appropriate. - Sometimes we would prefer to handle the error more gracefully. Graceful error handling requires making the user aware of the problem, as well as allowing the program to continue to function normally. - For example, we would present the user with some options to remedy the situation, like entering a different input, select a different file, retry a failed operation, etc.  - As our programming skills develop, we should get into the practice of writing bullet proof code - co...

Inheritance

  Inheritance - C++ supports inheritance like many object-oriented languages. Inheritance is the ability to declare a class (called  the child class or subclass) in such a way that it obtains all of the fields (called the parent class or superclass) and methods of another class.  - It is best used when two classes exhibit an is-a relationship. E.g. an imageButton class, instead of Button class. It add more features and overrides some behaviors of the parent class.  Button: parent class, super class or base class ImageButton: child class, subclass or derived class. Writing Classes with Inheritance - To use inheritance, the class declaration for the child class has a colon, followed by and access specifier, and then the parent class name between the class's name and the open curly brace of the class declaration. e.g. class BankAccount {     double balance;     unsigned long acctNumber; public:     void deposit(double amount);    ...

Templates

  Template - Polymorphism is the ability of the same code to operate on different types. This ability to operate on multiple types reduces code duplication by allowing the same piece of code to be reused across the different types it can operate on. - Polymorphism comes in a variety of forms. What we are interested in at the moment is parametric polymorphism, meaning that we can write our code so that it is parameterized over what type it operates on.  -That is, we want to declare a type parameter T and replace int with T in the above code. -Then, when we want to call the function, we can specify the type for T and get the function we desire. C++ provides parametric polymorphism through templates. Templated Functions - We can write a templated function by using the keyword template followed by the template parameters in angle brackets (<>). - Unlike function parameters, template parameters may be types, which are specified with typename where the type of the parameter wo...

Strings and IO Revisited

  Strings and IO Revisited - C++ introduces new classes for strings and for performing IO while C approaches are still valid. They provide a more object-oriented approach than C counterparts.  Strings - C++ provides a string class. And we can still define and manipulate C-style strings via a char* whenever it is appropriate. - The string class in C++ encapsulates the sequence of bytes that form a string into an object that provides a variety of methods and operators to operate on that type.  e.g. strings have length(), += operators to concat, [] to index. [] returns the type: char &. (If the string is non-const type, else it returns const char &) - The string class also provides constructors, including one that takes a const char *. It allows creation of C++ strings from C strings, including string literals. e.g. std::string s("Hello World"); can create a C++ string object Cautionary Example recursion e.g. #include <string> std::string reverse(const std::str...