Sie sind auf Seite 1von 42

Program 1

WAP to reverse a string.


Program code:
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void reverse(char *);
void main()
{ clrscr();
char str[20];
cout<<"Enter any string ¯ ";
gets(str);
reverse(str);
cout<<"Reversed String ¯ "<<str;
getch();
}
void reverse(char *str)
{
int i,j;
char str1[20];
for(i=0;*str!='\0';i++)
str++;
str--;
for(j=0;j<i;j++)
{
str1[j]=*str;
str--;
}
str++;
str1[j]='\0';
strcpy(str,str1);
}

Output:
Enter any string » hello
Reversed String » olleh
Program 2

WAP to check whether a string is a palindrome or not.


Program code:
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
void main()
{
clrscr();
char str[50],temp;
int l,i,j,flag;
cout<<"\nTo check Wether a String is Palindrome or not \nEnter the string: ";
cin>>str;
l=strlen(str);
flag=0;
for(i=0,j=l-1;i<l/2;i++,j--)
{
if(toupper(str[i])==toupper(str[j]))
flag=1;
else
{
flag=0;
break;
}
}
if(flag==1)
cout<<"\nThe string is Palindrome. ";
else
cout<<"\nThe string is not Palindrome. ";
getch();
}

Output:
To check Wether a String is Palindrome or not
Enter the sting: Madam
The string is a palindrome
Program 3

WAP to reverse the string


Program code:
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char str[50],temp;
int l,i,j;
cout<<"\nReverse a String \nEnter the string: ";
cin>>str;
l=strlen(str);
for(i=0,j=l-1;i<l/2;i++,j--)
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
}
cout<<"\nThe reversed string is: "<<str;
getch();
}

Output:
Reverse a String
Enter the String: HelloWorld
The reversed string is: dorlWolleH
Program 4

WAP to find out roots of a complex equation.


// C++ Program : To Calculate roots of a quadratic equation ax^2 + bx
+ c=0 (a!=0)
Program code:
#include <iostream.h>
#include<math.h>
int main()
{ float a, b, c, root1, root2, delta;
cout<< "Enter three numbers a, b & c (ax^2+bx+c): "<< endl;
cin >> a >> b >> c;
if (!a)
cout << " Value of a should not be zero \n" << "Aborting !!!!" << endl;
else
{ delta = b*b - 4*a*c; // beginning of else's body
if (delta>0)
{
root1 = (-b + sqrt(delta))/(2*a);
root2 = (-b - sqrt(delta))/(2*a);
cout << "Roots are real and unequal \n";
cout << "Root1= " << root1;
cout << ", Root2= " << root2 << "\n";
}
else if (delta == 0)
{
root1 = -b/(2*a);
cout << "Roots are real and equal \n";
cout << "Root1= " << root1;
cout << ", Root2= " << root1 << "\n";
}
else
cout << "Roots are complex and imaginary \n";
} // end of else's body
return 0;
}

Output:

Enter three numbers a,b &c (ax^2+bx+c):


12
2
5
Roots are complex and imaginary
Program 5

WAP to create a class account.

Program code:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class account
{
char name[31];
int acc_no;
char act;
float balance;

public:
void initial();
void deposit(float amt);
void withdraw(float amt);
void display();
int getacno()
{
return acc_no;
}
};
void account :: initial()
{
cout<<"Name: ";
gets(name);
cout<<"Account Number: ";
cin>>acc_no;
int c=1;
while(c)
{
cout<<"Account type saving/current (S/C)";
cin>>act;
if(toupper(act)=='S'||toupper(act)=='C')
break;
else
cout<<"Please enter C or S as your choice.";
}
act=toupper(act);
cout<<"Balance: ";
cin>>balance;
cout<<endl;
}
void account :: deposit(float amt)
{
balance+=amt;
cout<<"\nAmount Deposited\n";
}
void Account::withdraw(float amt)
{
if(balance-amt>=1000)
{
balance-=amt;
cout<<"\nAmount withdrawn.";
}
else
{
cout<<"Minimum balance has to be Rs. 1000/-"<<endl;
cout<<"You can withdraw only"<<balance-1000<<"rupees"<<endl;
}
}
void account::display()
{
cout<<"Account Number: "<<acc_no<<endl;
cout<<"Account Holder: ";
puts(name);
cout<<"\nAccount type: "<<act<<endl;
cout<<"Balance(Rs.): "<<balance<<endl;
}
void main()
{
account ac;
int f=1,c,amount;
clrscr();
if(f=1)
{
cout<<"Please create your account: \n\nEnter your details: ";
ac.initial();
}
clrscr();

c=1;
while(c)
{
cout<<"Main Menu\n";
cout<<"1.Deposit\n2.Withdraw\n3.balance enquiry\n4.Exit";
cout<<"\nEnter your choice";
cin>>c;
switch(c)
{
case 1:
cout<<"\nEnter the amount to deposit: ";
cin>>amount;
ac.deposit(amount);
break;
case 2:
cout<<"\nEnter the amount to withdraw: ";
cin>>amount;
ac.withdraw(amount);
break;
case 3:
ac.display();
break;
case 4:
exit(0);
break;
}
}
getch();
}

Output:
Please create your account
Enter your details:
Name: Raju Shrivastav
Account number: 005858
Account type Saving/Current(S/C): S
Balance: 79050

Main Menu
1. Deposit
2. Withdraw
3. Balance enquiry
4. Exit
Enter your choice: 1
Enter the amount to deposit: 5000
Amount Deposited.

Main Menu:
1. Deposit
2. Withdraw
3. Balance enquiry
4. Exit
Enter your choice: 4
Program 6

WAP to implement static data members.


Program code:
#include<iostream.h>
class inline_demo
{
public:
inline int sum(int,int);
};
inline int inline_demo::sum(int a,int b)
{
int c;
c=a+b;
return c;
}
void main()
{
inline_demo i1;
int i;
i=i1.sum(20,40);
cout<<"Sum is » "<<i;
}

Output:
Sum is >> 60
Program 7

WAP to concatenate all the characters of an array together.


Program code:
#include<iostream.h>
#include<stdio.h>
#include<string.h>
int main()
{
char a[20],b[20],c[40];
int l1,l2,i;
cout<<"\nEnter the elements of the first array: ";
gets(a);
cout<<"\nEnter the elements of the second array: ";
gets(b);
//concatenating
l1=strlen(a);
l2=strlen(b);
for(i=0;i<l1;i++)
c[i]=a[i];
for(i=l1;i<l2;i++)
c[i]=b[i];
cout<<"\nThe concatenated array is: ";
puts(c);
return 0;
}

Output:

Enter the elements of the first array: hello World


Enter the elements of the second array: my world
The concatenated array is:
Hello worldMy world
Program 8

WAP to implement inline function.


Program code:
#include<iostream.h>
#include<conio.h>
void main()
{
int x,y;
int fun();
clrscr();
x=fun();
cout<<"\n";
cout<<" \nThe value of x="<<x;
x=fun();
cout<<" \n\nnow the value of x="<<x;
getch();
}

fun()
{
static int a=10;
cout<<"\n\n"<<"a="<<a;
a++;
return(a);
}

Output:

a=10

The value of x=11

a=11

now the value of x=12


Program 9

WAP to implement different constructors versions.


Program code:
#include<iostream.h>
class deposit
{
long int principal;
int time;
float rate;
float total_amt;
public:
deposit();
deposit(long p,int t, float r);
deposit(long p,int t);
deposit(long p,float r);
void calc_amt(void);
void display(void);
}

deposit::deposit()
{
principal=0.0;
time=0.0;
rate=0.0;
}
deposit::deposit(long p,float r)
{
principal=p;
time=t;
rate=r;
}
deposit::deposit(long p,int t)
{
principal=p;
time=t;
rate=0.08;
}
deposit::deposit(long p,float r)
{
principal=p;
time=2;
rate=r;
}
void deposit::calc_amt(void)
{
total_amt=principal+(principal*time*rate)/100;
}
void deposit::display(void)
{
cout<<"\nPrincipal Amount: Rs. "<<principal;
cout<<"\tPeriod of investment: "<<time<<"years";
cout<<"\nRate of interest: "<<rate;
cout<<"\tTotal Amount: Rs."<<total_amt<<"\n";
}
int main()
{
deposit d1,d2(2000,2,0.07f),d3(4000,1),d4(3000,0.12f);
d1.calc_amt();
d2.calc_amt();
d3.calc_amt();
d4.calc_amt();
cout<<"Object 1\n";
d1.display();
cout<<"Object 2\n";
d2.display();
cout<<"Object 3\n";
d3.display();
cout<<"Object 4\n";
d4.display();
return 0;
}

Program 10

WAP to implement friend function.


Program code:
#include<iostream.h>
class friend2;
class friend1
{
int a,b;
public:
friend1(int x,int y)
{
a=x;
b=y;
}
friend int sum(friend1,friend2);
};
class friend2
{
int c,d;
public:
friend2(int x,int y)
{
c=x;
d=y;
}
friend int sum(friend1,friend2);
};

int sum(friend1 f1,friend2 f2)


{
int sum=0;
sum=f1.a+f1.b+f2.c+f2.d;
return sum;
}
void main()
{
friend1 f1(10,30);
friend2 f2(20,40);
int x;
x=sum(f1,f2);
cout<<"Sum is: "<<x;
}
Output:
Sum is: 100
Program 11

WAP to implement the use of copy constructor to copy one string to


another.
Program code:
#include<iostream.h>
#include<string.h>
class c1
{
char str[20];
public:
c1()
{
str[0]='\0';
cout<<str;
}
c1(char *str1)
{
strcpy(str,str1);
cout<<str;
}
};

void main()
{
c1 c,c2("Hello");
c=c2;
}

Output:

Hello
Program 12

WAP to create a matrix by dynamic array concepts.


Program code:
#include<iostream.h>
#include<conio.h>

void main()
{
int i=3;
int j=3;
int k;
int **array;
clrscr();
array=new int*[i];
for(k=0;k<i;k++)
*array=new int[j];
cout<<"Enter the matrix:\n";
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
cin>>array[i][j];
}
}
cout<<"The matrix you have entered is as follows:\n\n";
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
cout<<array[i][j];
cout<<" ";
}
cout<<"\n";
}
getch();
}

Output:
Enter the matrix:
123
456
789
The matrix you have entered is as follows:
1 2 3
4 5 6
7 8 9
Program 13
WAP to implement function overloading.
Program code:
#include<iostream.h>
int volume(int);
double volume(double,int);
long volume(long ,int ,int);
int main()
{
cout<<volume(10)<<endl;
cout<<volume(7.8,3)<<endl;
cout<<volume(30l,56,23)<<endl;
return 0;
}
// function definitions
int volume(int s) //CUBE
{
return(s*s*s);
}

double volume(double r,int h) //CYLINDER


{
return(3.14519*r*r*h);
}

long volume(long l,int b,int h) //RECTANGULAR BOX


{
return(l*b*h);
}

Output:

1000
574.06
38640
Program 14

WAP to overload ‘+’ operator.


Program code:
#include<iostream.h>
//complex operator +(complex,complex);
class complex
{
int real,img;
public:
complex(int,int);
complex operator + (complex);
complex();
void print(complex);
};
complex::complex()
{}
complex::complex(int i,int j)
{
real=i;
img=j;
}
complex complex::operator +(complex c1)
{
complex t;
t.real=c1.real+real;
t.img=c1.img+img;
return (t);
}
void complex::print(complex c3)
{
cout<<c3.real<<" "<<c3.img;
}
void main()
{
complex c1(10,40),c2(45,60),c3;
c3=c1+c2;
c3.print(c3);
}

Output:
55 100
Program 15

WAP to overload ‘-‘ operator.


Program code:
#include<iostream.h>
//complex operator +(complex,complex);
class complex
{
int real,img;
public:
complex(int,int);
complex operator - (complex);
complex();
void print(complex);
};
complex::complex()
{ }
complex::complex(int i,int j)
{
real=i;
img=j;
}
complex complex::operator -(complex c1)
{
complex t;
t.real=c1.real-real;
t.img=c1.img-img;
return (t);
}
void complex::print(complex c3)
{
cout<<c3.real<<" "<<c3.img;
}
void main()
{
complex c1(10,40),c2(45,60),c3;
c3=c1-c2;
c3.print(c3);
}

Output:
35 20
Program 16

WAP to overload new and delete function using friend function.


Program code:
#include <new.h>
#include <iostream.h>
#include<malloc.h>
#include<conio.h>
class C
{
public:
C(){};
friend void* operator new (size_t size); //implicitly declared as a
static member function
friend void operator delete (void *p); //implicitly declared as a static
member function
};

void* operator new (size_t size){


void * p = malloc(size);
if (p == 0) throw "allocation failure"; //instead of std::bad_alloc
return p;
}

void operator delete (void *p){


C* pc = static_cast<C*>(p);
free(p);
}

int main()
{
clrscr();
C *p = new C; // calls C::new
delete p; // calls C::delete
}
Program 17

WAP to overload new and delete function without using friend


function.
Program code:
#include <new.h>
#include <iostream.h>
#include<malloc.h>
class C {
public:
C(){};
void* operator new (size_t size); //implicitly declared as a static member
function
void operator delete (void *p); //implicitly declared as a static member
function
};

void* C::operator new (size_t size){


void * p = malloc(size);
if (p == 0) throw "allocation failure"; //instead of std::bad_alloc
return p;
}

void C::operator delete (void *p){


C* pc = static_cast<C*>(p);
free(p);
}
int main()
{
C *p = new C; // calls C::new
delete p; // calls C::delete
}
Program 18

WAP to overload unary – and unary ++ using friend function.


Program code:
#include<iostream.h>
class unary
{
static int desc,count;
public:
friend unary operator ++(unary);
friend unary operator --(unary);
void show(unary);
};
int unary::count=0;
int unary::desc=10;

unary operator ++(unary u)


{
++u.count;
return u;
}
unary operator --(unary u)
{
--u.desc;
return u;
}
void unary::show(unary u)
{
cout<<u.count<<" "<<u.desc;
}
void main()
{
unary u,t;
t=++u;
t=--u;
t.show(t);
}

Output:

1 9
Program 19

WAP to overload unary -- and unary ++ without using friend function.


Program code:
#include<iostream.h>
class unary
{
static int desc,count;
public:
unary operator ++();
unary operator --();
void show(unary);
};
int unary::count=0;
int unary::desc=10;

unary unary::operator ++()


{
unary u;
++count;
u.count=count;
return u;
}

unary unary::operator --()


{ unary u;
--desc;
u.desc=desc;
return u;
}
void unary::show(unary u)
{
cout<<u.count<<" "<<u.desc;
}
void main()
{
unary u,t;
t=++u;
t=--u;
t.show(t);
}
Output:

1 9
Program 20

WAP to overload ‘-‘ operator without using friend function.


Program code:
#include <iostream.h>
//using namespace std;
class location {
int longitude, latitude;
public:
location() {}
location(int lg, int lt) {
longitude = lg;
latitude = lt;
}
void show()
{
cout << longitude << " ";
cout << latitude << endl;
}
location operator,(location op2);
};
location location::operator,(location op2)
{
location temp;

//cout<<longitude<<latitude<<"\n";
temp.longitude = op2.longitude;
temp.latitude = op2.latitude;
cout << op2.longitude << " " << op2.latitude << endl;
return temp;
}
int main()
{
location object1(10, 20),
object2( 5, 30),
object3(1, 1);
object1.show();
object2.show();
object3.show();
cout << endl;
object1 = (object1, object3);
object1.show();
return 0;
}
Output:

10 20
5 30
1 1

1 1
1 1
Program 21

WAP to overload +=,= without using friend function.


Program code:
#include<iostream.h>
class sum
{
int a,b;
public:
sum operator +=(sum);
sum operator =(sum);
void show(sum);
sum()
{}
sum(int x,int y)
{
a=x;
b=y;
}
};
sum sum::operator +=(sum s)
{
sum s1;
s1.a=a+s.a;
s1.b=b+s.b;
return s1;
}
sum sum::operator =(sum s)
{
sum s1;
s1.a=s.a;
s1.b=s.b;
return s1;
}
void sum::show(sum s)
{
cout<<" "<<s.a<<" "<<s.b<<"\n";
}
void main()
{
sum s1(12,13),s2(14,56),s3;
s3=s1+=s2;
s3=s1;
s3.show(s3);
}

Output:
3516 3462
Program 22

WAP to derive a new class from 2 different classes.


Program code:
#include<iostream.h>
class base1
{
protected:
int a;
};
class base2{
public:
int b;
};
class derived : public base1,base2{
public:
derived()
{
a=0;
b=10;
}
void show()
{
cout<<"a = "<<a<<" b = "<<b;
}
};
void main()
{
derived d1;
d1.show();
}

Output:
a=0 b=10
Program 23

WAP to implement the given hierarchy of classes.


Program code:
#include<iostream.h>
class base
{
public:
int a;
};
class derived1 : public base{
public:
int b;
derived1()
{
a=0;
cout<<"Derived called !!!\n";
}
};
class derived2 : public derived1{
public:
derived2()
{
b=0;
a=10;
}
void show()
{
cout<<"a="<<a<<" b="<<b;
}
};
void main()
{
derived2 d1;
d1.show();
}

Output:

Derived called !!!


a=10 b=0
Program 24

WAP to implement multilevel inheritance.


Program code:
#include<iostream.h>
class base
{
public:
int a;
};
class derived1 : public base{
public:
int b;
derived1()
{
a=0;
cout<<"Derived called !!!\n";
}
};
class derived2 : public derived1{
public:
derived2()
{
b=0;
a=10;
}
void show()
{
cout<<"a="<<a<<" b="<<b;
}
};
void main()
{
derived2 d1;
d1.show();
}

Output:

Derived called!!!
a=0 b=10
Program 24

WAP to implement multiple inheritance.


Program code:
#include<iostream.h>
class base1
{
protected:
int a;
};
class base2
{
public:
int b;
};
class derived : public base1,base2{
public:
derived()
{
a=0;
b=10;
}
void show()
{
cout<<"a = "<<a<<" b = "<<b;
}
};
void main()
{
derived d1;
d1.show();
}

Output:

a=0 b=10
Program 26

WAP to implement hierarchical inheritance.


Program code:
#include<iostream.h>
class grandfather
{
// block of codes
};
class father:public grandfather{
// father's code
};
class son: public father{
//son's code
};
void main()
{
son s;
father t;
}
Program 27

WAP to use virtual function.


Program code:
#include<iostream.h>
class base
{
public:
virtual void f()
{
cout<<"Base Class";
}
};
class derived : public base{
public:
void f()
{
cout<<"\nDerived Class";
}
};
void main()
{
base *p,b;
p=&b;
p->f();
derived d;
p=&d;
p->f();
}

Output:

Base Class
Derived Class
Program 28

WAP to implement generic function.


Program code:
#include<iostream.h>
template<class x>void swap(x &a,x &b)
{
x temp;
temp=a;
a=b;
b=temp;
}
void main()
{
char a='x',b='z';
cout<<"Original -> "<<a<<" "<<b;
swap(a,b);
cout<<"\nswap -> "<<a<<" "<<b;
}

Output:
Original -> x z
Swap -> z x
Program 29

WAP to demonstrate catching all exceptions.


Program code:
#include<iostream.h>
#include<errno.h>
void main()
{ int j;
try{
j=10/0;
if(j)
{
throw(errno);
}
if(!errno)
throw errno;
}
catch(int i)
{
cout<<"Error number: "<<i<<" occurred";
}
}

Output:

Error number: 0 occured


Program 30

WAP that accepts an integer from user and throws an exception


invalid number if input is less than zero.
Program code:
#include<iostream.h>
//#include<excpt.h>
void main()
{
int no;
cout<<"Enter a number : ";
cin>>no;
try{
if(no<0)
throw "invlaid number";
else
cout<<"Correct Number";
}
catch(char *str)
{
cout<<str;
}
}

Output:

Enter a number : -12


Invalid number
Program 31

WAP to create file student and perform operations--------


1. Add
2. Modify
3. Delete
Program code:
#include<fstream.h>
#include<conio.h>
#include<iostream.h>
#include<process.h>
class student{
struct person{
char name[40];
int roll;
}p;
fstream file;
public:
student();
void addrec();
void deleterec();
void modify();
void list();
};
void main()
{
int choice;
student g;
cout<<"1.Add Record";
cout<<"\n2.Modify Record";
cout<<"\n3.Delete Record";
cout<<"\n4.List Records\n";
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
g.addrec();
break;
case 2:
g.modify();
break;
case 3:
g.deleterec();
break;
case 4:
g.list();
break;
}
}
student::student()
{
file.open("stu.dat",ios::binary|ios::in|ios::out);
if(!file)
{
cout<<"\nUnable to open";
exit(0);
}
}

void student::addrec()
{
file.seekp(0L,ios::end);
cout<<"Enter name and roll number : ";
cin>>p.name>>p.roll;
file.write((char *)&p,sizeof(p));
}
void student::list()
{
file.seekg(0L,ios::beg);
while(file.read((char*)&p,sizeof(p)))
{
cout<<p.name<<" "<<p.roll<<"\n";
}
}
void student::modify()
{
int code,count=0,pos;
cout<<"Enter roll number to be modified";
cin>>code;
file.seekg(0L,ios::beg);
while(file.read((char*)&p,sizeof(p)))
{
if(p.roll==code)
{
cout<<"Enter name and roll number ";
cin>>p.name>>p.roll;
pos=count*sizeof(p);
file.seekp(pos,ios::beg);
file.write((char*)&p,sizeof(p));
}
count++;
}
}
void student::deleterec() `
{
int code,pos,count=0;
cout<<"Enter roll number : ";
cin>>code;
file.seekg(0L,ios::beg);
while(file.read((char *)&p,sizeof(p)))
{
if(code==p.roll)
{
pos=count*sizeof(p);
file.seekp(pos,ios::beg);
file.write((char*)&p,sizeof(p));
}
count++;
}
file.clear();
}
Output:
1.Add Record
2.Modify Record
3.Delete Record
4.List Records
Enter your choice : 1
Enter name and roll number : Rajiv 61

1.Add Record
2.Modify Record
3.Delete Record
4.List Records
Enter your choice : 4
12 6951
Vaishali 141
Rajiv 61

Das könnte Ihnen auch gefallen