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

#include <cmath> #include <iostream> class Point { private: int x, y; public: Point(int xCoord, int yCoord); double distance(Point p); }; int main() { // Logic to demonstrate distance calculation return 0; }

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

#include <iostream> #include <vector> class ArrayManager { private: std::vector<int> elements; public: void add(int value); int getMax(); }; int main() { // Logic to demonstrate ArrayManager functionality return 0; }

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

#include <iostream> class Fraction { private: int numerator, denominator; public: Fraction(int num, int denom); Fraction operator+(const Fraction &f); void display(); }; int main() { // Logic to demonstrate operator overloading return 0; }

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

#include <iostream> class Node { public: int data; Node *next; Node(int val); }; class LinkedList { private: Node *head; public: LinkedList(); void add(int value); void display(); }; int main() { // Logic to demonstrate LinkedList functionality return 0; }

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

#include <iostream> #define MAX 100 class Stack { private: int arr[MAX]; int top; public: Stack(); void push(int x); int pop(); }; int main() { // Logic to demonstrate Stack functionality return 0; }

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

#include <iostream> class TreeNode { public: int data; TreeNode *left; TreeNode *right; TreeNode(int val); }; class BinaryTree { private: TreeNode *root; public: BinaryTree(); void insert(int value); int getMax(); }; int main() { // Logic to demonstrate BinaryTree functionality return 0; }

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

#include <iostream> class DynamicArray { private: int *arr; int capacity; int size; public: DynamicArray(); void add(int value); void remove(int index); void display(); }; int main() { // Logic to demonstrate DynamicArray functionality return 0; }

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

#include <iostream> class BankAccount { private: double balance; public: BankAccount(double initialBalance); void deposit(double amount); void withdraw(double amount); double getBalance(); }; int main() { // Logic to demonstrate BankAccount functionality return 0; }

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

#include <iostream> class Rectangle { private: double width, height; public: Rectangle(double w, double h); double area(); double perimeter(); }; int main() { // Logic to demonstrate Rectangle functionality return 0; }

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

#include <cmath> #include <iostream> class Shape { public: virtual double area() = 0; // Pure virtual function }; class Circle : public Shape { private: double radius; public: Circle(double r); double area(); }; int main() { // Logic to demonstrate inheritance return 0; }

Run Code?

Click Run Button to view compiled output

ad vertical

1 of 3