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>;
Examples:
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 |
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
- #include<iostream.h>
- int num1, num2, sum;
- cout<<“Enter number 1”<<endl;
- cin>>num1;
- cout<<“Enter number 2”<<endl;
- cin>>num2;
- sum = num1 + num2;
- 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:
- #include < iostream.h >
- void main()
- {
- int num1, num2, sum;
- cout << “Enter number1” << endl;
- cin >> num1;
- cout << “Enter number2” << endl;
- cin >> num2;
- sum = num1 + num2;
- cout << “The sum is: ” << sum;
- }
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:
class<class_name>
{
...
};
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) |
- class Sum
- {
- public void Add()
- {
-
- }
- }
| - class Sum
- {
- void Add()
- {
-
- }
- };
| - class Sum
- {
- void Add();
- };
- void Sum::Add()
- {
-
- }
|
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:
- class Sum
- {
- void Add();
- };
- void Sum::Add()
- {
- int num1, num2, sum;
- cout << “Enter number 1” << endl;
- cin >> num1;
- cout << “Enter number 2” << endl;
- cin >> num2;
- sum = num1 + num2;
- cout << “The sum is: ” << sum;
- }
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 } |