Professional Documents
Culture Documents
Project Oops
Project Oops
ROLL NO – 1914651
SEMESTER – 3
SECTION – Z(E2)
SUBJECT – OOPS PRACTICAL
g int mobileno;
ndname[10];
etinfo()
owinfo()
()
m1;
nfo();
winfo();
;
e:
ong int mobileno;
brandname[10];
c:
d getinfo();
d showinfo();
obile :: getinfo()
obile :: showinfo()
n()
e m1;
etinfo();
howinfo();
n 0;
Write a program to demonstrate the use of static data
mbers.
de<iostream>
amespace std;
ne
wo
one a;
c:
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()
num;
display();
n 0;
private:
int x,y;
public:
Demo()
{
x=40;
y=50;
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;
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()
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
int value;
public:
show(int v):value(v)//initializer list
{
cout<<"value is "<<value;
}
ain()
show obj(10);
return 0;
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;
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()
int a;
public:
show()
{
}
show(int x)
{
a=x;
}
void disp()
{
cout<<"the value of a = "<<a ;
}
ain()
float a;
public:
show()
{
a = 9.65;
}
operator float() //casting operator function or type conversion function
{
float x;
x=a;
return x;
}
ain()
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;
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;
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;
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;
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;
h[100], fname[20];
m fs,ft;
<"Enter file name with extension (like files.txt) : ";
name);
h;
<ch;
();