C++ Refresher - Part One

This article is about the ways in which C# and C++ are similar and where these languages differ. You are familiar with the concepts of OOP language. You may have already studied one such language, called C#. If not, then there is no problem.

In this session, you will learn about:

  • Variables and data types
  • Storing data in a variable and displaying data on the screen
  • Creating and executing a C++ program
  • Classes in C++
  • Member functions
Before we start the session, let us revisit the features of Object-Oriented Programming (OOP) language.


In order to perform a specific task on a computer, you need to write a set of instructions known as a program. While creating a program, you may need to temporarily store some values by using variables.

Variables and Data Types

The syntax to declare a variable is:
  • <data type><variable name>;
  • int num1;
  • char chr;
Data types can be categorized into the following types:


The following table lists the common built-in data types used in C++ and C#.

 Data Type Number of Bytes in C++ Number of Bytes in C#
 char 1    2
 int 2 or 4 4
 float 4 4
 double 8 8

data type

The data type equivalent to string in C++ is a character array.

The following table shows the use of a string variable and a character array.

 C# C++
 string str = “India”; char str[] = “India”;


Let us see how these objects are used in C++

In C++, we use predefined objects to store and display data.


Storing Data in a Variable and Displaying Data on the screen
  1. #include<iostream.h>  
  2. int num1, num2, sum;  
  3. cout<<“Enter number 1”<<endl;  
  4. cin>>num1;  
  5. cout<<“Enter number 2”<<endl;  
  6. cin>>num2;  
  7. sum = num1 + num2;  
  8. cout<<“The sum is:”<<sum;  

Once you have created a C++ program, you need to compile and execute it. Let us now compile and execute a C++ program,
  • Similar to C#, a C++ program must contain the main() function. 
  • C++ is not a pure object-oriented language like C#. 
  • Therefore, you can create a program in C++ without using a class as in the following code:
    1. #include < iostream.h >  
    2.     void main()  
    3.     {  
    4.         int num1, num2, sum;  
    5.         cout << “Enter number1” << endl;  
    6.         cin >> num1;  
    7.         cout << “Enter number2” << endl;  
    8.         cin >> num2;  
    9.         sum = num1 + num2;  
    10.         cout << “The sum is: ” << sum;  
    11.     }  
Classes in C++

Object-oriented programming languages, such as C# and C++, use classes to group objects having common attributes.
  • A class is declared by using the class keyword.
  • The syntax of a class in C++ is:

In C#, a program cannot be created without a class. However, in C++ you can create a program with/without a class.

A class consists of:
  • Attributes: Represented using member variables
  • Behaviors: Represented using member functions

Member Functions


A function consists of the following parts:
  • Declaration: Syntax for declaring a function:
<return type><function name><function parameters>;
  • Definition: Syntax for defining a function:
<return type><function name><function parameters>
... }


The following table shows the use of member functions in C# and C++.

 C# (function declared and defined inside the class body)C++ (function declared and defined inside the class body) 
C++ (function declared inside and defined outside the class body)
  1. class Sum  
  2. {  
  3.     public void Add()  
  4.   {  
  5.         //Function Body  
  6.     }  
  1. class Sum   
  2. {  
  3.     void Add()  
  4.   {  
  5.         //Function Body  
  6.     }  
  7. }; 
  1. class Sum  
  2. {  
  3.     void Add(); //Declaration  
  4. };  
  5. void Sum::Add() //Definition  
  6.     {  
  7.         //Function Body  
  8.     }  

Now, let us see how the same function can be modified to accept the numbers as arguments. Consider the following code snippet in which the Add() function accepts two numbers from a user and adds them:
  1. class Sum  
  2. {  
  3.     void Add(); //Declare the member function  
  4. };  
  5. void Sum::Add() //Defining the member function  
  6.     {  
  7.         int num1, num2, sum;  
  8.         cout << “Enter number 1” << endl;  
  9.         cin >> num1;  
  10.         cout << “Enter number 2” << endl;  
  11.         cin >> num2;  
  12.         sum = num1 + num2;  
  13.         cout << “The sum is: ” << sum;  
  14.     }  
Types of Function Arguments

A function can have the following types of arguments:
  • Default arguments
  • Constant arguments
The following table shows the use of default and constant arguments.

 Default arguments  Constant arguments
 void Add(int iNum1=10, int iNum2=20)
//Function Body
 void Add(constint iVar1, constint iVar2)
//Function Body