9 6093461787225621084

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Name - Davy Bansal

Roll No. - UE203033


Course - B.E.(CSE) , SEC-1
Object Oriented Programming
ASSIGNMENT - 3
________________________________________________________
//programme to demonstrate variable declarations including flexible
//declaration and runtime initialisation.
#include<iostream>
using namespace std;
int main(){
int n;
cout<<"Enter the value of n"<<endl;
cin>>n;
int a[n];
for(int i = 0; i<n; i++){
cout<<"Enter the value of element number "<<i+1<<endl;
cin>>a[i];//In runtime initialisation the values of the array has been initialised by the user while running the pro
gramme.
//In complied time initialisation array is defined as : int a[5] = {5, 78, 7, 89, 9234};
}
int *ptr = &a[0];//In c++ we can define the datatype of the variable at any scope of time and it is called flexible de
claration but in c we have to define it in the starting.
for(int i = 0; i<n; i++){
cout<<"The value of element number "<<i+1<<" is : "<<*ptr<<endl;
ptr++;
}
return 0;
}
//output
// Enter the value of n
// 3
// Enter the value of element number 1
// 4
// Enter the value of element number 2
// 5
// Enter the value of element number 3
//
// 6
// The value of element number 1 is : 4
// The value of element number 2 is : 5
// The value of element number 3 is : 6

--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------
//programme to demostrate control structure
#include<iostream>
using namespace std;
int main(){
int a;
cout<<"Here the choices for demostration are :"<<endl<<"1 : while loop with break statement"<<endl
<<"2 : do while loop with continue statement"<<endl<<"3 : for loop with goto statement"<<endl
<<"Now enter your choice"<<endl;
cin>>a;
switch(a){
case 1:{
int num = 1;
while(num>0){
cout<<"Number : "<<num<<endl;
num++;
if(num == 7)//Here when num = 7 then the break statement encountered which terminates the loop.
break;
}
break;
}
case 2:{
int n = 1;
do{
if(n == 7)
//Here when n = 7 then the continue statement is encountered due to which the loop jump to next iteration.
continue;
cout<<"Number : "<<n<<endl;
n++;
}while(n>0);
break;
}
case 3:{
int m, prime = 1;
cout<<"Enter the number"<<endl;
cin>>m;
for(int i=2;i< m;i++){
if(m%i==0){
prime = 0;
break;
}
}
if(prime == 0 || m == 1 || m == 0)
goto print;//goto statement transfer control to another line of code when encountered as in case of print.
else
cout<<"The number is prime"<<endl;
print:
cout<<"The number is not prime"<<endl;
}
break;
}
return 0;
}
/*output
Here the choices for demostration are :
1 : while loop with break statement
2 : do while loop with continue statement
3 : for loop with goto statement
Now enter your choice
2
Number : 1
Number : 2
Number : 3
Number : 4
Number : 5
Number : 6
*/

--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------

//programme to demonstrate primitive and derived datatype


#include<iostream>
using namespace std;
bool even_odd(int n){//A boolean variable can store true or false in it. Here true = 1 and false = 0.
if(n%2 == 0)
return true;
else
return false;
}
void character(char c){
if(c == 'a' || c == 'e' || c == 'o' || c == 'u' || c == 'i')
cout<<"Vowel"<<endl;
else
cout<<"Consonant"<<endl;
}
int main(){
int x = 10;
int &y = x;//reference variable is defined here.
long int n, l;
cout<<"Enter the number of subjects"<<endl;
cin>>l;
long int a[l];
long int *ptr = a;//pointer is a variable which stores the adress of another variable.
float sum = 0, avg;
char c;
//Here it demostrate array and pointers.
for(int i = 0; i<l; i++){
cout<<"Enter the marks of student in "<<i+1<<" subject (out of 100)"<<endl;
cin>>a[i];
}for(int i = 0; i<l; i++){
cout<<"The marks of student in "<<i+1<<" subject is : "<<endl;
cout<<*ptr<<endl;
sum = sum + *ptr;
ptr++;
}
avg = sum/l;
cout<<"The average marks of the student in "<<l<<" subjects are : "<<avg<<endl;
cout<<"Enter the number which we have to check whether it is even or odd"<<endl;
cin>>n;
if(even_odd(n))//This condition is used to demostrate function and bollean datatype.
cout<<"Even number"<<endl;
else
cout<<"Odd number"<<endl;
cout<<"Enter the character"<<endl;
cin>>c;
character(c);
//The demostration of refernce variable is done below
cout<<"The initial value of x is : "<<x<<endl;
y = 47;//Here y is reference variable of x. y has same adress as that of x. It is just like another name of x
//any change in the value of y will bring change in the value of x.
cout<<"The value of x after assigning value to y is : "<<x<<endl;
return 0;
}
/*output
Enter the number of subjects
3
Enter the marks of student in 1 subject (out of 100)
2
Enter the marks of student in 2 subject (out of 100)
1
Enter the marks of student in 3 subject (out of 100)
3
The marks of student in 1 subject is :
2
The marks of student in 2 subject is :
1
The marks of student in 3 subject is :
3
The average marks of the student in 3 subjects are : 2
Enter the number which we have to check whether it is even or odd
56
Even number
Enter the character
a
Vowel
The initial value of x is : 10
The value of x after assigning value to y is : 47*/

--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------

//programme to demontrate implicit and explicit


#include<bits/stdc++.h>
using namespace std;
int main()
{
int x = 10; // integer x
char y = 'a'; // character c

// y implicitly converted to int. ASCII


// value of 'a' is 97
x = x + y;

// x is implicitly converted to float


float z = x + 1.0;

cout << "x = " << x << endl;


cout<< "y = " << y << endl;
cout<< "z = " << z << endl;

//integer to char
int number=5;
char numberr=number+48;
cout<<"Integer>Character= "<<numberr<<endl;

//char to integer
char num='5';
int n=num-48;
cout<<"Character->Integer = "<<n<<endl;

double p = 1.2;

// Explicit conversion from double to int


int sum = (int)p + 1;
//float to int explicit conversion
float fl=2.65;
int flint=int(fl);
cout<<"float->int = "<<flint<<endl;
cout << "Sum = " << sum;

float f = 3.5;

// using cast operator


int b = static_cast<int>(f);

cout << b;
}
/*output
x = 107
y=a
z = 108
Integer>Character= 5
Character->Integer = 5
float->int = 2
Sum = 23*/

//programme to demontrate userdefined datatypes


#include<iostream>
using namespace std;
class complex{
int a, b;
public:
void set_number(int v1, int v2){
a = v1;
b = v2;
}void sum_complex(complex o1, complex o2){
a = o1.a + o2.a;
b = o1.b + o2.b;
}void print_number(){
cout<<"The complex number is : "<<a<<" + "<<b<<"i"<<endl;
}
};
struct student{
char roll_number[10];
char name[10];
char branch[10];
int section;
}d;
union demo{
int x;
char y;
float z;
};
enum shape{
circle,//In enum, the first element of the enum has been initialise to zero and there is increment of 1 for every next
element.
rectangle,
triangle
};
void input();
void display();
int main(){
typedef int K;//typedef keyword is used to assign altenative names to the existing datatypes.
enum shape type;
union demo m;
//Demostration of class is described below.
complex a, b, c;
K n1, n2, n3, n4;
cout<<"Enter the real and imaginary part of first complex number"<<endl;
cin>>n1>>n2;
a.set_number(n1, n2);
a.print_number();

cout<<"Enter the real and imaginary part of second complex number"<<endl;


b.set_number(n3, n4);
b.print_number();

c.sum_complex(a, b);//This function is used to compute the sum of the above two complex numbers.
c.print_number();
//Now the demostration of structure is there.
input();
display();
//Here union has been described.
m.x = 14;
m.y = 'e';
m.z = 78.54;
cout<<"x : "<<m.x<<endl<<"y : "<<m.y<<endl<<"z : "<<m.z<<endl;//Here only the value of z is stored. Rest x a
nd y values are garbage values.
//Demostartion of enum
type = rectangle;
cout<<"The shape number is : "<<type<<endl;

return 0;
}
void input(){
cout<<"Enter the roll number of the student"<<endl;
cin>>d.roll_number;
cout<<"Enter the name of the student"<<endl;
cin>>d.name;
cout<<"Enter the branch of the student"<<endl;
cin>>d.branch;
cout<<"Enter the section of the student"<<endl;
cin>>d.section;
}
void display(){
cout<<"Roll number : "<<d.roll_number<<endl<<"Name : "<<d.name<<endl<<"Branch : "<<d.branch<<endl<<"Se
ction : "<<d.section<<endl;
}
//output
/*2
3
The complex number is : 2 + 3i
Enter the real and imaginary part of second complex number
The complex number is : 4201888 + 6422400i
The complex number is : 4201890 + 6422403i
Enter the roll number of the student
ue203026
Enter the name of the student
Ashish
Enter the branch of the student
cse
Enter the section of the student
1
Roll number : ue203026
Name : Ashish
Branch : cse
Section : 1
x : 1117590651
y:{
z : 78.54
The shape number is : 1*/

--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------
//programme to demonstrate symbolic const , enumeration,reference var
#include <bits/stdc++.h>
using namespace std;
#define val 10
#define floatVal 4.5
#define charVal 'G'

enum week
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
};
int main()
{ // for symbolic const that above int main defined
cout << "Integer Constant: " << val << "\n";
cout << "Floating point Constant: " << floatVal << "\n";
cout << "Character Constant: " << charVal << "\n";

int a = 10;

/*reference variable is alias of other variable,


It does not take space in memory*/

int &b = a;

cout << endl


<< "Value of a: " << a;
cout << endl
<< "Value of b: " << b << endl;
//for enum which above the int main defined
week day;
day = Friday;
cout << "Day: " << day + 1 << endl;

return 0;
}

Integer Constant: 10
Floating point Constant: 4.5
Character Constant: G

Value of a: 10
Value of b: 10
Day: 5

--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------

//demonstrate expression
#include<iostream>
using namespace std;
int main(){
int a;
cout<<"The choices are :"<<endl<<"1 : constant expression"<<endl<<"2 : integer expression"<<endl
<<"3 : float expression"<<endl<<"4 : Logical and relational expression"<<endl<<"5 : Assignment and compound
expression"<<endl
<<"6 : Pointer expression"<<endl<<"7 : Bitwise expression"<<endl<<"Now enter your choice"<<endl;
cin>>a;
switch(a){
case 1:{
const int b = 45;
//Here if we assign b = 78 then compiler throws an error because b is read only value it cannot be changed.
cout<<"The value of b is : "<<b<<endl;
break;
}case 2:{
int x;
float y;
cout<<"Enter the two numbers"<<endl;
cin>>x>>y;
cout<<"The sum is :"<<x + (int)y<<endl;//here with the help of (int) the value comes out to be integer.
break;
}case 3:{
int z;
double l;
cout<<"Enter the two numbers"<<endl;
cin>>z>>l;
cout<<"The multiplication is : "<<(float)z * float(l)<<endl;//here with the help of (float) the value come out in f
loat type.
break;
}case 4:{
int x, y, z, a;
cout<<"Enter the two numbers"<<endl;
cin>>x>>y;
z = x<10 && y != 5 && y > 20;//here z = 1 if all the conditions gets true otherwise z = 0
a = x != 10 || y == 20;//here a = 1 if any of the conditions gets true and a = 0 if neither of the conditions get true.

cout<<"z : "<<z<<endl<<"a : "<<a<<endl;


break;
}case 5:{
float x, y;
//assignment expression
x = (y = 12.5);//here if we declare the datatype of these variables at same time we apply assignment expression
then compiler gives error.
//compound assignment expression
x += 10;// here it is same as x = x + 10.11
cout<<"The value of x :"<<x<<" and y :"<<y<<endl;
break;
}case 6:{
int x;
cout<<"Enter the number"<<endl;
cin>>x;
int *ptr = &x;
cout<<"The adress of x is "<<&x<<endl;
cout<<"The adress of x is "<<ptr<<endl;
cout<<"The value of x is "<<*ptr<<endl;
break;
}case 7:{
int x, y, z;
cout<<"Enter the two numbers"<<endl;
cin>>x>>y;
z = x&y;
cout<<"The value of x&y :"<<z<<endl;
z = x|y;
cout<<"The value of x|y :"<<z<<endl;
z = x<<2;
cout<<"The value of x<<2 :"<<z<<endl;
break;
}
}
return 0;
}
//output
// The choices are :
// 1 : constant expression
// 2 : integer expression
// 3 : float expression
// 4 : Logical and relational expression
// 5 : Assignment and compound expression
// 6 : Pointer expression
// 7 : Bitwise expression
// Now enter your choice
// 3
// Enter the two numbers
// 1
// 2
// The multiplication is : 2

You might also like