Search This Blog

Friday, May 22, 2020

(19) What are some important concepts when programming in C++? - Quora

https://www.quora.com/What-are-some-important-concepts-when-programming-in-C++

I guess I'm not sure what level you're programming at, so this depends on that. I think that for basic stuff there are four things that come to mind (maybe five (maybe six), we'll see).

  • Your compiler: This may not be the first thing to dive into, but if you're doing anything serious you should have at least a working knowledge of your toolset. This really applies to any language but at a lower level you are often going to need to know more about the build process - less is taken care of for you. Closely related, learn about Makefiles and some of their syntax. If you're using an IDE then this will be taken care of for you (still, if something goes wrong, you gotta fix it!), but if you're trying to be a C++ ninja, make is a very powerful tool that you should at least have some working knowledge of. Also, you will be getting cryptic compiler errors and you're going to have to do some digging to find out what's going on.
  • Object Oriented Programming: Uhhh, this is obvious but it obviously needs mentioning. Depending on your level of programming this might not be the first thing you dive into either. You should have a basic idea of what a class is (what is private/what is public), know about methods, constructors and destructors, etc. However, OOP is a very popular style and there are a lot of conventions and suggestions on how to solve certain problems. As Pawan mentions, learning about design patterns is a great idea. For example, say your are working through a tree and each node, possibly with different types, needs to be visited and have some work performed on it? The thing doing the visiting doesn't know exactly what each node is and so doesn't know what actions to perform. We could do some sort of switch statement, where each node has some enum: enum { NODETYPE1, NODETYPE2, …}; and access that. This is possible, but it's kind of messy. To help with this, there is a design pattern called the Visitor Pattern that gives a nice solution to the problem (read up on it!).
  • C++ is a bunch of languages strapped together!: This is one thing that trips people up, and it means there is a lot of extra learning to do to get up to speed. First you have your preprocessor. The C Preprocessor is a very minimal language that essentially does some text replacement/insertions for you and basic logic flow. You can get away with not knowing too much about about this, especially at first, and really you want to be careful using this. It's typeless which means you're potentially playing with fire, and it has some quirks to it since it is liable to be more literal than you want it to be. For example:
  1. #define MACRO(a,b) a + b
  2. int main(){
  3. int x = 2;
  4. int y = MACRO(1,x) * 3;
  5. // What do you think `y' is? 9? Wrong! You just got MACROD!
  6. printf("%d\n", y); // Outputs 7 -- why?
  7. }
  • (above point continued) It may be wise to use a const declaration instead of a macro in many cases, but macros are powerful so they shouldn't be completely avoided. They are great for inlining code, making things more readable, but just make sure you get it right! There are some weeeeird bugs out there that come from macro expansions not working how the programmer intended. After the preprocessor you then have your normal C code. C++ used to be a superset of C, but to keep up with modern trends it's had to abandon some things. However it is pretty darn close to C and it's syntax is almost indistinguishable at times. But then there is the C++ code. This is built on top of C and, while I usually find they meld together fairly nicely, it is definitely an addition and sometimes, if I've been programming in C a lot, some C++ specific syntax jumps up and bites me! Then there is also the standard library which has its own thing going on. This has syntax that is different from a lot of C++ and can almost be thought of as its own realm of C++
  • Memory Management: Always always always keep track of what method/class/struct is responsible for deallocating heap-stored data. If you don't have clear guidelines for who is responsible you will get memory leaks. Document this clearly in your comments. Sometimes you will want the thing that allocated the memory to be responsible for its release (like when the memory is allocated in-class, usually it's that classes responsibility). Other times, you want to pass that job off to someone else. For example, with a factory class that produces things and then forgets about them (this is another design pattern, btw):
  1. class Factory {
  2. public:
  3. /* makeKB creates a KB of raw memory for the caller and returns a pointer to
  4. * it. Caller is responsible for deallocation
  5. */
  6. char* makeKB(){ return new[] char[1024]; }
  7. };
  • Using other folks code: A lot of work has been done for you but it's your responsibility to track it down. With a low level language like C++ it will take you a long time to recreate something that someone has already done, and it's very likely that there will be more bugs in it (assuming the other developer knows what they are doing… Not always the case!) This means it's very much in your interest to learn about what's already out there. Check out the Boost Library - this does a lot of neat stuff.
  • Portability (technically Cross-Platform): I won't touch too much on this. C++ is portable but there are a lot of platform-dependent libraries that you might be using. Depending on your code you might have to do some macro-magic to include the right things/check if certain things are defined/etc. Since it is so low level you are very close to the hardware, and that means that there are weird issues that will come up when you switch hardware and OS's. So if you have portability in mind, make sure you do your research. Unlike Python/Java/etc you will have to figure out what you require on a system and see how to get it from each target you are compiling to. Depending on what you're doing there might be absolutely nothing you have to do, but then again, there might be. Just a heads up :)

Lastly I just want to mention that C++ is an intensely complicated language. There's a lot to it, and if you don't believe me, got grab a copy of Stroustrup's "The C++ Programming Language" and hold it up next to K&R's "The C Programming Language". The first is at least 5x the thickness of the second. C++ is trying to stay modern and yet remain low-level at the same time. It's trying to remain competitive in the world of Pythons and Javas and Gos and Haskells and Swifts… This means that there is a lot of stuff tacked on to it after the core language was created, and sometimes this doesn't always fit as nicely as one may like it to. So as you're working with it, if it seems crazy or difficult, just keep in mind that it's a tough language to use and you're not the only one who feels this way. It's also immensely rewarding to use and I wish I had more call to program in it.

Pleasant coding!


No comments:

Post a Comment

PHÂN BIỆT QUẢN TRỊ VÀ QUẢN LÝ

PHÂN BIỆT QUẢN TRỊ VÀ QUẢN LÝ Hội đồng quản trị, tiếng Anh là BOD (Board Of Directors). Còn Ban giám đốc hay Ban quản lý tiếng Anh là BOM (B...