class Interface
{
public:
Interface(){}
virtual ~Interface(){}
virtual void method1() = 0; // "= 0" part makes this method pure virtual, and
// also makes this class abstract.
virtual void method2() = 0;
};
class Concrete : public Interface
{
private:
int myMember;
A dynamically allocated array can be made using the new keyword. The result is a pointer pointing to an allocated block of the specified number of that type on the heap.
the syntax is
new TYPE[ NUMBER_OF_SPACES ]; // returns a TYPE* pointer.
Once you have allocated an array in such way, and wish to be done with it, just delete it using the syntax
delete[] PTR;
Alternatively, you can use a smart pointer so that you never have to worry about deleting the block yourself. They are a bit stiff though...
13 is an 'operator overload'. it says that when you use the + with this class, do this code. so later you can say
dynamic_string a,b,c;
a = c+b; //you defined what this 'addition' means.
14 is an extension of it, defining what to do in the case of +=, which is often a call to the + operator with a little juggling of where to put the result.
15 overloads the == "is this equal" operator, so you can now say if(a==b) and get the desired behavior.
many simplified, less powerful languages block operator overloading. I don't think java allows it, for example.
it can be abused; you can make the + operator do division or factorials for your own numerical type, for example.
It can also be beautiful. no one wants to see this:
my_result = a.add(b);
instead of
my_result = a+b;
especially in the middle of a 2-3 line long math equation.
is this a learning exercise? C++ <string> type already does all this stuff and is already dynamic. You can extend it to do more things, but you don't have to re-create all the guts if you are doing something useful and not just learning.
13) this function produces a new string by concatenating the dynamic string right to the original string. This does not effect the original string, it just includes it as one of it’s arguments.
So let’s look at this.
Step 1) make a new string.
Step 2) make the new string big enough to hold both of the strings.
Step 3) insert the original string into our new string
Step 4) insert our right string after the original string in our new string.
Step 5) return the new string!
keep in mind reserve() can change the size of a string ;)
14) this function does the same thing as above, but instead of generating an entire new string, it modifies the original string, so our process is only slightly changed.
Step 1) make a new string. <- we already have a string.
Step 2) make the original string big enough to hold both of the strings.
Step 3) insert the original string into our new string <- our string already has the original string.
Step 4) insert our right string after the original string in our new string.Step 5) return the new string! <- the function is void.
15) this function tests whether or not to string contain the exact same content. It returns a bool and does not effect either string. It’s really easy to make. Just loop through all the characters in the strings and make sure they are equal, if they all are, then your good, if some of them aren’t, then welp they aren’t equal. 👌
Any and all questions are welcome, if I didn’t explain something well, do tell.
size is just the length of characters in your string, whereas capacity is the actual length of your entire allocated block, which can differ. Generally I’m pretty sure those are recorded as a variable of type size_t which they edit whenever a += op or an = op or something happens.