Practical work: Entry level C++ (Junior)

code

Practical work: C++ (Junior-Intermediate level)

Below you’ll find a list of problems I’ve made for juniors.
Feel free to work on them in order to verify your C++ level.
The following topics are raised:

  • Classes
  • Access modifiers
  • Virtuals
  • static
  • const
  • mutable
  • size of..
  • explicit constructor
  • diamond problem
  • default parameters
  • deep vs shallow copy
  • default functions
  • pointer vs reference
  • class instantiation
  • heap allocation and deallocation
  • function overload / override
  • mutex, lock_guard
  • globals, namespace

 

Create a class with members in all access modifiers.
Create two classes: one inherits privately, the other publicly.
Add a virtual function in both base and derived class, call the function from a pointer to base class.(Base* base = new Derived)

Add a static variable and a static function. Call them through the class and through an instance.
Add a const function, try to modify class members.
Add mutable to a member data, change the value of it from the const function.

Create an empty class -> display the size of it.
Add an int -> display the size of it.
Add a virtual -> display the size of it.
Add another virtual -> display the size of it.

Add a constructor with one parameter (an int). Create an object through implicit conversion. (class Base { … }; … Base obj = 10;)
Add explicit in front of constructor. Notice the difference.
Check web for diamond problem. Create an example of it.
Add a function that receives two default params. Call it with given param and with no params at all, display the parameters received in the function.
Try to add default param only for the first parameter. Notice what happens.

Add an int pointer in the class, allocate it on constructor.
Create a copy of the object through copy constructor, see what happens. (this is deep vs shallow copy).
Create all 5 functions that are required when manipulating memory (dtor , copy and move constructor/assign operator).

Create a function that receives a pointer, and another one that receives a reference.
Try to send a null object in the functions. Notice the differences.

Create a class that contains a reference to an int. (class Base { int& myInt; }; ).
Create an instance of this class.

Create a class, add a cout message in ctor/dtor.
Allocate an array of that type( Data* objects = new Data[10]);. Use delete objects and see what happens. use delete[] afterwards.

Create a base and a derived class. Create function overload / override.

Create a global mutex.
Create a function in which you lock and unlock it.
Create another function in which you use a lock_guard instead. Use an unnamed scope for that lock_guard.

Create a global int, give it a value.
Create a function and add an int with the same name, give it another value.
Display the int to the screen.
Display the global int to the screen (from the same function).

Leave a comment

Your email address will not be published.