CPP00-04
by bhagenlo
Hey hey :)
Up to learning a new programming language, are we?
And as it is the case with every new tool one uses, there are different tradeoffs.
You have (quite likely) mostly used C in your programming journey so far. Since this is about to change, I encourage you to do a quick review of C (as a language in your toolkit) as described here.
Did you do it?
It's nice to be able to differentiate programming languages like this. With this foundation: How is C++ different from C?
C is a small language. Programs that are written in C tend to be relatively complex (meaning: You have keep many things in your working memory to understand them), and relatively verbose.
C++ is a very large language. Not only the programs that are written in it tend to be complex, one also has to potentially understand many language features to be even able to read a program.
This has several consequences:
More language features are better:
Many things that are verbose in C can be expressed quite concisely in C++. For example:
- You can overload operators.
- You can write object-oriented code natively.
- You can parameterize functions over types.
- More language features are worse: They make your code potentially more convoluted and harder to read, understand, and reason about.
How you resolve this tension is up to you. At every line you write.
#General remarks about the piscine
The subjects are good for getting to know a concept, but not so much for understanding it. Because of this, my advice for working on the subjects is the following:
- Read through each subject and take note of every concept/word you haven't heard about before.
- Try to understand each of them by doing a short internet search. (Since programming languages are way more alike than they are different, you can also use resources that aren't made for C++ specifically.)
- And only after you're sure you at least understood what the purpose/use case of each concept is, start with coding - and maybe reading what others did.
Two last things before you start:
Get familiar with the online documentation of C++:
First, the C++ FAQ. Then, read some example entries from cppreference.com and cplusplus.com.
- And I'm saying it again and again: But please, read. others. code.
This one is mostly for getting to know CPP, so let's use it to setting things up properly. Since there's no Norm in CPP anymore, I can only encourage you to write yourself a new Makefile. If you need some inspiration, you can look at mine: https://github.com/haglobah/minimal-Makefile. Apart from that, there's not that much you need to know up front: If you're just starting to learn C++ right now, and/or C++ is your second programming language, I can only encourage you not to do In case you're doing it, don't waste time on getting the destructor order 'right'. There's nothing you can do about it - this is even mentioned in the subject.#CPP 00
written for version 8
#During
darkest secret
has something filled in for every contact.ex02
. (You might want to come back later and do it then.)
Welcome to the monster module. It has a remarkable seven exercises, and the different sections are only very loosely related. The first two exercises are about memory allocation. You'll want to make sure that you know about the difference between the Stack and the Heap by now. Then, it's about pointers vs. references. Read up on their difference. After that, it's about being able to use For And, last but not least, get to know the #CPP 01
written for version 9.2
std::string
properly (and efficiently). Spend some time on the respective online documentation page, and on StackOverflow/Github :)ex05
, read the CPP FAQ on Pointers to member functions.switch (x) case ..: ...
construct.
Ookay. Let's get to know the Orthodox Canonical Form, shall we? Let's say your want to have Table Tennis-Ball objects. Then, the header file Per constructor, destructor (sometimes also abbreviated: ctor & dtor) and operator overload in the Header that is inside of a public declaration, you also need to have an implementation that goes in your TableTennisBall.cpp. That one should at least contain this: On how to implement those properly, refer to [this](https://cplusplus.com/articles/y8hv0pDG/) article. In the CPP Exam, this form is interestingly called Copliens Form. Why? Well, since this is done, back to the subject. Read the articles they're linking: The next challenge you might encounter is overloading operators. This is a little difficult at the beginning. Please ask someone for 5min of their time if you're unsure how to implement that. I've now regularly seen the post-increment operators done wrong. Make sure you understand what you should have been doing, what this smallest representable is, and that you did implement them correctly, too. Here's a test to make sure everything's working: (Yes, those And now — with Try to write the code so that other people can read it. That is not trivial in this case.#CPP 02
written for version 7.1
Inside the 'New rules' part, there is an attempt to explain what this is. I'm quite sure that this second sentence is a pure translation accident. To make that concrete and clear:TableTennisBall.hpp
should at least contain this:TableTennisBall.hpp
class TableTennisBall
{
private:
public:
TableTennisBall();
TableTennisBall(TableTennisBall &other);
TableTennisBall &operator=(TableTennisBall &other);
~TableTennisBall();
};
TableTennisBall.cpp
TableTennisBall(){}
TableTennisBall(TableTennisBall &other){}
TableTennisBall &operator=(TableTennisBall &other){}
~TableTennisBall(){}
I don't know.main.cpp
using std::cout;
using std::endl;
int main()
{
Fixed a(1);
cout << endl << "=== Mutation ===" << endl;
cout << "a: " << a << endl;
cout << "a--: " << a-- << endl;
cout << "a: " << a << endl;
cout << "++a: " << ++a << endl;
cout << "a: " << a << endl;
cout << "a++: " << a++ << endl;
cout << "a: " << a << endl;
cout << "--a: " << --a << endl;
cout << "a: " << a << endl;
}
using
s are allowed.)
You're able to figure out what the printout should be, aren't you?ex03
comes an actually interesting exercise.
In case you want to do that: Read about binary space partitioning.
You might want to start here for a general overview: BSP: Doom
After you're done reading that, a small internet search will surely be able to help you.
Wohoo, you've arrived at the core of class-based object oriented programming: Inheritance. So: Read up on how C++ inheritance works: After that, the keywords to search for are: Well, I'll be supplying a little more help. Here is what Wikipedia says about multiple inheritance in C++: Even though it's not that easy to understand, everything what you need is in there.#CPP 03
written for version 6
virtual
, protected
, and diamond of death cpp.D
object would actually contain two separate A
objects, and uses of A
's members have to be properly qualified. If the inheritance from A
to B
and the inheritance from A
to C
are both marked "virtual
" (for example, class B : virtual public A
), C++ takes special care to only create one A
object, and uses of A
's members work correctly. If virtual inheritance and nonvirtual inheritance are mixed, there is a single virtual A
, and a nonvirtual A
for each nonvirtual inheritance path to A
. C++ requires stating explicitly which parent class the feature to be used is invoked from i.e. Worker::Human.Age
. C++ does not support explicit repeated inheritance since there would be no way to qualify which superclass to use (i.e. having a class appear more than once in a single derivation list [class Dog : public Animal, Animal
]). C++ also allows a single instance of the multiple class to be created via the virtual inheritance mechanism (i.e. Worker::Human
and Musician::Human
will reference the same object).
Good luck! :)
This module is about understanding how abstract classes work in C++. So, spend some time on the internet to find out: With that, there is not much more than hard work awaiting :)#CPP 04
written for version 10
virtual
member functions are different from 'normal' ones
One last bit of advice for this module: Since the exam is almost exactly ex03 of CPP04, write it as soon you can. It is worth it.
Wow. What a giant amount of typing. But well done!
Good luck on your further journey through programming and C++.
And may your code never get too complex.