Oopls Lab Record
Oopls Lab Record
2022-23(ODD SEMESTER)
DEPARTMENT
Of
INFORMATION TECHNOLOGY
NAME :
REGISTER NO :
COURSE :B.Tech(IT)
YEAR/SEM/SEC :II/III/C
BONAFIDE CERTIFICATE
Register No:
Department:INFORMATION TECHNOLOGY
ALGORITHM :
PROGRAM :
#include <iostream.h>
#include<conio.h>
int main ()
{
// local variable declaration:
int a = 10;
return 0;
}
OUTPUT :
1
B.(i) SWITCH CASE STATEMENT
ALGORITHM:
The expression is evaluated once and compared with the values of each case label.
• If there is a match, the corresponding statements after the matching label are executed.
For example, if the value of the expression is equal to constant2, statements after case
constant2: are executed until break is encountered.
• If there is no match, the default statements are executed.
If we do not use break, all statements after the matching label are executed.
By the way, the default clause inside the switch statement is optional.
PROGRAM :
#include <iostream.h>
#include<conio.h>
int main() {
char oper;
float num1, num2;
cout << "Enter an operator (+, -, *, /): ";
cin >> oper;
cout << "Enter two numbers: " << endl;
cin >> num1 >> num2;
switch (oper) {
case '+':
cout << num1 << " + " << num2 << " = " << num1 + num2;
break;
case '-':
cout << num1 << " - " << num2 << " = " << num1 - num2;
2
break;
case '*':
cout << num1 << " * " << num2 << " = " << num1 * num2;
break;
case '/':
cout << num1 << " / " << num2 << " = " << num1 / num2;
break;
default:
// operator is doesn't match any case constant (+, -, *, /)
cout << "Error! The operator is not correct";
break;
}
return 0;
}
OUTPUT :
3
B.(ii) DO WHILE LOOP STATEMENT
ALGORITHM :
• The body of do...while loop is executed once. Only then, the test expression is evaluated.
• If the test expression is true, the body of the loop is executed again and the test
expression is evaluated.
• This process goes on until the test expression becomes false.
• If the test expression is false, the loop ends.
PROGRAM :
// C++ Program to print numbers from 1 to5 #
include <iostream.h>
#include<conio.h>
int main() {
int i = 1;
return 0;
}
4
OUTPUT :
5
C.FOR LOOP
ALGORITHM :
Step :01
• Start
Step :02 [Variable initialization]
• If condition True then Do your task, set i=i+N and go to Step :03 [Where N:Positive
Number]
• If condition False then go to Step :04
Step:04
• Stop
PROGRAM :
Here in the loop initialization part I have set the value of variable i to 1, condition is i<=6 and on
each loop iteration the value of i increments by
#include <iostream.h>
#include<conio.h>
int main()
{
for(int i=1; i<=6; i++)
{
/* This statement would be executed *
repeatedly until the condition
* i<=6 returns false.*/
cout<<"Value of variable i is: "<<i<<endl;
}
return 0;
}
6
OUTPUT :
7
D.WHILE LOOP
ALGORITHM :
• The while loop evaluates the test expression inside the parenthesis ().
• If the test expression is true, statements inside the body of while loop are executed. Then, the test expression
is evaluated again.
• The process goes on until the test expression is evaluated to false.
• If the test expression is false, the loop terminates (ends).
PROGRAM :
// C++ Program to print numbers from 1 to 5
#include <iostream.h>
#include<conio.h>
int main()
{
int i = 1;
8
OUTPUT :
9
EX.NO:02 PROGRAMS TO UNDERSTAND STRUCTURES & UNIONS
DATE:
AIM:
To write c++ program to understand structures & unions.
a. STRUCTURES
ALGORITHM :
struct structureName{
member1;
member2;
member3;
.
.
.
memberN;
};
PROGEAM :
#include <iostream.h>
#include<conio.h>
struct Student
{
char stuName[30];
int stuRollNo;
int stuAge;
};
int main()
{
Student s;
cout<<"Enter Student Name: ";
cin>>getline(s.stuName, 30);
10
cout<<"Enter Student Roll No: ";
cin>>s.stuRollNo;
cout<<"Enter Student Age: ";
cin>>s.stuAge;
cout<<"Student Record:"<<endl;
cout<<"Name: "<<s.stuName<<endl;
cout<<"Roll No: "<<s.stuRollNo<<endl;
cout<<"Age: "<<s.stuAge;
return 0;
}
OUTPUT :
11
B. UNION
SYNTAX : union
union_name
{
data-type member-1;
data-type member-2;
data-type member-3;
data-type member-4;
};
PROGRAM :
#include <iostream.h>
#include<conio.h>
int main()
{
struct student 1 s1;
union student2 u1;
12
cout << "size of structure : " << sizeof(s1) << endl;
cout << "size of union : " << sizeof(u1) << endl;
return 0;
}
OUTPUT :
13
EX.NO:03 PROGRAMS TO UNDERSTAND POINTER ARITHMETIC
DATE:
AIM:
To write c++ program to understand Pointer Arithmetic.
Increment a Pointer (++)
PROGRAM :
#include<iostream.h>
#include<conio.h>
const int MAX = 3;
int main ()
{
int var[MAX] = {10, 100, 200};
int *ptr;
//let us have array address in a pointer.
ptr = var;
for(int i = 0; i < MAX; i++)
{
cout<<"Address of var[" << i << "] = ";
cout<<ptr << endl;
cout<<"Value of var[" << i << "] = ";
cout<<*ptr << endl;
//point to the next location
ptr++; }
return 0;
}
14
OUTPUT :
Decrement a Pointer (- -)
PROGRAM :
#include<iostream.h>
#include<conio.h>
const int MAX = 3; int
main () {
int var[MAX] = {10, 100, 200};
int *ptr;
//let us have the address of the last element in the pointer.
ptr = &var[MAX-1];
for (int i = MAX; i > 0; i--) {
cout << "Address of var[" << i << "] = ";
cout << ptr << endl;
cout << "Value of var[" << i << "] = ";
cout << *ptr << endl;
//point to the previous location
ptr--;
}
return 0;
15
OUTPUT :
Pointer Comparisons
PROGRAM :
#include<iostream.h>
#include<conio.h>
const int MAX = 3;
int main () {
int var[MAX] = {10, 100, 200};
int *ptr;
//let us have the address of the first element in a pointer.
ptr = var;
int i = 0;
while ( ptr <= &var[MAX - 1] ) {
cout<< "Address of var[" << i << "] = ";
cout<< ptr << endl;
cout<< "Value of var[" << i << "] = ";
cout<< *ptr << endl;
//point to the previous location
ptr++;
i++;
16
return 0;
}
OUTPUT :
17
EX.NO:04 IMPLEMENTATION OF FUNCTION AND RECURSION
DATE:
AIM:
To write c++ program to implement function and recursion.
FUNCTION
SYNTAX :
return-type function-name(parameter1, parameter2, ...) {
// function-body }
PROGRAM :
#include <iostream.h>
#include<conio.h>
int main() {
int a = 10, b = 20;
18
cout << "m is " << m;
return 0;
}
OUTPUT :
19
RECURSION
SYNTAX :
int fact(int n)
{
if (n < = 1) // base case
return 1;
else
return n*fact(n-1);
}
PROGRAM :
// Factorial of n = 1*2*3*...*n
#include <iostream.h>
#include<conio.h>
int factorial(int);
int main() {
int n, result;
result = factorial(n);
cout << "Factorial of " << n << " = " << result;
return 0;
}
20
int factorial(int n) {
if (n > 1) {
return n * factorial(n - 1);
} else {
return 1;
}
}
OUTPUT :
21
EX.NO:05 IMPLEMENTATION OF INLINE FUNCTION
DATE:
AIM:
To write c++ program to implement Inline function.
SYNTAX :
inline return-type function-name(parameters)
{
// function code
}
PROGRAM :
#include <iostream.h>
#include<conio.h>
class operation
{
int a,b,add,sub,mul;
float div;
public:
void get();
void sum();
void difference();
void product();
void division();
};
inline void operation :: get()
{
cout << "Enter first value:";
cin >> a;
cout << "Enter second value:";
22
cin >> b;
}
int main()
23
{
cout << "Program using inline function\n";
operation s;
s.get();
s.sum();
s.difference();
s.product();
s.division();
return 0;
}
OUTPUT :
24
EX.NO:06 PROGRAMS TO UNDERSTAND DIFFERENT FUNCTION CALL
MECHANISM
DATE:
AIM:
To write c++ program to implement function call mechanism.
CALL BY REFERENCE
SYNTAX : //
Declaration
return_type function_name( type_of_argument,type_of_argument,..);
//Calling
function_name(&actual_argument,&actual_argument,..);
//Definition
return_type function_name(type_of_argument &alias_variable,..)
{
....;
}
PROGRAM :
#include<iostream.h>
#include<conio.h>
void swap(int *x, int *y)
{
int swap;
swap=*x;
*x=*y;
*y=swap;
}
25
int main()
{
int x=500, y=100;
swap(&x, &y); // passing value to function
cout<<"Value of x is: "<<x<<endl;
cout<<"Value of y is: "<<y<<endl;
return 0;
}
OUTPUT :
26
CALL BY VALUE
PROGRAM :
#include <iostream.h>
#include<conio.h>
void change(int data);
int main()
{
int data = 3;
change(data);
cout << "Value of the data is: " << data<< endl;
return 0;
}
void change(int data)
{
data = 5; }
OUTPUT :
27
EX.NO:07 IMPLEMENTATION OF STORAGE SPECIFIERS
DATE:
AIM:
To write c++ program to implement storagespecifiers.
SYNTAX :
storage_class var_data_type var_name;
C++ uses 5 storage classes, namely:
1. auto
2. register
3. extern
4. static
5. mutable
1. AUTO
PROGRAM :
#include <iostream.h>
#include<conio.h>
void autoStorageClass()
28
// printing the auto variables
cout << a << " \n";
cout << b << " \n";
cout << c << " \n";
cout << d << " \n";
}
int main()
{
return 0;
}
OUTPUT :
29
2. EXTERN PROGRAM:
# include <iostream>
#include<conio.h>
30
// extern variables 'x'
cout<< "Modified value of thevariable 'x'" << " declared as extern: \n"<< x;
}
int main()
StorageClass();
return 0;
}
OUTPUT :
31
3. STATIC
PROGRAM : #include
<iostream> #include<
conio.h>
int main()
{
32
// Calling the static parts
OUTPUT :
33
4. REGISTER :
PROGRAM :
#include <iostream.h>
#include<conio.h>
void registerStorageClass()
34
OUTPUT :
5. MUTABLE :
PROGRAM :
#include< iostream.h>
#include<conio.h>
class Test {
public:
int x;
Test() {
x = 4; y = 10;
} 35
};
int main()
{
// t1 is set to constant
const Test t1;
36
EX.NO:08 IMPLEMENTATION OF CONSTRUCTORS & DESTRUCTORS
DATE:
AIM:
To write c++ program to implement Constructors and Destructors.
CONSTRUCTOR
SYNTAX:
class A
{
public:
int x;
//constructor
A()
{
// object initialization
}
};
PROGRAM :
#include <iostream.h>
#include<conio.h>
class Line {
public:
void setLength( double len );
double getLength( void );
Line(); // This is the constructor
private:
double length;
37
};
return 0;
}
38
OUTPUT :
DESTRUCTOR
SYNTAX :
~constructor-name();
PROGRAM :
#include <iostream.h>
#include<conio.h>
class Line {
public:
void setLength( double len );
double getLength( void );
Line(); // This is the constructor declaration
~Line(); // This is the destructor: declaration
private:
double length;
};
39
Line::~Line(void) {
cout << "Object is being deleted" << endl;
}
void Line::setLength( double len ) {
length = len;
}
double Line::getLength( void ) {
return length;
}
return 0;
}
40
OUTPUT :
41
EX.NO:09 IMPLEMENTATION OF USE OF “THIS” POINTER USING CLASS
DATE:
AIM:
To write c++ program to implement use of “this” pointer using class.
SYNTAX :
delete this;
PROGRAM :
#include <iostream.h>
#include<conio.h>
class Employee {
public:
int id; //data member (also instance variable) string
name; //data member(also instance variable) float
salary;
Employee(int id, string name, float salary)
{
this->id = id;
this->name = name;
this->salary = salary;
}
void display()
{
cout<<id<<" "<<name<<" "<<salary<<endl;
}
};
int main(void) {
Employee e1 =Employee(101, "Sonoo", 890000); //creating an object of Employee
Employee e2=Employee(102, "Nakul", 59000); //creating an object of Employee
42
e1.display();
e2.display();
return 0;
}
OUTPUT :
43
EX.NO:10 IMPLEMENTATION OF INHERITANCE & FUNCTION
OVERRIDING
DATE:
AIM:
To write c++ program to implement Inheritance and Function overriding.
A. Multiple inheritance – Access Specifiers
SYNTAX:
//multiple inheritance
#include <iostream.h>
#include<conio.h>
44
public:
FourWheeler()
{
cout << "This is a 4 wheeler Vehicle" << endl;
}
};
};
// main function
int main()
{
// creating object of sub class will
// invoke the constructor of base classes
Car obj;
return 0;
}
OUTPUT:
45
B. Hierarchical inheritance – Function Overriding/Virtual Function
SYNTAX :
class base_class {
... .. ...
}
PROGRAM :
// C++ program to implement
// Hierarchical Inheritance
#include <iostream.h>
#include<conio.h>
// base class
class Vehicle
{
public:
46
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};
};
};
// main function
int main()
{
// creating object of sub class will
// invoke the constructor of base class
Car obj1;
Bus obj2;
return 0;
}
47
OUTPUT :
48
EX.NO:11 IMPLEMENTATION OF OVERLOAD UNARY & BINARY
OPERATORS AS MEMBER FUNCTION &NON MEMBER FUNCTION
DATE:
AIM:
To write c++ program to implement Overload unary and binary operators as member function
and non member function.
A. Unary Operator as Member
function ALGORITHM :
• Step 1: Start the program.
• Step 2: Declare the class.
• Step 3: Declare the variables and its member function.
• Step 4: Using the function getvalue() to get the two numbers.
• Step 5: Define the function operator ++ to increment the values
• Step 6: Define the function operator - -to decrement the values.
• Step 7: Define the display function.
• Step 8: Declare the class object.
• Step 9: Call the function getvalue()
• Step 10: Call the function operator ++() by incrementing the class object and call the
function display.
• Step 11: Call the function operator - -() by decrementing the class object and call the
function display.
• Step 12: Stop the program.
PROGRAM :
#include <iostream.h>
#include<conio.h>
class Number
49
{
private:
int x;
public:
Number(int p)
{
x = p;
}
void operator -()
{
x = -x;
}
void display()
{
cout<<"x = "<<x;
};
int main()
{
Number n(10);
-n;
n.display();
return 0;
}
50
OUTPUT:
51
B. Binary operator as non member
function ALGORITHM :
• Step 1: Start the program.
• Step 2: Declare the class.
• Step 3: Declare the variables and its member function.
• Step 4: Using the function getvalue() to get the two numbers.
• Step 5: Define the function operator +() to add two complex numbers.
• Step 6: Define the function operator –()to subtract two complex numbers.
• Step 7: Define the display function.
• Step 8: Declare the class objects obj1,obj2 and result.
• Step 9: Call the function getvalue using obj1 and obj2
• Step 10: Calculate the value for the object result by calling the function operator
+ and operator -.
• Step 11: Call the display function using obj1 and obj2 and result.
• Step 12: Return the values.
• Step 13: Stop the program.
PROGRAM :
#include <iostream.h>
#include<conio.h>
class Complex
{
private:
float real;
float imag;
public:
Complex(){} Complex(
float r, float i) {
real = r;
52
imag = i;
}
void display()
{
cout<<real<<"+i"<<imag;
}
friend Complex operator +(Complex &, Complex &);
};
Complex operator +(Complex &c1, Complex &c2)
{
Complex temp;
temp.real = c1.real + c2.real;
temp.imag = c1.imag + c2.imag;
return temp;
}
int main()
{
Complex c1(3, 4);
Complex c2(4, 6);
Complex c3 = c1+c2;
c3.display();
return 0;
}
53
OUTPUT :
54
EX.NO:12 IMPLEMENTATION OF FRIEND FUNCTION & FRIND CLASS
DATE:
AIM:
To write c++ program to implement Friend function and friend class.
A. FRIEND FUNCTION
SYNTAX : class
className {
... .. ...
friend returnType functionName(arguments);
... .. ...
}
PROGRAM :
#include <iostream.h>
#include <string.h>
#include<conio.h>
class sample{
int length, breadth;
public:
sample(int length, int breadth):length(length),breadth(breadth)
{}
friend void calcArea(sample s); //friend function declaration
};
//friend function definition
void calcArea(sample s){
cout<<"Area = "<<s.length * s.breadth;
55
}
int main()
{
sample s(10,15);
calcArea(s);
return 0;
}
OUTPUT :
56
B.FRIEND CLASS
SYNTAX :
class ClassB;
class ClassA {
// ClassB is a friend class of
ClassA friend class ClassB;
... ..
... }
class ClassB {
... .. ...
}
PROGRAM:
// C++ program to demonstrate the working of friend class
#include <iostream>
#include<conio.h>
// forward declaration
class ClassB;
class ClassA {
private:
int numA;
57
public:
// constructor to initialize numA to 12
ClassA() : numA(12) {}
};
class ClassB {
private:
int numB;
public:
// constructor to initialize numB to 1
ClassB() : numB(1) {}
int main() {
ClassB objectB;
cout << "Sum: " << objectB.add();
return 0;
}
58
OUTPUT :
59
EX.NO:13 IMPLEMENTATION OF CLASS TEMPLATE
DATE:
AIM:
To write c++ program to implement Class template.
SYNTAX :
1. template<class Ttype>
2. class class_name
3. {
4. .
5. .
6. }
PROGRAM:
#include<iostream.h>
#include<conio.h>
templateclass <T>
class A
{
public:
T num1 = 5;
T num2 = 6;
void add()
{
}
std::cout << "Addition of num1 and num2 : " << num1+num2<<std::endl;
};
60
int main()
{
A<int> d;
d.add();
return 0;
}
OUTPUT :
61