Object Oriented Programming in C++: Guru Nanak Dev Engg College
Object Oriented Programming in C++: Guru Nanak Dev Engg College
PRACTICAL FILE
Object Oriented
Programming in C++
c
Dev kumar
URN=2004902
CRN=2021024
D2-ITA1
Contents
1.1.1 Output
My name is Dev kumar.
1.2.1 Output :
Size of int is 4
Size of char is 1
Size of float is 4
Size of double is 8
3.1.1 Output:
Value of a = 6
Value of f = 0.43
Value of d = 22.432
Value of c = x
4 Different types of Operators in C++:
OPERATORS = Operators are symbols used in any language that
tells compiler to do different arithmetic, logical operations etc. on
operands , There are different types of operators used in C/C++.
4.1.1.1 Output :
Simple Interest = 174
4.2.1 Code:
#include <iostream>
using namespace std;
int main()
{
int num;
cout << "Enter your choice = ";
cin >> num;
if ((num >= 35) && (num <= 100))
cout << "You have passed the exam.";
else
cout << "Don't get demotived , Try your best next time ";
return 0;
}
4.2.1.1: Output
Enter your choice = 100 You have passed the exam.
Dev kumar Page 3
OOPs practical GNDEC
PS C:\Users\Dev
kumar\OneDrive\Desktop\Hackerrank_problem_solving> g++
a.cpp
PS C:\Users\Dev
kumar\OneDrive\Desktop\Hackerrank_problem_solving>
.\a.exe
Enter your choice = 20
Don't get demotived , Try your best next time
4.3.1 Code:
#include <iostream>
using namespace std;
int main()
{
int s = 10, b;
b = s;
b += s;
cout << "value of b is " << b << endl;
b /= s;
cout << "value of b is " << b << endl;
return 0;
}
Output :
value of b is 20
value of b is 2
return 0;
}
4.4.1.1Output:
a = 1 , b = 2
a & b = 0
a | b = 3
a ^ b = 3
b << 1 = 4
b >> 1 = 1
Output = a = 11
b = 2
{
// declare variables
int a = 10, b = 22, c = 2, x;
x = a + b / c;
// display result
cout << "The result of the expression is = " << x;
return 0;
}
6.1.1.1.1
Output:
a + y = 78
6.2.1.1.1 Output :
a + y = 94
7.1.1 Output:
Enter the Number to check Prime: 34 Number is not Prime.
Enter the Number to check Prime: 37 Number is Prime.
8.1.1 Output
Enter your week's choice =
6 Day is Friday.
9.1.1 Output
Enter marks out of 100
12
You failed.
Enter marks out of 100
77
You passed.
10.1.1 Output:
Enter name
Dev
Enter password
23
--INCORRECT PASSWORD--
return 0;
}
11.1.1.1 Output:
value of a: 10
11.2.1.1 Output:
value of a: 1
value of a: 2
value of a: 3
value of a: 4
value of a: 5
11.3.1.1 Output :
Enter the number whose table you want to calculate
6
6 X 1 = 6
6 X 2 = 12
6 X 3 = 18
6 X 4 = 24
6 X 5 = 30
6 X 6 = 36
6 X 7 = 42
6 X 8 = 48
6 X 9 = 54
6 X 10 = 60
12.1.1 Output:
0
1
2
3
13.1.1 Output:
Dev kumar Page 15
OOPs practical GNDEC
0
1
2
3
5
6
7
8
9
// function declaration
void swap(int x, int y)
{
int temp;
temp = x;
x = y;
y = temp;
int main()
{
int a = 10;
int b = 12;
swap(a, b);
return 0;
}
14.1.1.1 Output:
Before swapping, value of a :10
Before swapping, value of b :12
After swapping, value of a :12
After swapping, value of b :10
int main()
{
int x = 9;
int y = 2;
additon(x, y);
return 0;
}
Dev kumar Page 17
OOPs practical GNDEC
14.2.1.1 Output
Value of a = 19
Value of b = 12
Value of x = 19
Value of y = 12
return dec_value;
}
int main()
{
int num = 1020;
cout << octal_To_Decimal(num) << endl;
}
15.1.1 Output
Dev kumar Page 18
OOPs practical GNDEC
528
int base = 1;
int main()
{
int num = 1010100001;
cout << binary_To_Decimal(num) << endl;
}
16.2.1.1 Output
673
if (i == j)
cout << arr[i] << " ";
}
}
int main()
{
int arr[] = {1, 2, 45, 3, 2, 4, 3, 2, 12, 100};
int n = sizeof(arr) / sizeof(arr[0]);
print(arr, n);
return 0;
}
17.1.1.1 Output
1 2 45 3 4 12 100
#define R1 4
#define C1 4
#define R2 4
#define C2 4
int main(void) {
int mat1[R1][C1] = {
{1, 1, 1, 1},{2, 2, 2, 2}, {3, 3, 3, 3},
{4, 4, 4, 4}
};
int mat2[R2][C2] = {
{1, 1, 1, 1},{2, 2, 2, 2},{3, 3, 3, 3},
{4, 4, 4, 4}
};
if (C1 != R2) {
cout << "The number of columns in Matrix-1 must be equal
to the number of rows in "
"Matrix-2" << endl;
Dev kumar Page 21
OOPs practical GNDEC
cout << "Please update MACROs according to your array
dimension in #define section"
<< endl;
exit(EXIT_FAILURE);
}
mulMat(mat1, mat2);
return 0;
}
17.2.1.1 Output
Multiplication of given two matrices is:
10 10 10 10
20 20 20 20
30 30 30 30
40 40 40 40
count++;
}
sptr++;
}
return count;
}
int main()
{
char str[] = "object oriented programming";
return 0;
}
18.2.1.1 Output
Vowels in above string: 9
length = strlen(str);
21.3.1.1 Output
Enter String : divid
Dev kumar Page 24
OOPs practical GNDEC
divid is a palindrome
int main()
{
string str = "Praidime";
reverseStr(str);
cout << str;
return 0;
}
18.4.1.1 Output
emidiarP
18.5.1.1 Output
r count = 2
l count = 2
u count = 2
a count = 3
class Rectangle{
float length;
float breadth;
public:
void set_parameters(int l,int b){
if(l>=0 && b>=0){
length=l;
breadth=b;
}
}
Dev kumar Page 26
OOPs practical GNDEC
float area(){
return length*breadth;
}
float perimeter(){
return 2*(length+breadth);
}
/* NON Parametrized Constructor */
Rectangle(){
length=0;
breadth=0;
cout<<"Non parametrized constructor called "<<endl;
}
/* Parametrized constructor */
Rectangle(int a,int b){
length=a;
breadth=b;
cout<<"Parametrized constructor called "<<endl;
}
/* Copy Constructor */
Rectangle(Rectangle &rect){
length= rect.length;
breadth =rect.breadth;
cout<<"Copy constructor called "<<endl;
}
//destructor
~Rectangle(){
cout<<"destructor called"<<endl;
}
};
int main(){
Rectangle r1;
Rectangle r2(3,10);
Rectangle r3(r2);
return 0;
}
22.1.1.1 Output
Non parametrized constructor called
Dev kumar Page 27
OOPs practical GNDEC
Parametrized constructor called
Copy constructor called
destructor called
destructor called
destructor called