
C++ should be fascinating - all too often, it comes across as intimidating instead. But there is no reason for this to be the case! By the time you are done with this class, you too will come to like and appreciate C++ for its elegance and power.
C++ is an ocean and C is a very small river which feeds into that ocean. C is the foundation on which C++ is built, let's see the differences between C and C++ in a very simple program. This is a taste of things to come.
A class is a user-defined type - a template for creating variables.
Variables with the same name can be defined in different scopes. Accessing them requires the use of the scope resolution operator. This becomes super useful once we get to classes!
C++ allows multiple functions with the same name but different signatures. Things get even more hardcore. You can overload common operators such as +. -. *, /. [] etc to work with your own objects. Here is a sneak peak into these constructs.
C++ allows you to specify meaningful default values for function arguments. But once you bring overloaded functions into the mix things can get pretty complicated. Some do's and don'ts of working with default values passed to functions.
References are pointers in disguise and so easy to use! Const prevents accidental modifications of variables and the bool keyword makes the boolean a first class citizen along with ints, floats etc.
A sneak peak into these before we cover them in more detail later.
A C programmer might call a class a "struct with functions". This is a brief introduction to the basic ideas on which Object Oriented Programming is built.
A class is a logical component. It holds data and functions of a specific entity.
Set up your very first C++ class! An object is a variable of a class. Repeat that, remember that.
Invoke just means call a member function!
Constructors help setup and initialize objects of classes and destructors help clean up when these objects go out of scope. Destructors are especially useful when your class references resources which need clean up, like file handles or pointers.
Member variables and functions can be marked public, private or protected. Understand what those terms mean.
There are excellent reasons to separate declarations (.h files) and definitions (.cpp files). Let's understand why this usually makes sense in real-world projects, and how to do this right.
Let's see a real live example of how multiple files would be set up and used in production code.
Let's go back and brush up on what dynamic memory allocation is.
Dynamic memory allocation is quite different in C++ than in C (or in Java, for that matter). Here are a few rules which should help keep you on the right path while using the new and delete keywords.
Never use malloc or free in C++. Ever.
Use new to allocate and construct objects and delete to deallocate memory and destruct objects. Use the array variants where needed. The "new" keyword pairs with "delete" and "new[]" pairs with "delete[]".
new does more than simply allocate memory - it calls the constructor. delete does more than deallocate memory - it calls the destructor. new[] and delete[] operate on arrays - they painstakingly go through an array and do their thing, element-by-element.
So not mix-n-match the two!
Remember to clean up the variables in your class in the destructor.
Using malloc will allocate memory but not call the constructor. Using free will deallocate memory but not call the destructor. Not good.
See how nicely new and delete play with objects!
Construct and destruct arrays using new[] and delete[]. new/delete expects a single element, new[]/delete[] expects an array. The two forms won't mix.
An arcane C++ operator - use this when you need to specify upfront where your object should reside.
Every object knows itself. How? Because the C++ compiler injects a special pointer called the this pointer.
The C++ string class is so much cooler than the C char* type.
Simple string operations
Inputting a string with line breaks or spaces requires a special function called getline. Understand how to use this.
The set of operations we need to perform on strings is not that complicated in C++
Compare strings as if they were numbers.
Its really easy. Enough said.
References are pointers, but they pretend that they aren't. Working with references correctly require you to follow a whole bunch of rules - here they are in all their glory!
Create a simple reference variable and learn how to use them.
const references have some little nuances of their own.
Two ways to swap integers, the heavyweight C style vs the nifty C++ style.
References must be initialised - there is no way around that.
You might think you're reassigning a reference, but you might be doing something different.
If you have multiple references and variables pointing to the same data, one change will change them all.
Technically, a reference can never hold a null value. But if you try really hard, you can slide one by.
References to pointers: Just remember to free and delete the underlying pointers as usual.
References to references: The C++ standard prohibits references to references, or arrays of references.
References as return types: Its hard trying to return a reference from a function. If its a heap variable, who will deallocate it?
References to stack variables: Its hard trying to return a reference from a function. If its a stack variable, it will cease to exist before you can use it!
The const keyword is really powerful, and really subtle.
Define a const variable, see how it works.
The C++ compiler can guarantee bitwise constness, not semantic constness, understand what that means.
This is a popular interview question, so make sure you understand this one!
Const member functions: A member function can be marked const. That's a way of saying that it won't modify the object. It can't call any non-const functions, or modify any member variables of course.
Mutable variables: If you mark a member variable as mutable, a const method can change its value.
Overloading functions on const: This is something you will use surprisingly often.
const_cast: There's a backdoor way to get rid of constness.
Try and pass function parameters as const references, not by value. There are performance reasons for it, that we discuss here. There is also another reason - to avoid object slicing - that we will talk about later.
Try and pass function parameters as const references, not by value. There are performance reasons for it, that we discuss here. There is also another reason - to avoid object slicing - that we will talk about later.
Let's now see an example of this!
In C, static is a storage class. In C++, it is something different.
Are shared by all objects of a class (unlike member variables, which are 1-per-object). The process for defining static member variables is kinda nasty. Make sure you understand it.
These are member functions which are called on a class not an object. They can only access static member variables!
These have a twist - they need an in-class definition, and then a dummy definition outside. Oh well.
Jokes abound - but the idea has its uses, as we will see when we get to operator overloading.
Friends can be either functions or classes - here we learn about friend functions
When you declare a class to be your friend, all of its member variables become your friends in the bargain.
Like a gruff uncle, C++ seems intimidating, when its just being helpful. These 75 examples will help you understand that.
Let's parse that.
What's Included: