0% found this document useful (0 votes)
23 views

OOPS Lab File

The given document is a lab report submitted by Harman Singh Badgaal to Dr. Vanitha P.S of the Department of Information Technology at Dr B R Ambedkar National Institute of Technology, Jalandhar, Punjab. The report contains 10 experiments on various Object Oriented Programming concepts in C++ like control structures, functions, classes, inheritance etc. Each experiment has the aim, code, output and result. The experiments cover concepts like control statements, functions, classes, arrays of objects, constructors, destructors, inheritance etc.

Uploaded by

Harman Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

OOPS Lab File

The given document is a lab report submitted by Harman Singh Badgaal to Dr. Vanitha P.S of the Department of Information Technology at Dr B R Ambedkar National Institute of Technology, Jalandhar, Punjab. The report contains 10 experiments on various Object Oriented Programming concepts in C++ like control structures, functions, classes, inheritance etc. Each experiment has the aim, code, output and result. The experiments cover concepts like control statements, functions, classes, arrays of objects, constructors, destructors, inheritance etc.

Uploaded by

Harman Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 64

Dr B R Ambedkar National Institute of Technology

Grand Trunk Road, Barnala - Amritsar Bypass Road,


Jalandhar, Punjab 144011

ITPC-221 OBJECT-ORIENTED PROGRAMMING


CONCEPT LAB
B.Tech - II Year (3rd SEMESTER)

Submitted By

Harman Singh Badgaal

21124038

Submitted To

Dr Vanitha P S

Department of Information Technology


INDEX

Exp No Title Date Page No Signature


Index
Exp No Title Date Page No Signature
Question 1:
Program to Implement Various Control Structures: If statement, Switch case statement, do while loop, For
loop, While loop
Ex. No : 1 CONTROL STATEMENTS IN C++
Date : 2/08/2022

Aim: Program to implement various Control Structures

Code:
#include<iostream>
using namespace std;
class arrays
{
private:
int arr[100],n;
public:
void set_data()
{ cout<< "Enter array size "<<endl;
cin>>n;
for(int i=0;i<=n-1;i++)
cin>> arr[i];
}
void show_data()
{
for(int i=0;i<=n-1;i++)
{cout<<arr[i];}
cout<<endl;
}
void sort_array(){
for(int i=0;i<=n-1;i++)
for(int j=i+1;j<=n-1;j++)
if(arr[i]>arr[j])
swap(arr[i],arr[j]);
}
void reverse_array(){
int i=0;
int j=n-1;
while (i<=j){
swap(arr[i],arr[j]);
i++;
j--;
}
}
void increment_array(){
int i=0;
do{
arr[i]=arr[i]+1;
i++;
}
while(i<=n);
}
};

class calculator{
float num1,num2;
public:
void setData(){
cout<< "Enter two operands: ";
cin>> num1 >> num2;
}
void performOperations(){
while(1){
char op;
cout<<endl<< "Enter operator: +, -, *, /: OR Enter 'T' to exit "<<endl; ;
cin>> op;

switch(op) {
case '+':
cout<< num1 << " + " << num2 << " = " << num1 + num2;
break;

case '-':
cout<< num1 << " - " << num2 << " = " << num1 - num2;
break;

case '*':
cout<< num1 << " * " << num2 << " = " << num1 * num2;
break;

case '/':
cout<< num1 << " / " << num2 << " = " << num1 / num2;
break;

case 'T':
exit(0);

default:
cout<< "Error! operator is not correct";
break;
}
}
};
int main(){
arrays o1;
o1.set_data();
o1.sort_array();
o1.show_data();
o1.reverse_array();
o1.show_data();
o1.increment_array();
o1.show_data();0

calculator c1;
c1.setData();
c1.performOperations();
return 0;

Output:

Result:
The program is executed and verified.
Question 2:
Program to Implement inline function
Ex. No : 2 INLINE FUNCTION
Date : 23/08/2022

Aim:Program to make the use of an Inline Function

Code:
#include <iostream>
using namespace std;
class number{
int a,b;

public:
inline void setnum(int x,inty){
a=x;
b=y;
}

inline void sum() {


cout<<"Sum of "<<a<<" and "<<b<<" is "<<a+b<<endl;
}
};

int main() {
number o1,o2;
o1.setnum(3,4);
o1.sum();
o2.setnum(6,7);
o2.sum();
}

Output:
Result: The program is executed and verified.
Question 3:
Program to Implement Structure and Union
Ex. No : 3 IMPLEMENTING A STRUCTURE IN C++
Date : 23/08/2022

Aim: Program to demonstrate the concept of Structures.

Code:
#include <iostream>
using namespace std;

struct Person
{
char name[50];
int age;
float salary;
};

int main()
{
Person p1;

cout<< "Enter Full name: "<<endl ;


cin.get(p1.name, 50);
cout<< "Enter age: ";
cin>> p1.age;
cout<< "Enter salary: ";
cin>> p1.salary;

cout<< "Displaying Information." << endl;


cout<< "Name: " << p1.name << endl;
cout<<"Age: " << p1.age << endl;
cout<< "Salary: " << p1.salary;

return 0;
}
Output:

Result:The program is executed and verified.


Ex. No : 3 IMPLEMENTING A UNION IN C++
Date : 23/08/2022

Aim: Program to demonstrate the concept of Unions.

Code:
#include <iostream>
using namespace std;
union Job {
float salary;
int workerNo;
}j;

int main() {
j.salary= 12.3;
j.workerNo= 100;

cout<<"Salary = "<<j.salary<<endl;
cout<<"Number of workers = "<<j.workerNo;

Output:

Result:The program is executed and verified.


Question 4:
Program to Implement different types of function calling and passing methods and Recursion.
Ex. No : 4 DIFFERENT FUNCTION CALL AND PASS METHODS AND RECURSION

Date : 06/09/2022

Aim: Program to understand different Function Call Mechanisms: Argument with return

value, Argument without return value, No argument but return value, No argument and no
return value

Code:
#include<iostream>
using namespace std;
int factorial(int num){
if(num==0 || num==1)
return 1;
return num= num* factorial(num-1);
}
void swap(int *a ,int *b){
int temp;
temp = *a;
*a = *b;
*b = temp;
cout<<"After swap the values become a = "<< *a <<" and b = " << *b <<endl<<endl;
}
void sum_positive(){
int n ,sum=0;
cout<<"Enter the number of positive numbers whose sum is to be found"<<endl;
cin>>n;
for(int i=1 ;i<=n ;i++){
sum+=i;
}
cout<<"Sum of "<<n<<" positive numbers is "<<sum<<endl<<endl;
}
int insertion(){
int temp;
cout<<"Enter the number to be checked for even or odd"<<endl;
cin>>temp;
return temp;
}
int main(){
cout<<"Showing 'An argument passed (with call by value) and a return value' "<<endl;
int a;
cout<<"Enter the number whose factorial is to be found"<<endl;
cin>>a;
cout<<"Factorial of "<<a<< " is "<<factorial(a) <<endl<<endl;
cout<<"Showing 'An argument passed (call by reference) but no return value'"<<endl;
int x ,y;
cout<<"Enter the numbers a and b to be swaped"<<endl;
cin>>x;
cin>>y;
swap(&x, &y);
cout<<"Showing 'No argument passed but a return value'"<<endl;
int num;
num= insertion();
if(num%2==0)
cout<<"The number is Even"<<endl<<endl;
else
cout<<"The number is Odd"<<endl<<endl;
cout<<"Showing 'No argument passed and no return value'"<<endl;
sum_positive();
}

Output:

Result : The program is executed and verified.


Question 5:
Program to Implement Static Data Members and Static Member Functions in C++
Ex. No 5 STACTIC DATA MEMBERS AND MEMBER FUNCTIONS IN C++
Date : 13/09/2022

Aim: Program to implement Static data members and Static member functions

Code:
#include <iostream>
using namespace std;
class Student
{
public:
static int Class;
int gpa;
string name;
static void getData();
};
int Student::Class = 10;
void Student::getData()
{
cout << Class<<endl;
}
int main()
{
Student s1;
s1.gpa = 7;
s1.name = "Sham";
Student s2;
s2.gpa = 9;
s2.name = "Bhuvan";
s1.getData();
s2.getData();
if (s2.gpa > 8)
{ // it will change the s2 class also
s1.Class++;
}
cout << s1.Class << " ";
cout << s2.Class;
return 0;
}
Output:

Result: The program is executed and verified.


Question 7:
Program to Implement Array Of Objects in C++
Ex. No : 7 ARRAY OF OBJECTS IN C++
Date : 20/09/2022

Aim: Program to demonstrate the concept of array of objects.

Code:
#include <iostream>
using namespace std;
class Student {
public:
string name;
int rollno;

Student() {}

Student(string x, int y) {
name = x;
rollno= y;
}

void printDetails() {
cout<< rollno<< " - " << name << endl;
}
};

int main() {

Student students[5];

students[0] = Student("Karan", 48);


students[1] = Student("Navtej", 70);
students[2] = Student("Sachleen", 91);
students[3] = Student("Baljinder", 22);
students[4] = Student("Gurtej", 38);

for(int i=0; i< 5; i++) {


students[i].printDetails();
}
}
Output:

Result: The program is executed and verified.


Question 8:
Program to Implement Concept of Constructor and Destructor.
Ex. No : 8 CONSTRUCTORS AND DESTRUCTORS IN C++
Date : 27/09/2022

Aim: Program to demonstrate different types of Constructors: Default Constructor,


Parameterized Constructor, Copy Constructor and Destructor.

Code:
#include<iostream>
using namespace std;
class Complex{
int a,b;
public:
Complex(){
a = 0;
b = 0;
}
Complex(int x,int y){
a = x;
b = y;
}
Complex(int x){
a = x;
b = 0;
}
Complex(Complex &x){
a=x.a;
b=x.b
}
void display(){
cout<<"The complex number is "<<a<<" + "<<b<<"i"<<endl;
}
~Complex(){
cout<<"Now Destructor is invoked"<<endl;
}
};
int main(){
Complex c1(7,9);
c1.display();

Complex c2(5);
c2.display();

Complex c3;
c3.display();

Complex c4=c3;
c4.display();
}

Output:

Result: The program is executed and verified.


Question : 9
Program to show Multiple Inheritance.
Ex. No : 9
Date : 10/10/2022

Aim: Program to demonstrate concept of Multiple Inheritance

Code:
#include<iostream>
using namespace std;
class A
{
public:
int x;
void getx()
{
cout << "enter value of x: "; cin >> x;
}
};
class B
{
public:
int y;
void gety()
{
cout << "enter value of y: "; cin >> y;
}
};
class C : public A, public B //C is derived from class A and class B
{
public:
void sum()
{
cout << "Sum = " << x + y;
}
};
int main()
{
C obj1; //object of derived class C
obj1.getx();
obj1.gety();
obj1.sum();
}

Output:

Result: The program is executed and verified.


Question 10:
Program to show Multilevel Inheritance.
Ex. No : 10
Date : 10/10/2022

Aim: Program to demonstrate concept of Multilevel Inheritance

Code:
#include <iostream>
using namespace std;
class base //single base class
{
public:
int x;
void getdata()
{
cout << "Enter value of x= "; cin >> x;
}
};
class derive1 : public base // derived class from base class
{
public:
int y;
void readdata()
{
cout << "\nEnter value of y= "; cin >> y;
}
};
class derive2 : public derive1 // derived from class derive1
{
private:
int z;
public:
void indata()
{
cout << "\nEnter value of z= "; cin >> z;
}
void product()
{
cout << "\nProduct= " << x * y * z;
}
};
int main()
{
derive2 a; //object of derived class
a.getdata();
a.readdata();
a.indata();
a.product();
return 0;
}

Output:

Result: The program is executed and verified.


Question 11:
Program to show Hybrid Inheritance.
Ex. No : 11
Date : 10/10/2022

Aim: Program to demonstrate concept of Hybrid Inheritance

Code:
#include <iostream>
using namespace std;

class A
{
public:
int x;
};
class B : public A
{
public:
B() //constructor to initialize x in base class A
{
x = 10;
}
};
class C
{
public:
int y;
C() //constructor to initialize y
{
y = 4;
}
};
class D : public B, public C //D is derived from class B and class C
{
public:
void sum()
{
cout << "Sum= " << x + y;
}
};

int main()
{
D obj1; //object of derived class D
obj1.sum();
return 0;
}

Output:

Result: The program is executed and verified.


Question 12:
Program to Understand use of ‘this’ Pointer.
Ex. No : 12
Date : 10/10/2022

Aim: Program to understand the use of ‘this’ Pointer.

Code:
#include <iostream>
using namespace std;

class sample{
int a,b;
public:
void input(int a,int b)
{
this->a=a+b;
this->b=a-b;
}
void output()
{
cout<<"a = "<<a<<endl<<"b = "<<b;
}
};

int main()
{
sample x;
x.input(5,8);
x.output();
getch();
return 0;
}
Output:

Result: The program is executed and verified.


Question 13:
Program to Understand Storage Specifiers.
Ex. No : 13
Date : 10/10/2022

Aim: Program to Understand the concept of Storage Specifiers.

Code:
#include <iostream>
using namespace std;
int x;
void autoStorageClass()
{
cout << "Demonstrating auto storage specifier\n";
auto a = 21;
auto b = 32.3;
auto c = "karan";
cout << a << " \n";
cout << b << " \n";
cout << c << " \n";
}
int staticFun()
{
cout << "For static variables: ";
static int count = 0;
count++;
return count;
}
void registerStorageClass()
{
cout << "Demonstrating register class\n";
register char a = 'K';
cout << "Value of the variable a"<< " declared as register: " << a;
}
void externStorageClass()
{
cout << "Demonstrating extern class\n";
extern int x;
cout << "Value of the variable x"
<< "declared, as extern: " << x << "\n";
x = 2;
cout<< "Modified value of the variable 'x'"<< " declared as extern: \n"<< x;
}
int main()
{
autoStorageClass();
cout << staticFun() << "\n";
registerStorageClass();
externStorageClass();
return 0;
}
Output:

Result: The program is executed and verified.

Question 14:
Program to Understand Pointer Arithmetic.
Ex. No : 14
Date : 10/10/2022
Aim: Program to Understand the concept of Increment/Decrement a Pointer and Addition of an Integer to a
Pointer.

Code:
#include <iostream>
using namespace std;
int main()
{
int A[5] = { 2, 4, 6, 8, 10 };
int *p = A;
int *q = &A[3];

cout << "Addresses of elements of the Array:\n";


for (int i = 0; i < 5; i++)
{
cout << A + i << endl;
}

cout << "\nOperations:\n";


p++;
cout << "p++:" << p << endl;
p--;
cout << "p--:" << p << endl;
p = p + 3;
cout << "p = p + 3: " << p << endl;

Output:
Result: The program is executed and verified.

Question 15:
Program to show the concept of containership.

Ex. No : 15
Date : 17/10/2022

Aim: Program to Understand the concept of Containership.

Code:
#include <iostream>
using namespace std;
class first {
public:
first(){
cout << "Hello from first class"<<endl;
}
};
class second {
first f;
public:
second(){
cout << "Hello from second class";
}
};
int main(){
second s;
return 0;
}

Output:
Result: The program is executed and verified.

Question 16:
Program to show the concept of Run Time Polymorphism using Virtual function.
Ex. No : 16
Date : 17/10/2022

Aim: Program to Understand the concept of Run Time Polymorphism using Virtual function.

Code:
#include<iostream>
using namespace std;

class B {
public:
virtual void s() {
cout<<" In Base "<<endl;
}
};

class D: public B {
public:
void s() {
cout<<"In Derived ";
}
};

int main(void) {
D d;
B *b= &d;
b->s();
return 0;
}
Output:

Result: The program is executed and verified.


Question 17:
Program to Overload Unary Operator.
Ex. No : 17
Date : 17/10/2022

Aim: Program to Overload


a) decrement (--) operator
b) logical not (!) operator

Code:
a)
#include <iostream>
using namespace std;
class num{
private:
int i;
public:
num(){
i=3;
}
num operator -- ()
{
num temp;
temp.i = --i;
return temp;
}
num operator -- (int)
{
num temp;
temp.i = i--;
return temp;
}

void Display()
{ cout << "i = "<< i <<endl; }
};
int main()
{
num obj1,obj2;
cout<<"Initially"<<endl;
obj1.Display();
obj2.Display();

cout<<"Calling prefix decrement"<<endl;


obj2 = --obj1;
obj1.Display();
obj2.Display();

cout<<"Calling postfix decrement"<<endl;


obj2 = obj1--;
obj1.Display();
obj2.Display();

return 0;
}

Output:
b)
#include <iostream>
using namespace std;

class NUM {
private:
int n;

public:
void getNum(int x)
{n = x;}

void dispNum(void)
{cout << "Value of n is: " << n;}

void operator !(void)


{n = !n;}
};

int main()
{
NUM num;
num.getNum(10);
cout << "Before calling Operator Overloading:";
num.dispNum();
cout << endl;
!num;
cout << "After calling Operator Overloading:";
num.dispNum();
cout << endl;

return 0;
}
Output:

Result: The program is executed and verified.


Question 18:
Program to Overload Binary Operator.
Ex. No : 18
Date : 17/10/2022

Aim: Program to overload binary operator.


a) % operator
b) <= operator

Code:
#include<iostream>
using namespace std;
class A{
public:
int x;
void insertion(){
cin>> x ;
}
A operator% ( A obj){
A c;
c.x = x % obj.x ;
return c ;
}
void display(){
cout<<x;
}
void operator<= ( A obj){
if(this->x <= obj.x)
cout<<"q is greater than p";
else
cout<<"p is greater than q";
}

};
int main(){
A obj1 , remainder;
cout<<"Insert the value of a "<<endl;
obj1.insertion();

A obj2;
cout<<"Enter the value of b "<<endl;
obj2.insertion();
cout<<"Remainder of a/b is : ";
remainder = obj1 % obj2 ;
remainder.display();
cout<<endl;

A obj3 , obj4;
cout<<"Enter the value of p "<<endl;
obj3.insertion();
cout<<"Enter the value of q "<<endl;
obj4.insertion();
obj3 <= obj4 ;
}

OUTPUT

Result: The program is executed and verified.


Question 19:
Program to work with formatted and unformatted IO operations
Ex. No : 19
Date : 31/10/2022

Aim: To make use of formatted and unformatted IO operations

Code: #include<bits/stdc++.h>
using namespace std;
class Book{
char title[20];
string author;
char alias;
int pubyear;

public:
Book(){
for(int i=0; i<20; i++){
title[i] = '\0';
}
fflush(stdin);
cout << "Enter book title" << endl;
cin.getline(title, 20);
cout << "Give a single character alias to the book." << endl;
cin.get(alias);
fflush(stdin);
cout << "Give author name." << endl;
getline(cin ,author);

cout << "Give publication year." << endl;


cin >> pubyear;
}
void BookDetails(){
cout << "Book title : ";
cout.write(title, 20);
cout << endl;
cout << "Book Author : " ;
cout.width(15);
cout << author << endl;
cout << "Alias for the book : " ;
cout.put(alias);
cout << endl << "Publication Year : " << pubyear;
}
};
int main(){
Book b1, b2;
cout.fill('_');
b1.BookDetails();
cout << endl;
cout.fill('_');
b2.BookDetails();
}
Output:

Result: The program is executed and verified.


Question 20:
Program to execute File Handling in C++
Ex. No : 20
Date : 7/11/2022

Aim: Program to execute File Handling in C++


a) Open write and read the content from a file

Code: #include <iostream>

#include <fstream>

using namespace std;

int main(){

char text[200];

fstream file;

file.open ("example.txt", ios::out | ios::in );

cout << "Write text to be written on file." << endl;

cin.getline(text, sizeof(text));

// Writing on file

file << text << endl;

// Reading from file

file >> text;

cout << text << endl;

//closing the file

file.close();

return 0;

}
Output:

Result: The program is executed and verified.

You might also like