Go From Writing Methods To Object-Oriented Programming With Your Own Classes
Don’t just use C++ as a better version of C. Learn how to use classes to start getting the full benefits of object-oriented programming. Proper use of classes will make your code easier to understand and more natural to use. However, improper use of classes will just make things frustrating and cause other developers to avoid you and your code.
You’ll learn how classes can help you create simpler code and how to design your class interface so it follows expected behavior. This will make your code look like it belongs in a C++ project and was written by somebody who knows up from down.
You’ll learn how to encapsulate internal implementations so that only your class needs to worry about the details. Think of all the internal gears and motors in a robot and how they’re normally hidden from view.
And you’ll learn how to use inheritance so you can stop writing the same code over and over again and move it to a common place where the code can be reused.
This is where C++ will start helping you. When you can make your code more natural to use, then it’s like putting on a well-fitting jacket. It becomes a joy. Without that natural feel, it’s like trying to put on a jacket backwards that’s two sizes too small. It’s just awkward. And that leads to more bugs, more headaches, and longer development time.
With this power comes some gotchas that you’ll want to avoid.
For example, while there’s no real difference between classes and structs, there are normal and widely accepted places where each should be used. If you get this wrong, your code will likely still run just fine. But another developer will wonder which planet you came from.
This class is divided into three parts.
In the first part, you’ll learn how to declare a class or struct and where each piece goes. This is where you’ll really start using multiple source code files and creating header files to hold the class declaration and cpp files to hold the implementation.
You’ll learn about constructors which help make sure your classes start out in a valid state so they can remain valid. Imagine buying a new car that has no seats installed because the dealer wasn’t sure if you like to sit close or far away from the steering wheel. Wouldn’t it be better if the seats were installed with some reasonable position? At least the car would be in a valid state right from the beginning even if you do need to make adjustments.
You’ll learn about destructors. Especially when do you need a destructor and what are some rules you need to follow to avoid your code from coming to an abrupt crashing end.
Once you get this far, you’ll be able to create instances of your own classes and make sure they get properly cleaned up when you’re done with them.
Then part two will continue by showing you how to make your classes safer and more organized.
Getters and setters let you set the rules for how users can read and change data used by your classes. This lets you change things later if you need to without affecting other code.
And assignment lets you setup and copy your class values from one place to another.
You’ll understand how to reuse classes with inheritance so that you can write specialized classes that make use of more general behavior in base classes. This lets you write some code that applies to large or common ideas and then refine or add additional behavior that’s closer to what you want. Taking the car for example, maybe you have some behavior that applies to all vehicles including both cars and trucks. Then you can make your code fit the exact needs of a car while still using the code that applies to both cars and trucks.
Along with inheritance, you’ll learn how to protect and limit access to your methods and data with public, private, and protected access levels. This can help eliminate bugs in your code because you can be sure to write methods that work with your data and be confident that they’re the only way the data can change. It’s a lot harder to find a thief when everybody is a suspect. Limiting access is a great way to limit your bugs.
Then part three begins explaining how to make your classes even better.
This is where you’ll learn some of the finer points such as how to define interfaces that will specify a contract so other developers will know what to expect from your classes.
You’ll learn about static methods and member variables.
Along with the different kinds of methods, you’ll learn about virtual methods and abstract classes. This also rounds out the interfaces.
Once you have a class interface to work with, you might want to change things up a bit with polymorphism. This lets you substitute different implementations that all share a common interface and the compiler does all the difficult work of figuring out what all the different objects do.
Part three finishes by showing you how to copy class instances and declare your own operators. This lets your classes behave like other types and lets you and other developers work with your classes without being surprised by unexpected behavior.