Latest / CyberSecurity Summary / Professional C++ (Tech Today)
Transcript
- 0:00Speaker 1: Welcome to the deep dive. You know, for a lot
- 0:02Speaker 1: of people, C plus plus has this reputation maybe a
- 0:06Speaker 1: bit intimidating, right, like this super powerful tool that's also
- 0:10Speaker 1: well kind of complex.
- 0:11Speaker 2: It definitely has that aura sometimes. But the reality is
- 0:14Speaker 2: this isn't some dusty old language.
- 0:17Speaker 1: No exactly, it's constantly evolving, getting smarter really for today's challenges.
- 0:22Speaker 2: Absolutely, it's vibrant, it's living, built on this rock solid foundation.
- 0:27Speaker 2: And for this deep dive, we're cracking open professional C
- 0:30Speaker 2: plus plus. Think of it like our treasure.
- 0:33Speaker 1: Map, and where are the guides pointing out the gold?
- 0:36Speaker 1: Specifically for you, the learner.
- 0:38Speaker 2: That's the plant. We're pulling out the essentials about modern
- 0:41Speaker 2: C plus plus from this material.
- 0:43Speaker 1: Yeah, tracing its journey right from the beginnings to the
- 0:45Speaker 1: latest features, navigating that big standard library.
- 0:49Speaker 2: Consider this your express route. We want you to get
- 0:51Speaker 2: what's significant, what's exciting and C plus plus but without
- 0:54Speaker 2: getting totally bogged down.
- 0:56Speaker 1: We're hunting for those aha moments, the practical stuff you
- 0:59Speaker 1: can actually use.
- 1:00Speaker 2: Okay, so let's kick off with just how much C
- 1:02Speaker 2: plus plus has grown. It's pretty striking. Ah So, well,
- 1:05Speaker 2: you look at the C twenty three standard speck it's
- 1:08Speaker 2: what under eight hundred pages pretty lean? Okay, Then you
- 1:11Speaker 2: look at C plus plus twenty three over two thousand pages.
- 1:15Speaker 1: Whoa, that's a lot more.
- 1:16Speaker 2: It's not just pages, right, It shows this massive expansion
- 1:20Speaker 2: in what C plus plus can do. Features piled on features,
- 1:24Speaker 2: it really cements it as a true superset of C.
- 1:27Speaker 1: Okay, let's unpack that. The story is C plus plus
- 1:29Speaker 1: started as C with classes.
- 1:31Speaker 2: More or less, that was the kernel. Yeah, brings C's
- 1:34Speaker 2: power together with object oriented thinking.
- 1:37Speaker 1: And it sounds like it's been on a constant growth spurt,
- 1:39Speaker 1: evers men's fixing some of C's old issues along the way.
- 1:41Speaker 2: Precisely, there's share history, some familiar syntax if you know C,
- 1:45Speaker 2: but the paths have really diverged, and they keep diverging,
- 1:49Speaker 2: which means even if you know C or Java or
- 1:52Speaker 2: C sharp, maybe even praython, you've got to stay aware.
- 1:55Speaker 2: Modern C plus plus has its own unique syntax, new
- 1:58Speaker 2: features popping up all the time, always something new, right.
- 2:01Speaker 1: Which leads is perfectly into exploring some of those modern features.
- 2:04Speaker 1: That new syntax. Now, I saw something about needing a
- 2:06Speaker 1: special compiler switch to even use the latest stuff.
- 2:09Speaker 2: Ah. Yes, if you're using GCC the compiler, you'll likely
- 2:13Speaker 2: need STDC plus plus two B.
- 2:15Speaker 1: C plus plus two B.
- 2:17Speaker 2: Yeah, that's the flag right now to unlock C plus
- 2:20Speaker 2: plus twenty three features. It'll probably become STDC plus plus
- 2:23Speaker 2: twenty three eventually once things are fully finalized, but for now,
- 2:27Speaker 2: that's your key to the cutting edge.
- 2:29Speaker 1: Interesting, and one of those cutting edge things seems to
- 2:32Speaker 1: be how we even bring in the standard library.
- 2:34Speaker 2: Something about modules, Yeah, that's a pretty fundamental shift. C
- 2:38Speaker 2: plus plus twenty three introduces this idea of a standard
- 2:41Speaker 2: named module. It's just called std.
- 2:43Speaker 1: Okay.
- 2:44Speaker 2: The long term goal is you'll just write import std
- 2:47Speaker 2: and boom, you get the whole standard library, much cleaner,
- 2:50Speaker 2: more efficient than the old hashtag include way.
- 2:53Speaker 1: Sounds great, but.
- 2:55Speaker 2: Compiler support for that single STD module is still well, it's.
- 2:58Speaker 1: Early days, ah so for now.
- 3:01Speaker 2: For now, you'll probably still need to import individual bits,
- 3:03Speaker 2: like if you want the new print functions, you do
- 3:05Speaker 2: import print.
- 3:06Speaker 1: Notice the difference. No angle brackets for the main module
- 3:09Speaker 1: import std, but you use them for specific headers import print.
- 3:14Speaker 2: Got it. So moving towards cleaner imports, but we're not
- 3:18Speaker 2: quite there yet. It's still a bit like hashtag include,
- 3:20Speaker 2: but with import.
- 3:21Speaker 1: Pretty much for the time being. Okay, now the real
- 3:25Speaker 1: basics numbers, characters, int, double char, has anything changed in how.
- 3:30Speaker 2: We declare those The types themselves, the fundamental ones are
- 3:34Speaker 2: the same, but how you initialize them. Modern C plus
- 3:38Speaker 2: plus really pushes for uniform initialization using curly braces, so.
- 3:42Speaker 1: Like int ii eleven instead of into ee eleven exactly.
- 3:47Speaker 2: And the nice thing is that syntax works consistently everywhere
- 3:50Speaker 2: for all types and all sorts of situations. It helps
- 3:52Speaker 2: avoid some old weird ambiguities.
- 3:54Speaker 1: Makes sense. Consistency is good.
- 3:56Speaker 2: And we also have newer character types like chart char sixteenth,
- 3:59Speaker 2: chart thirty two. Handling different unicode encodings properly super important Nowadays.
- 4:03Speaker 1: Bedifort initialization sounds more predictable, easier to read. Now I
- 4:07Speaker 1: saw some other let's say, unusual syntax, something with double
- 4:10Speaker 1: square brackets. Fall through.
- 4:12Speaker 2: Yes, fall through, that's an attribute you use it inside
- 4:14Speaker 2: switch statements. Okay, well, you know how in C plus
- 4:17Speaker 2: plus C if you forget a break in a case,
- 4:19Speaker 2: execution just falls through to the next one.
- 4:22Speaker 1: Yeah, that can cause nasty bugs.
- 4:23Speaker 2: Right, but sometimes you want it to fall through, so
- 4:26Speaker 2: fall through is like putting up a big sign saying, compiler,
- 4:29Speaker 2: I meant to do this, no bug here makes the
- 4:33Speaker 2: intent super clear.
- 4:34Speaker 1: Oh okay. Explicitly saying I know there's no break and
- 4:37Speaker 1: that's okay makes sense for clarity. What about I think
- 4:41Speaker 1: they called it this spaceship operator.
- 4:44Speaker 2: That's weird, the spaceship operator. Officially, it's the three way
- 4:47Speaker 2: comparison operator. It's fantastic.
- 4:49Speaker 1: Why is that?
- 4:50Speaker 2: Because it does all six comparisons less than, greater than,
- 4:54Speaker 2: equal to, not equal to, less greater than or equal
- 4:57Speaker 2: all in one go. Really, how it returned turns a
- 5:00Speaker 2: special result, often a type like strong ordering. The value
- 5:04Speaker 2: tells you if the first thing is less, greater or
- 5:06Speaker 2: equal to the second, okay, And it's smart for things
- 5:09Speaker 2: like floating point numbers where you have magnan not a number.
- 5:13Speaker 1: Which isn't really lesser greater than anything exactly.
- 5:16Speaker 2: The spaceship operator can return a partial ordering for those
- 5:19Speaker 2: acknowledging they aren't strictly comparable. It's a game changer for
- 5:22Speaker 2: writing generic comparisons. Plus, the compiler can often autogenerate all
- 5:26Speaker 2: six comparison operators for you if you define just this
- 5:29Speaker 2: one less code fewer mistakes.
- 5:32Speaker 1: Wow, okay, one operator all the info even handles trippy
- 5:37Speaker 1: stuff like nan powerful now functions. I saw a note
- 5:41Speaker 1: about empty parameter lists. Any change there.
- 5:44Speaker 2: Yeah, small simplification in C plus plus A if a
- 5:47Speaker 2: function takes no parameters, just use empty parentheses void my function.
- 5:51Speaker 1: No need for void inside the parentheses like in some
- 5:54Speaker 1: older SEA styles.
- 5:55Speaker 2: Nope, not needed. But crucially you still must use void
- 5:58Speaker 2: for the return type if the function doesn't return any thing.
- 6:00Speaker 1: Got it empty for no input, void return for no output.
- 6:04Speaker 1: Simple enough? What about text? We still stuck with those
- 6:07Speaker 1: tricky C style character arrays?
- 6:09Speaker 2: Thankfully no. Modern C plus plus gives us STD dot string.
- 6:12Speaker 2: You get it from the string header and that's better
- 6:14Speaker 2: because oh it's miles better, easier, safer. You can assign text,
- 6:18Speaker 2: join strings, access characters easily, all without the manual memory
- 6:22Speaker 2: management headaches of char It basically EXE a built in type.
- 6:25Speaker 1: Now anyone who's undered a null terminator bug will appreciate that. Yeah, okay,
- 6:30Speaker 1: so C plus plus is clearly more than just a
- 6:33Speaker 1: better see it's properly object oriented. Quick refresher, what's the
- 6:37Speaker 1: core idea there? In modern C plus plus? Go right?
- 6:41Speaker 2: Oop object oriented programming. It's a shift from just thinking
- 6:44Speaker 2: about sequences of actions. You think about objects.
- 6:47Speaker 1: And objects are.
- 6:48Speaker 2: They bundle data? They're attributes and the functions or methods
- 6:50Speaker 2: that work on that data. In C plus plus A,
- 6:53Speaker 2: the blueprints for these objects are classes.
- 6:55Speaker 1: Okay, and where do we define these classes? Now? With
- 6:58Speaker 1: modules and all.
- 6:58Speaker 2: Often you'll define them in modern interface files. These usually
- 7:01Speaker 2: have a dot CPPM extension. Then you explicitly export the
- 7:05Speaker 2: module and the class definitions you want others to use.
- 7:07Speaker 1: So the dot CPPM file is like the public facing
- 7:10Speaker 1: design exactly.
- 7:11Speaker 2: Imagine a module airline ticket. Inside you export class airline ticket.
- 7:16Speaker 2: It would have public stuff functions like calculate price end
- 7:18Speaker 2: dollars or get passenger name, things.
- 7:20Speaker 1: You can call from outside right, and.
- 7:22Speaker 2: It would have private members. The internal data like in
- 7:25Speaker 2: passenger name or number of miles hidden managed internally. That's encapsulation,
- 7:31Speaker 2: bundling data and functions, controlling access.
- 7:34Speaker 1: Public private classic oop. Got it. When we create one
- 7:39Speaker 1: of these airline ticket objects, how do those private data
- 7:43Speaker 1: members like number oh miles get their starting values?
- 7:46Speaker 2: Modern C plus plus has in class initializers. Write in
- 7:50Speaker 2: the class definition you can give a data member a
- 7:52Speaker 2: default value oh neat Yeah, Like in an employee class,
- 7:55Speaker 2: you could write in salary seventy five thousand right there.
- 7:58Speaker 2: If you create an employ object without specifying a salary,
- 8:01Speaker 2: it gets that default. If a basic type doesn't have one,
- 8:04Speaker 2: it usually gets zero. Initialized helps ensure objects start in
- 8:07Speaker 2: a known state.
- 8:08Speaker 1: Setting defaults right where you declare them. Seems handy. Okay,
- 8:12Speaker 1: let's tackle a topic that sometimes makes people nervous. Pointers
- 8:14Speaker 1: and references. How do they fit into modern C plus
- 8:17Speaker 1: plus sac has that changed much?
- 8:20Speaker 2: Coiners are still around, definitely needed for low level stuff
- 8:23Speaker 2: or interacting with C libraries, but modern C plus plus
- 8:28Speaker 2: really encourages minimizing manual pointer use, especially for memory ownership.
- 8:33Speaker 1: Right less direct fiddling with memory addresses exactly now.
- 8:37Speaker 2: With constant pointers, there's a useful rule. Const applies to
- 8:40Speaker 2: what's immediately to its left. If there's nothing left, it
- 8:43Speaker 2: applies to their right. It constan applies left unless it's
- 8:46Speaker 2: at the very beginning. So in const ptr means a
- 8:49Speaker 2: pointer to a constant integer. You can change where ptr points,
- 8:53Speaker 2: but not the in value via ptrh And.
- 8:56Speaker 1: In const ptr, yeah.
- 8:57Speaker 2: Const is to the left of ptr. So it's a
- 9:00Speaker 2: constant pointer to a non constant integer, you can change
- 9:03Speaker 2: the NS value, but you can't make ptr points somewhere else.
- 9:06Speaker 2: And in constant ptr both constant pointer to a constant
- 9:10Speaker 2: integer can't change the in value, can't change where the
- 9:12Speaker 2: pointer points. That finer control helps right safer code.
- 9:16Speaker 1: Constant plies left. Okay, that's a good knemonic. What about references?
- 9:19Speaker 1: They feel similar to pointers, but different they are different.
- 9:23Speaker 2: Think of our reference as an alias, just another name
- 9:25Speaker 2: for an existing variable. When you say nten x, refix
- 9:30Speaker 2: x ref is x.
- 9:32Speaker 1: The key thing.
- 9:33Speaker 2: Once a reference is initialized, it cannot be changed to
- 9:36Speaker 2: refer to a different variable. Ever, you can change the
- 9:39Speaker 2: value of the variable it refers to if it's not
- 9:42Speaker 2: a constant reference, but the referance itself is.
- 9:44Speaker 1: Stuck and references to constant like.
- 9:46Speaker 2: Constant in treface. Some ind they're simpler in a way
- 9:49Speaker 2: because the reference itself is implicitly constant regarding what it
- 9:53Speaker 2: refers to, and you can't change the value through it.
- 9:55Speaker 1: Okay, So when do you pick a pointer versus a reference?
- 9:58Speaker 1: In modern code?
- 10:00Speaker 2: It mainly for dynamic memory you get from new you
- 10:03Speaker 2: need a pointer to hold that address. Also, if you
- 10:05Speaker 2: need something that might be null and optional value pointers
- 10:08Speaker 2: can be null, ptr references.
- 10:10Speaker 1: Can't, and polymorphism yep.
- 10:12Speaker 2: Storing different derived types via a base class pointer in containers.
- 10:16Speaker 2: But the big thing about ownership modern C plus plus
- 10:19Speaker 2: is avoid raw pointers for owning memory. Use smart pointers instead.
- 10:23Speaker 2: We'll get to those so smart.
- 10:25Speaker 1: Pointers handle the ownership when our reference is the better choice.
- 10:28Speaker 2: Often for function parameters, if you need to guarantee the
- 10:31Speaker 2: function gets a valid object. References are great because they
- 10:35Speaker 2: can't be null and if the function might modify the
- 10:37Speaker 2: object and it's not a constant reference. They often lead
- 10:40Speaker 2: to cleaner syntax too.
- 10:42Speaker 1: It's like a variable as a house. A reference is
- 10:44Speaker 1: a nickname for the house. A pointer is the address
- 10:47Speaker 1: on a piece of paper. You can change the address
- 10:49Speaker 1: to point to a different house.
- 10:51Speaker 2: That's a great analogy. References are bound to one house.
- 10:54Speaker 2: Pointers can be retargeted.
- 10:55Speaker 1: Smart pointers definitely sounds like the way forward from memory. Okay,
- 11:01Speaker 1: zooming out best practices for writing good C plus plus code.
- 11:05Speaker 2: Overall several things. One use getters and setters, accessor and
- 11:09Speaker 2: mutator methods, even to access data members within the same.
- 11:12Speaker 1: Class, really even inside the class.
- 11:15Speaker 2: Why it gives you a layer of abstraction maybe later
- 11:17Speaker 2: you need to add validation or change how the data
- 11:19Speaker 2: is stored internally or log access. Using methods means you
- 11:23Speaker 2: can change the implementation without breaking how other parts of
- 11:26Speaker 2: the class use the data.
- 11:27Speaker 1: Ah, future proofing makes sense. What else?
- 11:30Speaker 2: Clear code structure? Breakdown functions? Seriously, if a function does
- 11:35Speaker 2: three different things, it should probably be three functions. Makes
- 11:38Speaker 2: it easier to read, test, maintain single responsibility principle.
- 11:43Speaker 1: Basically, smaller focused functions. Got it right. What about naming
- 11:47Speaker 1: things variable names? I've seen debates about prefixes like M
- 11:50Speaker 1: for members.
- 11:51Speaker 2: Yeah, that's a bit contentious. Prefixes like M or G
- 11:54Speaker 2: or ptr. Some find them helpful, but the modern trend
- 11:58Speaker 2: is often against them. Why they can hurt maintainability. If
- 12:02Speaker 2: you change a variables type or scope, you have to
- 12:04Speaker 2: remember to change the prefix everywhere miss one and the
- 12:07Speaker 2: name becomes misleading. Better to choose names that clearly describe
- 12:11Speaker 2: the variable's purpose, not just its type. The compiler knows
- 12:15Speaker 2: the type.
- 12:15Speaker 1: Focus on purpose, not type hints in the name. Okay, okay.
- 12:19Speaker 1: We also said references are often safer than rob pointers.
- 12:22Speaker 1: Any common mix ups there, especially with function calls.
- 12:24Speaker 2: Yeah, A big one is thinking that seeing an ampersand
- 12:27Speaker 2: in a function call automatically means the original variable will
- 12:30Speaker 2: be changed, or that no ampersand means it won't be changed.
- 12:33Speaker 2: That's not true, not necessarily. You have to look at
- 12:35Speaker 2: how the parameter is declared in the function's definition. Is
- 12:38Speaker 2: it TN non constant reference consts TNT non constant pointer
- 12:44Speaker 2: const T Only the const versions guarantee no modification through
- 12:47Speaker 2: that parameter. The call site syntax alone doesn't tell the
- 12:49Speaker 2: whole story.
- 12:50Speaker 1: Okay, crucial distinction. Look at the function declaration, not just
- 12:54Speaker 1: the call. Got it shifting gears? Planning? How important is
- 12:58Speaker 1: designing stuff before writing c P plus plus code?
- 13:01Speaker 2: Hugely important, especially for bigger projects. It's like needing blueprints
- 13:04Speaker 2: before building.
- 13:05Speaker 1: A house avoids chaos exactly.
- 13:08Speaker 2: Design helps define how parts interact, who's responsible for what,
- 13:11Speaker 2: how requirements are met. Without it, you risk missing connections,
- 13:14Speaker 2: missing chances for code reuse, picking inefficient solutions. It's the
- 13:17Speaker 2: big picture and it's documentation.
- 13:19Speaker 1: But what about agile ideas like working software over comprehensive documentation.
- 13:25Speaker 2: Good point, It's a balance. Agile doesn't mean no documentation.
- 13:29Speaker 2: You still absolutely need up to date design docs for
- 13:32Speaker 2: the key architectural stuff. Essential for the long run.
- 13:36Speaker 1: Okay, finding that balance? What are some core design principles
- 13:40Speaker 1: C plus plus dev should really internalize.
- 13:42Speaker 2: Two huge ones, abstraction and reuse abstraction?
- 13:46Speaker 1: First, what's the essence.
- 13:48Speaker 2: Designing so users of your class or function interact via
- 13:52Speaker 2: a clear interface without needing to know the messy internal details.
- 13:55Speaker 2: Like the chessboard example. Earlier you make moves, you don't
- 13:58Speaker 2: need to know if the board is totor or does
- 14:00Speaker 2: an array or pointers or whatever implementation hidden?
- 14:03Speaker 1: Hide the complexity behind a clean interface and reuse.
- 14:07Speaker 2: Just like the baking analogy, you don't build an oven
- 14:09Speaker 2: every time you bake a cake. Right reuse means actively
- 14:13Speaker 2: looking for existing code libraries, frameworks, your own pass code
- 14:18Speaker 2: that does what you need. Saves massive time and effort,
- 14:21Speaker 2: usually leads to more robust code because it's already tested.
- 14:24Speaker 2: Don't reinvent the wheel if a perfectly good one exists.
- 14:27Speaker 1: Makes total sense. But how do you choose what to reuse?
- 14:30Speaker 1: Picking the right library?
- 14:32Speaker 2: Say good question? First, Really understand its capabilities and its limitations.
- 14:36Speaker 2: Read the docs, study the API. If you can look
- 14:39Speaker 2: at the source, talk to people. Definitely ask other devs
- 14:42Speaker 2: who've used it. What are the strengths, weaknesses, any traps.
- 14:45Speaker 2: Get the core idea, then think about your specific needs.
- 14:48Speaker 1: Understand it fully, read the manual, ask around solid advice now.
- 14:54Speaker 1: The source mentioned a mental shift moving from procedural thinking
- 14:57Speaker 1: to object oriented thinking. How does that usually happen?
- 15:01Speaker 2: Often there's an aha moment. Programmers start seeing how data
- 15:04Speaker 2: and the functions that act on it naturally belong together
- 15:07Speaker 2: in well classes.
- 15:09Speaker 1: At lays they're grouping things.
- 15:10Speaker 2: Yeah, some might refactor existing procedural code bit by bit,
- 15:13Speaker 2: turning related stuff into classes. Others might dive straight into
- 15:17Speaker 2: an O design for a new project. The source notes
- 15:20Speaker 2: two main approaches using classes selectively we're convenient or building
- 15:24Speaker 2: the whole system around interacting objects from the start. No
- 15:27Speaker 2: single right path, but understanding the benefits is key.
- 15:31Speaker 1: A spectrum then from tactically used to full adoption. What
- 15:34Speaker 1: about going too far the other way? Designing classes that
- 15:37Speaker 1: are too general?
- 15:38Speaker 2: Ah, the overly general class. That's usually a bad sign.
- 15:41Speaker 2: Trying to make one class handle too many unrelated things like.
- 15:44Speaker 1: The media organizer example, photos, music, movies, journals all in one.
- 15:49Speaker 2: Class exactly, It ends up confusing, hard to understand, hard
- 15:53Speaker 2: to use generic properties like data a performed method that
- 15:57Speaker 2: does wildly different things. It's better to have specific classes,
- 16:02Speaker 2: each representing one clear concept.
- 16:05Speaker 1: One class to rule them all usually rules none of them.
- 16:08Speaker 2: Well, gotcha? How do classes relate to each other? The
- 16:11Speaker 2: source mentioned has.
- 16:12Speaker 1: A and is a fundamental relationships, has a is composition
- 16:16Speaker 1: or containment. One class holds an instance of another as a.
- 16:19Speaker 2: Member, like car has an engine.
- 16:22Speaker 1: Perfect example. The engine is a component is inheritance. A
- 16:26Speaker 1: derived class is a specialized version of a base class.
- 16:29Speaker 1: Sports car is a car. It gets all the car stuff,
- 16:31Speaker 1: maybe adds more specific things.
- 16:32Speaker 2: Has a for components, is a for specialization. Clear? What's
- 16:35Speaker 2: the main win from using inheritance code reuse?
- 16:38Speaker 1: Primarily the derived class automatically gets the base class members
- 16:42Speaker 1: and functions, no need to rewrite common stuff. It works
- 16:44Speaker 1: best when the base class defines a common interface maybe
- 16:47Speaker 1: abstract the derived classes implement.
- 16:50Speaker 2: You need to be careful. Yeah, if a derived class
- 16:53Speaker 2: ends up overwriting or changing most of its parent stuff,
- 16:56Speaker 2: maybe inheritance wasn't the right fit. There is a relationship
- 17:00Speaker 2: should feel natural?
- 17:01Speaker 1: Makes sense. The source also mentioned multiple inheritance C plus
- 17:04Speaker 1: plus allows.
- 17:05Speaker 2: That it does. A class can inherit directly from more
- 17:08Speaker 2: than one base class. The example was maybe an animal base,
- 17:11Speaker 2: then hierarchies for size, diet movement. A dog bird could
- 17:16Speaker 2: inherit from dog like and bird like bases.
- 17:19Speaker 1: Sounds potentially complicated.
- 17:22Speaker 2: It can be, especially with the diamond problem. If a
- 17:24Speaker 2: class inherits from two classes that both inherit from the
- 17:27Speaker 2: same grandparent class, you get ambiguity. Which grandparent member do
- 17:31Speaker 2: you use? C plus plus has mechanisms like virtual inheritance
- 17:34Speaker 2: to solve it, but yeah, it adds complexity, needs careful handling.
- 17:37Speaker 1: The diamond problem something to watch out for. Okay, beyond
- 17:40Speaker 1: reuse and abstraction any other key design principles C plus
- 17:44Speaker 1: plus dev should keep front.
- 17:45Speaker 2: Of mind definitely clear code structure. Like we said, usable
- 17:49Speaker 2: interface is intuitive for others to use, balancing generality versus specificity.
- 17:53Speaker 2: Don't make it too complex by trying to handle everything,
- 17:56Speaker 2: but don't make it so specific it's inflexible and all
- 18:00Speaker 2: to principles five core OO design guidelines for maintainable flexible code.
- 18:05Speaker 2: We won't dive deep into solid now, but they're worth learning.
- 18:08Speaker 1: Lots to think about for good design. The source used
- 18:11Speaker 1: a cool analogy for the strategy pattern interchangeable parts like
- 18:15Speaker 1: a car and its engine. Can you expand on that?
- 18:18Speaker 2: Yeah, that illustrates it well. Instead of hard coding engine
- 18:21Speaker 2: logic inside the car class, the strategy pattern says, define
- 18:24Speaker 2: a separate engine interface or base class. Maybe have gasoline
- 18:28Speaker 2: engine electric engine.
- 18:29Speaker 1: Implementations, and the car just holds one.
- 18:31Speaker 2: Right, the car class holds a pointer or reference to
- 18:34Speaker 2: some kind of engine. This makes it super flexible. You
- 18:36Speaker 2: can swap engine types easily, maybe even at run time,
- 18:39Speaker 2: without changing the car class itself, just like real cars.
- 18:41Speaker 2: Modularity adaptability design for swappable parts makes software more flexible.
- 18:46Speaker 1: Okay. Template programming now the huge C plus plus feature
- 18:49Speaker 1: design considerations there.
- 18:51Speaker 2: Templates let you write code parameterized by type. Think std
- 18:55Speaker 2: dot vectorant and std dot vector double. Same vector code.
- 18:58Speaker 1: Works for both once, use it for many types.
- 19:02Speaker 2: That's the core idea. Create generic classes or functions that
- 19:05Speaker 2: work with different data types without duplicating code. It can
- 19:08Speaker 2: get really advanced, but even basic templates give huge wins
- 19:12Speaker 2: for reuse and flexibility, like a single sort template sorting,
- 19:16Speaker 2: ince double strings, anything comparable. We'll dig into writing custom
- 19:20Speaker 2: templates later.
- 19:21Speaker 1: Generic code adapting to types powerful stuff. Now shifting to
- 19:25Speaker 1: safety and robustness. How do we design C plus plus
- 19:28Speaker 1: code to be reliable?
- 19:30Speaker 2: It's about proactively building in checks and safeguards. How do
- 19:33Speaker 2: you use signal errors instead of just magic numbers or
- 19:36Speaker 2: error codes? Modern clus plus has better tools like what
- 19:39Speaker 2: std't at optional for values that might be absent, forces
- 19:42Speaker 2: the caller to check, or exceptions for more serious errors
- 19:45Speaker 2: the current code can't handle. Let someone else catch and
- 19:47Speaker 2: deal with it. We'll cover exceptions more soon. The main
- 19:50Speaker 2: thing is anticipating problems and handling them gracefully, not just crashing.
- 19:54Speaker 1: Be proactive, use the right tools like optional or exceptions.
- 19:58Speaker 2: Got it? The source also distinguish between internal contracts and
- 20:02Speaker 2: external APIs what's the difference?
- 20:04Speaker 1: Important distinction. An internal interface is the contract between parts
- 20:09Speaker 1: of your own code, how your modules talk to each other.
- 20:12Speaker 1: An API application programming interface is the public contract for
- 20:16Speaker 1: other developers, maybe outside your team or company, to use
- 20:19Speaker 1: your library or product.
- 20:20Speaker 2: So APIs need more care.
- 20:22Speaker 1: Much more people rely on public API staying stable. Breaking
- 20:26Speaker 1: changes can wreck other people's code, so careful planning documentation,
- 20:30Speaker 1: maybe talking to users is crucial before exposing an API.
- 20:34Speaker 1: Internal interfaces allow a bit more flexibility since you control
- 20:36Speaker 1: all the pieces. Think household rules versus city laws.
- 20:39Speaker 2: Internal rules versus public laws. Good analogy. Okay, let's dive
- 20:42Speaker 2: into something that often trips people up, dynamic memory management.
- 20:46Speaker 2: What's the modern guidance.
- 20:48Speaker 1: Strong advice Avoid low level manual memory stuff as much
- 20:51Speaker 1: as you possibly can.
- 20:53Speaker 2: Steer clear of raw pointers, owning memory allocated with new
- 20:56Speaker 2: avoid old C functions like malik and free. Instead, lean
- 21:01Speaker 2: heavily on safe C plus plus features and the library like.
- 21:05Speaker 1: Std dot string, std dot vector.
- 21:07Speaker 2: Exactly and crucially smart pointers for dynamically allocated objects. Let
- 21:12Speaker 2: the language and library manage the memory for you.
- 21:14Speaker 1: Minimize the manual juggling. Rely on RAII. What's RAI again? Quickly?
- 21:20Speaker 2: Resource acquisition is initialization tie a resources lifetime memory file
- 21:25Speaker 2: handles sockets to an object's lifetime. Object created, acquire a
- 21:28Speaker 2: resource and constructor. Object destroyed goes out of scope, maybe
- 21:31Speaker 2: due to exception, release resource and.
- 21:33Speaker 1: Destructive, so automatic cleanup pretty much.
- 21:36Speaker 2: The destructor is guaranteed to run when the object's lifetime ends.
- 21:39Speaker 2: If the destructor releases the resource, like calling delete on
- 21:42Speaker 2: memory allocated in the constructor, leaks become virtually impossible. It's
- 21:46Speaker 2: fundamental to modern C plus plus rid.
- 21:48Speaker 1: RAI resource life tied to object life robust.
- 21:52Speaker 2: So why is avoiding manual management so vital? What's the
- 21:55Speaker 2: big danger?
- 21:55Speaker 1: Memory leaks the absolute classic C plus plus nightmare.
- 21:59Speaker 2: All memory with new, but you forget or fail to
- 22:03Speaker 2: delete it later, that memory just sits there, allocated but unusable.
- 22:07Speaker 2: Accumulate enough leaks, Your program eats, memory slows down, gets unstable,
- 22:10Speaker 2: maybe crashes. The source had that simple leaky function. Example
- 22:15Speaker 2: new int then return poof memory orphaned.
- 22:20Speaker 1: A slow death for your app. So if you must
- 22:23Speaker 1: use new.
- 22:24Speaker 2: The absolute rule, every new needs a matching delete. And
- 22:28Speaker 2: if you use new to allocate an array you new delete,
- 22:30Speaker 2: you absolutely must use delete. Mismatching new delete or new
- 22:33Speaker 2: delete is undefined behavior bad news, stack arrays by the way,
- 22:37Speaker 2: or manage automatically no delete needed or allowed. The danger
- 22:40Speaker 2: is the heap the free store managed with.
- 22:42Speaker 1: Rematches, delete, new, matches delete, got it, But modern C
- 22:45Speaker 1: plus plus pushes us away from that. How do RAII
- 22:48Speaker 1: classes like vector and the smart pointers.
- 22:50Speaker 2: Help STD string, std dot vector They are RAII internally
- 22:55Speaker 2: they manage their own dynamic memory. When a vector object
- 22:58Speaker 2: goes out of scope, its destructor runes and automatically frees
- 23:01Speaker 2: the memory it was using. You don't touch new or
- 23:03Speaker 2: delete yourself for its contents.
- 23:04Speaker 1: The object handles its own cleanup. Nice and smart pointers.
- 23:07Speaker 2: They bring RIII to any dynamically allocated object their classes.
- 23:12Speaker 2: Wrapping a raw pointer.
- 23:13Speaker 1: Like std dot uniquetr.
- 23:15Speaker 2: Right uniqueptra means exclusive ownership. Only one uniquepture owns the object.
- 23:20Speaker 2: When that uniqu capture eyes, the object gets deleted. Simple
- 23:24Speaker 2: clear ownership.
- 23:25Speaker 1: And std dot shared ptr.
- 23:27Speaker 2: Allows multiple owners. It keeps a reference count. The object
- 23:30Speaker 2: is only deleted when the last shared ptr pointing to
- 23:33Speaker 2: it goes away. Useful when ownership is naturally shared.
- 23:36Speaker 1: Automatic deletion via RAI rappers exactly.
- 23:39Speaker 2: The source even showed using shared ptr with a custom
- 23:41Speaker 2: deleter to manage a C style file handle from fopen
- 23:45Speaker 2: when the last shared ptr went away. The custom delet
- 23:47Speaker 2: called f close automatically, very flexible for managing all sorts
- 23:51Speaker 2: of resources, especially from older APIs, though C plus plus
- 23:54Speaker 2: filestreams do this automatically too.
- 23:56Speaker 1: Smart pointers seem incredibly useful for safer C plus plus PI. Okay,
- 24:00Speaker 1: we've hit a lot modern features op safer memory management.
- 24:04Speaker 1: When we continue, we'll go deeper into classes inheritance, modules
- 24:07Speaker 1: versus headers, templates, and then tackle a huge C plus
- 24:10Speaker 1: plus standard library containers, algorithms, io exceptions, and more. Stick
- 24:14Speaker 1: around for part two of our deep dive into professional
- 24:16Speaker 1: C plus plus buy