You are on page 1of 111

Program 1: swapping of two numbers

#include <iostream>
int main()
{
for (int g = 0; g < 50; g++)
{
std::cout << "=";
}
int a, b;
std::cout << "\nEnter two numbers : ";
std::cin >> a >> b;
std::cout << "\nBefore swapping a : " << a << " b : " << b;
a = a + b;
b = a - b;
a = a - b;
std::cout << "\nAfter swapping a : " << a << " b : " << b;
std::cout << "\n";
return 0;
}
Output-:

Source code :- with using third variable


#include <iostream>

int main()
{

for (int g = 0; g < 50; g++)


{
std::cout << "=";
}

int a, b, temp;
std::cout << "\nEnter first number :: ";
std::cin >> a;
std::cout << "\nEnter second :: ";
std::cin >> b;

std::cout << "\nBefore Swapping: First Number :: " << a << "
Second Number :: " << b;

temp = a;
a = b;
b = temp;
std::cout << "\nAfter Swapping: First Number :: " << a << " Second
Number :: " << b;
std::cout << "\n";
return 0;
}

Program 1 B: Factorial of a number


#include <iostream>
int fact(int n)
{
if (n > 1)
return n * fact(n - 1);
else
return 1;
}//end of fact()
int main()

int n;

std::cout << "\nEnter a positive integer :: ";


std::cin >> n;
std::cout << "Factorial of " << n << " :: " << fact(n);
std::cout << "\n";
return 0;
}

Program 1 C prime numbers


#include <iostream>
using namespace std;
int main()
{
for (int g = 0; g < 50; g++)
{
cout << "=";
}
int num, i, n;
cout << "\nEnter the Nth Number : ";
cin >> n;

cout << endl << "Prime numbers upto " << n << " are : 2, ";

for (num = 2; num <= n; num++)


{

for (i = 2; i <= (num / 2); i++)


{

if (num % i == 0) {
i = num;
break;
}
}
if (i != num)
{
cout << num << ", ";
}
}
cout << "\n\n";
return 0;
}
Output

Program 1 D: ASCII Value


#include<iostream>
int main()
{
for (int g = 0; g < 50; g++)
{
std::cout << "=";
}

char ch ;

std::cout << "\n";


char c;
for (ch = 'A'; ch <= 'Z'; ch++)
{
std::cout << ch << " :: " << (int)ch <<" ||";
}

std::cout << "\n\n";


return 0;
}
Output-:

Program 1 E: Reverse of an array

#include<iostream>
#define max 50
void swap(int a[], int size)
{
int temp;
int i = 1, j = size;
while (i < j)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
i++, j--;
}
}

int main()
{
for (int g = 0; g < 50; g++)

int size;
int A[max];

std::cout << "\nEnter the size of an array : ";


std::cin >> size;
std::cout << "\nEnter " << size << " Elements of array :" <<
std::endl;
for (int i = 1; i <= size; i++)
{
std::cout << "A[" << i << "] : " ;
std::cin >> A[i];
std::cout << "\n";
}
std::cout << "\nEntered array is : " << std::endl;
for (int i = 1; i <= size; i++)
{
std::cout << "A[" << A[i] << "]";
std::cout << "\n";
}
swap(A, size);
std::cout << "\nReverse Array : " << std::endl;
for (int i = 1; i <= size; i++)
{
std::cout << "A[" << A[i] << "]";
std::cout << "\n";
}
}
Output-:
Program 1 E: Reverse of an array

#include<iostream>
float area(float radius)
{
return (3.14 * radius * radius);
}

float circum(float radius)


{
return(2 * 3.14 * radius);
}
int main()
{
for (int g = 0; g < 50; g++)
{
std::cout << "=";
}
std::cout << "\n";
int radius;
std:: cout << "\n Enter Radius of Circle: ";
std::cin >> radius;
std::cout << "\n Area of Circle : " << area(radius);
std::cout << "\n Circumference of Circle : " << circum(radius);
return 0;
}
Program 2: Program to understand the concept of Classes and objects.

#include <iostream>
#include <string>
class example
{
private:
int id;
std::string name;
public:
void input_e1()
{
std::cout << "Enter Name Of The Student 1 :";
std::getline(std::cin, name);
std::cout << "\nEnter Enrollment No Of The Student 1 :";
std::cin >> id;
std::cin.ignore();
}

void display_e1()
{
std::cout << "\nName Of The Student 1 :" << name;
std::cout << "\nEnrollment No Of The Student 1" << id;

void input_e2();
void display_e2();

};

void example :: input_e2()


{
std::cout << "\nEnter Name Of The Student 2 :";
std::getline(std::cin, name);
std::cout << "\nEnter Enrollment No Of The Student 2 :";
std::cin >> id;
}//in_2

void example :: display_e2()


{
std::cout << "\nName Of The Student 1 :" << name;
std::cout << "\nEnrollment No Of The Student 1" << id;
}//out_2
int main()
{
for (int g = 0; g < 50; g++)

std::cout << "\n";


example e1, e2;
e1.input_e1();
e2.input_e2();
std::cout << "\n-------------------------\n";
e1.display_e1();
std::cout << "\n-------------------------\n";
e2.display_e2();
std::cout << "\n";
return 0;
}
Program 3: Create a 'DISTANCE' class with:
- feet and inches as data members
- member function to input distance
- member function to output distance - member
function to add two distance objects .Write a main function to create
objects of DISTANCE class. Input two distances and output the sum.
#include <iostream>
class distance
{
private:
int inch, feet;
public:
void input();
void output();
void add(distance, distance);
};

void distance::input()
{

std::cout << "\nEnter Value of feet : ";


std::cin >> feet;
std::cout << "\nEnter value of inches : ";
std::cin >> inch;
}

void distance::output()
{

std::cout << "Feet : " <<feet<< " and " << "Inch : " << inch;
std::cout <<
"\n===========================================\n";
}

void distance::add(distance d1, distance d2)


{
feet = d1.feet + d2.feet;
inch = d1.inch + d2.inch;
feet = feet + (inch/ 12);
inch = inch % 12;
}

int main()
{
for (int g = 0; g < 50; g++)
{
std::cout << "=";
}//end of for
distance d1, d2, d3;
d1.input();
std::cout << "\n=======================";
d2.input();
std::cout << "\nFirst Distance : ";
d1.output();
std::cout << "\nSecond Distance : " << std::endl;
d2.output();
std::cout << "\n\nnAddtiton of two distance is :";
d3.add(d1, d2);
d3.output();

return 0;
}
Program 4: Create a class called ‘EMPLOYEE’ that has
a. EMPCODE and EMPNAME as data members
b. Member function getData() to input data
c. Member function display() to input data
Write a main function to create EMP, an array of EMPLOYEE objects
that accepts and display the details of employee.
#include <iostream>
#include<string>
#include<process.h>

#define max 50

class employee
{
private:
int e_id;
std::string e_name, dept;
int salary;

public:
void input()
{
std::cout << "\nEnter employee's ID no. : ";
std::cin >> e_id;
std::cin.ignore();
std::cout << "\nEnter employee's name : ";
std::getline(std::cin, e_name);
std::cout << "\n3Enter current salary of the employee : ";
std::cin >> salary;
std::cin.ignore();
std::cout << "\nEnter employee's department :";
std::getline(std::cin, dept);

}
void output();
int searchz(int);

};

int employee::searchz(int look)


{
if (look == e_id)
{
output();
return 1;
}
}

void employee::output()
{

{
std::cout << "\n-----Employee Detail-----";
std::cout << "\nNAME : " << e_name;
std::cout << "\nID no. : " << e_id;
std::cout << "\nEmployee salary :" << salary;
std::cout << "\nEmployee department :" << dept;
}
std::cout <<
"\n==============================================\n";
}

int main()
{

int look, find = 0, no = 0;


static int count = 0;
int ch;
employee emp[max];

for (int g = 0; g < 50; g++)


{
std::cout << "=";
}//end of for
do
{

std::cout << "\n1. Enter details of new employee :\n2. Display


detail of the Employee :\n3. Search for employee using ID no :\n4. Exit
: \nEnter your choice : ";
std::cin >> ch;
switch (ch)
{
case 1:
{
std::cout << "\nEnter no. of employee : ";
std::cin >> no;
no = no + count;
for (static int i = 1; i <= no; i++)
{
std::cout << "\nEmployee : " << i;
emp[i].input();
std::cout << "\n------------------------\n";
count++;

}
break;
}

case 2:
{
for (int j = 1; j <= no; j++)
{
emp[j].output();
}
break;
}//c2
case 3:
{
std::cout << "\nEnter id no. of the employee you want to search
:";
std::cin >> look;
for (int i = 1; i <= no; i++)
{
find = emp[i].searchz(look);
}
(find == 1) ? std::cout << "----Searching complete----\n" :
std::cout << "----Invalid ID no. plz try again----\n";

break;
}
case 4: std::cout << "\nTotal : " << no;
exit(0);
}
} while (ch != 4);
return 0;
}
Program 5: Program to demonstrate the concept of Static variables,
function and object numbers.
#include <iostream>
#include <string>
using namespace std;

void demo()
{

static int count = 0;


cout << count << " ";
count++;
}

int main()
{

for (int g = 0; g < 50; g++)


{
std::cout << "=";
}//end of for
std::cout << "\n";
for(int i = 0; i < 5; i++)
demo();
std::cout << endl;
return 0;
}

Static member function

#include <iostream>
static int value(int x)
{
return x++;
}
int main()
{

for (int g = 0; g < 50; g++)


{
std::cout << "=";
}
std::cout << "\n";
std::cout << "\n";
for (int i = 1; i < 10; i++)
{
std::cout << " " << value(i);
}
std::cout << std::endl;
return 0;
}

Static Objects
#include <iostream>
class Base
{
public:
int func()
{
int a = 20;
std::cout << "The value of a : " << a;
return 0;
}
};
int main()
{
for (int g = 0; g < 50; g++)
{
std::cout << "=";
}
std::cout << "\n";
static Base b;
b.func();
std::cout << "\n";
return 0;
}
Output:
Program 6: Program to make the percentage calculator and assign the
grades according to the percentage obtained.

#include <iostream>
#include <string>
#include <iomanip>
#include <stdlib.h>
#include <process.h>

#define max 60

using namespace std;


using std::string;

class student_fun
{
private:
int e_no;
float total = 0;
std::string name;
char course[10];
float m[5];
public:
void getdata()
{
cin.ignore();
cout << "\nEnter name of the student : ";
std::getline(std::cin,name);
cout << "\nEnter the enrollment number of the student :";
cin >> e_no;
cout << "\nEnter couse : ";
cin >> course;
cout << "\nEnter marks of 5 subjects :" << endl;
for (int i = 0; i < 5; i++)
{
cout << "Subject " << i + 1 << "th : ";
cin >> m[i];
total = total + m[i];
cout << "\n";
}
int z = 0;
while (z < 50)
{
cout << '=';
z++;
}
cout << "\n";
}
void display()
{
int j = 0;
cout << "\n";
cout << setw(7) << "Name" << setw(30) << "Enrollment no."
<< setw(15) << "Course" << endl;
cout << "--------------------------------------------------------------------
----------------------------------------\n";
cout << setw(9) << name << setw(26) << e_no << setw(25)
<< course << endl;
cout << "\n";
cout << "--------------------------------------------------------------------
----------------------------------------\n";
cout << "OOPS" << setw(8) << "CA" << setw(12) << "MATHS"
<< setw(7) << "C++" << setw(17) << "Accounts" << endl;
cout << "--------------------------------------------------------------------
----------------------------------------\n";
for (int i = 0; i < 5; i++)
{
cout << m[i] << setw(10);
}
cout << "\n";
while (j < 50)
{
cout << "=";
j++;
}
}
void result()
{
int k = 0;
cout << "\nName" << setw(15) << "Enrollment no." <<
setw(15) << "Couse" << setw(15) << "GPA" << setw(15) << "Grade" <<
endl;
cout << "--------------------------------------------------------------------
----------------------------------------\n";
cout << name << setw(19) << e_no << setw(15) << course <<
setw(19) << perce(total) << setw(20) << grade(perce(total));
cout << "\n";
while (k < 50)
{
cout << "=";
k++;
}
}//end of result

float perce(float marks)


{
float percentage;
percentage = (marks / 500) * 100;
return percentage;

}
string grade(float per)
{
string str;

if (per >= 90 && per <= 100)


{
str = "A+";
return str;
}
if (per >= 80 && per < 90)
{
str = "A";
return str;
}
if (per >= 70 && per < 80)
{
str = "B+";
return str;
}
if (per >= 60 && per < 70)
{
str = "B";
return str;
}
if (per >= 50 && per < 60)
{
str = "C+";
return str;
}
if (per >= 40 && per < 50)
{
str = "C";
return str;
}
if (per >= 33 && per < 40)
{
str = "D";
return str;
}
if (per >= 0 && per < 33)
{
str = "F";
return str;
}
}
int search(int se)
{
if (se == e_no)
{
display();
return 1;
}
else
return 0;
}
};
int main()
{
int j, ch, no = 0;
int se, find = 0;
student_fun stud[max];

for (int g = 0; g < 50; g++)


{
std::cout << "=";
}
cout << "**********STUDENT RERSULT**********" << endl;
do {
cout << "\n";
cout << "1.Enter record of the student(s)\n2.Display student
details\n3.Display students result\n4.search for student using
enriollment numeber\n4.Exit" << endl;
cin >> ch;
switch (ch)
{
case 1:
{
cout << "\nEnter no. of students : ";
cin >> no;
for (static int i = 1; i <= no; i++)
{
stud[i].getdata();
}
break;
}
case 2:
{
cout << "\n**********Student
Details**********\n\n";
cout << "\n\n";
for (int i = 1; i <= no; i++)
{
stud[i].display();
}
break;
}
case 3:
{
cout << "\n**********Student
Result**********\n\n";
for (int i = 1; i <= no; i++)
{
stud[i].result();
}
break;
}
case 4:
{
cout << "\nEnter enrollment number to be searched :
";
cin >> se;
for (int i = 0; i <= no; i++)
{
find = stud[i].search(se);
}
(find == 1) ? cout << "\n**Search complete press any
key to continue**" : cout << "\nInvalid Enrollment number please
continue";
break;
}
case 5:
{
exit(3);
}

}
} while (ch != 5);

return 0;
}
Program 7: Program to demonstrate call by value and call by
reference method to print Fibonacci series using functions
#include<iostream>
using namespace std;
void fibo(int x)
{
cout << " By call by value method : \n";
int a = 0, b = 1, c;
cout << a << " " << b << " ";
for (int i = 2; i < x; i++)
{
c = a + b;
cout << c << " ";
a = b;
b = c;
}
cout << endl;
}
void fibo1(int* x)
{
cout << "\nBy call by reference method \n";
int a = 0, b = 1, c;
cout << a << " " << b << " ";
for (int i = 2; i < *x; i++)
{
c = a + b;
cout << c << " ";
a = b;
b = c;
}
}
int main()
{
for (int g = 0; g < 50; g++)
{
std::cout << "=";
}
int n;
cout << "\n\nEnter a posiive integer :";
cin >> n;
cout << endl;
fibo(n);
fibo1(&n);
return 0;
}
Output-:
Program 8: Program to pass objects as function arguments By value
and By reference
#include<iostream>
using namespace std;
class call
{
int x, sum;
public:
void input()
{
cout << "Enter a positive integer :: ";
cin >> x;
}
void value(call a, call b)
{
cout << "\nBy call by value ::";
sum = a.x + b.x;
cout<<"\n" << sum;
}

void ref(call& a, call& b)


{
cout << "\nBy call by ref. ::";
sum = a.x + b.x;
cout << "\n" << sum;
}

};

void main()
{
for (int g = 0; g < 50; g++)
{
std::cout << "=";
}
cout << "\n\n";
call a, b, c;
a.input();
b.input();
c.value(a, b);
c.ref(a, b);

}
Output-:
Program 9: Write a main function to add two COMPLEX objects.
#include<iostream>
class base
{
int val1, val2;
public:
void get()
{
std::cout << "Enter two values:";
std::cin >> val1 >> val2;
}
friend float sum(base ob);
};

float sum(base ob)


{
return float(ob.val1 + ob.val2);
}

void main()
{
for (int g = 0; g < 50; g++)
{
std::cout << "=";
}
std::cout << "\n\n";
base obj;
obj.get();
std::cout << "\n Sum of two numbers is : " << sum(obj);
}
Output-:
Program 10: Create a class 'COMPLEX' to hold a complex number.
write a friend function to add two complexes:
numbers. Write a main function to add two COMPLEX objects.
#include <iostream>
class complex
{
private:

float img, real;

public:
void input()
{
std::cout << "\n";
std::cout << "Enter the real part of the number :";
std::cin >> real;
std::cout << "\n";
std::cout << "enter the imaginary part of the :";
std::cin >> img;

}//end of imput
friend complex addition(complex, complex);//f1
friend complex subtraction(complex, complex);//f2
friend void display(complex);//f3

};//end of class

complex addition(complex n1, complex n2)


{
complex addi;
addi.real = n1.real + n2.real;
addi.img = n1.img + n2.img;
return addi;

}//end of addition

complex subtraction(complex n1, complex n2)


{

complex sub;
sub.real = n1.real - n2.real;
sub.img = n1.img - n2.img;

return sub;
}
void display(complex n)
{
if (n.real == 0 && n.img == 0)
{
std::cout << "\nNumber is purely real : 0 ";
}
else
if (n.real == 0 && n.img != 0)
{
std::cout << "\nNumber is purely imaginary :";
std::cout << n.img << "i";
}
else
if (n.img == 0 && n.real != 0)
{
std::cout << "\nNumber is purely real :";
std::cout << n.real;
}
else
{
std::cout << "\n";
std::cout << n.real << "+" << n.img << "i";
}
}
int main()
{
for (int g = 0; g < 50; g++)
{
std::cout << "=";
}
std::cout << "\n";
complex num_1, num_2, add, sub;
std::cout << "Enter first number :- ";
num_1.input();
std::cout << "\nEnter second number :- ";
num_2.input();
add = addition(num_1, num_2);
sub = subtraction(num_1, num_2);
for (int k = 0; k < 35; k++)
{
std::cout << "=";
}
std::cout << "\nAddition of two complex number is :- ";
display(add);
std::cout << "\n\n";
for (int i = 0; i < 35; i++)
{
std::cout << "=";
}
std::cout << "\nSubtraction of two complex number is :- ";
display(sub);
std::cout << "\n\n";
return 0;
}
Output-:
Program 11: Program to implement overloading of Unary operator
using friend function & member function.
#include<iostream>
class u_operator
{
int x, y;
public:
u_operator(int a, int b)
{
x = a;
y = b;
}
void display()
{
std::cout << "\n x : " << x << std::endl;
std::cout << "\n y : " << y << std::endl;
}
friend void operator -(u_operator&);
};
void operator -(u_operator& i)
{
i.x = -i.x;
i.y = -i.y;
}
int main()
{
std::cout << "\n\n";
u_operator a(2, 3), b(-15, 17);
-a;
-b;
std::cout << "Class Object A :: " << std::endl;
a.display();
std::cout << "\nClass Object B :: " << std::endl;
b.display();
return 0;
}
Output-:
Program 12: Program to implement overloading of Binary operator
using friend function & member function.
#include <iostream>
class b_operator
{
int a;

public:

b_operator()
{
std::cout << "\nEnter fist number : ";

std::cin >> a;
}

friend void operator ++(b_operator&);


friend void display1(b_operator&);

friend void operator --(b_operator&);


friend void display2(b_operator&);
};
void operator ++(b_operator& x)
{
++x.a;
}

void operator --(b_operator& x)


{
--x.a;
}
void display1(b_operator& x)
{
std::cout << "\nNumber after incrementing :: " <<x.a;
}

void display2(b_operator& x)
{
std::cout << "\nNumber after decrementing two times :: " <<
x.a;
}

int main()
{
std::cout << "\n\n";

b_operator x;
++x;

display1(x);
--x;
--x;
std::cout << "\n";
display2(x);
return 0;
}
Output-:
Program 13: Program to check demonstrate function overloading, to
check whether the given number and string is palindrome or not.
#include <iostream>
#include <string.h>

class palin
{
public:

int rev(std::string str)


{
std::string temp;
temp = str;
int len = str.size();

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


{
if (temp[i] != temp[len - i - 1])
{
return 0;
}
return 1;
}

}
int rev(int val)
{
int temp = val;
int reve = 0 , rem;

while(temp!= 0)
{
rem = temp % 10;
reve = reve * 10 + rem;
temp = temp/10;
}

if (reve == val)
return 1;
else
return 0;
}
};
int main()
{

std::cout << "\n\n";

palin p;
std::string val = "was it a cat I saw";
std::cout << "Input string is ::" << val << std::endl;
int test0 = p.rev(val);
(test0 == 1) ? std::cout << "String is palindrom" : std::cout <<
"String is not palindrom" << std::endl;
std::cout <<"\n\n"<< std::endl;
int num = 535;
std::cout << "Input number is ::" << num << std::endl;
int test1 = p.rev(num);
(test1 == 1) ? std::cout << "Number is palindrom" : std::cout <<
"Number is not palindrom" << std::endl;
std::cout << "\n\n" << std::endl;
return 0;
}
Output-:
Program 14: Write a C++ program to implement function overloading
in order to compute power (m, n) where i ) m is double and n is int ii)
m and n are int.
#include<iostream>
#include<math.h>

int power(int m, int n)


{
int result;
result = pow(m, n);
return result;
}

double power(double m, int n)


{
double result1;
result1 = pow(m, n);
return result1;
}

int main()
{
std::cout << "\n\n";
int m, n, k;
double d;

std::cout << "Enter The Two Integer Values:";


std::cin >> m >> n;

std::cout << power(m, n);

std::cout << "Enter one Double and one Integer value:";


std::cin >> d >> k;

std::cout << std::endl << power(d, k);

return 0;
}
Output-:
Program 15: Program to find the largest of three numbers using inline
function.
#include <iostream>
inline int larger (int a, int b, int c) {return a > b? (a > c? a: c): (b >
c? b: c);}
int main ()
{
std::cout << "\n\n";
int x, y, z;
std::cout << "Enter first number :: ";
std::cin >> x;
std::cout << "Enter second number :: ";
std::cin >> y;
std::cout << "Enter third number :: ";
std::cin >> z;
int greatest = larger(x, y, z);
std::cout << "Largest of three number is :: ::" << greatest <<
std::endl;
return 0;
}
Output-:
Program 17: Write a program to implement constructor and
destructor in c++;
#include <iostream>
using namespace std;
class sample
{
int a = 0 , b = 0, sum = 0;
public:
sample()
{
cout << "\nConstructor is called";
cout << "\nEnter first number ::";
cin >> a;
cout << "\nEnter Second number ::";
cin >> b;
sum = a + b;
}
void display()
{
cout << "\nSum of number is :: " << sum;
}
~sample()
{
cout << "Destructor '~sample' is called as object
goes out of scope\n\n";
}
};
int main()
{
std::cout << "\n\n";
sample s1;
s1.display();
cout << "\n\n";
return 0;
}
Output-:
Program 18: Program to demonstrate the use of this pointer.
#include <iostream>
using namespace std;
class this_example
{
private:
int x, y;
int diff = 0;
public:
this_example(int a, int b)
{
this->x = a;
this->y = b;
cout << "\nNumber first ::" << a;
cout << "\nNumber Second ::" << b;
diff = x - y;
}
void display()
{
cout << "\nDifference of two number is :: " << diff;
}
};
int main()
{
std::cout << "\n\n";
this_example e1(10, 7);
e1.display();
cout << endl;
return 0;
}
Output-:
Program 19: Program to find whether the roots of a quadratic
equation are real or not.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
std::cout << "\n\n";
float a, b, c, x1, x2, dn, rl, imag;
cout << "Enter coefficients a, b and c: ";
cin >> a >> b >> c;
dn = b * b - 4 * a * c;
if (dn > 0) {
x1 = (-b + sqrt(dn)) / (2 * a);
x2 = (-b - sqrt(dn)) / (2 * a);
cout << "Roots are real and different." << endl;
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
}
else if (dn == 0) {
cout << "Roots are real and same ." << endl;
x1 = -b / (2 * a);
cout << "x1 = x2 =" << x1 << endl;
}
else {
rl = -b / (2 * a);
imag = sqrt(-dn) / (2 * a);
cout << "Roots are not real" << endl;
cout << "x1 = " << rl << "+" << imag << "i" << endl;
cout << "x2 = " << rl << "-" << imag << "i" << endl;
}
return 0;
}
Output-:
Program 21: Program to implement single inheritance.
#include <iostream>
using namespace std;
class base
{
public:
int x;
void getdata()
{
cout << "Enter the first number :: "; cin >> x;
}
};
class derive : public base
{
private:
int y;
public:
void readdata()
{
cout << "Enter second number :: "; cin >> y;
}
void product()
{
cout << "Product of number = " << x * y;
}
};
int main()
{
std::cout << "\n\n";
derive a;
a.getdata();
a.readdata();
a.product();
return 0;
}
Output-:
Program 22: Program to implement multi inheritance.
#include<iostream>
using namespace std;
class B
{
public:
B()
{
cout << "\nB ";
}
~B()
{
cout << "\n~B ";
}
};
class C
{
public:
C()
{
cout << "\nC ";
}
~C()
{
cout << "\n~C ";
}
};
class D : public B
{
C data_;
public:
D()
{
cout << "\nD " << endl;
}
~D()
{
cout << "\n~D ";
}
};
int main()
{
D d;
return 0;
}
Output-:
Program 23: WAP to implement multilevel inheritance
#include <iostream>
using namespace std;
class base
{
public:
int x;
void getdata()
{
cout << "Enter value of x= "; cin >> x;
}
};
class derive1 : public base
public:
int y;
void readdata()
{
cout << "\nEnter value of y= "; cin >> y;
}
};
class derive2 : public derive1
{
private:
int z;
public:
void indata()
{
cout << "\nEnter value of z= "; cin >> z;
}
void product()
{
cout << "\nProduct= " << x * y * z;
}
};
int main()
{
derive2 a;
a.getdata();
a.readdata();
a.indata();
a.product();
return 0;
}
Program 24 : WAP to implement hierarchical inheritance.
#include <iostream>
using namespace std;

class A
{
public:
int x, y;
void getdata()
{
cout << "\nEnter value of x and y:\n"; cin >> x >> y;
}
};
class B : public A
{
public:
void product()
{
cout << "\nProduct= " << x * y;
}
};
class C : public A
{
public:
void sum()
{
cout << "\nSum= " << x + y;
}
};
int main()
{
B obj1;
C obj2;
obj1.getdata();
obj1.product();
obj2.getdata();
obj2.sum();
return 0;
}
Program 25 : WAP to implement hybrid inheritance.
#include <iostream>
using namespace std;

class A
{
public:
int x;
};
class B : public A
{
public:
B()
{
x = 10;
}
};
class C
{
public:
int y;
C()
{
y = 4;
}
};
class D : public B, public C
{
public:
void sum()
{
cout << "Sum= " << x + y;
}
};

int main()
{
D obj1;
obj1.sum();
return 0;
}
Program 26: WAP to implement unary operator overloading.
#include <iostream>
using namespace std;

class Distance {
private:
int feet;
int inches;

public:

Distance() {
feet = 0;
inches = 0;
}
Distance(int f, int i) {
feet = f;
inches = i;
}

void displayDistance() {
cout << "F: " << feet << " I:" << inches <<endl;
}

Distance operator- () {
feet = -feet;
inches = -inches;
return Distance(feet, inches);
}
};

int main() {
Distance D1(11, 10), D2(-5, 11);
-D1;
D1.displayDistance();
-D2;
D2.displayDistance();
return 0;
}
Program 28 : WAP to implement binary operator overloading.
#include<iostream.h>
#include<conio.h>

class Rectangle
{

int L,B;

public:

Rectangle()
{
L = 0;
B = 0;
}

Rectangle(int x,int y)
{
L = x;
B = y;
}
Rectangle operator+(Rectangle Rec)
{
Rectangle R;

R.L = L + Rec.L;
R.B = B + Rec.B;

return R;
}

void Display()
{
cout<<"\n\tLength : "<<L;
cout<<"\n\tBreadth : "<<B;
}

};

void main()
{
Rectangle R1(2,5),R2(3,4),R3;

cout<<"\n\tRectangle 1 : ";
R1.Display();

cout<<"\n\n\tRectangle 2 : ";
R2.Display();

R3 = R1 + R2; Statement 1

cout<<"\n\n\tRectangle 3 : ";
R3.Display();
}
Program 29 : WAP to implement nested class in C++.
#include<iostream>

using namespace std;

class enclose

private:

int x;

class nest

private :

int y;

public:
int z;

void prn()

y=3;z=2;

cout<<"\n The product of"<<y<<'*'<<z<<"= "<<y*z<<"\n";

};
nest n1;

public:

nest n2;

void square()
{

n2.prn();

n2.z=4;

cout<<"\n The product of " <<n2.z<<'*'<<n2.z<<"=


"<<n2.z*n2.z<<"\n";

cout<<"\n The product of " <<x<<'*'<<x<<"= "<<x*x; }

};
int main()

enclose e;

e.square();

}
Program 30 : WAP to swap two numbers using function templates
#include<iostream>
using namespace std;

template <class T>

void Swap(T &x, T &y) {

T temp;

temp = x;
x = y;
y = temp;
}

int main() {

int x, y;

cout << "Enter two numbers:";


cin >> x>>y;
cout << "Before Swap:";

cout << "\nx value is:" << x;


cout << "\ny value is:" << y;

Swap(x, y);

cout << "\n\nAfter Function Templates:\n";

cout << "\nx value is:" << x;


cout << "\ny value is:" << y;

return 0;
}
Program 31 : WAP to demonstrate the concept of template
overloading.
#include<iostream>
using namespace std;
int sum (int a, int b)
{
return a+b;
}
double sum (double a, double b)
{
return a+b;
}
int main ()
{
cout << sum (10,20) << '\n';
cout << sum (1.0,1.5) << '\n';
return 0;
}
Program 32 : To implement file handling concept using sequential
access.
#include <iostream>
#include <fstream>
using namespace std;
#include <string>

int main()
{
double payroll = 0.0;
ofstream outFile;

outFile.open("Introductory 19.txt", ios::app);

if (outFile.is_open())
{
while (payroll != -1)
cout << "Enter payroll (-1 to end)";
cin >> payroll;
}
else
cout << "Introductory 19.txt could not be opened" << endl;
return 0;
}
Program 34 : WAP to demonstrate exception handling in C++.
#include <iostream>
using namespace std;

int main()
{
int x = -1;

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

You might also like