Professional Documents
Culture Documents
#Include Using Namespace STD : 1. WAP To Overload Increment and Decrement Operators. - Operator
#Include Using Namespace STD : 1. WAP To Overload Increment and Decrement Operators. - Operator
#Include Using Namespace STD : 1. WAP To Overload Increment and Decrement Operators. - Operator
-- Operator
#include<iostream>
class Distance
public:
Distance(int f, int i)
this->feet=f;
this->inch=I;
void operator-()
feet--;
inch--;
};
int main()
Distance d1(8,9);
-d1;
return 0;
}
++ Operator
#include<iostream>
class Distance
public:
Distance(int f, int i)
this->feet=f;
this->inch=I;
void operator+()
feet++;
inch++;
};
int main()
Distance d1(8,9);
+d1;
return 0;
Binary + Operator
#include<iostream>
class Complex
public:
Complex(int r, int i)
real=r;
imag=i;
Complex res;
return res;
void print()
cout<<real<<”+i”<<imag<<endl;
}
};
int main()
Complex c3 = c1 + c2;
c3.print();
2. WAP to get the marks of a Student in six subjects and calculate the result.
#include<iostream>
int main()
cin>>s1>>s2>>s3>>s4>>s5>>s6;
total= s1+s2+s3+s4+s5+s6;
perc= (total/600)*100;
cout<<”Precentage :- ”<<perc<<endl;
return 0;
}
3. WAP to check whether the entered number is Armstrong or not.
#include<iostream>
int main()
cout<<”Enter a Number”<<endl;
cin>>number;
int q=number;
while(q!=0)
q=q/10;
count++;
cnt=count;
q=number;
while(q!=0)
rem=q%10;
while(cnt!=0)
mul = mul*rem;
cnt--;
}
result = result + mul;
cnt = count;
q = q/10;
mul = 1;
if(result == number)
else
4. WAP to take input of an object array and sort the strings stored in it.
#include <iostream>
#include <string>
int main()
string str;
char temp;
getline(cin, str);
cout << "\n String before sorting: " << str << " \n";
{
for (int j = 0; j < len - 1; j++)
temp = str[j];
str[j + 1] = temp;
cout << "\n String after sorting: " << str << " \n";
return 0;
5. WAP that creates Static Data Members and Member Functions and
#include <iostream>
class Demo
private:
static int X;
public:
{
cout <<"Value of X: " << X << endl;
};
int main()
Demo X;
X.display();
return 0;
#include<iostream>
#include<stdilb.h>
int main()
int num,fact=1;
cout<<”Enter a Number”<<endl;
cin>>num;
fact=fact*I;
#include<iostream>
#include<stdlib.h>
int main()
int year;
cin>>year;
if(year%400==0)
cout<<”LEAP YEAR”<<endl;
else if(year%100==0)
else if(year%4==0)
{
cout<<”LEAP YEAR”<<endl;
else
return 0;
#include<iostream>
#include<stdlib.h>
void square()
cin>>side;
area=side*side;
cout<<”Area=”<<area<<” sq.units”<<endl;
}
void rectangle()
cin>>length;
cin>>breadth;
area=length*breadth;
cout<<”Area=”<<area<<” sq.units”<<endl;
void circle()
cin>>radius;
area=3.14*radius*radius;
cout<<”Area=”<<area<<” sq.units”<<endl;
int main()
square();
rectangle();
circle();
return 0;
}
9. WAP to Swap the values of any two variables using Call by Reference.
#include<iostream>
int main()
cout<<”A= ”<<a<<endl;
cout<<”B= ”<<b<<endl;
swap(a, b);
return 0;
int temp;
temp=a;
a=b;
b=temp;
}
10. WAP for Array Rotation.
#include<iostream>
#include<stdlib.h>
arr[n-1] = temp;
leftRotatebyOne(arr, n);
}
int main()
int arr[] = { 1, 2, 3, 4, 5, 6, 7 };
leftRotate(arr, 2, n);
printArray(arr, n);
return 0;
11. WAP to remove those characters from the 1st string that are present in
#include<iostream>
#include<string.h>
count[*(string2+i)]++;
}
int i = 0, j = 0;
while(*(string1 + i))
if(count[temp] == 0)
j++;
i++;
*(string1+j) = '\0';
return string1;
int main()
cout<<"Input strings:\n";
cout<<"string1: ";
cout<<string1[i];
cout<<"\nstring2: ";
for (int i = 0; i < strlen(string2); ++i)
cout<<string2[i];
cout<<"\nOutput: ";
cout<<RemoveChars(string1, string2);
return 0; }
#include<iostream>
#include<math.h>
double r;
r=pow(num, p);
return r;
int main()
double n, res;
int m;
cin>>n;
cin>>m;
res=Power(n, m);
cout<<n<<”^”<<m<<”= ”<<res<<endl;
return 0;
#include <iostream>
int main ()
cout << " Please, enter the total no. you want to enter: ";
cout << " Enter the element " << i+1 << ": ";
cin >> arr[i];
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return 0;
14. WAP to change every letter in the given string with the letter following it
in the alphabet.
#include <iostream>
#include <string>
using namespace std;
string change_letter(string str)
{
int char_code;
if (char_code == 122)
{
str[x] = char(97);
}
else if (char_code == 90)
{
str[x] = char(65);
}
else if (char_code >= 65 && char_code <= 90 || char_code >=97
&& char_code <= 122)
{
str[x] = char(char_code + 1);
}
return str;
}
int main()
{
cout << "Original string: ThisIsAstring";
cout << "\nNew string: " << change_letter("ThisIsAstring");
cout << "\n\nOriginal string: ItChangedNow";
cout << "\nNew string: " << change_letter("ItChangedNow");
cout<<endl;
return 0;
}
return 0;
}
16. WAP to multiply Two 4x4 matrices and print the resultant matrix in
tabular form.
#include <iostream>
#include <string>
using namespace std;
return 0;
int main()
{
int A[4][4], B[4][4];
cout<<"Enter the First Matrix...."<<endl;
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
cin>>A[i][j];
}
}
mult(A,B);
return 0;
}
#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;
class Bill
{
public:
string name;
void getData();
float Charge(int);
void Display(int, int);
};
void Bill::getData()
{
cout<<"Enter Consumer Name...."<<endl;
getline(cin, name);
}
float Bill::Charge(int u)
{
if(u>0 && u<=100)
return (0.6*u);
if(u>100 && u<=300)
return ((0.6*100) + (u-100)*0.8);
if(u>300)
return ((0.6*100) + (0.8*200) + (u-300)*90);
}
void Bill::Display(int u, int c)
{
cout<<"Name:- "<<name<<endl;
cout<<"Units Consumed:- "<<u<<endl;
cout<<"Bill = Rs."<<c<<endl;
}
int main()
{
Bill obj;
int units;
obj.getData();
obj.Display(units, charge);
return 0; }
23. WAP to overload the ‘+’ operator and use it to add two strings.
#include<iostream>
#include<string.h>
using namespace std;
class A
{
string a;
public:
void get()
{
cout<<"Enter the string: ";
cin>>a;
}
void friend operator+(A , A);
};
void operator+(A str1 , A str2)
{
A str3;
str3.a=str1.a+" "+str2.a;
cout<<"After adding the two strings: "<<str3.a<<endl;
}
int main()
{
A str1,str2;
str1.get();
str2.get();
str1+str2;
return 0;
}
27. WAP to create a class “Tollbooth”. Create two data elements of type
‘unsigned int’ to hold the total number of cars and a ‘double’ to hold the
amount of money collected. A member function initializes both of these
to zero. A member function ‘Paying_Car( )’ increments the car total and
add 50 to total money collected. Another function ‘Non_Paying_Car( )’
increments the car total but add nothing to the Money Collected. Finally
function ‘Display( )’, displays all the data computed.
Make the program Menu Driven.
#include<iostream>
#include<stdlib.h>
using namespace std;
class TollBooth
{
public:
unsigned int car;
double money;
void initial()
{
car=0;
money=0.0;
}
void Paying_Car()
{
car=car+1;
money=money+50.0;
}
void Non_Paying_Car()
{
car=car+1;
}
void Display()
{
cout<<"Total number of cars :- "<<car<<endl;
cout<<"Money Collected :- "<<money<<endl;
}
};
int main()
{
TollBooth obj;
int ch;
obj.initial();
while(1)
{
cout<<"### MENU ###"<<endl;
cout<<"1. Paying Car"<<endl;
cout<<"2. Non Paying Car"<<endl;
cout<<"3. Display Data"<<endl;
cout<<"4. Exit"<<endl;
switch(ch)
{
case 1:
obj.Paying_Car();
break;
case 2:
obj.Non_Paying_Car();
break;
case 3:
obj.Display();
break;
case 4:
exit(1);
}
}
}
#include<iostream>
class Test
{
public:
void input()
cin>>n1>>n2;
void divide()
try
if(cin.fail())
if(n1==0 || n2==0)
throw 0;
cout<<”Answer :- ”<<n1/n2<<endl;
else
cout<<”Answer :- ”<<n2/n1<<endl;
catch(const int n)
}
catch(const char* str)
cout<<str;
};
int main()
Test obj;
obj.input();
obj.divide();
return 0;