Sum of n number of odd natural number using constructor and destructor in C++

By Prof. Fazal Rehman Shamil

Let’s see the Sum of n number of an odd natural number using constructor and destructor in C++.

Constructor

The constructor T4Tutorials_Sum() is a member function of the class T4Tutorials_Sum. The constructor T4Tutorials_Sum() has the same name as the name of its class T4Tutorials_Sum.

  1. When a new object uof the class T4Tutorails_Series is executed, the constructor T4Tutorials_Sum()also executed automatically.
  2. The constructor T4Tutorials_Sum()has no data type. Even we can’t use void also.
  3. The constructor T4Tutorials_Sum() can have arguments.
  4. The constructor T4Tutorials_Sum() can be only public.
  5. There is no inheritance of the constructor T4Tutorials_Sum().

Destructor

The destructor ~T4Tutorials_Sum() is a member function of the class T4Tutorials_Sum. Destructor  ~T4Tutorials_Sum() has the same name as the name of its class T4Tutorials_Sum. The tild sign ∼ is used before the name of the destructor ~T4Tutorials_Sum().

  • When the object obj(5)of the class T4Tutorials_Sumdestroyed, then the destructor ~T4Tutorials_Sum() also destroyed automatically.
  • One class T4Tutorials_Sum can have only one destructor ~T4Tutorials_Sum(). However, one class can have many constructors.
  • Destructor ~T4Tutorials_Sum()overloading is impossible.
  • The Destructor ~T4Tutorials_Sum()can’t have any arguments(parameters).
  • The destructor ~T4Tutorials_Sum() has no data type.

Output

Please enter the value to print odd numbers.

5

1

3

5

7

9

T4Tutorials_Sum of given input = 25

The destructor destroys everything of the constructor.

Prof.Fazal Rehman Shamil (Available for Professional Discussions)
1. Message on Facebook page for discussions,
2. Video lectures on Youtube
3. Email is only for Advertisement/business enquiries.

Leave a Reply