0% found this document useful (0 votes)
28 views30 pages

MTECH OOT 1stsem

This document is a lab manual for Object Oriented Technology at Gyan Ganga Institute of Technology and Sciences, Jabalpur, detailing various C++ programming experiments. It includes a list of practical exercises covering topics such as dynamic memory allocation, inheritance, operator overloading, and constructors. Each experiment is accompanied by example code and expected input/output results.
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)
28 views30 pages

MTECH OOT 1stsem

This document is a lab manual for Object Oriented Technology at Gyan Ganga Institute of Technology and Sciences, Jabalpur, detailing various C++ programming experiments. It includes a list of practical exercises covering topics such as dynamic memory allocation, inheritance, operator overloading, and constructors. Each experiment is accompanied by example code and expected input/output results.
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
You are on page 1/ 30

GYAN GANGA INSTITUTE OF TECHNOLOGY AND SCIENCES,

JABALPUR
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
LAB MANUAL

Object Oriented Technology


(MCSE-104)

NAME:

ENROLLMENT NUMBER:

TEACHER IN CHARGE: -
DECEMBER 2024
INDEX
S.NO Name Of Experiments Date Remarks
1. Write a C++ program to find the sum of
individual digits of a positive integer.
2. Write a C++ Program to find both the
largest and smallest number in a list of
integers.
3. Write a C++ program to sort a list of
numbers in ascending order.
4. Write a Program to illustrate New and
Delete Keywords for dynamic memory
allocation.
5. Write a C++ Program to illustrate default
constructor, parameterized constructor and
copy constructors.
6. a) Write a program to demonstrate the i)
Operator Overloading ii) Function
Overloading.
b) Write a Program to demonstrate friend
function and friend class.
7. Write a C++ Program that illustrate single
inheritance.
8. Write a C++ Program that illustrate multiple
inheritance
9. Write a C++ Program that illustrate
multilevel inheritance.

10. Write a C++ program to illustrate the order


of execution of constructors and
destructors.
Practical 1
Write a C++ program to find the sum of individual digits of a
positive integer
Program:-
#include<iostream.h>
intsum_of_digits(int n)
{
intdigit, sum =0;
while (n!= 0)
{
digit=n%10;
sum=sum + digit;
n=n/10;
}
return sum;
}
int main ()
{
intnumber, digits_sum;
cout<<"Enter Positive integer within the range:";
cin>>number;
digits_sum=sum_of_digits(number);
cout<<"sum of digts of "<<number<<" is "<<digits_sum;
return 0;
}
Input: Enter Positive integer within the range:4321
Output:
sum of digits of 4321 is 10
Practical-2
Write a C++ Program to find both the largest and smallest
number in a list of integers.
Program:
#include<iostream.h>
int main ()
{
int a[50], i, n, small, large;
cout<<"Enter The Array Size:";
cin>>n;
cout<<"ENTER ELEMENTS OF ARRAY";
for(i=0;i<n;i++)
cin>>a[i];
small=a[0];
large=a[0];
for(i=0;i<n;i++)
{
if(a[i]<small) small=a[i];
if(a[i]>large) large=a[i];
}
cout<<"largest value is"<<large<<endl;
cout<<"smallest value is:"<<small<<endl;
return 0;
}
Input:
Enter The Array Size:5
ENTER ELEMENTS OF ARRAY5 4 3 2 1
Output:
largest value is5
smallest value is:1
Practical 3
Write a C++ program to sort a list of numbers in ascending
order.
Program:
#include<iostream.h>
void sort(int data[],int n)
{
for(int i=0;i<n;i++)// read the elements of an array
for(int j=0;j<n-1;j++)
{
int t;
if(data[j]>data[j+1])
{
t=data[j];
data[j]=data[j+1];
data[j+1]=t;
}
}
}
int main() {
int a[50],i,n;
cout<<"Enter How many elements to sort:";
cin>>n;
cout<<"Enter Elements:";
for(i=0;i<n;i++) // read the elements of an array
cin>>a[i];
cout<<"Sorted array is \n";
for(i=0;i<n;i++)
cout<<a[i]<<"\t";
return 0;
}
Input:
Enter How many elements to sort:5
Enter Elements5 4 3 2 1
Output:
Sorted array is 5 4 3 2 1
Practical-4
Write a Program to illustrate New and Delete Keywords for
dynamic memory allocation.
Program
#include<iostream.h>
int sum(int *a,int n)
{
int s=0;
for(int i=0;i<n;i++)
s=s+*(a+i);
return s;
}
int main()
{
int *p,i,n;
cout<<"enter how many values to be read:";
cin>>n;
p=new int[n];
cout<<"Enter values :";
for(int i=0;i<n;i++)
cin>>p[i];
intArray_sum=sum(p,n);
cout<<"sum of all values are "<<Array_sum;
return 0;
}
Input:
enter how many values to be read:4
Enter values :1 2 3 4
Output: sum of all values are 10
Practical-5
Write a C++ Program to illustrate default constructor,
parameterized constructor and copy constructors.
Program:
#include<iostream.h>
class code
{
int id;
int count;
public: code()
{
cout<<"Default constructor called\n";
id=0;
cout<<"id="<<id<<endl;
}
code(int a)
{
cout<<"Parameterized constructor called\n";
id=a;
cout<<"id="<<id<<endl;
}
code(code&x )
{
cout<<"copy constructor called\n";
id=x.id;
cout<<"id="<<id<<endl;
}
void display()
{
cout<<"id="<<id<<endl;
}
~code()
{
cout<<"Object Destroyed"<<endl;
}
}
int main()
{
code a(100);//calls parameterized constructor
code b(a); //calls copy constructor
code c(a); //calls copy constructor
code d;//calls default constructor
cout<<"\n For object d id=";
d.display();
cout<<"\n For object a id=";
a.display();
cout<<"\n For object b id=";
d.display();
cout<<"\n For object c id=";
d.display();
return 0;
}
Output:
Parameterized constructor called id=100
copy constructor called id=100
copy constructor called id=100
Default constructor called id=0
For object d id=id=0
For object a id=id=100
For object b id=id=0
For object c id=id=0
Object Destroyed
Object Destroyed
Object Destroyed
Object Destroyed
Practical -6
a)Write a program to demonstrate the
i)Operator Overloading
ii)Function Overloading.

i)Operator Overloading:-The mechanism of giving a special


meaning to an operator is called operator overloading. This
can be achieved by special function “operator” Syntax: return
type classname:: operaotor op(list of arguments)
{ ………………………………. }
Program:
#include<iostream.h>
class complex
{
floatreal,img;
public: complex();
complex(float x,float y);
voidread_complex();
complex operator+(complex);
complex operator-(complex);
void display();
};
complex::complex()
{
real=img=0;
}
complex::complex(float x,float y)
{
real=x; img=y;
}
void complex::display()
{
char sign; if(img<0)
{
sign='-'; img=-img;
}
else
{
sign='+';
}
cout<<real<<sign<<"i"<<img<<endl;
}
complex complex::operator+(complex c)
{
complex r;
r.real=real+c.real;
r.img=img+c.img;
return r;
}
complex complex::operator-(complex c)
{
complex r;
r.real=real-c.real;
r.img=img-c.img;
return r;
}
void complex::read_complex()
{
cout<<"Enter real part of complex number;";
cin>>real;
cout<<"Enter Imaginary part of complex number:";
cin>>img;
}
int main()
{
complex a;
a.read_complex();
complex b;
b.read_complex();
complex c;
c=a+b;
cout<<"After Addition of two complex numbers";
c.display();
c=a-b;
cout<<"Difference of two complex numbers";
c.display();
}
Output:
Enter real part of complex number;1
Enter Imaginary part of complex number:2
Enter real part of complex number;2
Enter Imaginary part of complex number:4
After Addition of two complex numbers 3+i6
Difference of two complex numbers-1-i2
ii)Function Overloading
#include<iostream> usingnamespacestd;
classprintData
{
public:
void print (int i)
{
cout<<"Printing int: "<< i <<endl;
}
voidprint(double f)
{
cout<<"Printing float: "<< f <<endl;
}
voidprint(char*c)
{
cout<<"Printing string: "<< c <<endl;
}
};
int main(void)
{
printDatapd; // Call print to print
integer pd.print(5); // Call print to print
float pd.print(500.263); // Call print to print
character pd.print("Hello C++");
return0;
}
Output:
Printingint:5
Printingfloat:500.263
Printing string:Hello C++
b) Write a Program to demonstrate friend function and friend
class.
Program: #include<iostream>
using namespace std;
class sample2;
class sample1
{
int x;
public:
sample1(int a);
friend void max(sample1 s1,sample2 s2);
};
sample1::sample1(int a)
{
x=a;
}
class sample2
{
int y;
public:
sample2(int b);
friend void max(sample1 s1,sample2 s2);
};
sample2::sample2(int b)
{
y=b;
}
void max (sample1 s1,sample2 s2)
{
If(s1.x>s2.y)
cout<<"Data member in Object of class sample1 is larger
"<<endl; else cout<<"Data member in Object of class sample2
is larger "<<endl;
}
int main()
{
sample1 obj1(3);
sample2 obj2(5);
max(obj1,obj2);
}
Output
Data member in Object of class sample2 is larger.
Practical-7
Write a C++ Program that illustrate single inheritance.
The mechanism of deriving a new class from an old one is
called inheritance or derivation class derived-class-name :
visibility-mode base-class-name { ……… ……… }
Program:
#include<iostream>
using namespace std;
class A
{
protected: inta,b;
public:
void get()
{
cout<<"Enter any two integer values";
cin>>a>>b;
}
};
class B:public A
{
int c;
public:
void add()
{
c=a+b;
cout<<a<<"+"<<b<<"="<<c;
}
};
int main()
{
B b;
b.get();
b.add();
}
Output:
Enter any two integer values1
2
1+2=3
Practical-8
Write a C++ Program that illustrate multipe inheritance.
Program:
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int rno,m1,m2;
public: void get()
{
cout<<"Enter the Roll no :";
cin>>rno;
cout<<"Enter the two marks :";
cin>>m1>>m2;
}
};
class sports
{
protected:
intsm;
public:
{
voidgetsm() // sm = Sports mark cout<<"\nEnter the sports
mark :";
cin>>sm;
}
};
classstatement:publicstudent,public sports {
inttot,avg;
public:
void display()
{
tot=(m1+m2+sm);
avg=tot/3;
cout<<"\n\n\tRoll No : "<<rno<<"\n\tTotal : "<<tot;
cout<<"\n\tAverage : "<<avg;
}
};
void main()
{
clrscr();
statementobj;
obj.get();
obj.getsm();
obj.display();
getch();
}
Output:
Enter the Roll no: 100
Enter two marks 90 80
Enter the Sports Mark: 90
Roll No: 100
Total : 260
Average: 86.66
Practical-9
Write a C++ Program that illustrate multi level inheritance.
Program:
#include<iostream.h>
#include<conio.h>
class top //base class
{
public :
int a;
voidgetdata()
{
cout<<"\n\nEnter first Number :::\t";
cin>>a;
}
voidputdata()
{
cout<<"\nFirst Number Is :::\t"<<a;
}
}; //First level inheritance
class middle :public top // class middle is derived_1
{
public: int b;
void square()
{
getdata();
b=a*a;
cout<<"\n\nSquare Is :::"<<b;
}
};
//Second level inheritance class bottom :public middle // class
bottom is derived_2
{
public: int c;
void cube()
{
square(); c=b*a; cout<<"\n\nCube :::\t"<<c;
}
};
int main()
{
clrscr();
bottom b1;
b1.cube();
getch();
}
Input
Enter first number r ::: 4
Output:
Square Is ::: 16
Cube ::: 64
Practical-10
Write a C++ program to illustrate the order of execution of
constructors and destructors.
Program:
#include<iostream.h>
class Base
{
public:
Base ( )
{
cout<< "Inside Base constructor" <<endl;
}
~Base ( )
{
cout<< "Inside Base destructor" <<endl;
}
};
class Derived : public Base
{
public:
Derived ( )
{
cout<< "Inside Derived constructor" <<endl;
}
~Derived ( )
{
cout<< "Inside Derived destructor" <<endl;
}
};
void main( )
{
Derived x;
}
Output:
Inside Base constructor
Inside Derived constructor
Inside Derived destructor
Inside Base destructor

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy