Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 25

NAME – VAIBHAV

ROLL NO – 1914651
SEMESTER – 3
SECTION – Z(E2)
SUBJECT – OOPS PRACTICAL

Write a program that uses a class where the functions are


defined inside a class .
<iostream>
mespace std;
obile

g int mobileno;
ndname[10];

etinfo()

\n Enter mobile number : ";


obileno;
\n Enter mobile brand : ";
andname;

owinfo()

\n Mobile number is :"<<mobileno;


\n mobile brand is :"<<brandname; }

()

m1;
nfo();
winfo();
;

Write a program that uses a class where the functions are


ned outside a class.
de<iostream>
amespace std;
obile

e:
ong int mobileno;
brandname[10];
c:
d getinfo();
d showinfo();

obile :: getinfo()

<"\n Enter mobile number : ";


mobileno;
<"\n Enter mobile brand : ";
brandname;

obile :: showinfo()

cout<<"\n Mobile number is :"<<mobileno;


cout<<"\n mobile brand is :"<<brandname;

n()

e m1;
etinfo();
howinfo();
n 0;
Write a program to demonstrate the use of static data
mbers.
de<iostream>
amespace std;
ne

<"\n 1's constructor called";

wo

one a;
c:

<"\n 2's constructor called";

n()

,b,c;
;
n 0;
Write a program to demonstrate the use of const data
mbers.
de<iostream>
amespace std;
emo

e:
int x;
c:
mo():x(50)

display()

<"\n value of x = "<<x ;

n()

num;
display();
n 0;

Write a program to demonstrate the use of zero argument and


parameterized constructor.
de<iostream>
amespace std;
emo

private:
int x,y;
public:
Demo()
{
x=40;
y=50;

o(int x1,int y1)

50;
y=y1;

}
int getx()
{
return x;
}
int gety()
{
return y;
}
n()

Demo d1(60,70);
Demo d2;
cout<<"\n value of d1.x = " <<d1.getx();
cout<<"\n value of d1.y = " <<d1.gety();
cout<<"\n value of d2.x = " <<d2.getx();
cout<<"\n value of d2.y = " <<d2.gety();
return 0;

Write a program to demonstrate the use of dynamic constructor .


de<iostream>
amespace std;
emo

int* d;
public:
demo()
{
d = new int[3]{1,2,3}; //allocating memory at run time
for(int i=0;i<3;i++)
{
cout<<d[i]<<" ";
}
cout<<endl;

n()

demo* obj = new demo[5];

Write a program to demonstrate the use of explicit constructor.


de<iostream>
amespace std;
mplicit

private:
int mv;
public:
Implicit(int v):mv{v}
{
cout<<"Implicit constructor called"<<endl;
}

xplicit

public:
//use explicit specifier for explicit constructor
explicit Explicit(int v):mv{v}
{
cout<<"Explicit constructor called"<<endl;
}
private:
int mv;

n()

//implicit initializtion
Implicit implicit1(1);

//Non-explicit initialization

Implicit implicit2=1;
//Explicit initialization
Explicit explicit1(1);
//explicity specifier prevents non explicit initialization
//Explicit explicit3={1};//invalid
//Explicit explicit4=1;//invalid

Write a program to demonstrate the use of initializer list.


de<iostream>
namespace std;
show

int value;
public:
show(int v):value(v)//initializer list
{
cout<<"value is "<<value;
}

ain()

show obj(10);
return 0;

Write a program to demonstrate the overloading of increment and


decrement operators.
de<iostream>
namespace std;
show

int i,j;
public:
show()
{
i=0;
j=0;
}
//class name operator symbol()
show operator ++()
{
show temp;
++i;
temp.i=i;
return temp;
}
show operator --()
{
show bla;
--j;
bla.j=j;
return bla;
}
void display()
{
cout<<"i=="<<i<<endl;
cout<<"j=="<<j<<endl;
}

ain()

show obj,obj1,obj2;
obj.display();
obj1=++obj;
obj.display();
obj2=--obj;
obj.display();
return 0;

. Write a program to demonstrate the overloading of memory


management operators.
de<iostream>
de<stdlib.h>
namespace std;
student

string name;
int age;
public:
student()
{
cout<<"constructor is called\n";
}
student(string name,int age)
{
this->name = name;
this->age = age;
}
void display()
{
cout<<"Name : "<<name<<endl;
cout<<"Age : "<<age<<endl;
}
void * operator new(size_t size)
{
cout<<"Overloading new operator with size: "<<size<<endl;
void *p = ::new student();
//void *p = malloc(size); will also work fine
return p;
}
void operator delete(void *p)
{
cout<<"Overloading delete operator"<<endl;
free(p);
}

ain()

student *p = new student("Akshit",96);


p->display();
delete p;

. Write a program to demonstrate the typecasting of basic type of


class type.
de<iostream>
namespace std;
show

int a;
public:
show()
{
}
show(int x)
{
a=x;
}
void disp()
{
cout<<"the value of a = "<<a ;
}

ain()

int n=20 ; //basic


show s ; //user defined type
s = n ; //basic to class type conversion
s.disp();
return 0;
2 Write a program to demonstrate the typecasting of class type to
basic type .
de<iostream>
namespace std;
show

float a;
public:
show()
{
a = 9.65;
}
operator float() //casting operator function or type conversion function
{
float x;
x=a;
return x;
}

ain()

show s ; //user defined type


float y = s ; //compiler doesn't support automatic type conversion
cout<<"The value of y is: = "<<y;
3 Write a program to demonstrate the typecasting of class type to
class type .
de<iostream>
namespace std;
product

private:
int m,n;
public:
void setdata(int x , int y)
{
m=x;
n=y;
}
int getm()
{
return (m);
}
int getn()
{
return (n);
}

tem
int a,b;
public:
void showdata()
{
cout<<"The value of a is : "<<a<<endl;
cout<<"The value of b is : "<<b<<endl;
}
item()
{
}
item(product P)
{
a = P.getm();
b = P.getn();
}

ain()

item i1;
product p1;
p1.setdata(10,20);
i1=p1;
i1.showdata();
return 0;

4 Write a program to demonstrate the multiple inheritances .


de<iostream>
namespace std;
A

public:
A()
{
cout<<"A's constructor called"<<endl;
}

public:
B()
{
cout<<"B's constructor called"<<endl;
}

C : public A,public B

public:
C()
{
cout<<"C's constuctor called"<<endl;
}

ain()
C c;
return 0;

5 Write a program to demonstrate the runtime polymorphism .


de<iostream>
namespace std;
Mybase

public:
void show()
{
cout<<"Base class show function called"<<endl;
}
virtual void print() //virtual function
{
cout<<"Base class print function called"<<endl;
}

Myderived:public Mybase

public:
void show()
{
cout<<"Derived class show function called"<<endl;
}
void print()
{
cout<<"Derived class print function called"<<endl;
}

ain()

Mybase *baseptr;
Myderived derivedobj;
baseptr = &derivedobj;
baseptr->print();
baseptr->show();
return 0;

6 Write a program to demonstrate the exception handling .


de<iostream>
namespace std;
ain()

int x = -1;
cout<<"before try"<<endl;
try{
cout<<"inside try"<<endl;
if(x<0)
{
throw x;
cout<<"after throw(never executed)"<<endl;
}
}
catch(int x){
cout<<"exception caught"<<endl;
}
cout<<"after catch (will be executed)";
return 0;

7 Write a program to demonstrate the use of class template .


de<iostream>
namespace std;
ate<typename T>
Array

private:
T *ptr;
int size;
public:
Array(T arr[],int s);
void print();

ate<typename T>
<T>::Array(T arr[],int s )
ptr = new T[s];
size = s;
for(int i=0;i<size;i++)
{
ptr[i]=arr[i];
}

ate<typename T>
Array<T>::print()

for(int i=0;i<size;i++)
{
cout<<" "<<*(ptr + i);
}
cout<<endl;

ain()

int arr[5]={1,2,3,4,5};
Array<int> a(arr,5);
a.print();
return 0;

8 Write a program to demonstrate the reading and writing of


mixed type of data .
de<iostream>
de<conio.h>
de<fstream>
de<stdlib.h>
namespace std;
ain()

h[100], fname[20];
m fs,ft;
<"Enter file name with extension (like files.txt) : ";
name);

open file in read mode to read data from the file


n(fname,ios::in);

<"Error in opening source file..!!";


();
);

<"Content written in file:";

ting file data on output window


(ft.eof()==0)

h;
<ch;

sing file after reading


e();

ening file to append data in file


en(fname,ios::app);

<"\nEnter Data to write in file:";

ng input from user to write data in file


h);

ting data in file


h;
<"Data copied successfully..!!";

sing file to save changes


se();

();

You might also like