Você está na página 1de 71

OUTPUT

1
PROGRAM NO. 1

1.1 W.A.P to implement if- statement.

#include<iostream>

using namespace std;

int main()

int i,n;

cout<<"enter number";

cin>>n;

if((n>=0)&&(n<=9))

cout<<"number is between 0 and 9 ="<<n;

else if((n>=10)&&(n<=19))

cout<<"number is between 10 and 19 ="<<n;

else if((n>=20)&&(n<=29))

cout<<"number is between 20 and 29 ="<<n;

else if((n>=30)&&(n<=39))

cout<<"number is between 30 and 39 ="<<n;

else if((n>=40)&&(n<=49))

cout<<"number is between 40 and 49 ="<<n;

else

cout<<"number is greater than 49 and number ="<<n;}

2
OUTPUT

3
1.2 W.A.P to implement switch-case statement

// Following is a simple C program


// to demonstrate syntax of switch.
#include <stdio.h>
int main()
{
int x = 2;
switch (x)
{
case 1: printf("Choice is 1");
break;
case 2: printf("Choice is 2");
break;
case 3: printf("Choice is 3");
break;
default: printf("Choice other than 1, 2 and 3");
break;
}
return 0;
}

4
OUTPUT

5
1.3 W.A.P to implement do-while loop

#include<iostream>

using namespace std;

int main()

int i=0,n;

cout<<"enter number";

cin>>n;

cout<<"print all number upto n";

do

cout<<i<<"\n";

i++;

}while(i!=n+1);

6
OUTPUT

7
1.4 W.A.P to implement while-loop

#include<iostream>

using namespace std;

int main()

int i=0,n;

cout<<"enter number";

cin>>n;

cout<<"print all number upto n";

while(i!=n+1)

cout<<i<<"\n";

i++;

8
OUTPUT

9
1.5 W.A.P to implement for-loop

#include <iostream>

using namespace std;

int main(){

for(int i=1; i<=6; i++){

/* This statement would be executed

* repeatedly until the condition

* i<=6 returns false.

*/

cout<<"Value of variable i is: "<<i<<endl;

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::get()


{
cout<<"enter the two numbers";
cin>>a>>b;
}

inline void operation::add()


{
cout<<"addition of numbers is"<<a+b<<endl;
}

inline void operation::subs()


{
cout<<"substraction of two numbers is"<<a-b<<endl;

}
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()
{

cout<<"program for inline function\n ";


operation o;
o.get();
o.add();
o.subs();
o.mul();
o.div();
return 0;
}

13
OUTPUT

14
3. W.A.P to implement the concept of structure

#include<iostream>

using namespace std;

typedef struct student

char name[30];

int rollno,marks;

} stud;

void display( stud s[],int n)

cout<<"details of students are "<<endl;

for(int i=0;i<n;i++)

cout<<"details of student "<<i+1<<endl;

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];

cout<<"enter the details of students"<<endl;

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

4.1 W.A.P to implement copy constructor


#include<iostream>

using namespace std;

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>

using namespace std;

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>

using namespace std;

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>

using namespace std;

int count=0;

class alpha

public:

alpha()

count++;

cout<<"\nno. of objects created\n"<<count;

~alpha()

cout<<"\nno. of objects destroyed\n"<<count;

count--;

};

int main()

cout<<"enter main\n";

alpha a1,a2,a3,a4;

25
cout<<"\nenter block 1\n";

alpha a5;

cout<<"\nenter block 2\n";

alpha a6;

cout<<"\nre enter main\n";

return 0;

26
OUTPUT

27
6. W.A.P to implement friend function
#include<iostream>

using namespace std;

class complex

float x,y;

public:

complex(){};

complex(float a)

x=y=a;

complex(float real,float img)

x=real;

y=img;

friend complex sum(complex,complex);

friend void show(complex);

};

complex sum(complex c1,complex c2)

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>

using namespace std;

class space

int x;

int y;

int z;

public:

void getdata(int a,int b,int c)

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>

using namespace std;

class Box {

double length; // Length of a box

double breadth; // Breadth of a box

double height; // Height of a box

public:

double getVolume(void) {

return length * breadth * height;

void setLength( double len ) {

length = len;

void setBreadth( double bre ) {

breadth = bre;

void setHeight( double hei ) {

height = hei;

34
}

// Overload + operator to add two Box objects.

Box operator+(const Box& b) {

Box box;

box.length = this->length + b.length;

box.breadth = this->breadth + b.breadth;

box.height = this->height + b.height;

return box;

};

// Main function for the program

int main() {

Box Box1; // Declare Box1 of type Box

Box Box2; // Declare Box2 of type Box

Box Box3; // Declare Box3 of type Box

double volume = 0.0; // Store the volume of a box here

// 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();

cout << "Volume of Box1 : " << volume <<endl;

// volume of box 2

volume = Box2.getVolume();

cout << "Volume of Box2 : " << volume <<endl;

// Add two object as follows:

Box3 = Box1 + Box2;

// volume of box 3

volume = Box3.getVolume();

cout << "Volume of Box3 : " << volume <<endl;

return 0;

36
OUTPUT

37
9. W.A.P to implement function with recursion
#include<iostream>

using namespace std;

int fact(int);

int main()

int n;

cout<<"enter the number";

cin>>n;

cout<<"factorial of " << n <<" is="<<fact(n)<<endl;

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>

using namespace std;

// Function prototype

void swap(int*n1,int*n2);

int main()

int a = 1, b = 2;

cout << "Before swapping" << endl;

cout << "a = " << a << endl;

cout << "b = " << b << endl;

swap(&a, &b);

cout << "\nAfter swapping" << endl;

cout << "a = " << a << endl;

cout << "b = " << b << endl;

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>

using namespace std;

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>

using namespace std;

class student

protected:

int rollno;

public:

void get_no(int a)

rollno=a;

void put_no()

cout<<"roll no ="<<rollno<<endl;

};

class test:public student

protected:

float m1,m2;

public:

void get_marks(float a,float b)


45
{

m1=a;

m2=b;

void put_marks()

cout<<"marks in m1="<<m1<<endl;

cout<<"marks in m2="<<m2<<endl;

};

class result:public test

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>

using namespace std;

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

// C++ program to implement

// Hierarchical Inheritance

#include <iostream>

using namespace std;

// base class

class Vehicle

public:

Vehicle()

cout << "This is a Vehicle" << endl;

};

// first sub class

class Car: public Vehicle

};

// second sub class

52
class Bus: public Vehicle

};

// main function

int main()

// creating object of sub class will

// invoke the constructor of base class

Car obj1;

Bus obj2;

return 0;

53
OUTPUT

54
15. W.A.P to implement hybrid inheritance
#include<iostream>

using namespace std;

class student

protected:

int rollno;

public:

void get_no(int a)

rollno=a;

void put_no()

cout<<"rollno="<<rollno<<endl;

};

class test:public student

protected:

float m1,m2;

public:

void get_marks(float x,float y)

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;

};

class result:public sports,public test

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>

using namespace std;

class string1

char *p;

int len;

public:

string(){p=0;len=0;}

string(const char*s);

string(const string &s);

~string(){delete p;}

friend string operator+(const string&s,const string&t);

friend int operator<=(const string&s,const string&t);

friend void show(const string s);

};

std::string::string(const char *s)

len=strlen(s);

p=new char[len+1];

strcpy(p,s);

59
}

std::string::string(const string &s)

len=s.len;

p=new char[len+1];

strcpy(p,s.p);

string operator+(const string &s,const string &t)

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 operator<=(const string &s,const string &t)

int m=strlen(s.p);

int n=strlen(t.p);

if(m<=n) return(1);

else return(0);

void show(const string s)

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);

cout<<"smaller than ";

show(t3);

printf("\n");

61
else

show(t3);

cout<<"smaller than ";

show(t1);

cout<<"\n";

return(0);

62
OUTPUT

63
17. W.A.P to implement this pointer
#include<iostream>

#include<cstring>

using namespace std;

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);

cout<<"elder person is\n";

p.display();

p=p1.greater(p2);

cout<<"elder person is\n";

p.display();

return 0;

65
OUTPUT

66
18. W.A.P to implement virtual function
#include<iostream>

using namespace std;

class base

public:

void display()

cout<<"\ndisplay base";

virtual void show()

cout<<"\nshow base";

};

class derived:public base

public:

void display()

cout<<"\ndisplay derived";

void show()

67
cout<<"\nshow derived";

};

int main()

base b;

derived d;

base*bptr;

cout<<"\n bptr points to base";

bptr=&b;

bptr->display();

bptr->show();

cout<<"\n bptr points to derived";

bptr=&d;

//bptr->display;

bptr->show();

return 0;

68
OUTPUT

69
19. W.A.P to implement exception handling

#include <iostream>

using namespace std;

int main()

int x = -1;

// Some code

cout << "Before try \n";

try {

cout << "Inside try \n";

if (x < 0)

throw x;

cout << "After throw (Never executed) \n";

catch (int x ) {

cout << "Exception Caught \n";

cout << "After catch (Will be executed) \n";

return 0;}

70
71

Você também pode gostar