-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Here, you will be able to view the documentation for this project, sample programs, features and the current status of this project.
- Features and Usage
- Sample Programs
- Documentation
The var
object contains functionality that can satisfy the demands of most programs. It also has its own limitations, some of which can be addressed and some which can not. You will be able to learn the various ways in which you can use the var
object and the ways you cannot, in this section.
Important notes or tips will be found as comments in the sample programs.
var
objects can be initialised in a program in the same way as that of most primitive data-types.
#include<iostream>
using namespace std;
int main(){
var integerNum=10;
var floatNum=10.5;
var longNum=12345678;
var doubleNum=2345.5678912;
var stringVar="DynaVar";
var charVar="s";
stringVar.destroy();
charVar.destroy();
return 0;
}
/*Characters, when being assigned to a var object, must always be in double quotes.*/
/*Always remember to use the destroy() function on var objects which currently contain or have contained variables
of type 'string' or 'char'*/
Just like normal data-types, var
objects can also have different values assigned to them over the course of a program. However, unlike primitive data-types, where only the same type of values can be assigned to a variable, here, we can assign different types of values.
#include<iostream>
using namespace std;
int main(){
var numString=12;
numString=23.45;
numString="arjun";
numString.destroy();
return 0;
}
In C++, we can read values from the user and store them in variables using the cin
statement. The same can be done with var
objects.
#include<iostream>
using namespace std;
int main(){
var numString;
cout<<"Enter a value --> ";
cin>>numString;
cout<<"The value entered is --> "<<numString;
numString.destroy();
return 0;
}
The ```var`` object supports operations of an arithmetic and logical nature performed on it. The various operators supported by DynaVar are listed below.
-
+ (Addition)
-
- (Subtraction)
-
* (Multiplication)
-
/ (Division Quotient)
-
% (Division Remainder)
-
++ (Unary Increment)
-
-- (Unary Decrement)
-
+= (Addition Shorthand)
-
-= (Subtraction Shorthand)
-
*= (Multiplication Shorthand)
-
/= (Division Quotient Shorthand)
-
%= (Division Remainder Shorthand)
-
= (Assignment Operator)
-
== (Equality operator)
-
!= (Not Equal)
-
> (Greater Than)
-
>= (Greater Than/Equal To)
-
< (Lesser Than)
-
<= (Lesser Than/Equal To)
-
+ (String concatenate)
Here are a few sample programs to demonstrate this.
#include<iostream>
using namespace std;
int main(){
var varNum=23;
varNum++; //It is now 24
varNum=23.5;
varNum--; //It is now 22.5
varNum="hello";
varNum+=" world!"; //It is now 'hello world!'
varNum*=10; //This is an ERROR! Cannot multiply these types.
varNum=23/10; //It is 2
varNum=23.0/10; //It is 2.3
varNum=23%2; //It is 1
varNum.destroy();
return 0;
}