Escolar Documentos
Profissional Documentos
Cultura Documentos
1
PROGRAM NO. 1
#include<iostream>
int main()
int i,n;
cout<<"enter number";
cin>>n;
if((n>=0)&&(n<=9))
else if((n>=10)&&(n<=19))
else if((n>=20)&&(n<=29))
else if((n>=30)&&(n<=39))
else if((n>=40)&&(n<=49))
else
2
OUTPUT
3
1.2 W.A.P to implement switch-case statement
4
OUTPUT
5
1.3 W.A.P to implement do-while loop
#include<iostream>
int main()
int i=0,n;
cout<<"enter number";
cin>>n;
do
cout<<i<<"\n";
i++;
}while(i!=n+1);
6
OUTPUT
7
1.4 W.A.P to implement while-loop
#include<iostream>
int main()
int i=0,n;
cout<<"enter number";
cin>>n;
while(i!=n+1)
cout<<i<<"\n";
i++;
8
OUTPUT
9
1.5 W.A.P to implement for-loop
#include <iostream>
int main(){
*/
return 0;
10
OUTPUT
11
2 W.A.P to implement inline function
#include<iostream>
using namespace std;
class operation
{
int a,b;
public:
void get();
void add();
void subs();
void mul();
void div();
};
}
inline void operation::mul()
{
cout<<"multiplication of two number is"<<a*b<<endl;
}
inline void operation::div()
{
cout<<"division of two number is"<<a/b<<endl;
12
}
int main()
{
13
OUTPUT
14
3. W.A.P to implement the concept of structure
#include<iostream>
char name[30];
int rollno,marks;
} stud;
for(int i=0;i<n;i++)
cout<<"name="<<s[i].name<<endl;
cout<<"rollno="<<s[i].rollno<<endl;
cout<<"marks="<<s[i].marks<<endl<<endl;
15
int main()
stud s[3];
for(int i=0;i<3;i++)
cout<<"enter name"<<endl;
cin>>s[i].name;
cout<<"rollno"<<endl;
cin>>s[i].rollno;
cout<<"marks"<<endl;
cin>>s[i].marks;
display(s,3);
return 0;
16
OUTPUT
17
PROGRAM CONSTRUCTOR
class point
int x,y;
public:
point(int a,int b)
x=a;
y=b;
point(const point&p2)
x=p2.x;
y=p2.y;
int getx()
return(x);
int gety()
18
return(y);
};
int main()
point p1(10,20);
point p2=p1;
cout<<"p1.x="<<p1.getx()<<"\t"<<"p1.y="<<p1.gety()<<endl;
cout<<"p2.x="<<p2.getx()<<"\t"<<"p2.y="<<p2.gety();
return 0;
19
OUTPUT
20
4.2 W.A.P to implement default constructor
#include<iostream>
class construct
public:
int a,b;
construct()
int x,y;
a=10;
b=20;
};
int main()
construct p;
cout<<"x="<<p.a <<"y="<<p.b;
return 0;
21
OUTPUT
22
4.3 W.A.P to implement parameterized constructor
#include<iostream>
class point
int a,b;
public:
point(int x,int y)
a=x;
b=y;
int getx()
return(a);
int gety()
return(b);
};
int main()
{point p(10,15);
cout<<"p.x="<<p.getx() <<"p.y"<<p.gety();
return 0;}
23
OUTPUT
24
5. W.A.P to implement destructor
#include<iostream>
int count=0;
class alpha
public:
alpha()
count++;
~alpha()
count--;
};
int main()
cout<<"enter main\n";
alpha a1,a2,a3,a4;
25
cout<<"\nenter block 1\n";
alpha a5;
alpha a6;
return 0;
26
OUTPUT
27
6. W.A.P to implement friend function
#include<iostream>
class complex
float x,y;
public:
complex(){};
complex(float a)
x=y=a;
x=real;
y=img;
};
complex c3;
c3.x=c2.x+c1.x;
28
c3.y=c2.y+c1.y;
return(c3);
void show(complex c)
cout<<c.x<<"+j"<<c.y<<endl;
int main()
complex a(2.4,3.5);
complex b(1.6);
complex c;
c=sum(a,b);
cout<<"a=";show(a);
cout<<"b=";show(b);
cout<<"c=";show(c);
29
OUTPUT
30
7. W.A.P to implement unary operator
#include<iostream>
class space
int x;
int y;
int z;
public:
x=a;
y=b;
z=c;
void displaydata()
cout<<x<<endl;
cout<<y<<endl;
cout<<z<<endl;
space operator-()
x=-x;
y=-y;
31
z=-z;
};
int main()
space s;
s.getdata(30,40,50);
cout<<"s\n";
s.displaydata();
-s;
cout<<"s\n";
s.displaydata();
return 0;
32
OUTPUT
33
8.W.A.P to implement binary operator
#include <iostream>
class Box {
public:
double getVolume(void) {
length = len;
breadth = bre;
height = hei;
34
}
Box box;
return box;
};
int main() {
// box 1 specification
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
// box 2 specification
35
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
// volume of box 1
volume = Box1.getVolume();
// volume of box 2
volume = Box2.getVolume();
// volume of box 3
volume = Box3.getVolume();
return 0;
36
OUTPUT
37
9. W.A.P to implement function with recursion
#include<iostream>
int fact(int);
int main()
int n;
cin>>n;
return 0;
int fact(int n)
if( (n==0)||(n==1))
return 1;
else
return n*fact(n-1) }
38
OUTPUT
39
10. W.A.P to implement call by references
#include <iostream>
// Function prototype
void swap(int*n1,int*n2);
int main()
int a = 1, b = 2;
swap(&a, &b);
return 0;
void swap(int*n1,int*n2)
int temp;
temp = *n1;
*n1 = *n2;
*n2 = temp;
40
OUTPUT
41
11. W.A.P to implement single inheritance
#include<iostream>
class b
public:
int a;
int b;
void get_ab()
a=5;
b=10;
void show()
cout<<"a="<<a;
};
class D:public b
{
42
int c;
public:
void mul()
c=b*a;
void display()
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
cout<<"c="<<c<<endl;
};
int main()
D d;
d.get_ab();
//d.show();
d.mul();
d.display();
d.b=20;
d.mul();
d.display(); return(0);}
43
OUTPUT
44
12. W.A.P to implement multilevel inheritance
#include<iostream>
class student
protected:
int rollno;
public:
void get_no(int a)
rollno=a;
void put_no()
cout<<"roll no ="<<rollno<<endl;
};
protected:
float m1,m2;
public:
m1=a;
m2=b;
void put_marks()
cout<<"marks in m1="<<m1<<endl;
cout<<"marks in m2="<<m2<<endl;
};
float total;
public:
void display()
total=m1+m2;
put_no();
put_marks();
cout<<"total="<<total<<endl;
};
int main()
46
{
result student1;
student1.get_no(18124016);
student1.get_marks(75.0,59.5);
student1.display();
return(0);
47
OUTPUT
48
13. W.A.P to implement multiple inheritance
#include<iostream>
class M
protected:
int m;
public:
void get_m(int a)
m=a;
};
class N
protected:
int n;
public:
void get_n(int y)
n=y;
};
49
class P:public M,public N
public:
void display()
cout<<"m="<<m<<endl;
cout<<"n="<<n<<endl;
cout<<"m*n="<<m*n<<endl;
};
int main()
P p;
p.get_m(10);
p.get_n(20);
p.display();
return(0);
50
OUTPUT
51
14. W.A.P to implement heirarchial inheritance
// Hierarchical Inheritance
#include <iostream>
// base class
class Vehicle
public:
Vehicle()
};
};
52
class Bus: public Vehicle
};
// main function
int main()
Car obj1;
Bus obj2;
return 0;
53
OUTPUT
54
15. W.A.P to implement hybrid inheritance
#include<iostream>
class student
protected:
int rollno;
public:
void get_no(int a)
rollno=a;
void put_no()
cout<<"rollno="<<rollno<<endl;
};
protected:
float m1,m2;
public:
m1=x;
55
m2=y;
void put_marks()
cout<<"m1="<<m1<<endl;
cout<<"m2="<<m2<<endl;
};
class sports
protected:
float score;
public:
void get_score(float s)
score=s;
void put_score()
cout<<"score="<<score<<endl;
};
56
{
float total;
public:
void display()
total=m1+m2+score;
put_no();
put_marks();
put_score();
cout<<"total"<<total<<endl;
};
int main()
result s1;
s1.get_no(18124016);
s1.get_marks(27.05,33.0);
s1.get_score(6.0);
s1.display();
return(0);
57
OUTPUT
58
16. W.A.P to implement string manipulation
#include<string>
#include<iostream>
class string1
char *p;
int len;
public:
string(){p=0;len=0;}
string(const char*s);
~string(){delete p;}
};
len=strlen(s);
p=new char[len+1];
strcpy(p,s);
59
}
len=s.len;
p=new char[len+1];
strcpy(p,s.p);
string temp;
temp.len=s.len+t.len;
temp.p=new char[temp.len+1];
strcpy(temp.p,s.p);
strcat(temp.p,t.p);
return(temp);
int m=strlen(s.p);
int n=strlen(t.p);
if(m<=n) return(1);
else return(0);
60
{
cout<<s.p;
int main()
string s1="new";
string s2="york";
string s3="delhi";
string t1,t2,t3;
t1=s1;
t2=s2;
t3=s1+s3;
cout<<"nt1=";show(t1);
cout<<"\nt2=";show(t2);
cout<<"\nt3=";show(t3);
if(t1<=t3)
show(t1);
show(t3);
printf("\n");
61
else
show(t3);
show(t1);
cout<<"\n";
return(0);
62
OUTPUT
63
17. W.A.P to implement this pointer
#include<iostream>
#include<cstring>
class person
char name[20];
float age;
public:
person(char*s,float a)
strcpy(name,s);
age=a;
person&greater(person&x)
if(x.age>=age)
return(x);
else
return(*this);
void display()
cout<<"name="<<name<<"\n";
64
cout<<"age="<<age<<"\n";
};
int main()
person p1("john",37.50),
p2("ahmed",29.50),
p3("hebber",37.50);
person p=p1.greater(p3);
p.display();
p=p1.greater(p2);
p.display();
return 0;
65
OUTPUT
66
18. W.A.P to implement virtual function
#include<iostream>
class base
public:
void display()
cout<<"\ndisplay base";
cout<<"\nshow base";
};
public:
void display()
cout<<"\ndisplay derived";
void show()
67
cout<<"\nshow derived";
};
int main()
base b;
derived d;
base*bptr;
bptr=&b;
bptr->display();
bptr->show();
bptr=&d;
//bptr->display;
bptr->show();
return 0;
68
OUTPUT
69
19. W.A.P to implement exception handling
#include <iostream>
int main()
int x = -1;
// Some code
try {
if (x < 0)
throw x;
catch (int x ) {
return 0;}
70
71