0% found this document useful (0 votes)
11 views

Oops Practical File

The document contains 15 code snippets demonstrating various concepts in C++ OOP like classes, objects, member functions, static members, function overloading, constructors, operator overloading, inheritance, polymorphism and exceptions. Each snippet is followed by sample output.

Uploaded by

Anuj Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Oops Practical File

The document contains 15 code snippets demonstrating various concepts in C++ OOP like classes, objects, member functions, static members, function overloading, constructors, operator overloading, inheritance, polymorphism and exceptions. Each snippet is followed by sample output.

Uploaded by

Anuj Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

1.WAP to implement the concept of class and object.

#include<iostream>
using namespace std;
class student
{
public:
int a,b,temp;
void get()
{
cout<<"\n enter the value of a and b";
cin>>a>>b;
}
void show()
{
temp=a;
a=b;
b=temp;
cout<<"\n after swaping the value is:";
cout<<"a="<<a<<""<<"b="<<b;
}
};
int main()
{
student s1;
s1.get();
s1.show();
return 0;
}

OUTPUT:

1
2. WAP that uses a class where member functions are defined inside the
class.
#include<iostream>
using namespace std;
class student
{
public:
int a,b;
int add()
{
cout<<"\n enter the value of a and b"<<endl;
cin>>a>>b;
cout<<"\n sum ="<<a+b;
}
};
int main()
{
student s1;
s1.add();
return 0;
}

OUTPUT:

2
3. WAP that uses a class where member functions are defined outside
the class.
#include<iostream>
using namespace std;
class hello
{
public:
int a,b,c;
int add();
};
int hello::add()
{
cout<<"\n enter the value of a and b"<<endl;
cin>>a>>b;
c=a-b;
cout<<"\n difference of two no="<<c;

}
int main()
{
hello h1;
h1.add();
return 0;
}

OUTPUT:

3
4. WAP to demonstrate the use of static data member.
#include<iostream>
using namespace std;
class demo
{
public:
static int x;
static int y;
void input()
{
cout<<"\n enter the value of x and y:"<<endl;
cin>>x>>y;
}
void disp()
{
cout<<"\n the value of x and y are:";
cout<<x<<" "<<y;
}
};
int demo::x;
int demo::y;
int main()
{
demo ob,ob1,ob2;
ob.input();
ob.disp();
ob1.disp();
ob2.disp();
}

OUTPUT:

4
5. WAP to implement function overloading.
#include<iostream>
using namespace std;
class abc
{
public:
void vol(int a){
cout<<"\n volume of cube="<<a*a*a<<endl;
}
void vol(int l,int b,int h){
cout<<"\n volume of cuboide="<<l*b*h<<endl;
}
void vol(float r,float h){
cout<<"\n volume of cylinder="<<(r*r*h)*3.14<<endl;
}
};
int main()
{
abc a1;
a1.vol(20);
a1.vol(3,9,12);
a1.vol(9,6);
return 0;

OUTPUT:

5
6. WAP to implement parametrised constructor.
#include<iostream>
using namespace std;
class add
{
public:
int a,b,c;
add( int m, int n)
{
a=m;
b=n;
c=a+b;
cout<<"sum="<<c;
}
};
int main()
{
add a1 (55,22);
return 0;
}

OUTPUT:

6
7. WAP to implement multiple constructor.
#include<iostream>
using namespace std;
class add
{
public:
add()
{
cout<<"\n i am student"<<endl;
}
int a;
add(int m)
{
a=m;
cout<<"\n value of a ="<<a<<endl;
}
int b,c,d,e;
add(int p,int q,int r)
{
b=p;
c=q;
d=r;
e=b+c+d;
cout<<"\n the value of e ="<<e;
}
};
int main()
{
add p1;
add q1(10);
add r1(11,23,12);
return 0;
}

OUTPUT:

7
8. WAP to implement copy constructor.
#include<iostream>
using namespace std;
class student
{
int a,b;
public:
student(int m,int n)
{
a=m;
b=n;
cout<<"\n before copy:"<<endl;
cout<<a<<" "<<b;
}
student(student & s1)
{
a=s1.a;
b=s1.b;
cout<<"\n after copy:"<<endl;
cout<<a<<" "<<b;
}
};
int main()
{
student a1(10,20);
student a2(a1);
return 0;
}

OUTPUT:

8
9. WAP to implement friend function using two classes.
#include<iostream>
using namespace std;
class demo2;
class demo1
{
int a;
public:
void getdata(){
cout<<"\n enter the value of a :";
cin>>a;
}
friend int sum(demo1,demo2);
};
class demo2
{
int b;
public:
void showdata(){
cout<<"\n enter the value of b :";
cin>>b;
}
friend int sum(demo1,demo2);
};
int sum(demo1 a1,demo2 b1){
cout<<"\n sum of two no=";
return a1.a+b1.b;
}
int main()
{
demo1 a1;
demo2 b1;
a1.getdata();
b1.showdata();
cout<<sum(a1,b1);
return 0;
}

OUTPUT:

9
10. WAP to implement the operator overloading using unary operator.
#include<iostream>
using namespace std;
class demo
{
int a,b;
public:
demo(int x,int y)
{
a=x;
b=y;
}
void show()
{
cout<<"A="<<a<<" "<<"B="<<b<<endl;
}
void operator-()
{
cout<<"\n after reversing the sign:"<<endl;
a=-a;
b=-b;
}

};
int main()
{
demo d1(-20,60);
d1.show();
-d1;
d1.show();
return 0;
}

OUTPUT:

10
11. WAP to implement operator overloading using binary operator.
#include<iostream>
using namespace std;
class complex
{

public:
int real,imag;
complex(int r=0,int i=0){
real=r;
imag=i;
}
complex operator+(complex const &obj)
{
complex dummy;
dummy.real=real+obj.real;
dummy.imag=imag+obj.imag;
return dummy;
}
};
int main()
{
complex c1(19,16),c2(22,7);
complex c=c1+c2;
cout<<c.real<<"+i"<<c.imag<<endl;
return 0;
}

OUTPUT:

11
12. WAP to implement multiple inheritance.
#include<iostream>
using namespace std;
class demo
{
public:
demo(){
cout<<"\n hello world";
}
};
class demo1
{
public:
demo1(){
cout<<"\n hi ! how are you ?";
}
};
class demo2
{
public:
demo2(){
cout<<"\n where are you ?";
}
};
class demo3:public demo,public demo1,public demo2{
public:
void getdata(){
cout<<"\n i am fine thanku";
}
};
int main()
{
demo3 d1;
d1.getdata();
return 0;
}

OUTPUT:

12
13. WAP to implement hybrid inheritance.
#include<iostream>
using namespace std;
class demo
{
public:
demo()
{
cout<<"\n hello world";
}
};
class demo1:public demo
{
public:
demo1()
{
cout<<"\n how are you";
}
};
class demo2
{
public:
demo2()
{
cout<<"\n i am fine";
}
};
class demo3:public demo1,public demo2
{
public:
demo3()
{
cout<<"\n thanku";
}
};
int main()
{
demo3 d1;
return 0;
}

OUTPUT:

13
14. WAP to implement virtual function.
#include<iostream>
using namespace std;
class base
{
public:
virtual void show()
{
cout<<"\n base class";
}
};
class derived1:public base{
public:
void show()
{
cout<<"\n derived class 1";
}
};
class derived2:public base{
public:
void show()
{
cout<<"\n derived class 2";
}
};

int main()
{
base *b;
derived1 d1;
derived2 d2;
b=&d1;
b->show();
b=&d2;
b->show();
return 0;
}

OUTPUT:

14
15. WAP of exception thrown by try block.
#include<iostream>
using namespace std;
int main()

{
try
{
int age=16;
if(age>=18){
cout<<"\n Access granted- you are old";
}
else{
throw(age);
}
}
catch(int myNum)
{
cout<<"\n Access denied-you must be 18 years old";
cout<<"\n Age is:"<<myNum;
}
return 0;
}

OUTPUT:

15

You might also like