Simple C++ Programs for Beginners with Source Code

Simple C++ Programs for Beginners with Source Code

Are you new to C++ programming and want to try your programming skills with some simple problems?

Then this tutorial is for you. In this tutorial, you will learn the basic, yet essential, programs for understanding C++ fundamentals. We will explore a variety of programs that will help you get a solid foundation in C++.

Whether you are a beginner just starting to learn this popular programming language or an experienced developer looking to refresh your knowledge, this blog has something for you. So, let’s get started!

Related: See our guide on C Programs for Practice: Learn Simple C Programs for Beginners for more details.

1. Demonstrate the usage of the Constructor and Destructor

  1. Define a class data with data member acct_no, balance containing constructor data() to initialize data member, and a member function display() for output.

Here is how the output of the program will appear when you run it:

OBJECT 1
Account number: 101
Balance: 100.58

OBJECT 2
Account number: 39
Balance: 200.21

2. Program to demonstrate usage of a Constructor and Destructor function

Declare a class with a public data member count. The class contains one constructor and destructor to maintain updated information about active objects i.e.

  1. A number of objects were created.
  2. A number of objects were destroyed.

Here is how the output of the program will appear when you run it:

1 constructor.
2 constructor.
3 constructor.
4 constructor.
5 constructor.
5 destructor.
4 destructor.
3 destructor.
2 destructor.
1 destructor.

3. Program to accept the distance between city 1st & 2nd, city 2nd & 3rd. Calculate the distance between city 1st & 3rd. Define a class road with private data member km, m, d1, d2, d3 containing member function getdata to accept values of d1, d2 and calculate for calculating distance

Here is how the output of the program will appear when you run it:

Enter distance between cities 1st and 2nd: 200
Enter distance between cities 2nd and 3rd: 300
Calculated distance from city 1st and 3rd in kilometer: 500
Calculated distance from city 1st and 3rd in meter: 500000

4. Demonstrate the use of operators overloading (string manipulation: + for concatenation and relational operators for alphabetical comparison)

Here is how the output of the program will appear when you run it:

Enter first string: c++
Enter second string: programming
String after concatenation: c++programming

Enter first string: c++
Enter second string: java
Both strings are NOT equal!

5. In a bank N depositor deposit the amount, write a program to find total amount deposited in the bank. Declare a class deposit with private data member Rupee and Paisa containing member function getdata, putdata

  1. Use an array of objects
  2. Use Operator ‘+’ overloading

Here is how the output of the program will appear when you run it:

Enter number of depositors: 2
Enter Rupee and Paisa: 101 20
Enter Rupee and Paisa: 102 90

All depositors balance:
Rupees: 101, Paisa: 20
Rupees: 102, Paisa: 90

Total amount deposited in bank (Rupee and Paisa):
Rupees: 204, Paisa: 10

Related: See our guide on Simple C Programs Based on Discrete Mathematics for more details.

6. Declare class event and accept time of first event and second event and find the difference between 1st and 2nd event. Containing public member function getdata and display with private data member hour, minute, second and total

  1. Use Operator ‘-‘ overloading.

Here is how the output of the program will appear when you run it:

Enter time of first event:
Enter hours: 4
Enter minutes: 50
Enter seconds: 20

Enter time of second event:
Enter hours: 1
Enter minutes: 20
Enter seconds: 10

Time of first event:
Hours: 4
Minutes: 50
Seconds: 20

Time of second event:
Hours: 1
Minutes: 20
Seconds: 10

Difference in both event:
Hours: 3
Minutes: 30
Seconds: 10

7. Program to demonstrate Single Inheritance. Declare a class B and derive publically class D from B

  1. Class B contains private data member a, and public data member b with member functions get_ab(), get_a(), show_a().
  2. The derived class D contains data member c with member function mul() and display().

Here is how the output of the program will appear when you run it:

a: 5
a: 5
b: 10
c: 50
a: 5
b: 20
c: 100

8. Program to demonstrate Multiple Inheritances. Declare class M and N and derive publically class P from M and N

  1. Declare a class M with protected data member m and public member function get_m().
  2. Declare a class N with protected data member n containing member function get_n().
  3. Declare class P containing member function display().

Here is how the output of the program will appear when you run it:

m: 10
n: 20
n * n: 200

9. Program to demonstrate Multilevel Inheritance. Declare a class student and derive publically a class test and derive publically class result from class test

  1. The class student contains protected data member roll_number with public member functions get_number() and put_number().
  2. The class test contains protected data member sub1, sub2 with public member functions get_marks() and put_marks().
  3. The class result contains data member total and public member function display().

Here is how the output of the program will appear when you run it:

Roll number: 111
Marks in subject 1: 75
Marks in subject 2: 59
Total marks: 134

10. Program to demonstrate Hierarchical Inheritance. Declare a class Side and derive publically class Square from base class side and also derive publically class cube from base class side

  1. Class Side contains protected data member L with a member function set_values().
  2. Class Square contains member function sq().
  3. Class Cube contains member function cub().

Here is how the output of the program will appear when you run it:

Enter value of side: 10
The area of square: 100
Enter value of side: 20
The area of cube: 2400

11. Program to demonstrate usage of normal virtual function and pure virtual Function with abstract class

Here is how the output of the program will appear when you run it:

display() of class C
show() of class C
display() of class D
show() of class D

12. Program to determine whether the input is +ve or -ve through exception

Here is how the output of the program will appear when you run it:

Enter any number: 15
+ve exception!

13. Program to raise an exception if an attempt is made to perform divide-by-zero

Here is how the output of the program will appear when you run it:

Enter first number: 15
Enter second number: 0
Division by zero condition.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top