#Include Using Namespace STD : 1. WAP To Overload Increment and Decrement Operators. - Operator

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 46

1. WAP to overload Increment and Decrement operators.

-- Operator

#include<iostream>

using namespace std;

class Distance

public:

int feet, inch;

Distance(int f, int i)

this->feet=f;

this->inch=I;

void operator-()

feet--;

inch--;

cout<<”\n Feet and Inches (Decrement) :”<<feet<<”’”<<inch;

};

int main()

Distance d1(8,9);

-d1;

return 0;
}

++ Operator

#include<iostream>

using namespace std;

class Distance

public:

int feet, inch;

Distance(int f, int i)

this->feet=f;

this->inch=I;

void operator+()

feet++;

inch++;

cout<<”\n Feet and Inches (Decrement) :”<<feet<<”’”<<inch;

};

int main()

Distance d1(8,9);

+d1;
return 0;

Binary + Operator

#include<iostream>

using namespace std;

class Complex

int real, imag;

public:

Complex(int r, int i)

real=r;

imag=i;

Complex operator +(Complex &obj)

Complex res;

res.real = real + obj.real;

res.imag = imag + obj.imag;

return res;

void print()

cout<<real<<”+i”<<imag<<endl;
}

};

int main()

Complex c1(10, 5), c2(2, 4);

Complex c3 = c1 + c2;

c3.print();

2. WAP to get the marks of a Student in six subjects and calculate the result.

#include<iostream>

using namespace std;

int main()

float s1, s2, s3, s4, s5, s6;

float total, perc;

cout<<”Enter Marks for 6 Subjects”<<endl;

cin>>s1>>s2>>s3>>s4>>s5>>s6;

total= s1+s2+s3+s4+s5+s6;

perc= (total/600)*100;

cout<<”Total Marks :- ”<<total<<endl;

cout<<”Precentage :- ”<<perc<<endl;

return 0;

}
3. WAP to check whether the entered number is Armstrong or not.

#include<iostream>

using namespace std;

int main()

int number, count=0, result=0, mul=1, cnt, rem;

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)

cout<<number<<” is an Armstrong Number”<<endl;

else

cout<<number<<” is not an Armstrong Number”<<endl;

4. WAP to take input of an object array and sort the strings stored in it.

#include <iostream>

#include <string>

using namespace std;

int main()

string str;

char temp;

cout << "Enter the string to be sorted: ";

getline(cin, str);

int len = str.length();

cout << "\n String before sorting: " << str << " \n";

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

{
for (int j = 0; j < len - 1; j++)

if (str[j] > str[j + 1])

temp = str[j];

str[j] = str[j + 1];

str[j + 1] = temp;

cout << "\n String after sorting: " << str << " \n";

return 0;

5. WAP that creates Static Data Members and Member Functions and

demonstrate how to access them.

#include <iostream>

using namespace std;

class Demo

private:

static int X;

public:

static void display()

{
cout <<"Value of X: " << X << endl;

};

int Demo :: X =10;

int main()

Demo X;

X.display();

return 0;

6. WAP to find the factorial of any given number.

#include<iostream>

#include<stdilb.h>

using namespace std;

int main()

int num,fact=1;

cout<<”Enter a Number”<<endl;

cin>>num;

for(int i=1; i<=num; i++)

fact=fact*I;

cout<<”Factorial of”<<num<<”: ”<<fact;


return 0;

7. WAP to find weather the entered year is a Leap Year or not.

#include<iostream>

#include<stdlib.h>

using namespace std;

int main()

int year;

cout<<”Enter any Year”<<endl;

cin>>year;

if(year%400==0)

cout<<”LEAP YEAR”<<endl;

else if(year%100==0)

cout<<”NOT A LEAP YEAR”<<endl;

else if(year%4==0)

{
cout<<”LEAP YEAR”<<endl;

else

cout<<”NOT A LEAP YEAR”<<endl;

return 0;

8. WAP to calculate Area of Square, Area of Rectangle and Area of Circle.

#include<iostream>

#include<stdlib.h>

using namespace std;

void square()

float side, area;

cout<<”Enter the Side of the Square”<<endl;

cin>>side;

area=side*side;

cout<<”Area=”<<area<<” sq.units”<<endl;

}
void rectangle()

float length, breadth, area;

cout<<”Enter the Length and Breadth of the Rectangle”<<endl;

cin>>length;

cin>>breadth;

area=length*breadth;

cout<<”Area=”<<area<<” sq.units”<<endl;

void circle()

float radius, area;

cout<<”Enter the Radius of the Circle”<<endl;

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>

using namespace std;

void swap(int &, int &);

int main()

{ int a=4, b=5;

cout<<”A= ”<<a<<endl;

cout<<”B= ”<<b<<endl;

swap(a, b);

cout<<”A after SWAP= ”<<a<<endl;

cout<<”B after SWAP= ”<<b<<endl;

return 0;

void swap(int &a, &b)

int temp;

temp=a;

a=b;

b=temp;

}
10. WAP for Array Rotation.

#include<iostream>

#include<stdlib.h>

using namespace std;

 void leftRotatebyOne(int arr[], int n)

    int temp = arr[0], i;

    for (i = 0; i < n - 1; i++)

        arr[i] = arr[i + 1];

    arr[n-1] = temp;

void leftRotate(int arr[], int d, int n)

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

        leftRotatebyOne(arr, n);

void printArray(int arr[], int n)

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

        cout << arr[i] << " ";

}
 

int main()

    int arr[] = { 1, 2, 3, 4, 5, 6, 7 };

    int n = sizeof(arr) / sizeof(arr[0]);

    leftRotate(arr, 2, n);

    printArray(arr, n); 

    return 0;

11. WAP to remove those characters from the 1st string that are present in

the 2nd string.

#include<iostream>

#define ASCII_SIZE 256

#include<string.h>

using namespace std;

char *RemoveChars(char *string1, char *string2)

{ int *count = (int *)calloc(sizeof(int), ASCII_SIZE);

for(int i = 0; *(string2+i); i++)

count[*(string2+i)]++;

}
int i = 0, j = 0;

while(*(string1 + i))

char temp = *(string1 + i);

if(count[temp] == 0)

*(string1 + j) = *(string1 + i);

j++;

i++;

*(string1+j) = '\0';

return string1;

int main()

char string1[] = "computer";

char string2[] = "programming";

cout<<"Input strings:\n";

cout<<"string1: ";

for (int i = 0; i < strlen(string1); ++i)

cout<<string1[i];

cout<<"\nstring2: ";
for (int i = 0; i < strlen(string2); ++i)

cout<<string2[i];

cout<<"\nOutput: ";

cout<<RemoveChars(string1, string2);

return 0; }

12. WAP to create a function Power() to raise a number n to the power n.

Return the appropriate result if he function takes a Double value for

n and an Integer value for m.

User inputs the value of n and m.

#include<iostream>

#include<math.h>

using namespace std;

double Power(double num, int p)

double r;

r=pow(num, p);

return r;

int main()

double n, res;
int m;

cout<<”Enter the Number”<<endl;

cin>>n;

cout<<”Enter the Power”<<endl;

cin>>m;

res=Power(n, m);

cout<<n<<”^”<<m<<”= ”<<res<<endl;

return 0;

13. WAP to reverse array elements entered by the user.

#include <iostream>

using namespace std;

int main ()

int arr[50], num, temp, i, j;

cout << " Please, enter the total no. you want to enter: ";

cin >> num;

for (i = 0; i < num; i++)

cout << " Enter the element " << i+1 << ": ";
cin >> arr[i];

for ( i = 0, j = num - 1; i < num/2; i++, j--)

temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

cout << "\n Reversed Array: " << endl;

for ( i = 0; i < num; i++)

cout << arr[i] << " ";

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;

for (int x = 0; x < str.length(); x++)


{
char_code = int(str[x]);

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

15. WAP to count the number of Palindrome words in a string.


#include <bits/stdc++.h>
using namespace std;
bool check(string extra)
{
   int len = extra.length();
   transform(extra.begin(), extra.end(), extra.begin(), ::tolower);
   for (int i = 0; i < len; i++,len--){
      if (extra.at(i) != extra.at(len - 1)){
         return false;
   }
   }
   return true;
}
int palindrome(string str, int length)
{
   int count = 0;
   string extra = "";
   for (int i = 0; i < length; i++){
      char temp = str.at(i);
      if (temp != ' '){
         extra = extra + temp;
   }
      else{
         if (check(extra))
            { count++; }
         extra = "";
   }
   }
   return count;
}
int main()
{
   string str = "nitin wants nitin for his company named nitin after nitin";
   str = str + " ";
   int length = str.length();
 cout<<"Count of palindrome words in a sentence are:
“<<palindrome(str, length);

   return 0;
}

16. WAP to multiply Two 4x4 matrices and print the resultant matrix in
tabular form.

#include <iostream>
#include <string>
using namespace std;

int mult( int A[4][4], int B[4][4] )


{

int C[4][4], num;

for (int i=0; i<4; i++){


for(int j=0; j<4; j++){
num = 0;
for(int k=0; k<4; k++){
num += A[i][k]*B[k][j];
}
C[i][j]=num;
cout << num << " ";
}
cout << endl;
}

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

cout<<"Enter the Second Matrix...."<<endl;


for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
cin>>B[i][j];
}
}

mult(A,B);
return 0;
}

17. An Electricity board charges the following rates to domestic users to


discourage large consumption of energy:
For first 100 units - 60P per unit
For next 200 units- 80P per unit
For beyond 300 units- 90P per unit
All users are charged a minimum of rupees 50.00. If the total amount
exceed Rs. 300, then an additional surcharge of 15% is added as well.
WAP to read the names of the users and number of units consumed and
print out the charges along with names.

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

cout<<"Enter the Units Consumed...."<<endl;


cin>>units;
float charge=obj.Charge(units);
charge=charge+50;
if(charge>300)
{
charge=charge+ (0.15*charge);
}

obj.Display(units, charge);

return 0; }

18. WAP to create a pointer to a derived class.


#include<iostream>
using namespace std;
class base
{
protected:
int a;
public:
void seta(int x)
{
a=x;
}
void display(void)
{
cout<<"A: "<<a<<endl;
}
};
class derived : public base
{
int b;
public:
void setb(int y)
{
b=y;
}
void display(void)
{
cout<<"B: "<<b;
}
};
int main()
{
base *ptr;
base obj;
ptr=&obj;
ptr->seta(20);
ptr->display();
derived obja;
derived *p;
*p=obja;
p->setb(200);
p->display();
}

19. WAP to show a Constructor in a derived class.


#include<iostream>
using namespace std;
class A
{
protected:
int a;
public:
A(int x)
{
a=x;
}
void display1()
{
cout<<"Value of A: "<<a<<endl;
}
};
class B
{
protected:
int b;
public:
B(int y)
{
b=y;
}
void display2()
{
cout<<"Value of B: "<<b<<endl;
}
};
class C : public A , public B
{
int c;
public:
C(int x , int y , int f) : A(x) , B(y)
{
c=f;
}
void display3()
{
cout<<"Value of C: "<<c<<endl;
}
};
int main()
{
C obj(4,5,6);
obj.display1();
obj.display2();
obj.display3();
return 0;
}

20. WAP to make a Class Virtual.


#include<iostream>
using namespace std;
class student
{
protected:
int rollno;
public:
void getroll(int);
void putroll();
};
class test : public virtual student
{
protected:
int maths, physics;
public:
void getm(int , int);
void putm();
};
void student :: getroll(int x)
{
rollno=x;
}
void student :: putroll()
{
cout<<"Roll number:"<<rollno<<endl;
}
void test :: getm(int x, int y)
{
maths=x;
physics=y;
}
void test :: putm()
{
cout<<"Maths marks: "<<maths<<endl;
cout<<"Physics marks:"<<physics<<endl;
}
class sports : public virtual student
{
protected:
int sp;
public:
void getsp(int);
void putsp();
};
void sports ::getsp(int x)
{
sp=x;
}
void sports :: putsp()
{
cout<<"Sports marks: "<<sp<<endl;
}
class result : public test , public sports
{
int total;
public:
void display()
{
total=maths+physics+sp;
putroll();
putm();
putsp();
cout<<"Total marks:"<<total<<endl;
}
};
int main()
{
result obj;
obj.getroll(75);
obj.getm(88,91);
obj.getsp(79);
obj.display();
return 0;
}

21. WAP to demonstrate Multi-Level Inheritance.


#include<iostream>
using namespace std;
class stu
{
protected:
int roll;
public:
void getroll();
void putroll();
};
void stu :: getroll()
{
cout<<"Enter the roll number: ";
cin>>roll;
}
void stu :: putroll()
{
cout<<"Roll no.: "<<roll<<endl;
}
class test : public stu
{
protected:
float m1,m2;
public:
void getmarks();
void putmarks();
};
void test :: getmarks()
{
cout<<"Enter the first marks: ";
cin>>m1;
cout<<"Enter the second marks: ";
cin>>m2;
}
void test :: putmarks()
{
cout<<"Marks of "<<"Test 1: "<<m1<<endl<<"Marks of Test 2:
"<<m2<<endl;
}
class result : public test
{
float total;
public:
void display()
{
total=m1+m2;
putroll();
putmarks();
cout<<"Total: "<<total<<endl;
}
};
int main()
{
result res;
res.getroll();
res.getmarks();
res.display();
return 0;
}

22. WAP to demonstrate Multiple Inheritance.


#include<iostream>
using namespace std;
class A
{
protected:
int a;
public:
void geta();
};
void A :: geta()
{
cout<<"Enter the value of A: ";
cin>>a;
}
class B
{
protected:
int b;
public:
void getb();
};
void B :: getb()
{
cout<<"Enter the value of B: ";
cin>>b;
}
class C : public A , public B
{
int c;
public:
void add();
void display();
};
void C :: add()
{
c=a+b;
}
void C :: display()
{
cout<<"Value of A and B: "<<a<<" "<<b<<endl<<endl;
cout<<"Value of addition: "<<c<<endl;
}
int main()
{
C obj;
obj.geta();
obj.getb();
obj.add();
obj.display();
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;
}

24. WAP to overload the ‘+’ operator using Friend Function.


#include<iostream>
using namespace std;
class A
{
int a;
public:
void get()
{
cout<<"Enter the number: ";
cin>>a;
}
void friend operator+(A , A);
};
void operator+(A obj1 , A obj2)
{
cout<<"Addition of the numbers: "<<obj1.a+obj2.a;
}
int main()
{
A obj1,obj2;
obj1.get();
obj2.get();
obj1+obj2;
return 0;
}

25. WAP to overload the ‘*’ operator.


#include<iostream>
using namespace std;
class A
{
int a;
public:
void get()
{
cout<<"Enter the number: ";
cin>>a;
}
void operator*(A obj1)
{
cout<<"Addition of the numbers: "<<a+obj1.a;
}
};
int main()
{
A obj1, obj2;
obj1.get();
obj2.get();
obj1*obj2;
return 0;
}

26. WAP to count the number of objects created in a class.


#include<iostream>
using namespace std;
class Class
{
public:
static int c;
Class()
{
c=c+1;
}
static int count()
{
return c;
}
};
int Class::c;
int main()
{
Class obj1, obj2, obj3, obj4;
int cnt;
cnt = Class::count();
cout<<"Number of Objects :- "<<cnt<<endl;
}

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;

cout<<"Enter Your Choice"<<endl;


cin>>ch;

switch(ch)
{
case 1:
obj.Paying_Car();
break;
case 2:
obj.Non_Paying_Car();
break;
case 3:
obj.Display();
break;
case 4:
exit(1);
}
}
}

28. Write a C++ program for: -

i. A function to read double type of number from the keyboard.


ii. A function to calculate division of these two numbers.
iii. A try block to throw an exception when a wrong type of data is keyed
in.
iv. A try block to detect and throw an exception if the NULL condition
divide by 0 occurs.

Appropriate catch block to handle the exception thrown.

#include<iostream>

using namespace std;

class Test
{

public:

double n1, n2;

void input()

cout<<”Enter Two Double Type numbers”<<endl;

cin>>n1>>n2;

void divide()

try

if(cin.fail())

throw “Invalid Input”;

if(n1==0 || n2==0)

throw 0;

if(n1 > n2)

cout<<”Answer :- ”<<n1/n2<<endl;

else

cout<<”Answer :- ”<<n2/n1<<endl;

catch(const int n)

cout<<”Division by”<<n<<” is not possible.”<<endl;

}
catch(const char* str)

cout<<str;

};

int main()

Test obj;

obj.input();

obj.divide();

return 0;

29. WAP to do read and write operations on a file.


#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;
class str
{
int id;
char name[25];
public:
void getstr()
{
cout<<"Enter the ID:";
cin>>id;
cout<<"Enter the name:";
cin>>name;
cout<<"Student details are:";
cout<<"ID= "<<id<<endl;
cout<<"Name= "<<name<<endl;
}
};
int main()
{
str s;
ofstream file;
file.open("str.dat");
char op;
do
{
s.getstr();
file.write((char*)&s,sizeof(s));
cout<<"one row is added"<<endl;
cout<<"Any more student.[Y/N]"<<endl;
cin>>op;
}
while(op=='y'||op=='Y');
file.close();
}
30. WAP to create a template and pass integer and float array to it and print
their sum.
#include<iostream>
using namespace std;
template < class t>
t sum(t a[],int size)
{
t s = 0;
for(int i=0 ; i< size;i++)
s=s+a[i];
return s;
}
int main()
{
int x[5]={1,5,7,9,12};
float y[3]= {5.2,2.1,3.5};
cout<<" Int array sum:"<<sum(x,5)<< endl;
cout<<" float array sum:"<< sum (y,3)<< endl;
return 0;
}

You might also like