C++ Refresher - Part Two

You can visit the previous part of the article here:

In this session, you will learn about:

  • Creating objects
  • Initializing and De-initializing objects
  • Creating constructors with parameters
  • Life cycle of an object
  • Conditional and decisional loops

Creating Object

Creating Object

When an object is created, the required amount of memory is allocated to it. This memory can be freed, when the object is no longer required.

The following table shows how a class is instantiated in C# and C++.

C#

  1. class Math  
  2. {... < member variables > void add()  
  3.     {... < function body >  
  4.     }  
  5.     public static void Main(string[] args)  
  6.     {  
  7.         Math obj = new Math();  
  8.         obj.add();  
  9.     }  
  10. //end of class  
C++
  1. class Math  
  2. {... < member variables > publicvoid add()  
  3.     {... < function body >  
  4.     }  
  5. }; //end of class  
  6. void main()  
  7. {  
  8.     Math obj;  
  9.     Math.add();  
  10. }  
access specifier

The following table shows access specifier in C# and C++.

C#

 

  • Public
  • Private
  • Protected
  • Internal
  • Protected Internal

C++

  • Public
  • Private
  • Protected

Initializing and Deinitializing Objects

While creating an object, you may want to initialize its member variables.

How did you achieve this task in C#?

Initializing and Deinitializing Objects

The following table shows the declaration of a constructor in C# and C++.

C#

  1. class Math  
  2. {  
  3.     int number1, number2;  
  4.     public Math()  
  5.     {  
  6.         number1 = 10;  
  7.         number2 = 3;  
  8.     }  
  9. }  
C++
  1. class Math  
  2. {  
  3.     int number1, number2;  
  4.     public: Math()  
  5.     {  
  6.         number1 = 10;  
  7.         number2 = 3;  
  8.     }  
  9. };  
Note that the name of the class and constructor are always the same.

In the preceding example, the member variables are initialized during program creation.

However, there exist situations when you need to initialize variables with the user supplied values. For this, you need to pass the user input to the constructor using parameters.

Constructor with Parameters

The following table shows the use of constructors with parameters in C# and C++.

C#
  1. class Calculator  
  2. {  
  3.     static int number1, number2;  
  4.     Calculator(int x, int y)  
  5.     {  
  6.         number1 = x;  
  7.         number2 = y;  
  8.     }  
  9.     public static void Main(string[] args)  
  10.     {  
  11.         int var1, var2;  
  12.         //write statements to accept the   
  13.         //value of var1 and var2  
  14.         Calculator C1 = new  
  15.         Calculator(var1, var2);  
  16.     }  
  17. }  
C++
  1. class Calculator  
  2. {  
  3.     privateint number1, number2,  
  4.     public: Calculator(int x, int y)  
  5.     {  
  6.         number1 = x;  
  7.         number2 = y;  
  8.     }  
  9. };  
  10. void main()  
  11. {  
  12.     int var1, var2;  
  13.     //write statements to accept the   
  14.     //value of var1 and var2  
  15.     Calculator c1(var1, var2);  
  16. }  
Destructors

When an object goes out of scope, the memory allocated to the object needs to be freed. And this is done through destructors.

The following table shows the declaration of a destructor in C# and C++.

C#
  1. class Calculator  
  2. {  
  3.     Calculator()  
  4.     {  
  5.         Console.WriteLine(“Constructor Invoked”);  
  6.     }~Calculator() //Destructor  
  7.     {  
  8.         Console.WriteLine(“Destructor Invoked”);  
  9.     }  
  10. }  
C++
  1. class Calculator  
  2. {  
  3.     public: Calculator()  
  4.     {  
  5.         cout << “Constructor Invoked”;  
  6.     }~Calculator() //Destructor  
  7.     {  
  8.         cout << “Destructor Invoked”;  
  9.     }  
  10. };  
Note that the destructor has the same name as its class but prefixed with a ~ (tilde) symbol.

Conditional and Looping Constructs

Conditional and Looping

Conditional constructs used in C# and C++

 

Conditional constructs

Looping constructs used in C# and C++

Looping constructs