You are on page 1of 73

Subject Name: Object Oriented Programming With C++

Subject code:BCS18L02
Regulation: 2022-2023
RECORD NOTEBOOK

BCS18L02-OBJECT ORIENTED PROGRAMMING WITH C++ LAB

2022-2023(ODD SEMESTER)

DEPARTMENT
OF
COMPUTER SCIENCE AND ENGINEERING

NAME :YADAVA THARUN

REGISTER NO: 211191101167

COURSE: B.TECH – CSE-DS(AI)

YEAR/SEM/SEC : || / ||| / “C”


BONAFIDE CERTIFICATE
Register no: 211191101167
Name of the lab : OBJECT ORIENTED PROGRAMMING WITH C++ LAB(BCS18L02)

Department : COMPUTER SCIENCE AND ENGINEERING

Certified that this is the bonafide record of work done by YADAVA THARUN
of II Year.B.Tech.[CSE DS(AI),Sec-C in the OBJECT ORIENTED PROGRAMMING
WITH C++ LAB during the year 2022-2023.

Signature of Lab-in-Charge Signature of Head Of Dept

submitted for the practical examination held on

Internal Examiner External Examiner


EX.NO DATE NAME OF EXERCISE PAGE NO SIGN
1a 11/08/22 Program for checking prime
number
1b 11/08/22 Program to implement
switch-case statement and do
While

1c 25/08/22 Program to display the square


of numbers from1 to 5 using
for loop

1d 25/08/22 Program for even number


checking using while loop
2a 01/09/22 Program to display the book
details using Structure
2b 01/09/22 Program to display the
employee details using union
3 04/09/22 Program to understand
pointer arithmetic
4a 04/09/22 Program to find the smallest
number using function
4b 15/09/22 Program to find the factorial
of a number using recursion
5 15/09/22 Program to find the cube of a
number using inline function
6a 22/09/22 Program to swap two
numbers using call by value
6b 22/09/22 Program to swap two
numbers using call by
reference
7 06/10/22 Programs to Understand
Storage Specifiers
8 06/10/22 Program to add two numbers
using constructor and
destructor
9 20/10/22 Program to display the value
using this pointer
10a 20/10/22 Program to implement
Multiple inheritances –
Access Specifiers
10b 27/10/22 Program to implement
Hierarchical inheritance –
Function Overriding /Virtual
Function
11a 27/10/22 Program for Unary operator
as member function
11b 29/10/22 Program to implement binary
operator as member function
12a 29/10/22 Programs to implement
Friend Function
12b 31/10/22 Programs to implement
Friend Class
13 31/10/22 Program to implement class
template
EX NO: 1A - PROGRAM FOR CHECKING PRIME NUMBER

DATE: - 11/08/2022

AIM:
To find whether a given number is prime or not

ALGORITHM:
1. Start the program
2. Initialize the variables
3. This program takes a positive integer from user and stores it in variable n.
4. Then, for loop is executed which checks whether the number entered by user
is perfectly divisible by i or not.
5. The for loop initiates with an initial value of i equals to 2 and increasing the value
of i in each iteration.
6. If the number entered by user is perfectly divisible by i then, isPrime is set to false
and the number will not be a prime number.
7. But, if the number is not perfectly divisible by i until test condition i<=n/2 is true
means, it is only divisible by 1 and that number itself. So, the given number is a prime
number.

1
PROGRAM
// To find whether a given number is prime or not
#include <iostream.h>
#include <conio.h>

int main()
{
int n, i;
bool isPrime = true;
clrscr();
cout << "Enter a positive integer: ";
cin >> n;
for(i = 2; i <= n / 2; ++i)
{
if(n % i == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
cout << "This is a prime number";
else
cout << "This is not a prime number";
getch();
return 0;
}

2
OUTPUT

RESULT:
Thus the C++ program for checking prime number was verified and executed
successfull

3
EX NO: 1B PROGRAM TO IMPLEMENT SWITCH-CASE STATEMENT AND DO
WHILE

DATE: - 11/08/2022

AIM :
To implement the switch case statement and do while in C++

ALGORITHM:

1. Start the program


2. Initialize the variables
3.Display a menu of health club packages.
4. The menu-driven Health Club membership program carries
out the appropriate actions based on the menu choice entered using
switch - case
5.A do-while loop allows the program to repeat until the user
selects menu choice 4.

4
PROGRAM:
// Program to implement the switch case statement and do while
#include <iostream.h>
#include <conio.h>

int main()
{
const double ADULT_RATE = 40.0;
const double CHILD_RATE = 20.0;
const double SENIOR_RATE = 30.0;
int choice; // Menu choice
int months; // Number of months
double charges; // Monthly charges
clrscr();
do
{
// Display the menu and get the user's choice
cout << "\n Health Club Membership Menu\n\n";
cout << "1. Standard Adult Membership\n";
cout << "2. Child Membership\n";
cout << "3. Senior Citizen Membership\n";
cout << "4. Quit the Program\n\n";
cout << "Enter your choice: ";
cin >> choice;
// Validate the menu selection
while ((choice < 1) || (choice > 4))
{
cout << "Please enter 1, 2, 3, or 4: ";
cin >> choice;
}
// Process the user's choice
if (choice != 4)
{
cout << "For how many months? ";
cin >> months;
// Compute charges based on user input
switch (choice)
{
case 1:
charges = months * ADULT_RATE;
break;
case 2:
charges = months * CHILD_RATE;
break;
case 3:
charges = months * SENIOR_RATE;
}
// Display the monthly charges
cout << fixed << showpoint << setprecision(2);
cout << "The total charges are $" << charges << endl;
}
5
} while (choice != 4); // Loop again if the user did not
// select choice 4 to quit
getch();
return 0;
}

6
OUTPUT:

RESULT:

Thus the C++ program to implement the switch case statement and do while was verified and
executed successfully

7
EX NO: 1C DISPLAY THE SQUARE OF NUMBERS FROM1 TO 5 USING FOR
LOOP

DATE: - 25/08/2022

AIM:
To display the square of numbers from1 to 5 using for loop

ALGORITHM:
1. Start the program
2.Initialize the variables
3. Initialise the for loop to find the square of numbers
4. Stop the program

8
PROGRAM:
// This program uses a for loop to display the numbers 1-5 and their squares .
#include
<iostream.h>
#include <conio.h>

int main()
{
int nr;
clrscr();
cout << "Number
Squared\n"; cout << " \
n";
for (nr = 1; nr <= 5; nr++)
cout << setw(4) << nr << setw(7) << (nr * nr) <<
endl; getch();
return 0;
}

9
OUTPUT

RESULT:
Thus the C++ program to display the square of numbers from1 to 5 using for loop
was verified and executed successfully

10
EX NO: 1D EVEN NUMBER CHECKING USING WHILE LOOP

DATE: - 25/08/2022

AIM:
To print all the even numbers from 1 to certain number entered by user using while loop

ALGORITHM:
1. Start the program
2. Initialise the variables
3. Enter the number of numbers
4. Check the numbers whether it is even or not using if statement
5. If n is divisible by 2 then it is an even number
6. Repeat until it reaches the number of numbers

11
PROGRAM:
// To print all the even numbers from 1 to certain number entered by user using while
loop
#include <iostream.h>
#include <conio.h>

int main()
{
int n,i=1;
clrscr();
cout <<"Enter a number:";
cin>>n;
while (i <= n)
{
if (i % 2 == 0)
cout<<i<<endl;
i++;
}
getch();
return 0;
}

12
OUTPUT:

RESULT:
Thus the C++ program To print all the even numbers from 1 to certain number
entered by user using while loop was verified and executed successfully.

13
EX NO: 2A TO PRINT THE BOOK DETAILS USING STRUCTURE

DATE: - 01/09/2022

AIM:

To print the book details using structure in C++

ALGORITHM:

1. Start the program


2. Give the book details in structure
3. Declare the structure variables
4. Display the book details using structure variable and structure member
5. Stop the program

14
PROGRAM:
// To print the book details using structure
include
<iostream.h>
#include
<cstring.h>
#include <conio.h>

struct Books
{ char
title[50];
char author[50];
char
subject[100]; int
book_id;
};
int main() {
struct Books Book1; // Declare Book1 of type
Book struct Books Book2; // Declare Book2 of
type Book clrscr();
// book 1 specification
strcpy( Book1.title, "Learn C++
Programming"); strcpy( Book1.author,
"Chand Miyan");
strcpy( Book1.subject, "C++
Programming"); Book1.book_id = 6495407;
// book 2 specification
strcpy( Book2.title, "Telecom
Billing"); strcpy( Book2.author,
"Yakit Singha");
strcpy( Book2.subject, "Telecom");
Book2.book_id = 6495700;
// Print Book1 info
cout << "Book 1 title : " << Book1.title <<endl;
cout << "Book 1 author : " << Book1.author
<<endl; cout << "Book 1 subject : " <<
Book1.subject <<endl; cout << "Book 1 id : "
<< Book1.book_id <<endl;
// Print Book2 info
cout << "Book 2 title : " << Book2.title <<endl;
cout << "Book 2 author : " << Book2.author
<<endl; cout << "Book 2 subject : " <<
Book2.subject <<endl; cout << "Book 2 id : "
<< Book2.book_id <<endl; getch();
return 0;

15
OUTPUT:

RESULT:

Thus the C++ program to print the book details using structure was verified and
executed successfully

16
EX NO: 2B TO PRINT THE EMPLOYEE DETAILS USING UNION

DATE: - 01/09/2022

AIM
To display the employee details using union

ALGORITHM
1. Start the program
2. Give the employee details in union
3. Declare the union variables
4. Display the employee details using union object and union member
5. Stop the program

17
PROGRAM:
// To display the employee details using union
#include <iostream.h>
#include <conio.h>

union Emp
{
int num;
double sal;
};
int main()
{
Emp value;
clrscr();
value.num = 2;
cout << "Employee Number::" << value.num<< "\nSalary is:: " << value.sal << endl;
value.sal = 2000.0;
cout << "Employee Number::" << value.num<< "\nSalary is:: " << value.sal << endl;
getch();
return 0;
}

18
OUTPUT :

RESULT:
Thus the C++ program to print the employee details using union was verified and
executed successfully

19
EX NO: 3 PROGRAM FOR POINTER ARITHMETIC

DATE: - 04/09/2022

AIM:

To implement the concept pointer arithmetic in C++

ALGORITHM:
1. Start the program
2. Initialise the variables
3. Initialise the pointer
4. Using for loop print the address and value of the data
5. Stop the program

20
PROGRAM
// To implement the concept pointer arithmetic in C++//
#include <iostream.h>
#include <conio.h>

const int MAX = 3;

int main () {
int var[MAX] = {10, 100, 200};
int *ptr;
clrscr();
// let us have array address in pointer.
ptr = var;
for (int i = 0; i < MAX; i++) {
cout << "Address of var[" << i << "] = ";
cout << ptr << endl;
cout << "Value of var[" << i << "] = ";
cout << *ptr << endl;
// point to the next location
ptr++;
}
getch();
return 0;
}

21
OUTPUT:

RESULT:
Thus the C++ program to implement the concept pointer arithmetic
was verified and executed successfully

22
EX NO: 4a PROGRAM TO FIND THE SMALLEST NUMBER USING FUNCTION

DATE: - 04/09/2022

AIM:
To find the smallest number using function in C++

ALGORITHM:
1. Start the program
2. Declare the function
3. Define the function to compare two numbers
4. Create a function call to execute
5. Stop the program

23
PROGRAM:
// Simple Example Program Of Function Find Smallest Number //
#include <iostream.h>
#include <conio.h>
// Find Smallest Number compare Function
int compare(int a, int b) {
return (a < b) ? a : b;
}
int main() {
clrscr();
cout << "\nSmallest Number :" << compare(1, 10);
cout << "\nSmallest Number :" << compare(31, 10);
cout << "\nSmallest Number :" << compare(11, 8);
getch();
return 0;
}

24
OUTPUT

RESULT:

Thus the C++ program to find the smallest number using function
was verified and executed successfully

25
EX NO: 4b PROGRAM TO FIND THE FACTORIAL OF A NUMBER USING
RECURSION

DATE: - 15/09/2022

AIM:
To find the factorial of a number using recursion in C++

ALGORITHM:
1. Start the program
2. Get the input value n
3. Create a function call for finding the factorial f a given
number 3.If n = 0 ,then declare that n! = 1.
4. Otherwise, n must be positive. Solve the sub problem of computing (n−1)!, multiply
this result by n, and declare n! equal to the result of this product.
5. Stop the program

26
PROGRAM:
/* Program For Factorial Value Using Recursion In C++*/
#include <iostream.h>
#include <conio.h>

//Function Declaration
long factorial(int);

int main() {
// Variable Declaration
int counter, n;
clrscr();
// Get Input Value
cout << "Enter the Number :";
cin >> n;
// Factorial Function Call
cout << n << " Factorial Value Is " << factorial(n);
// Wait For Output Screen
getch();
return 0;
}
// Factorial recursion Function
long factorial(int n) {
if (n == 0)
return 1;
else
return (n * factorial(n - 1));
}

27
Output:

RESULT:

Thus the C++ program to find the factorial of a number using recursion was verified
and executed successful

28
EX NO: 5 PROGRAM TO FIND THE CUBE OF A NUMBER USING INLINE
FUNCTION

DATE: - 15/09/2022

AIM:

To find the cube of a number using inline function in C++

ALGORITHM:

1. Start the program


2. Get the input value s
3. Define the function as inline
4. Execute the function
5. Stop the program

29
PROGRAM:
// To find the cube of a number using inline function
#include <iostream.h>
#include <conio.h>

inline int cube(int s)


{
return s*s*s;
}
int main()
{
clrscr();
cout << "The cube of 3 is: " << cube(3) << "\n";
getch();
return 0;
}

30
OUTPUT:

RESULT:

Thus the C++ program to find the cube of a number using inline function was
verified and executed successfully

31
EX NO: 6A PROGRAM TO SWAP TWO NUMBERS USING CALL BY VALUE.

DATE: - 22/09/2022

AIM:
To write a C++ program to swap two numbers using call by value.

ALGORITHM:
1. Start the program.
2. Set a ← 100 and b ← 200
3. Call the function swap(a,b)
4. Start function.
5. Assign temp ← a
6. Assign a ← b
7. Assign b ← temp
8. Print a and b.
9. End function.
10. Stop the program

32
PROGRAM:
// To swap two numbers using call by value
#include <iostream.h>
#include <conio.h>

void swap(int a, int b)


{
int temp;
temp=a;
a=b;
b=temp;
}

void main()
{
int a=100, b=200;
clrscr();
swap(a, b); // passing value to function
cout<<"Value of a"<<a;
cout<<"Value of b"<<b;
getch();
}

33
OUTPUT

RESULT:

Thus the C++ program to swap two numbers using call by value was verified and
executed successfully

34
EX NO: 6B PROGRAM TO SWAP TWO NUMBERS USING CALL BY
REFERENCE.

DATE: - 22/09/2022

AIM :To write a C program to swap two numbers using call by reference.

ALGORITHM:

1. Start the program.


2. Set a ← 100 and b ← 200
3. Call the function swap(&a,&b)
4. Start fuction
5. Assign temp ← *a
6. Assign *a ← *b
7. Assign *b ← temp
8. End function
9. Print x and y.
10. Stop the program.

35
PROGRAM
// To swap two numbers using call by reference.
#include <iostream.h>
#include <conio.h>

void swap(int *a, int *b)

{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void main()
{
int a=100, b=200;
clrscr();
swap(&a, &b); // passing address to function
cout<<"Value of a"<<a;
cout<<"Value of b"<<b;
getch();
}

36
OUTPUT

RESULT:

Thus the C++ program to swap two numbers using call by reference was verified and
executed successfully

37
EX NO: 7 PROGRAM TO CREATE AUTOMATIC, GLOBAL, STATIC
AND REGISTER VARIABLES

DATE: - 06/10/2022

AIM:

To write a C++ program to create automatic, global, static and register variables.

ALGORITHM
1. Start the program
2. Declare , g as a global variable, s as static, r as register and a as
automatic variable.
3. Define two function, first is main() and another is test_function().
4. Since g is global variable, it can be used in both function.
5. Variables r and s are declared inside test_function() so can
only be used inside that function
6. When test_function() is called for the first time, r is initialized to
5 and the value of s is 10 which is calculated from the
statement,s=s+r*2;
7. After the termination of test_function(), r is destroyed but s still
holds 10.
8. When it is called second time, r is created and initialized to 5
again. Now, the value of s becomes 20 since s initially held 10.
9. Variable a is declared inside main() and can only be used
inside main().
10. Print the output
11. Stop the program

38
PROGRAM:
// To create automatic, global, static and register variables.
#include <iostream.h>
#include <conio.h>
int g; //global variable, initially holds 0
void test_function()
{
static int s; //static variable, initially holds 0
register int r; //register variable
r=5;
s=s+r*2;
cout<<"Inside test_function"<<endl;
cout<<"g = "<<g<<endl;
cout<<"s = "<<s<<endl;
cout<<"r = "<<r<<endl;
}
int main()
{
int a; //automatic variable
clrscr();
g=25;
a=17;
test_function();
cout<<"Inside main"<<endl;
cout<<"a = "<<a<<endl;
cout<<"g = "<<g<<endl;
test_function();
getch();
return 0;
}

39
OUTPUT

RESULT:

Thus the C++ program to create automatic, global, static and register variables was
verified and executed successfully

40
EX NO: 8 PROGRAM TO ADD TWO NUMBERS USING CONSTRUCTOR AND
DESTRUCTOR

DATE: - 06/10/2022

AIM:
To write a C++ program to add two numbers using constructor and destructor.

ALGORITHM:

1. Start the program.


2. Create a class as add
3. Create a constructor and perform addition of two numbers.
4. Use destruction function.
5. End of the program

41
PROGRAM:
// To add two numbers using constructor and destructor.
#include <iostream.h>
#include <conio.h>
class add
{
int c;
public:
add(int a, int b); //constructor function
void display();
~add() //destructor function
{
cout<<"Objects are destroyed"<<endl;
}
};
add::add(int a, int b)
{
c=a+b;
}
void add:: display()
{
cout<<"The addition of 2 number is"<<c;
}
void main()
{
int c,d; clrscr();
clrscr();
cout<<"Enter the value of c and d"<<"\n";
cin>>c>>d;
add a1(c,d);
a1.display();
getch();
}

42
OUTPUT:

RESULT:

Thus the C++ program to add two numbers using constructor and destructor was
verified and executed successfully

43
EX NO: 9 PROGRAM TO DISPLAY THE VALUE USING THIS POINTER

DATE: - 20/10/2022

AIM
To display the value using this pointer in C++

ALGORITHM
1. Start the program
2. Get the input value
3. 'a' is a private member of the class This_Pointer.
4. Also, the arguments received by the member function printData() is also a.
5. Hence, if we do not use this pointer explicitly both the 'a' will be considered
as data members.
6. Print the data
7. End the program

44
PROGRAM:
// To display the value using this pointer
#include <iostream.h>
#include <conio.h>

class
This_Pointer{ int
a;
public:
void setData(int a){
this->a = a;
}
void printData(){
cout<<"The value of a is"<<a<<endl;
}
};

int main()
{ This_Pointer
tp; clrscr();
tp.setData(10);
tp.printData();
getch();
return 0;

45
OUTPUT:-

RESULT:

Thus the C++ program to display the value of this pointer was verified and executed
successfully

46
EX NO: 10A PROGRAM TO IMPLEMENT MULTIPLE INHERITANCES –
ACCESS SPECIFIERS

DATE: - 20/10/2022

AIM
To implement multiple inheritances –access specifiers

ALGORITHM

1. Start the program.


2. Declare the base class base1.
3. Declare and define the function set_value() to set the values and
display1() to display the value.
4. Declare the other class base2.
5. Declare and define the function set_values() to set the values and
display2() to display the value.
6. Create the class statement derived from base1 and base2
7. Declare and define the function display() to display the values
8. Declare the derived class object,call the functions get(),getsm() and
display().
9. Stop the program.

47
PROGRAM:
// To implement multiple inheritances –access specifiers
#include <iostream.h>
#include <conio.h>
class base1
{
protected:
int z;
private:
int y;
public:
int x;
void set_value(int xx,int yy, int zz)
{
x=xx;
y=yy;
z=zz;
}
void display1()
{
cout<<"\nX="<<x;
cout<<"\ny="<<y;
cout<<"\nZ="<<z;
}
};
class base2
{
protected:
int a;
private:
int b;
public:
int c;
void set_values(int aa,int bb, int cc)
{
a=aa;
b=bb;
c=cc;
}
void display2()
{
cout<<"\nA="<<a;
cout<<"\nB="<<b;
}
};
class derived:public base1,private base2
{
public:
void display()
{
a=100;
c=200;
cout<<"\nValue of X in derived class="<<x;
//cout<<"\nValue of Y in derived class="<<y; base1::y not accessible cout<<"\
nValue of Z in derived class="<<z;
cout<<"\nValue of A in derived class="<<a;
//cout<<"\nValue of B in derived class="<<b;base2::b not accessible cout<<"\
nValue of C in derived class="<<c;

}
};

48
OUTPUT:

RESULT:

Thus the C++ program to implement multiple inheritances –access specifiers was

verified and executed successfully.

49
EX NO: 10B HIERARCHICAL INHERITANCE – FUNCTION OVERR IDING
/VIRTUAL FUNCTION

DATE: - 27/10/2022

AIM
To implement Hierarchical inheritance – Function Overriding /Virtual Function

ALGORITHM
1. Start the program
2. Create a class polygon with member function get_data() and float area()
3. Create a class rectangle with member function float area()
4. Create a class triangle with member function float area()
5. In the main function create objects of respective class
6. Access the member function with the help of objects
7. End the program

50
PROGRAM:
// Hierarchical inheritance – Function Overriding /Virtual Function
#include <iostream.h>
#include <conio.h>

class c_polygon
{
protected:
float a,b;
public:
void get_data()
{
cout<<"\nEnter any two floating values:\n";
cin>>a>>b;
}
virtual float area()
{
return 0;
}
};

class c_rectangle:public c_polygon


{
public:
float area()
{
return (a*b);
}
};

class c_triangle:public c_polygon


{

public:
float area()
{
return (b*a/2);
}
};

void main()
{
clrscr();
c_rectangle r;
c_triangle t;
c_polygon *p;
p=&r;
p->get_data();
cout<<"\nArea of rectangle is "<<p->area();
p=&t;
p- >get_data();
cout<<"\nArea of triangle is "<<p->area();
getch();
}
51
OUTPUT:

RESULT:

Thus the C++ program to implement multiple inheritances –access specifiers was
verified and executed successfully

52
EX NO: 11A UNARY OPERATOR AS MEMBER FUNCTION.

DATE: - 27/10/2022

AIM
To write a program to implement operator overloading using unary operator

ALGORITHM

1. Start the program.


2. Declare the class.
3. Declare the variables and its member function.
4. Using the function getvalue() to get the two numbers.
5. Define the function operator ++ to increment the values
6. Define the function operator - -to decrement the values.
7. Define the display function.
8. Declare the class object.
9. Call the function getvalue()
10. Call the function operator ++() by incrementing the class object and
call the function display.
11. Call the function operator - -() by decrementing the class object and
call the function display.
12. Stop the program.

53
PROGRAM:

// Unary operator as member function


#include <iostream.h>
#include <conio.h>

class complex
{ int a, b, c;
public:
complex() {
}
void getvalue() {
cout << "Enter the Two Numbers:";
cin >> a>>b;
}
void operator++() {
a = ++a;
b = ++b;
}
void operator--() {
a = --a;
b = --b;
}
void display() {

cout << a << "+\t" << b << "i" << endl;


}
};
void main()
{ clrscr();
complex obj;
obj.getvalue();
obj++;
cout << "Increment Complex Number\n";
obj.display();
obj--;
cout << "Decrement Complex Number\n";
obj.display();
getch();
}

54
SAMPLE OUTPUT:

RESULT:

Thus the C++ program to implement Unary operator as member function was verified and
executed successfully

55
EX 11B BINARY OPERATOR AS NON MEMBER FUNCTION.

DATE: - 29/10/2022

AIM:
TO implement binary operator without member function in C++

ALGORITHM:

1. Start the program


2. Declare the private members
3. Create a default constructor and parameterized constructors
4. Make operator overloading declaration as friend function
5. Print the value
6. Define operator overloading function
7. Declare the object by calling parameterized constructor
8. Subtract the objects (Binary - operator overloading)
9. End the program

56
PROGRAM:

// Binary operator as non member function


#include <iostream.h>
#include<conio.h>
//Sample class to demonstrate operator overloading
class Sample
{
//private data members
private:
int value;
public:
//default constructor
Sample()
{ value = 0;}
//Parameterized constructor
Sample(int c)
{ value = c;}
//making operator overloading declaration as
//friend function
friend Sample operator-(Sample &S1, Sample &S2);

//printing value
void printValue()
{
cout<<"Value is : "<<value<<endl;

}
};
//operator overloading function definition
Sample operator-(Sample &S1, Sample &S2)
{
Sample S;
S = S1.value-S2.value;
return S;
}
//main program
int main()
{
int i = 0;
//object declaration by calling parameterized constructor
Sample S1(600);
Sample S2(200);
Sample S3;
//subtracting objects (Binary - operator overloading)
S3 = S1 - S2;

cout<<"S1 :"<<endl;
57
S1.printValue();

cout<<"S2 :"<<endl;
S2.printValue();
cout<<"S3 :"<<endl;
S3.printValue();
getch();
return 0;
}

58
Sample Output:

RESULT:
Thus the C++ program to implement Binary operator as member function was verified and
executed successfully

59
EX 12a MEAN VALUE USING "FRIEND" FUNCTION.

DATE: - 29/10/2022

AIM:
To write a C++ program to find mean value of given numbers using "friend" Function.

ALGORITHM:
1) Start the program.
2) Declare a class "myclass" with a "friend" function.
3) Define the friend function.
4) Call the friend function from the main function using object as argument.
5) Display the Output.
6) Stop the program.

60
PROGRAM:
//Mean value using "friend" function
#include <iostream.h>
#include <conio.h>
class myclass
{
int val1,val2;
public:
void getdata()
{
cout<<"Enter two values:";
cin>>val1>>val2;
}
friend void mean(myclass m2);
};
void mean(myclass m2)
{
cout<<"Mean value: "<<(m2.val1 + m2.val2)/2;
}
void main()
{
clrscr();
myclass m1;
m1.getdata();
mean(m1);
getch();
}

61
OUTPUT:

RESULT:
Thus the C++ program to mean value of given numbers using "friend" Function has been
executed and verified successfully.

62
EX 12b AREA OF RECTANGLE AND SQUARE USING FRIEND CLASS.

DATE: - 31/10/2022

AIM
To write a program in C++ to implement friend class

ALGORITHM

1. Start the program


2. create two classes Rectangle and Square.
3. Using statement 1 make Square class, a friend class of Rectangle class.
4. In order to access the private and protected members of Rectangle class into
Square class explicitly pass an object of Rectangle class to the member functions
of Square class as shown in statement 2
5. Display the output
6. End the program

63
PROGRAM
// To implement friend class
#include<iostream.h>
#include<conio.h>
class Rectangle
{
int L,B;
public:
Rectangle()
{
L=10;
B=20;
}
friend class Square; //Statement 1
};
class Square
{
int S;
public:
Square()
{
S=5;
}
void Display(Rectangle Rect)
{
cout<<"\n\n\tLength : "<<Rect.L;
cout<<"\n\n\tBreadth : "<<Rect.B;
cout<<"\n\n\tSide : "<<S;
}
};
void main()
{
Rectangle R;
Square S;
clrscr();
S.Display(R); //Statement
2 getch();
}

64
SAMPLE OUTPUT:

RESULT:

Thus the C++ program to find area of rectangle and square using friend class was verified
and executed successfully.

65
EX 13 CLASS TEMPLATE.

DATE: - 31/10/2022

AIM
To write a program in C++ to illustrate class template

ALGORITHM

1. Start the program


2. Test constructor has two arguments of generic type.
3. The type of arguments is mentioned inside angle brackets < >while creating
objects.
4. When argument is more than one, they are separated by commas.
5. Following statement Test test1 (1.23, 123); tells the compiler that the first
argument is of type float and another one is int type.
6. During creation of objects, constructor is called and values are received by
template arguments.
7. Display the output
8. End the program

66
PROGRAM :
// Class template with multiple parameters

#include<iostream.h>
#include<conio.h>

// Class template with two parameters


template<class T1, class T2>
class Test
{
T1 a;
T2 b;
public:
Test(T1 x, T2 y)
{
a = x;
b = y;
}
void show()
{
cout << a << " and " << b << endl;
}
};

// Main Function
int main()
{
// instantiation with float and int type
Test <float, int> test1 (1.23, 123);
// instantiation with float and char type
Test <int, char> test2 (100, 'W');
clrscr();

test1.show();
test2.show();

getch();
return 0;
}

67
SAMPLE OUTPUT:

RESULT:

Thus the C++ program to implement class template was verified and executed
successfully.

68

You might also like