0% found this document useful (0 votes)
38 views64 pages

OOP (CO1201) Unit-2 - Introduction To C++

Uploaded by

patelsuhani718
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views64 pages

OOP (CO1201) Unit-2 - Introduction To C++

Uploaded by

patelsuhani718
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 64

UNIT-2: Introduction to C++

Constants:

When you do not want others (or yourself) to change existing


variable values, use the const keyword (this will declare the variable
as "constant", which means unchangeable and read-only):

#include <iostream>
using namespace std;

int main()
{
const int MYNUM = 15;
MYNUM = 10;
cout << MYNUM;
Output:
return 0;
} In function 'int main()':
6.9: error: assignment of
read-only variable 'myNum'
Example: Enumeration Type

#include <iostream>
using namespace std;
enum week
{
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
};
int main()
{
week today;
today = Wednesday;
cout << "Day " << today+1;
return 0; Output
Day 4
}
Implicit Type Conversion :
Program to convert int to float type using implicit type conversion

#include <iostream>
using namespace std;
int main ()
{
// assign the integer value
int num1 = 25;
// declare a float variable
float num2;
// convert int value into float variable using implicit conversion
num2 = num1;
cout << " The value of num1 is: " << num1 << endl;
cout << " The value of num2 is: " << num2 << endl;
return 0;
}
Output:

The value of num1 is: 25


The value of num2 is: 25
Explicit Type Conversion:

Program to convert one data type into another using the assignment operator

#include <iostream>
using namespace std;
int main ()
{
// declare a float variable
float num2;
// initialize an int variable
int num1 = 25;

// convert data type from int to float


num2 = (float) num1;
cout << " The value of int num1 is: " << num1 << endl;
cout << " The value of float num2 is: " << num2 << endl;
return 0;
}

Output:
The value of int num1 is: 25
The value of float num2 is: 25.0

You might also like