0% found this document useful (0 votes)
274 views45 pages

OOP Lab Practical Guide

The document appears to be a lab report or practical file submission for an Object Oriented Programming course. It contains 20 programs written in C++ with the aim, code, and output of each program. The programs cover topics like data types, functions, classes, inheritance, dynamic memory allocation, and more. Each program is numbered and includes the student's name, course, enrollment number, and date submitted.

Uploaded by

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

OOP Lab Practical Guide

The document appears to be a lab report or practical file submission for an Object Oriented Programming course. It contains 20 programs written in C++ with the aim, code, and output of each program. The programs cover topics like data types, functions, classes, inheritance, dynamic memory allocation, and more. Each program is numbered and includes the student's name, course, enrollment number, and date submitted.

Uploaded by

Darpan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

MAHARAJA SURAJMAL INSTITUTE OF

TECHNOLOGY

OBJECT ORIENTED PROGRAMMING


(ETCS-210)
NAME: BIJENDER KUMAR
BRANCH: IT-1 MORNING
ENROLLMENT NO.: 03915003119
SUBMITTED TO: Ms. PREETI RATHEE
OOPS LAB PRACTICAL FILE
INDEX
S.NO PRACTICAL DATE SIGN

1. WAP to check whether 30/3/21


given number is even
or odd
2. WAP to generate table 30/3/21
of numbers entered by
user
3. WAP to generate 30/3/21
following pattern upto
15
4. WAP to check whether 30/3/21
given number is
positive or negative
5. WAP to print greatest 6/4/2021
number using ternary
operator
6. WAP to generate list of 6/4/2021
leap years between
1900 to 2008
7. WAP of class cylinder
having the following:-
Data types- length,
height, pi=3.14 and
Functions-
calculatevolume(),
surfaearea(), area() of
cylinder using scope
resolution operator(::)

8. WAP to show
constructor
overloading
9. WAP to implement
copy constructor
10. WAP to calculate the
square of a function
using inline function
11. WAP to implement 25/5/21
multiple inheritance
12. WAP to implement 25/5/21
hybrid inheritance
13. WAP to implement 1/6/21
constructor and
destructor in multiple
inheritance
14. WAP to implement 1/6/21
paramaterized
constructor in
multilevel inheritance
15. WAP to overload 8/6/21
increment operator in
both prefix and postfix
16. WAP to implement 8/6/21
runtime polymorphism
that is virtual function
17. WAP of class student 4/5/21
and display data of a
student of a class
having 30
students,having
following datatypes-
name, rollno, marks
percentage and
functions- readdata(),
displaydata(),
computedata()
18. WAP for dynamic 18/5/21
memory allocation of
object using new
operator.
19. WAP to create 18/5/21
dynamic array using
new and delete
operators.
20. WAP to show this 18/5/21
pointer.
21. WAP to count number 1/6/21
of objects using static
counter, constructor
and destructor.

PROGRAM 1

AIM: To check whether the given number is even or odd

CODE:

#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"ENTER A NUMBER=";
cin>>num;
if(num%2==0)
{
cout<<"\nTHE NUMBER IS EVEN";
}
else{cout<<"\nTHE NUMBER IS ODD";}
return 0;
}

OUTPUT:
PROGRAM 2

AIM: To check whether the given number is even or odd

CODE:
#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"ENTER A NUMBER=";
cin>>num;
for(int i=0; i<=10;i++)
{
cout<<num<<" X "<<i<<" = "<<num*i<<endl;
}
return 0;
}
OUTPUT:

PROGRAM 3

AIM: To check whether the given number is even or odd

CODE:
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
int n,j;
cout<<"ENTER n=";
cin>>n;
for(int i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
cout<<" ";

}
for(j=1;j<=(2*i-1);j++)
{
cout<<"* ";
}
cout<<endl;
}
for(int i=n;i>=1;i--)
{
for(j=1;j<=n-i;j++)
{
cout<<" ";

}
for(j=1;j<=(2*i-1);j++)
{
cout<<"* ";
}
cout<<endl;
}
}

PROGRAM 4
AIM: To check whether the given number is positive or negative.
CODE:
#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"ENTER A NUMBER=";
cin>>num;
if(num<0)
{
cout<<"\nTHE NUMBER IS NEGATIVE";
}
else{cout<<"\nTHE NUMBER IS POSITIVE";}
return 0;
}

OUTPUT:
PROGRAM-5
AIM: - To print greatest no. using ternary operator.
CODE:
#include<iostream>
using namespace std;
int main()
{
int a, b;
cout <<" enter the values of a & b respectively" ;
cin >> a;
cin >> b;
string greatest = (a>b)? "a is greatest" : "b is greatest";
cout<< greatest;

}
PROGRAM-6
AIM: - To generate the list of leap years 1900 to 2008.
CODE:
#include<iostream>
using namespace std;
int main() {
int year=1900 ;
cout<<"List of leap years between 1900 and 2008"<<endl;
for(year=1900;year<=2008;year++)
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
cout<<year<<"\n";
else
cout<<" " ;
return 0;
}
PROGRAM-7
AIM:- write a program of class student & display the data of a student of a
class having students
Write a class student having the following
Data types—name, Roll number, marks, percentage &
Functions:- readdata(),displaydata(),computedata()
CODE:
#include <iostream>
using namespace std;

#define MAX 10

class student
{
private:
char name[30];
int rollNo;
int total;
float perc;
public:
void readdata(void);
void displaydata(void);
void computedata(void);
};

void student::readdata(void){
cout << "Enter name: " ;
cin >> name;
cout << "Enter roll number: ";
cin >> rollNo;
cout << "Enter total marks out of 500: ";
cin >> total;

void student::displaydata(void){
cout << "Name:"<< name<<"\n" << "Roll Number: " << rollNo <<"\n"<<
"Total:" << total<<"\n" ;
}
void student::computedata(void){
perc=(float)total/500*100;
cout << "Percentage of student:" << perc<<"\n";
}

int main()
{
student std[MAX];
int n,i;

cout << "Enter total number of students: ";


cin >> n;

for(i=0;i< n; i++){
cout << "Enter details of student " << i+1 << ":\n";
std[i].readdata();
}

cout << endl;

for(i=0;i< n; i++){
cout << "Details of student " << (i+1) << ":\n";
std[i].displaydata();
}
for(i=0;i<n;i++){
std[i].computedata();
}

return 0;
}

PROGRAM-8
AIM:- WAP of the class Cylinder having the following
Data types:- length , height , pi=3.14 &
Functions:- calculate volume(), surface area (),
Area () of cylinder
Using Scope Resolution Operator (::).
CODE
#include<iostream>

using namespace std;

class cylinder{
public:
int radius,height;
const float pi = 3.14;
double volume();
double area();
double surface_area();
}b;

double cylinder :: volume(){


return (pi*radius*radius*height);
}
double cylinder :: surface_area(){
return ((2*pi*radius*radius) + (2*pi*radius*height));
}
int main()
{
double vol = 0;
double surface = 0;
cout<<"enter the radius of the cylinder ";
cin>>b.radius;
cout<<"enter the height of the cylinder";
cin>>b.height;
vol= b.volume();
cout<<"Volume "<<vol<<endl;
surface= b.surface_area();
cout<<"Surface Area "<<surface<<endl;
return 0;
}

PROGRAM-9
AIM:- Write a program to show the constructor overloading.
CODE:
#include <iostream>
using namespace std;
class room{
private:
double length;
double breadth;

public:
room() {
length = 6.9;
breadth = 4.2;
}

room (double l, double b) {


length = l;
breadth = b;
}

room(double len) {
length = len;
breadth = 7.2;
}

double calculatearea() {
return length*breadth;
}
};

int main()
{
room room1,room2(8.2,6.6),room3(8.2);
cout<<"when no argument is passed:"<<endl;
cout<<"area of room ="<<room1.calculatearea()<<endl;
cout<<"\n when(8.2,6.6) is passed:"<<endl;
cout<<"area of room ="<<room2.calculatearea()<<endl;
cout<<"\n when breadth is fixed to 7.2 and (8.2) is passed:"<<endl;
cout<<"area of room ="<<room3.calculatearea()<<endl;
return 0;
}

PROGRAM-10
AIM:- WAP to calculate the square of a function using inline function.
CODE:-
#include <iostream>
using namespace std;
inline int sqr (int x)
{
return x*x;

}
int main()
{
int x;
cout<<"enter number: ";
cin>>x;
cout<<"square of number: "<<sqr(x);
return 0;
}
PROGRAM-11
AIM:- Write a program to show the copy constructor.
CODE:
include <iostream>
using namespace std;
class wall{
private:
double length;
double height;

public:
wall(double len, double hgt)
{
length= len;
height= hgt;
}

wall(wall &obj){

length= obj.length;
height= obj.height;
}

double calculatearea()
{
return length*height;
}
};
int main() {

wall wall1 (1876.6,8878.96);


cout<<"area of wall 1: "<<wall1.calculatearea()<<endl;
wall wall2= wall1;

cout<<"area of wall 2: "<<wall2.calculatearea()<<endl;

return 0;
}
PROGRAM – 12
AIM- write a program for dynamic memory allocation of an object using new
operator.
CODE:

#include <iostream>
using namespace std;
int main ()
{
int* B = NULL;
B = new int;
if (!B)
cout<<"Allocation of memory failed.\n";
else
{
*B = 67;
cout<<"Value of B: "<<*B<<endl;
}
float *A = new float;
*A = 95.55;
cout<<"Value of A: "<<*A<<endl;
delete B;
delete A;
return 0;
}
PROGRAM – 13
Aim:- write a program to create dynamic array using new and delete
operators.
CODE:
#include <iostream>
using namespace std;
int main ()
{
int n;
cout<<"Enter size of array: ";
cin>>n;
int *q = new int[n];
if (!q)
cout<<"Allocation of memory failed.\n";
else
{
for(int i=0;i<n;i++)
q[i] = i*i;
cout<<"Values stored in block of memory: ";
for (int i=0;i<n;i++)
cout<<q[i]<<" ";
}
delete[] q;
return 0;
}
PROGRAM -14
AIM:- write a program to show this pointer.
CODE:
#include<iostream>
using namespace std;
class Test
{private:
int x;
int y;
public:
Test(int x = 0, int y = 0)
{this->x = x;
this->y = y;
}
Test &setX(int a)
{ x = a;
return *this;
}
Test &setY(int b)
{ y = b;
return *this;
} void print()
{
cout << "x = " << x << " y = " << y << endl;
}
};

int main()
{
Test obj1(5, 5);
obj1.print();
obj1.setX(10).setY(20);
obj1.print();
return 0;
}
PROGRAM-15
Aim- write a program to count number of objects using static counter,
constructor and destructor.
CODE:
#include <iostream>
using namespace std;

class student{int rno;


static int objcount;
public:
student(){
rno = ++objcount;
}
~student(){
--objcount;
}
void printnum(){
cout<<"Object number= "<<rno<<"\n";
}
static void printobjcountfinal(){
cout<<"Count= "<<objcount<<"\n";
}
};
int student::objcount;
int main()
{
student s1,s2;
student::printobjcountfinal();
student s3;
student::printobjcountfinal();
s1.printnum();
s2.printnum();
s3.printnum();

return 0;
}
PROGRAM-16
AIM:- write a program to show the multiple inheritance
CODE:
#include<iostream>
using namespace std;
class btech{
public:
btech(){cout<<"btech is an engineering course" << endl;}
};
class information_technology{
public:
information_technology(){cout<<"information technology is a branch of
btech"<<endl;}
};
class student: public btech, public information_technology {};
int main(){
student s1;
return 0;
}
PROGRAM-17
AIM :- write a program for the hybrid inheritance.
Code:
#include <iostream>
using namespace std;

class A
{
public:
int x;
};
class B : public A
{
public:
B()
{
cout << "enter first number";
cin>> x ;
}
};
class C
{
public:
int y;
C()
{
cout <<"enter second number";
cin>>y;
}
};
class D : public B, public C
{
public:
void sum()
{
cout << "Sum= " << x + y;
}
};

int main()
{
D obj1;
obj1.sum();
return 0;
}
PROGRAM-18
AIM:- Write a program for constructor and destructor in multiple inheritance
CODE:
#include <iostream>
using namespace std;

class base1
{
public:
base1 (void)
{
cout << " constructor of class base1\n";

}
~base1 ()
{
cout << " destructor of class base1\n";

};

class base2
{
public:
base2(void)
{
cout << " constructor of class base2\n";

}
~base2()
{

cout << " destructor of class base2\n";

};
class derive1 : public base1, public base2
{

public:
derive1(void)
{
cout << " constructor of class derive1\n";

}
~derive1()
{
cout << " destructor of class derive1\n";

}
};

main ()
{
derive1 x;
cout << " Destructors are: "<<endl;

PROGRAM:- 19
AIM:- Write a program to implement parameterized constructor in multilevel
inheritance.
CODE:
#include <iostream>
using namespace std;
class Student
{

string name;
float percent;

int Chemistry;
int Physics;
int Maths;

public:

Student()
{
name = "Abc";
percent = 0;
Chemistry = 0;
Physics = 0;
Maths = 0;
}

Student(string name,int Physics, int Chemistry, int Maths)


{
this->name = name;
this->Chemistry = Chemistry;
this->Physics = Physics;
this->Maths = Maths;
percent = ( Maths + Chemistry + Physics ) / 3;
}
void display()
{
cout<<" Name: "<<name<<endl;
cout<<" Maths: "<<Maths<<endl;
cout<<" Physics: "<<Physics<<endl;
cout<<" Chemistry: "<<Chemistry<<endl;
cout<<" Percentage: "<<percent<<endl;
cout<<endl;
}
};

int main()
{
int num;

cout<<" Enter number of students: ";


cin>>num;

Student *snum = new Student[num];

string Name;
int maths,phy,chem;
cout<<" Enter details in following format: "<<endl;

for(int i =0; i<num; i++)


{
cout<<" \n Name: "; cin>>Name;
cout<<" Physics: "; cin>>phy;
cout<<" Chemistry: "; cin>>chem;
cout<<" Maths: "; cin>>maths;
snum[i] = Student(Name,phy,chem,maths);
}

cout<<"\n Student details: \n"<<endl;

for(int i =0; i<num; i++)


snum[i].display();

delete[]snum;
return 0;
}
PROGRAM-20
AIM:- Write a program to overload increment operator in both prefix and
postfix
CODE:
#include <iostream>
using namespace std;

class Integer {
private:
int value;
public:
Integer(int v) : value(v) { }
Integer operator++();
Integer operator++(int);
int getValue() {
return value;
}
};

Integer Integer::operator++()
{
value++;
return *this;
}

Integer Integer::operator++(int)
{
const Integer old(*this);
++(*this);
return old;
}

int main()
{
Integer i(10);

cout << "Post Increment Operator" << endl;


cout << "Integer++ : " << (i++).getValue() << endl;
cout << "Pre Increment Operator" << endl;
cout << "++Integer : " << (++i).getValue() << endl;
return 0;
}

PROGRAM-21
AIM:- WAP to count number of objects using static counter, constructor and
destructor.
CODE:
#include<iostream>
using namespace std;
class student
{
int rno;
static int objcount;
public: student()
{
rno = ++objcount;
}
~student()
{
--objcount;
}
void printnum()
{
cout<<"Object number= "<<rno<<"\n";
}
static void printobjcountfinal()
{
cout<<"Count= "<<objcount<<"\n";
}
};
int student::objcount;
int main()
{
student s1,s2;
student::printobjcountfinal();
student s3;
student::printobjcountfinal();
s1.printnum();
s2.printnum();
s3.printnum();
return 0;
}

You might also like