Learn By Example: C++ Programming - 75 Solved Problems

C++ will never seem intimidating again, once you're done with these examples.
4.3 (101 ratings)
Instead of using a simple lifetime average, Udemy calculates a
course's star rating by considering a number of different factors
such as the number of ratings, the age of ratings, and the
likelihood of fraudulent ratings.
967 students enrolled
70% off
Take This Course
  • Lectures 87
  • Length 16 hours
  • Skill Level All Levels
  • Languages English
  • Includes Lifetime access
    30 day money back guarantee!
    Available on iOS and Android
    Certificate of Completion
Wishlisted Wishlist

How taking a course works


Find online courses made by experts from around the world.


Take your courses with you and learn anywhere, anytime.


Learn and practice real-world skills and achieve your goals.

About This Course

Published 2/2016 English

Course Description

Like a gruff uncle, C++ seems intimidating, when its just being helpful. These 75 examples will help you understand that.

Let's parse that.

  • C++ seems intimidating because all too often, what you see is not what you get.
  • Usually, that's because C++ is trying to help you, but you don't realise that. C++ is incredibly deep, and it sometimes struggles to get you - the programmer - to understand what it's saying
  • These 75 examples will help. Each is self-contained, has its source code attached, and gets across a specific C++ use-case. Each example is simple, but not simplistic.

What's Included:

  • Moving to C++ from C: If you are a C programmer, this section will run through what you need to know in order to move seamlessly to C++.
  • Objects, Classes and Object-Oriented Programming: Access modifiers, classes, objects, the this pointer, new/delete and dynamic memory allocation gotchas
  • Operator overloading is a particularly complicated topic - C++ is virtually alone in the ubiquity of overloaded operators. Make sure this doesn't trip you up. Also go deep into the workings of const, static and friend
  • Inheritance in C++ is considerably more complicated than in Java, mostly because of multiple inheritance, and because of the co-existence of both virtual and non-virtual methods.
  • Templates are a classic generic programming technique that were revolutionary when first added to C++. Understand template functions and classes, as well as template specializations.
  • STL - the Standard Template Library - is incredibly powerful. Get a good sense of collections, iterators and algorithms - the major components of the STL
  • C++ casts are quite different than C-casts. Understand const_cast, static_cast and dynamic_cast, as well as Real Time Type Identification (RTTI), and the manner in which explicit conversions can be performed using static_cast
  • Exceptions and exception handling in C++

Talk to us!

  • Mail us about anything - anything! - and we will always reply :-)

What are the requirements?

  • Students should have an understanding of programming - concepts such as variables, loops and if-else statements will not be explained

What am I going to get from this course?

  • Harness the full power of C++ without being intimidated by the language's complexities
  • Use inheritance, operator overloading, templates, STL and all major C++ language features

Who is the target audience?

  • Yep! Java and C# programmers - who understand Object Oriented Programming, but are new to C++
  • Yep! Folks with prior programming experience in C. No prior object oriented programming experience is needed

What you get with this course?

Not for you? No problem.
30 day money back guarantee.

Forever yours.
Lifetime access.

Learn on the go.
Desktop, iOS and Android.

Get rewarded.
Certificate of completion.


Section 1: Introducing C++

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.

Section 2: Moving from C to C++

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.

Section 3: Objects and Classes

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.

Section 4: Multi-file Programs

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.

Section 5: Dynamic Memory Allocation: new and delete

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.

Section 6: The C++ string Class

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.

Section 7: References

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!

Section 8: The const Keyword

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!

Section 9: The static Keyword

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.

Section 10: The friend Keyword

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.

Section 11: Operator Overloading

What is operator overloading? Understand internal operators, defined as member functions of a class vs external operators defined as global functions. External operators are commonly friend functions of the class on which they operate.


When would you choose to have an internal operator implementation vs an external operator implementation? Sometimes you don't have a choice!


All the nitty gritty details to get the += correct. With operator overloading the function signatures are the most complicated, pay close attention to those. Why does this operator return a reference?


All the nitty gritty details to get the + operator correct. With operator overloading the function signatures are the most complicated, pay close attention to those. Why does this operator return a const object?


The "++" operator can mean the pre-increment or the post-increment operator. How does C++ disambiguate?


This is the operator that's implemented the most often! It's also tricky and has many details which have to be exactly right.


A flashback to how streams and buffers work in C and C++.


The bitwise shift operators work with input and output streams when overloaded. Learn how to make your custom classes work with IO steams using operator overloading.

Section 12: Inheritance

Let's take a brief detour to remind us of how objects and classes work.


Inheritance models "is-a" relationships from the real world. Understand logically the layout of an inherited class.


When would we model entities using inheritance. A simple, logical example of an image editing tool .


At the heart of inheritance is the idea that a derived class object "is-a" base-class object


Access levels within a class determine what members directly derived classes and the external world can access. The inheritance type determines what indirectly derived classes ("grandchild" classes) can access and see in their base classes.


Implement a circle class which inherits from shape and see how the class is constructed, destructed and assigned. Pay special attention to how the assignment operator is overloaded!


Inheritance comes in 3 flavors in C++, public, private and protected. Let's understand these flavors.


A virtual function is "dynamically resolved" - its only at runtime that we know what function exactly will be called. This is a powerful, complicated, and important concept.


Objects of classes with even a single virtual method carry around extra baggage in the form of the vptr. This baggage is what makes runtime polymorphism work.


A pure virtual function is one that has no implementation at all in the base class. A class with any pure virtual functions is called an abstract base class, and it can't be instantiated.


Multiple inheritance is complicated - that's why Java, for instance, does not allow it at all. Let's see why things can get complicated when multiple inheritance is involved.


Virtual inheritance is a rather arcane concept - but its important to understand if you are using multiple inheritance.


When you pass a derived class object by value to a function expecting a base class object, something bad happens (its called slicing, and its as painful as it sounds)


In C++, never include calls to any virtual functions from a constructor or a destructor. Really - this is important.


If your class is - or ever will be - a base class in an inheritance hierarchy, it should have a virtual destructor.


Virtual functions are dynamically bound, while default parameters are statically bound. The two just don't mix.


Name hiding is what happens when the same function name appears in the base and derived classes, but with differing signatures. (Same name, different signature = name hiding). Don't do it.


Related to name hiding, but subtly different, is overriding a non-virtual base class method. This leads to weird behaviour of a different kind - don't do this either.

Section 13: Templates

Templates are an awesome way to reuse code in C++. They are the basis of the Standard Template Library, and are a form of compile-time polymorphism.


Just so we wrap our heads around the idea of a function template, and a template parameter.


What happens when we wish to override the default template instantiation for some specific type? Its easy!


Possibly the most interesting example in this course: a smart pointer class, built using template classes. Smart pointers are used very widely, so you should really make sure to understand this example.


Total and partial template specialisations are not at all as scary or complicated as they sound.

Section 14: STL - The Standard Template Library

The STL is a little universe of its own, powerful, and elegant. Its really worth your while learning about the STL - it has way too much cool stuff that folks don't use because they find it a bit..intimidating.


The STL vector class is the most commonly used container, and with good reason.


Iterators sound boring ("classes to do stuff, element-by-element with contents of a container") but that bland description belies their power and utility.


We already explored the vector, which is a sequential STL container - now, let's do some stuff with the map, an associative container.


Algorithms are the most powerful - and least widely used - part of the STL. Let's see how they work. Oh, and did you know that the C++ string is actually an STL container, with some clever typedefs so that you are n't scare off by it?

Section 15: C++ Casts

C++ casts are surprisingly versatile. Understand the 4 types of casts, and how they each differ from old-school C-style casts (which you should entirely stop using, btw)


The const_cast eliminates constness..


..dynamic_cast is the only safe way to downcast (i.e. to go from base class to derived class). It makes use of the RTTI, C++'s Runtime Type Identification System.


static_casts are quite smart too - its just that they perform their magic at compile-time, not runtime. Learn how they work with the explicit keyword and make conversions work.

Section 16: Exceptions

Students Who Viewed This Course Also Viewed

  • Loading
  • Loading
  • Loading

Instructor Biography

Loony Corn, A 4-person team;ex-Google; Stanford, IIM Ahmedabad, IIT

Loonycorn is us, Janani Ravi, Vitthal Srinivasan, Swetha Kolalapudi and Navdeep Singh. Between the four of us, we have studied at Stanford, IIM Ahmedabad, the IITs and have spent years (decades, actually) working in tech, in the Bay Area, New York, Singapore and Bangalore.

Janani: 7 years at Google (New York, Singapore); Studied at Stanford; also worked at Flipkart and Microsoft

Vitthal: Also Google (Singapore) and studied at Stanford; Flipkart, Credit Suisse and INSEAD too

Swetha: Early Flipkart employee, IIM Ahmedabad and IIT Madras alum

Navdeep: longtime Flipkart employee too, and IIT Guwahati alum

We think we might have hit upon a neat way of teaching complicated tech courses in a funny, practical, engaging way, which is why we are so excited to be here on Udemy!

We hope you will try our offerings, and think you'll like them :-)

Ready to start learning?
Take This Course