C++ tutorials

Started by
93 comments, last by taby 1 year, 7 months ago

I am trying to teach C++ to someone with a bit of C and Javascript experience.

I came up with 22 sample codes. Are there any of your favourite topics that are missing?

The codes are at: https://github.com/sjhalayka/sample_cpp_code

The topics covered are:

  1. Plain old data types
  2. Functions with references
  3. Function overloading and templates
  4. Conditionals
  5. Arrays and vector
  6. Loops
  7. Basic classes
  8. Containers and iterators
  9. Streams
  10. Binary files
  11. Constructor, destructor
  12. Exceptions
  13. argc, argv
  14. Namespaces
  15. High-resolution timing
  16. Mersenne Twister PRNG
  17. File systems
  18. Regular expressions
  19. Function pointers
  20. Multithreading
  21. Overloading operators
  22. Custom allocator
Advertisement

So the big topic I find missing is lambdas, specially since you mention function pointers.

You are also really light on certain topics and only show how the basics work and leave out the pitfalls.

Some of the code you have is just out right wrong, specially the performance test in your function pointer example, you are not even comparing code that is doing equivalent things there, which makes the comparison invalid.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

Even i'm personally no fan, i miss OOP topics like polymorphism, encapsulation, abstraction… all those wishful thoughts : )

Though, maybe that's beyond the scope of language introduction.

Agree on lambdas, which have the most impact on me, compared to the old C days.

And you really need a whole chapter on the auto keyword, hihihi ;P

NightCreature83 said:

So the big topic I find missing is lambdas, specially since you mention function pointers.

Yep.

You are also really light on certain topics and only show how the basics work and leave out the pitfalls.

I am also referring them to The C++ Programming Language 4th edition, STL Tutorial and Reference Guide, and cppreference.com

Some of the code you have is just out right wrong, specially the performance test in your function pointer example, you are not even comparing code that is doing equivalent things there, which makes the comparison invalid.

What if you have a half-dozen integration methods, and you want to switch between them at runtime? Obviously, the function pointer method works best, instead of an if-else or switch. I have other examples, but I doubt that you're interested.

JoeJ said:

Even i'm personally no fan, i miss OOP topics like polymorphism, encapsulation, abstraction… all those wishful thoughts : )

Though, maybe that's beyond the scope of language introduction.

Agree on lambdas, which have the most impact on me, compared to the old C days.

And you really need a whole chapter on the auto keyword, hihihi ;P

Thanks man! LOL auto.

OK I will add in a code on some more OOP features, and lambda expressions.

taby said:

NightCreature83 said:

So the big topic I find missing is lambdas, specially since you mention function pointers.

Yep.

You are also really light on certain topics and only show how the basics work and leave out the pitfalls.

I am also referring them to The C++ Programming Language 4th edition, STL Tutorial and Reference Guide, and cppreference.com

Some of the code you have is just out right wrong, specially the performance test in your function pointer example, you are not even comparing code that is doing equivalent things there, which makes the comparison invalid.

What if you have a half-dozen integration methods, and you want to switch between them at runtime? Obviously, the function pointer method works best, instead of an if-else or switch. I have other examples, but I doubt that you're interested.

So I missed something last night since it was late when I looked at this code, they do the same thing but this is not code you would write this way often. When you compile this code however there is no change between the FP version or the if-else version when you turn optiimisations on and you should do this when you do performance measurements. When I compiled the code in VS2022 on release the results are 0 nanoseconds for each version. Optimisations completely compile out that if else because you are always taking the same branch and thus will just be a function call.

Auto is a very very useful feature in C++ and you shouldn't be so quick to dismiss its use and in some cases you can't do the same thing without auto eg: capturing a lambda (this has no known type to the programmer, the compiler knows however).

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

So, basically, lambda expressions are little functions (anonymous functors) that can be placed in any scope? I read that they now support templates!

Yeah, I have had no luck getting lambda templates working lol

Here is the code that I'm trying:

// https://www.programiz.com/cpp-programming/functors
// https://www.cppstories.com/2020/05/lambdasadvantages.html/
// https://en.cppreference.com/w/cpp/language/lambda

#include <iostream>
using std::cout;
using std::endl;

template <typename T>
class A
{
public:

	void print(void)
	{
		cout << "A" << endl;
	}
};

int main(void)
{
	auto x = []<typename T>(const A<T> t)
	{
		t.print();
	};

	A<float> a;

	// error C2275: 'A<float>': 
	// expected an expression instead
	// of a type
	x<A<float>>(a);

	return 0;
}

Why are you trying this the hard way.

auto lambda = [](const auto& item) { item.Print(); };

Any item that supports a “Print()” function can be passed to this lambda, auto is a template more or less.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

This works, but there's one problem… I cannot figure out how to move the lambda expression into the main() function.

// https://www.programiz.com/cpp-programming/functors
// https://www.cppstories.com/2020/05/lambdasadvantages.html/
// https://en.cppreference.com/w/cpp/language/lambda

#include <iostream>
using std::cout;
using std::endl;

template <typename T>
class A
{
public:

	T x;
};

template <typename T>
class lambda
{
public:

	static void f(const T &t)
	{
		auto lambda_functor = [](const T &t)
		{
			cout << typeid(T).name() << endl;
		};

		lambda_functor(t);
	}
};

int main(void)
{
	A<int> a;

	lambda<A<int>> l;
	l.f(a);

	return 0;
}

This topic is closed to new replies.

Advertisement