30 C++ Basic Exercises for Advanced with Solutions
Master advanced C++ skills with our comprehensive list of top 30 exercises. Dive into coding challenges that improve your understanding and proficiency in C++, setting a solid foundation for professional-level challenges. Start your journey to C++ mastery today!
Learning Objectives:
By the end of these exercises, you will master advanced C++ concepts, including templates, the Standard Template Library (STL), file handling, and performance optimization.
Exercise Instructions:
- Start with the first exercise and attempt to solve it before checking the hint or solution.
- Ensure you understand the logic behind each solution, as this will help you in more complex problems.
- Use these exercises to reinforce your learning and identify areas that may require further study.
1. Create a class representing a point in 2D space with methods to calculate the distance from another point.
Required Input:
Point p1(3, 4); Point p2(6, 8);
Expected Output:
Distance between points: 5
Code In Cpp
Run Code?
Click Run Button to view compiled output
2. Implement a class to manage a simple integer array with methods to add an element and get the maximum value.
Required Input:
ArrayManager arr; arr.add(5); arr.add(10); arr.add(3);
Expected Output:
Maximum value: 10
Code In Cpp
Run Code?
Click Run Button to view compiled output
3. Create a program to demonstrate operator overloading for a Fraction class.
Required Input:
Fraction f1(1, 2); Fraction f2(3, 4);
Expected Output:
Sum: 10/8
Code In Cpp
Run Code?
Click Run Button to view compiled output
4. Write a program to implement a basic singly linked list with methods to add and display elements.
Required Input:
LinkedList list; list.add(10); list.add(20); list.add(30);
Expected Output:
List elements: 30 20 10
Code In Cpp
Run Code?
Click Run Button to view compiled output
5. Create a class to manage a simple stack using an array with methods for push and pop.
Required Input:
Stack s; s.push(5); s.push(10);
Expected Output:
Popped element: 10
Code In Cpp
Run Code?
Click Run Button to view compiled output
6. Implement a program to find the maximum element in a binary tree.
Required Input:
A binary tree with values: 10, 5, 15, 3, 7, 13, 18.
Expected Output:
Maximum value: 18
Code In Cpp
Run Code?
Click Run Button to view compiled output
7. Create a class that manages a simple dynamic array with methods to add and remove elements.
Required Input:
DynamicArray arr; arr.add(1); arr.add(2); arr.remove(1);
Expected Output:
Current elements: 2
Code In Cpp
Run Code?
Click Run Button to view compiled output
8. Implement a class to represent a bank account with methods to deposit, withdraw, and check balance.
Required Input:
BankAccount account(100); account.deposit(50); account.withdraw(30);
Expected Output:
Current balance: 120
Code In Cpp
Run Code?
Click Run Button to view compiled output
9. Create a class to represent a rectangle with methods to calculate area and perimeter.
Required Input:
Rectangle rect(5, 10);
Expected Output:
Area: 50, Perimeter: 30
Code In Cpp
Run Code?
Click Run Button to view compiled output
10. Implement a program to demonstrate inheritance with a Shape base class and a Circle derived class.
Required Input:
Circle circle(5);
Expected Output:
Area of circle: 78.5398
Code In Cpp
Run Code?
Click Run Button to view compiled output