0% found this document useful (0 votes)
87 views59 pages

Ankush Oops File.

The document contains programs demonstrating various OOP concepts in C++ like inheritance, polymorphism, encapsulation etc. It includes 12 programs with documentation of each program, input/output and screenshots of output. The programs cover concepts like call by reference, finding min/max in array, student class, static data members, access specifiers, constructor overloading, operator overloading, destructor, multiple inheritance and multilevel inheritance.

Uploaded by

Anonymous BLVYSw
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)
87 views59 pages

Ankush Oops File.

The document contains programs demonstrating various OOP concepts in C++ like inheritance, polymorphism, encapsulation etc. It includes 12 programs with documentation of each program, input/output and screenshots of output. The programs cover concepts like call by reference, finding min/max in array, student class, static data members, access specifiers, constructor overloading, operator overloading, destructor, multiple inheritance and multilevel inheritance.

Uploaded by

Anonymous BLVYSw
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/ 59

B.

Tech/CSE/3rd 18BT110302

PRACTICAL NO-1
PROGRAM:-Write a program in C++ to exchange the content of
two variables using call by reference

#include<iostream.h>
#include<conio.h>
class reff
{
public:
void swap(int *a, int *b)
{
int t;
t = *a;
*a = *b;
*b = t;
}
}
void main()
{
int x,y;
clrscr();
cout.width(40);
cout<<"CALL BY REFRENCE"<<endl;
cout<<"enter the 1 value=";
cin>>x;
cout<<"enter the 2 value=";
cin>>y;

Page 1
B.Tech/CSE/3rd 18BT110302

cout<<"x="<<x<<endl<<"y="<<y<<endl;
reff r;
r.swap(&x,&y);
cout<<"x="<<x<<endl<<"y="<<y;
getch();
}

Page 2
B.Tech/CSE/3rd 18BT110302

OUTPUT:-

Page 3
B.Tech/CSE/3rd 18BT110302

PRACTICAL NO - 2
PROGRAM:- Write a program in C++ to search the 2nd largest &
smallest element in an array.
#include<iostream.h>
#include<conio.h>
class found
{
public:
int a[15],n,first,second,last,i;
void putdata()
{
cout<<"enter the size of array="<<endl;
cin>>n;
cout<<"enter elements of array="<<endl;
for(i=0; i<n; i++)
{
cin>>a[i];
}
}
void search_data(void);
}
void found :: search_data()
{
first=a[0];
for(i=0;i<n;i++)
{

Page 4
B.Tech/CSE/3rd 18BT110302

if(a[i]>first)
first=a[i];
}
second=a[0];
for(i=0;i<n;i++)
{
if(a[i]>second && a[i]<first)
second=a[i];
}
last=a[0];
for(i=0;i<n;i++)
{
if(a[i]<last)
last=a[i];
}
cout<<"the second largest element in the list="<<second<<endl;
cout<<"the smallest element in the list="<<last<<endl;
}
void main()
{
clrscr();
cout.width(40);
cout<<"Found"<<endl;
found x;
x.putdata();
x.search_data();
getch();
}

Page 5
B.Tech/CSE/3rd 18BT110302

OUTPUT:-

Page 6
B.Tech/CSE/3rd 18BT110302

PRACTICAL NO-3
PROGRAM:- Write a C++ program to implement a student class
having roll no., name, rank, addresses as data members.
#include<iostream.h>
#include<conio.h>
class student
{
int rollno,rank;
char name[20],address[20];
public:
void getdata();
void putdata()
{
cout<<"\n my rollno is:"<<rollno;
cout<<"\n my rank:"<<rank;
cout<<"\n my name:"<<name;
cout<<"\n address:"<<address;
}
}
void student::getdata()
{
cout<<"my rollno is=";
cin>>rollno;
cout<<"my rank=";
cin>>rank;
cout<<"my name=";

Page 7
B.Tech/CSE/3rd 18BT110302

cin>>name;
cout<<"my address=";
cin>>address;
}
void main()
{
clrscr();
cout.width(40);
cout<<"Student"<<endl;
student s;
s.getdata();
s.putdata();
getch();
}

Page 8
B.Tech/CSE/3rd 18BT110302

OUTPUT:-

Page 9
B.Tech/CSE/3rd 18BT110302

PRACTICAL NO-4
PROGRAM: Write a program in C++ demonstrating the Static
Data member.
#include<iostream.h>
#include<conio.h>
class demo
{
private:
int a,b;
static int c;
public:
void setdata(int x, int y)
{
a=x;
b=y;
cout<<"a="<<a<<endl<<"b="<<b<<endl;
}
static void getdata(int z)
{
c=z;
cout<<"c="<<c<<endl;
}
}
int demo::c;
void main()
{

Page
10
B.Tech/CSE/3rd 18BT110302

clrscr();
cout.width(40);
cout<<"Demonstrate The Static Members"<<endl;
demo d;
d.setdata(3,4);
demo::getdata(2.5);
getch();
}

Page
11
B.Tech/CSE/3rd 18BT110302

OUTPUT:-

Page
12
B.Tech/CSE/3rd 18BT110302

PRACTICAL NO-5
PROGRAM: Write a program in C++ demonstrating the public,
protected and private parameters.
#include <iostream.h>
#include<conio.h>
class Example
{
private:
int val;
public:
//function declarations
void init_val(int v);
void print_val();
}
//function definitions
void Example::init_val(int v)
{
val=v;
}
void Example::print_val()
{
cout<<"val: "<<val<<endl;
}
int main()
{
//create object
clrscr();
Example Ex;
Ex.init_val(100);
Ex.print_val();
getch();
}

Page
13
B.Tech/CSE/3rd 18BT110302

OUTPUT:-

Page
14
B.Tech/CSE/3rd 18BT110302

PRACTICAL NO-6
PROGRAM: Write a program in C++ to demonstrate constructor
with default argument.
#include<iostream.h>
#include<conio.h>
class cons
{
int a;
public:
cons()
{
cout<<"default constructor";
}
}
void main()
{
clrscr();
cout.width(40);
cout<<"Constructor"<<endl;
cons c;
getch();
}

Page
15
B.Tech/CSE/3rd 18BT110302

OUTPUT:-

Page
16
B.Tech/CSE/3rd 18BT110302

PRACTICAL NO-7
PROGRAM: Write a program in C++ to demonstrate the
Constructor Overloading, assume desired parameters.

#include<iostream.h>
#include<conio.h>
class cons_ovr
{
int a,b;
public:
cons_ovr()
{
cout<<"default constructor"<<endl;
}
cons_ovr(int x)
{
a=x;
cout<<"a="<<a<<endl;
}
cons_ovr(int x,int y)
{
a=x;
b=y;
cout<<"a="<<a<<endl<<"b="<<b;
}
}
void main()
{
clrscr();
cout.width(40);
cout<<"Constructor Overloading"<<endl;
cons_ovr x,x1=3,x2(5,6);
getch();
}

Page
17
B.Tech/CSE/3rd 18BT110302

OUTPUT:-

Page
18
B.Tech/CSE/3rd 18BT110302

PRACTICAL NO- 8
PROGRAM: Write a program in C++ to create the class shape, and
overload the function to return the parameters of the different
shapes.
#include<iostream.h>
#include<conio.h>
class shape
{
int a,b;
public:
void shapeb(int x,int y)
{
a=x;
b=y;
}
void show_display()
{
cout<<"a="<<a<<endl<<"b="<<b<<endl;
}
shape add_mul(shape c)
{
shape temp;
temp.a=a+c.a;
temp.b=b*c.b;
return(temp);
}

Page
19
B.Tech/CSE/3rd 18BT110302

shape operator++()
{
shape temp;
temp.a=++a;
temp.b=++b;
return(temp);
}
}
void main()
{
clrscr();
cout.width(40);
cout<<"Program for operator overloading"<<endl;
shape a1,a2,a3,a4;
a1.shapeb(2,3);
a2.shapeb(4,5);
a3=a1.add_mul(a2);
a3.show_display();
a4=a2.operator++();
a4.show_dispaly();
getch();
}

Page
20
B.Tech/CSE/3rd 18BT110302

OUTPUT:-

Page
21
B.Tech/CSE/3rd 18BT110302

PRACTICAL NO- 9
PROGRAM: Write a program in C++ to demonstrate destructor in
inheritance.
#include<iostream.h>
#include<conio.h>
class useful
{
public:
int a;
useful(int x)
{
a=x;
cout<<"a="<<a<<endl;
}
~useful()
{}
}
class vain:public useful
{
public:
int c;
vain(int x,int y):useful(y)
{
c=x;
cout<<"c="<<c<<endl;
}

Page
22
B.Tech/CSE/3rd 18BT110302

~vain()
{}
}
void main()
{
clrscr();
cout.width(45);
cout<<"Program for destructor inheritance"<<endl;
vain k1(7,5);
getch();
}

Page
23
B.Tech/CSE/3rd 18BT110302

OUTPUT:-

Page
24
B.Tech/CSE/3rd 18BT110302

PRACTICAL No-10
PROGRAM: Write a Program in C++ to Demonstrate Multiple
Inheritance
#include<iostream.h>
#include<conio.h>
class A
{
public:
void show_star()
{
cout<<”****”;
}
}
class B
{
public:
void show_hash()
{
cout<<”####”;
}
}
class C:public A,public B
{
void show_dollar()
{
cout<<”$$$$$”;

Page
25
B.Tech/CSE/3rd 18BT110302

}
}
void main()
{
clrscr();
A obj1;
B obj2;
C obj3;
cout.width(45);
cout<<”Multiple Inheritance”<<endl;
obj1.show_star();
obj2.show_hash();
obj3.show_dollar();
obj2.show_hash();
getch();
}

Page
26
B.Tech/CSE/3rd 18BT110302

OUTPUT:-

Page
27
B.Tech/CSE/3rd 18BT110302

PRACTICAL-11
PROGRAM: Write a Program in C++ to demonstrate the Multilevel
Inheritance
#include<iostream.h>
#include<conio.h>
class A
{
public:
void show_star()
{
cout<<”****”;
}
}
class B:public A
{
public:
void show_hash()
{
cout<<”####”;
}
}
class C:public B
{
void show_dollar()
{
cout<<”$$$$$”;
}

Page
28
B.Tech/CSE/3rd 18BT110302

}
void main()
{
clrscr();
A obj1;
B obj2;
C obj3;
cout.width(45);
cout<<”Multilevel Inheritance”<<endl;
obj1.show_star();
obj2.show_hash();
obj3.show_dollar();
obj2.show_hash();
getch();
}

Page
29
B.Tech/CSE/3rd 18BT110302

OUTPUT:-

Page
30
B.Tech/CSE/3rd 18BT110302

PRACTICAL NO-12
PROGRAM :Write a Program in C++ to demonstrate
public,private and protected inheritance.
#include<iostream.h>
#include<conio.h>
class A
{
private:
int a,b ;
protected:
void getdata(int x,int y)
a=x;
b=y;
cout<<”a=”<<a<<” “<<”b=”<<b<<endl;
}
};
class B:public A
{
public:
void setdata(int x,int y)
{
getdata(x,y);
}
};

Page
31
B.Tech/CSE/3rd 18BT110302

void main()
{
clrscr();
cout.width(45);
cout<<”Public , Private and Protected Inheritance”<<endl;
B B1;
B1.setdata(4,5);
getch();
}

Page
32
B.Tech/CSE/3rd 18BT110302

OUTPUT:-

Page
33
B.Tech/CSE/3rd 18BT110302

PRACTICAL-13
PROGRAM- Write a program in C++ to demonstrate the virtual
function
#include<iostream.h>
#include<conio.h>
class base
{
public:
base(int x)
{
I=x;
}
virtual void function()
{
cout<<”Using base version of function():”;
cout<<i<<”\n”;
}
}
class derived1:public base
{
public:
derived(int x):base(x){ }
void function()
{
cout<<”using derived 1’s version of function():”;

Page
34
B.Tech/CSE/3rd 18BT110302

cout<<i+i<<”\n”;
}
}
class derived2:public base
{
public:
derived2(int x) :base(x){ }
void function()
{
cout<<”using derived2’s version of function():”;
cout <<i*i<<”\n”;
}
}
void main()
{
clrscr();
cout.width(45);
cout<<”Virtual Function”
base *p;
int num:
cout<<”\n Enter Some Number:”;
cin>>num;
base obj(num);
derived1 d1_obj(num);
derived2 d2_obj(num);

Page
35
B.Tech/CSE/3rd 18BT110302

p=&obj;
p->function();
p=&d1_obj;
p->function();
p=&d2_obj;
p->function();
getch();
}

Page
36
B.Tech/CSE/3rd 18BT110302

OUTPUT:-

Page
37
B.Tech/CSE/3rd 18BT110302

PRACTICAL NO-14
PROGRAM: Write a program in C++ to demonstrate friend
function.
#include<iostream.h>
#include<conio.h>
class demo;
class frnd
{
int a;
public:
void setdata(int m)
{
a=m;
}
friend void getdata(frnd,demo);
}
class demo
{
int b;
public:
void setdata(int n)
{
b=n;
}
friend void getdata(frnd,demo);
}
void getdata(frnd x, demo y)
{
cout<<"Sum of these="<<x.a+y.b;
}
void main()
{
clrscr();
cout.width(40);

Page
38
B.Tech/CSE/3rd 18BT110302

cout<<"Demonstrate The Friend Function"<<endl;


frnd f;
f.setdata(6);
demo d;
d.setdata(5);
getdata(f,d);
getch();
}

Page
39
B.Tech/CSE/3rd 18BT110302

OUTPUT:-

Page
40
B.Tech/CSE/3rd 18BT110302

PRACTICAL NO-15
Program: write a Program to demonstrate the function overriding.
#include<iostream.h>
#include<conio.h>
class base
{
public:
void getdata()
{
cout<<”Base class Get data”<<endl;
}
void setdata()
{
cout<<”Base class set data”<<endl;
}
}
class derived1:public base
{
public:
void get data()
{
cout<<”Derived class get data”<<endl;
}
}
void main()

Page
41
B.Tech/CSE/3rd 18BT110302

{
clrscr();
cout.width(45);
cout<<”Function overriding”<<endl;
derived1 d1;
d1.getdata();
d1.setdata();
getch();
}

Page
42
B.Tech/CSE/3rd 18BT110302

OUTPUT:-

Page
43
B.Tech/CSE/3rd 18BT110302

PRACTICAL NO-16
Program: c
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
int main()
{
ifstream fin;
fin.open("data.txt",ios::in);
ofstream fout;
fout.open("new.txt",ios::out);
char ch;
char line[75];
int i = 1;
while(fin.get(ch))
{
fin.get(line,75,'.');
fout<<"Line "<<i<<" : "<<line<<endl;
i++;
}
fin.close();
cout<<"done"<<endl;
getch();
}

OUTPUT:-

Page
44
B.Tech/CSE/3rd 18BT110302

Page
45
B.Tech/CSE/3rd 18BT110302

PRACTICAL NO-17
Program: Write a C++ program implement a class 'Complex' of
complex numbers. The class should be include member functions to
add and subtract two complex numbers.
#include <iostream.h>
#include<conio.h>
class Complex
{
private:
int real;
int imag;
public:
Complex(int r = 0, int i = 0): real(r), imag(i) {};
void setComplex(void)
{
cout << "Enter the real and imaginary parts : ";
cin >> this->real;
cin >> this->imag;
}
Complex add(const Complex& c)
{
Complex comp;
comp.real = this->real + c.real;
comp.imag = this->imag + c.imag;
return comp;
}

Page
46
B.Tech/CSE/3rd 18BT110302

Complex subtract(const Complex& c)


{
Complex comp;
comp.real = this->real - c.real;
comp.imag = this->imag - c.imag;
return comp;
}
void printComplex(void)
{
cout << "Real : " << this->real << endl
<< "Imaginary : " << this->imag << endl;
}
}
int main()
{
Complex a, b, c, d;
cout << "Setting first complex number " << endl;
a.setComplex();
cout << "Setting second complex number " << endl;
b.setComplex();

Page
47
B.Tech/CSE/3rd 18BT110302

/* Adding two complex numbers */


cout << "Addition of a and b : " << endl;
c = a.add(b);
c.printComplex();
/* Subtracting two complex numbers */
cout << "Subtraction of a and b : " << endl;
d = a.subtract(b);
d.printComplex();
getch();
}

Page
48
B.Tech/CSE/3rd 18BT110302

OUTPUT:-

Page
49
B.Tech/CSE/3rd 18BT110302

PRACTICAL NO-18
Program: Write a C + + program to implement matrix class. Add
member function to transpose the matrix.
#include <iostream.h>
#include<stdlib.h>

#include<conio.h>
class matrix3
{
int a[10][10],b[10][10],c[10][10],d[10][10],e[10][10],f[10][10],x,y,i,j;
public :
void values();
void transpose();
void sum();
void diff();
}
void matrix3::values()
{
cout << "Enter the rows "; cin >> x;
cout << "Enter the columns "; cin >> y;
cout << "Enter elements of first matrix\n\n";
for (i=1; i<=x; i++)

Page
50
B.Tech/CSE/3rd 18BT110302

{
for ( j=1; j<=y; j++)
{
cin >> a[i][j];
}
}
cout << "Enter elements of second matrix\n\n";
for (i=1; i<=x; i++)
{
for (j=1; j<=y; j++)
{
cin >> c[i][j];
}
}
}
void matrix3::sum()
{
cout << "Sum of Matrices 1 and 2 is\n";
for (i=1; i<=x; i++)
{
for ( j=1; j<=y; j++)
{
e[i][j]=a[i][j]+c[i][j];
cout << e[i][j] << "";
}

Page
51
B.Tech/CSE/3rd 18BT110302

cout << endl;


}
}
void matrix3::diff()
{
cout << "Difference of Matrices 1 and 2 (1-2) is\n";
for (i=1; i<=x; i++)
{
for ( j=1; j<=y; j++)
{
f[i][j] = a[i][j]-c[i][j];
cout << f[i][j] << "";
}
cout << endl;
}
}
void matrix3::transpose()
{
cout << "transpose of the matrix is\n";
for ( i=1; i<=x; i++)
{
for ( j=1; j<=y; j++)
{
b[i][j] = a[j][i];
cout << b[i][j] << "";

Page
52
B.Tech/CSE/3rd 18BT110302

}
cout << endl;
}
cout << "Transpose of the second matrix is\n";
for ( i=1; i<=x; i++)
{
for ( j=1; j<=y; j++)
{
d[i][j] = c[j][i];
cout << b[i][j] << "";
}
cout << endl;
}
}
int main()
{
int input;
char ch;
matrix3 m;
m.values();
do
{
cout << "Enter your choice\n";
cout << " 1. Sum of 1 and 2\n" << " 2. Difference of 1 and 2\n" << " 3. Transpose
of both 1 amd 2\n";
cin >> input;

Page
53
B.Tech/CSE/3rd 18BT110302

switch (input)
{
case 1:
m.sum();
break;

case 2:
m.diff();
break;

case 3:
m.transpose();
break;
}
cout << "\nDo another y/n?";
cin >> ch;
}
while (ch!= 'n');
cout << "\n";
system ("pause");
cin>>"%d";
return 0;
}

Page
54
B.Tech/CSE/3rd 18BT110302

OUTPUT:-

Page
55
B.Tech/CSE/3rd 18BT110302

Page
56
B.Tech/CSE/3rd 18BT110302

PRACTICAL NO-19
Program: Write a C ++ program to implement a class for complex numbers
with add and multiply as member functions. Overload ++ operator to
increment a complex number.
#include<iostream.h>
#include<conio.h>
class A
{
int a,b;
public:
A(){};
A(int i,int j)
{
a=i;
b=j;
}
void show()
{
cout<<a<<"+i"<<b;
}
A operator +(A);
}
A A::operator +(A obj)
{
A temp;

Page
57
B.Tech/CSE/3rd 18BT110302

temp.a=a+obj.a;
temp.b=b+obj.b;
return(temp);
}
int main()
{
A c1(5,6),c2(7,8),c3;
cout<<"The 1st no. is:";
c1.show();
cout<<"\nThe 2nd no. is:";
c2.show();
c3=c1+c2;
cout<<"\nSum is:";
c3.show();
getch();
}

Page
58
B.Tech/CSE/3rd 18BT110302

OUTPUT:-

Page
59

You might also like