0% found this document useful (0 votes)
70 views64 pages

Oopls Lab Record

The document contains a record notebook for a lab course on Object Oriented Programming with C++, including the student's name and details, contents listing experiments completed with dates and marks, and examples of programs written for various concepts taught like control structures, structures and unions, and pointer arithmetic.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views64 pages

Oopls Lab Record

The document contains a record notebook for a lab course on Object Oriented Programming with C++, including the student's name and details, contents listing experiments completed with dates and marks, and examples of programs written for various concepts taught like control structures, structures and unions, and pointer arithmetic.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 64

RECORD NOTEBOOK

BCS18L02-OBJECT ORIENTED PROGRAMMING WITH C++ LAB

2022-23(ODD SEMESTER)

DEPARTMENT
Of
INFORMATION TECHNOLOGY

NAME :
REGISTER NO :
COURSE :B.Tech(IT)
YEAR/SEM/SEC :II/III/C
BONAFIDE CERTIFICATE
Register No:

Name of Lab:Object Oriented Programming with C++(BCS18L02).

Department:INFORMATION TECHNOLOGY

Certified that this is the bonafide record of work done by


of II Year B.Tech. (IT), Sec-B 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


TABLE OF CONTENTS
S.NO DATE NAME OF THE EXPERIMENT PAGE NO. MARKS STAFF SIGNATURE

1 25/08/2022 Implementation of various


control structures
a. If statement
b. (i)switch case statement
(ii)do while loop
c. For loop
d. While loop
2 01/09/2022 Programs to understand structures &
unions
a. Structure
b. Union
3 01/09/2022 Programs to understand pointer
arithmetic
4 15/09/2022 Implementation of function and
recursion
5 15/09/2022 Implementation of inline
Function
6 22/09/2022 Programs to understand different
function call mechanism
a. Call by reference
b. Call by value

7 22/09/2022 Implementation ofstorage


specifier
8 29/09/2022 Implementation of
constructor and destructor

9 06/10/2022 Implementation of use of “this”


pointer using class
10 13/10/2022 Implementation of
inheritance and function
overriding
11 13/10/2022 Implementation of overload unary &
binary operators as member function
&non-member function
12 20/10/2022 Implementation of friend function
and friend Class

13 27/10/2022 Implementation of class template


EX.NO:01 IMPLEMENTATION OF VARIOUS CONTROL STRUCTURES
DATE:
A. IF STATEMENT
AIM:
To write a C++ program to Implement various control structures.

ALGORITHM :

1. Start the program.


2. Declare and get the input.
3. Check the condition using if statement.
4. If the condition is true the statement inside if statement will be executed.
5. If the condition is false the statement outside if statement will be executed.
6. End the program.

PROGRAM :

#include <iostream.h>

#include<conio.h>

int main ()
{
// local variable declaration:
int a = 10;

// check the boolean condition if


( a < 20 ) {
// if condition is true then print the following
cout << "a is less than 20;" << endl;
}
cout << "value of a is : " << a << endl;

return 0;
}
OUTPUT :

RESULT :Thus the program is executed and the output is verified.

1
B.(i) SWITCH CASE STATEMENT
ALGORITHM:
The expression is evaluated once and compared with the values of each case label.
• If there is a match, the corresponding statements after the matching label are executed.
For example, if the value of the expression is equal to constant2, statements after case
constant2: are executed until break is encountered.
• If there is no match, the default statements are executed.
If we do not use break, all statements after the matching label are executed.
By the way, the default clause inside the switch statement is optional.

PROGRAM :

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

int main() {
char oper;
float num1, num2;
cout << "Enter an operator (+, -, *, /): ";
cin >> oper;
cout << "Enter two numbers: " << endl;
cin >> num1 >> num2;

switch (oper) {
case '+':
cout << num1 << " + " << num2 << " = " << num1 + num2;
break;
case '-':
cout << num1 << " - " << num2 << " = " << num1 - num2;

2
break;
case '*':
cout << num1 << " * " << num2 << " = " << num1 * num2;
break;
case '/':
cout << num1 << " / " << num2 << " = " << num1 / num2;
break;
default:
// operator is doesn't match any case constant (+, -, *, /)
cout << "Error! The operator is not correct";
break;
}

return 0;
}
OUTPUT :

3
B.(ii) DO WHILE LOOP STATEMENT
ALGORITHM :
• The body of do...while loop is executed once. Only then, the test expression is evaluated.
• If the test expression is true, the body of the loop is executed again and the test
expression is evaluated.
• This process goes on until the test expression becomes false.
• If the test expression is false, the loop ends.

PROGRAM :
// C++ Program to print numbers from 1 to5 #

include <iostream.h>

#include<conio.h>

int main() {
int i = 1;

// do...while loop from 1 to5 do


{
cout << i << " "; ++i;
}
while (i <= 5);

return 0;
}

4
OUTPUT :

RESULT :Thus the program is executed and the output is verified.

5
C.FOR LOOP
ALGORITHM :
Step :01

• Start
Step :02 [Variable initialization]

• Set counter: i< --- K [Where K:Positive Number]


Step :03[Condition Check]

• If condition True then Do your task, set i=i+N and go to Step :03 [Where N:Positive
Number]
• If condition False then go to Step :04
Step:04

• Stop

PROGRAM :
Here in the loop initialization part I have set the value of variable i to 1, condition is i<=6 and on
each loop iteration the value of i increments by
#include <iostream.h>
#include<conio.h>
int main()
{
for(int i=1; i<=6; i++)
{
/* This statement would be executed *
repeatedly until the condition
* i<=6 returns false.*/
cout<<"Value of variable i is: "<<i<<endl;
}
return 0;
}

6
OUTPUT :

RESULT :Thus the program is executed and the output is verified.

7
D.WHILE LOOP
ALGORITHM :
• The while loop evaluates the test expression inside the parenthesis ().
• If the test expression is true, statements inside the body of while loop are executed. Then, the test expression
is evaluated again.
• The process goes on until the test expression is evaluated to false.
• If the test expression is false, the loop terminates (ends).

PROGRAM :
// C++ Program to print numbers from 1 to 5

#include <iostream.h>

#include<conio.h>

int main()
{
int i = 1;

// while loop from 1 to 5 while (i =5)


{
cout << i << " "; ++i;
} return 0;
}

8
OUTPUT :

RESULT :Thus the program is executed and the output is verified.

9
EX.NO:02 PROGRAMS TO UNDERSTAND STRUCTURES & UNIONS
DATE:
AIM:
To write c++ program to understand structures & unions.
a. STRUCTURES
ALGORITHM :
struct structureName{
member1;
member2;
member3;
.
.
.
memberN;
};

PROGEAM :
#include <iostream.h>
#include<conio.h>
struct Student
{
char stuName[30];
int stuRollNo;
int stuAge;
};
int main()
{
Student s;
cout<<"Enter Student Name: ";
cin>>getline(s.stuName, 30);

10
cout<<"Enter Student Roll No: ";
cin>>s.stuRollNo;
cout<<"Enter Student Age: ";
cin>>s.stuAge;
cout<<"Student Record:"<<endl;
cout<<"Name: "<<s.stuName<<endl;
cout<<"Roll No: "<<s.stuRollNo<<endl;
cout<<"Age: "<<s.stuAge;
return 0;
}
OUTPUT :

11
B. UNION
SYNTAX : union
union_name
{
data-type member-1;
data-type member-2;
data-type member-3;
data-type member-4;
};
PROGRAM :

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

struct student1 { // defining a struct


int roll_no;
char name[40];
int phone_number;
};

union student2 { // defining a union


int roll_no;
char name[40];
int phone_number;
};

int main()
{
struct student 1 s1;
union student2 u1;

12
cout << "size of structure : " << sizeof(s1) << endl;
cout << "size of union : " << sizeof(u1) << endl;
return 0;
}

OUTPUT :

RESULT :Thus the program is executed and the output is verified.

13
EX.NO:03 PROGRAMS TO UNDERSTAND POINTER ARITHMETIC
DATE:
AIM:
To write c++ program to understand Pointer Arithmetic.
Increment a Pointer (++)
PROGRAM :
#include<iostream.h>
#include<conio.h>
const int MAX = 3;

int main ()
{
int var[MAX] = {10, 100, 200};
int *ptr;
//let us have array address in a 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++; }
return 0;
}

14
OUTPUT :

Decrement a Pointer (- -)
PROGRAM :
#include<iostream.h>
#include<conio.h>
const int MAX = 3; int

main () {
int var[MAX] = {10, 100, 200};
int *ptr;
//let us have the address of the last element in the pointer.
ptr = &var[MAX-1];
for (int i = MAX; i > 0; i--) {
cout << "Address of var[" << i << "] = ";
cout << ptr << endl;
cout << "Value of var[" << i << "] = ";
cout << *ptr << endl;
//point to the previous location
ptr--;
}
return 0;

15
OUTPUT :

Pointer Comparisons
PROGRAM :
#include<iostream.h>
#include<conio.h>
const int MAX = 3;

int main () {
int var[MAX] = {10, 100, 200};
int *ptr;
//let us have the address of the first element in a pointer.
ptr = var;
int i = 0;
while ( ptr <= &var[MAX - 1] ) {
cout<< "Address of var[" << i << "] = ";
cout<< ptr << endl;
cout<< "Value of var[" << i << "] = ";
cout<< *ptr << endl;
//point to the previous location
ptr++;
i++;

16
return 0;
}
OUTPUT :

RESULT :Thus the program is executed and the output is verified.

17
EX.NO:04 IMPLEMENTATION OF FUNCTION AND RECURSION
DATE:
AIM:
To write c++ program to implement function and recursion.
FUNCTION
SYNTAX :
return-type function-name(parameter1, parameter2, ...) {
// function-body }
PROGRAM :

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

int max(int x, int y) {


if (x > y)
return x;
else return
y;
}

int main() {
int a = 10, b = 20;

// Calling above function to find max of 'a' and 'b'


int m = max(a, b);

18
cout << "m is " << m;
return 0;
}

OUTPUT :

19
RECURSION
SYNTAX :
int fact(int n)
{
if (n < = 1) // base case
return 1;
else
return n*fact(n-1);
}
PROGRAM :
// Factorial of n = 1*2*3*...*n
#include <iostream.h>
#include<conio.h>
int factorial(int);
int main() {
int n, result;

cout << "Enter a non-negative number: ";


cin >> n;

result = factorial(n);
cout << "Factorial of " << n << " = " << result;
return 0;
}

20
int factorial(int n) {
if (n > 1) {
return n * factorial(n - 1);
} else {
return 1;
}
}
OUTPUT :

RESULT :Thus the program is executed and the output is verified.

21
EX.NO:05 IMPLEMENTATION OF INLINE FUNCTION
DATE:
AIM:
To write c++ program to implement Inline function.
SYNTAX :
inline return-type function-name(parameters)
{
// function code
}
PROGRAM :
#include <iostream.h>
#include<conio.h>
class operation
{
int a,b,add,sub,mul;
float div;
public:
void get();
void sum();
void difference();
void product();
void division();
};
inline void operation :: get()
{
cout << "Enter first value:";
cin >> a;
cout << "Enter second value:";

22
cin >> b;
}

inline void operation :: sum()


{
add = a+b;
cout << "Addition of two numbers: " << a+b << "\n";
}

inline void operation :: difference()


{
sub = a-b;
cout << "Difference of two numbers: " << a-b << "\n";
}

inline void operation :: product()


{
mul = a*b;
cout << "Product of two numbers: " << a*b << "\n";
}

inline void operation ::division()


{
div=a/b;
cout<<"Division of two numbers: "<<a/b<<"\n" ;
}

int main()

23
{
cout << "Program using inline function\n";
operation s;
s.get();
s.sum();
s.difference();
s.product();
s.division();
return 0;
}
OUTPUT :

RESULT :Thus the program is executed and the output is verified.

24
EX.NO:06 PROGRAMS TO UNDERSTAND DIFFERENT FUNCTION CALL
MECHANISM
DATE:
AIM:
To write c++ program to implement function call mechanism.
CALL BY REFERENCE
SYNTAX : //
Declaration
return_type function_name( type_of_argument,type_of_argument,..);
//Calling
function_name(&actual_argument,&actual_argument,..);
//Definition
return_type function_name(type_of_argument &alias_variable,..)
{
....;
}
PROGRAM :
#include<iostream.h>
#include<conio.h>
void swap(int *x, int *y)
{
int swap;
swap=*x;
*x=*y;
*y=swap;
}

25
int main()
{
int x=500, y=100;
swap(&x, &y); // passing value to function
cout<<"Value of x is: "<<x<<endl;
cout<<"Value of y is: "<<y<<endl;
return 0;
}
OUTPUT :

26
CALL BY VALUE
PROGRAM :
#include <iostream.h>
#include<conio.h>
void change(int data);

int main()
{
int data = 3;
change(data);
cout << "Value of the data is: " << data<< endl;
return 0;
}
void change(int data)
{
data = 5; }
OUTPUT :

RESULT :Thus the program is executed and the output is verified.

27
EX.NO:07 IMPLEMENTATION OF STORAGE SPECIFIERS
DATE:
AIM:
To write c++ program to implement storagespecifiers.
SYNTAX :
storage_class var_data_type var_name;
C++ uses 5 storage classes, namely:
1. auto
2. register
3. extern
4. static
5. mutable
1. AUTO
PROGRAM :
#include <iostream.h>

#include<conio.h>

void autoStorageClass()

cout << "Demonstrating auto class\n";

// Declaring an auto variable


// No data-type declaration needed
auto a = 32;
auto b = 3.2;
auto c = "GeeksforGeeks";
auto d = 'G';

28
// printing the auto variables
cout << a << " \n";
cout << b << " \n";
cout << c << " \n";
cout << d << " \n";
}

int main()
{

// To demonstrate auto Storage Class


autoStorageClass();

return 0;
}
OUTPUT :

29
2. EXTERN PROGRAM:

# include <iostream>

#include<conio.h>

// declaring the variable which is to


// be made extern an intial value can
// also be initialized to x
int x;
void externStorageClass()

cout << "Demonstrating extern class\n";


// telling the compiler that the variable
// x is an extern variable and has been
// defined elsewhere(above the main// functio)
extern int x;

// printing the extern variables 'x'


cout << "Value of the variable 'x'"<< "declared, as extern: " << x << "\n";
// value of extern variable x modified x = 2;

// printing the modified values of

30
// extern variables 'x'
cout<< "Modified value of thevariable 'x'" << " declared as extern: \n"<< x;
}
int main()

// To demonstrate extern Storage Class extern

StorageClass();

return 0;
}

OUTPUT :

31
3. STATIC
PROGRAM : #include
<iostream> #include<
conio.h>

// Function containing static variables //


memory is retained during execution int
staticFun()
{
cout << "For static variables: ";
static int count = 0;
count++; return
count;
}

// Function containing non-static variables


// memory is destroyed
int nonStaticFun()
{
cout << "For Non-Static variables: ";
int count = 0;
count++;
return count;
}

int main()
{

32
// Calling the static parts

cout << staticFun() << "\n";

cout << staticFun() << "\n";

// Calling the non-static parts


cout << nonStaticFun() << "\n";

cout << nonStaticFun() << "\n";


return 0;
}

OUTPUT :

33
4. REGISTER :
PROGRAM :
#include <iostream.h>

#include<conio.h>

void registerStorageClass()

cout << "Demonstrating register class\n";


// declaring a register variable
register char b = 'G';

// printing the register variable 'b'


cout << "Value of the variable 'b'"<< " declared as register: " << b;
}
int main()

// To demonstrate register Storage Class


registerStorageClass();
return 0;
}

34
OUTPUT :

5. MUTABLE :
PROGRAM :

#include< iostream.h>

#include<conio.h>

class Test {

public:
int x;

// defining mutable variable y

// now this can be modified mutable


int y;

Test() {
x = 4; y = 10;

} 35
};

int main()
{
// t1 is set to constant
const Test t1;

// trying to change the value


t1.y = 20;
cout << t1.y;

// Uncommenting below lines


// will throw error
// t1.x = 8;
// cout << t1.x;
return 0;
}
OUTPUT :

RESULT :Thus the program is executed and the output is verified.

36
EX.NO:08 IMPLEMENTATION OF CONSTRUCTORS & DESTRUCTORS
DATE:
AIM:
To write c++ program to implement Constructors and Destructors.
CONSTRUCTOR
SYNTAX:
class A
{
public:
int x;
//constructor
A()
{
// object initialization
}
};
PROGRAM :
#include <iostream.h>
#include<conio.h>

class Line {
public:
void setLength( double len );
double getLength( void );
Line(); // This is the constructor
private:
double length;

37
};

// Member functions definitions including constructor


Line::Line(void) {
cout << "Object is being created" << endl;
}
void Line::setLength( double len ) {
length = len;
}
double Line::getLength( void ) {
return length;
}

// Main function for the program


int main() {
Line line;

// set line length


line.setLength(6.0);
cout << "Length of line : " << line.getLength() <<endl;

return 0;
}

38
OUTPUT :

DESTRUCTOR
SYNTAX :
~constructor-name();
PROGRAM :
#include <iostream.h>
#include<conio.h>
class Line {
public:
void setLength( double len );
double getLength( void );
Line(); // This is the constructor declaration
~Line(); // This is the destructor: declaration

private:
double length;
};

// Member functions definitions including constructor


Line::Line(void) {
cout << "Object is being created" << endl;
}

39
Line::~Line(void) {
cout << "Object is being deleted" << endl;
}
void Line::setLength( double len ) {
length = len;
}
double Line::getLength( void ) {
return length;
}

// Main function for the program


int main() {
Line line;

// set line length


line.setLength(6.0);
cout << "Length of line : " << line.getLength() <<endl;

return 0;
}

40
OUTPUT :

RESULT :Thus the program is executed and the output is verified.

41
EX.NO:09 IMPLEMENTATION OF USE OF “THIS” POINTER USING CLASS
DATE:
AIM:
To write c++ program to implement use of “this” pointer using class.
SYNTAX :
delete this;
PROGRAM :
#include <iostream.h>
#include<conio.h>
class Employee {
public:
int id; //data member (also instance variable) string
name; //data member(also instance variable) float
salary;
Employee(int id, string name, float salary)
{
this->id = id;
this->name = name;
this->salary = salary;
}
void display()
{
cout<<id<<" "<<name<<" "<<salary<<endl;
}
};
int main(void) {
Employee e1 =Employee(101, "Sonoo", 890000); //creating an object of Employee
Employee e2=Employee(102, "Nakul", 59000); //creating an object of Employee

42
e1.display();
e2.display();
return 0;
}
OUTPUT :

RESULT :Thus the program is executed and the output is verified.

43
EX.NO:10 IMPLEMENTATION OF INHERITANCE & FUNCTION
OVERRIDING
DATE:
AIM:
To write c++ program to implement Inheritance and Function overriding.
A. Multiple inheritance – Access Specifiers
SYNTAX:

class subclass_name: access_mode base_class1, access_mode base_class2, ........ {


//body of subclass
};
PROGRAM:
// C++ program to explain

//multiple inheritance

#include <iostream.h>

#include<conio.h>

// first base class


class Vehicle {
public:
Vehicle() {
cout << "This is a Vehicle" << endl; }
};

// second base class


class FourWheeler {

44
public:
FourWheeler()
{
cout << "This is a 4 wheeler Vehicle" << endl;
}
};

// sub class derived from two base classes


class Car: public Vehicle, public FourWheeler {

};

// main function
int main()
{
// creating object of sub class will
// invoke the constructor of base classes
Car obj;
return 0;
}
OUTPUT:

45
B. Hierarchical inheritance – Function Overriding/Virtual Function
SYNTAX :
class base_class {
... .. ...
}

class first_derived_class: public base_class {


... .. ...
}

class second_derived_class: public base_class {


... .. ...
}

class third_derived_class: public base_class {


... .. ...
}

PROGRAM :
// C++ program to implement
// Hierarchical Inheritance
#include <iostream.h>
#include<conio.h>

// base class
class Vehicle
{
public:

46
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};

// first sub class


class Car: public Vehicle
{

};

// second sub class


class Bus: public Vehicle
{

};

// main function
int main()
{
// creating object of sub class will
// invoke the constructor of base class
Car obj1;
Bus obj2;
return 0;
}

47
OUTPUT :

RESULT :Thus the program is executed and the output is verified.

48
EX.NO:11 IMPLEMENTATION OF OVERLOAD UNARY & BINARY
OPERATORS AS MEMBER FUNCTION &NON MEMBER FUNCTION
DATE:
AIM:
To write c++ program to implement Overload unary and binary operators as member function
and non member function.
A. Unary Operator as Member
function ALGORITHM :
• Step 1: Start the program.
• Step 2: Declare the class.
• Step 3: Declare the variables and its member function.
• Step 4: Using the function getvalue() to get the two numbers.
• Step 5: Define the function operator ++ to increment the values
• Step 6: Define the function operator - -to decrement the values.
• Step 7: Define the display function.
• Step 8: Declare the class object.
• Step 9: Call the function getvalue()
• Step 10: Call the function operator ++() by incrementing the class object and call the
function display.
• Step 11: Call the function operator - -() by decrementing the class object and call the
function display.
• Step 12: Stop the program.
PROGRAM :
#include <iostream.h>
#include<conio.h>
class Number

49
{
private:
int x;
public:
Number(int p)
{
x = p;
}
void operator -()
{
x = -x;
}
void display()
{
cout<<"x = "<<x;

};
int main()
{
Number n(10);
-n;
n.display();
return 0;
}

50
OUTPUT:

51
B. Binary operator as non member
function ALGORITHM :
• Step 1: Start the program.
• Step 2: Declare the class.
• Step 3: Declare the variables and its member function.
• Step 4: Using the function getvalue() to get the two numbers.
• Step 5: Define the function operator +() to add two complex numbers.
• Step 6: Define the function operator –()to subtract two complex numbers.
• Step 7: Define the display function.
• Step 8: Declare the class objects obj1,obj2 and result.
• Step 9: Call the function getvalue using obj1 and obj2
• Step 10: Calculate the value for the object result by calling the function operator
+ and operator -.
• Step 11: Call the display function using obj1 and obj2 and result.
• Step 12: Return the values.
• Step 13: Stop the program.
PROGRAM :
#include <iostream.h>
#include<conio.h>
class Complex
{
private:
float real;
float imag;
public:
Complex(){} Complex(
float r, float i) {
real = r;
52
imag = i;
}
void display()
{
cout<<real<<"+i"<<imag;
}
friend Complex operator +(Complex &, Complex &);
};
Complex operator +(Complex &c1, Complex &c2)
{
Complex temp;
temp.real = c1.real + c2.real;
temp.imag = c1.imag + c2.imag;
return temp;
}
int main()
{
Complex c1(3, 4);
Complex c2(4, 6);
Complex c3 = c1+c2;
c3.display();
return 0;
}

53
OUTPUT :

RESULT :Thus the program is executed and the output is verified.

54
EX.NO:12 IMPLEMENTATION OF FRIEND FUNCTION & FRIND CLASS
DATE:
AIM:
To write c++ program to implement Friend function and friend class.
A. FRIEND FUNCTION
SYNTAX : class
className {
... .. ...
friend returnType functionName(arguments);
... .. ...
}

PROGRAM :
#include <iostream.h>
#include <string.h>
#include<conio.h>
class sample{
int length, breadth;

public:
sample(int length, int breadth):length(length),breadth(breadth)
{}
friend void calcArea(sample s); //friend function declaration

};
//friend function definition
void calcArea(sample s){
cout<<"Area = "<<s.length * s.breadth;

55
}
int main()
{
sample s(10,15);
calcArea(s);

return 0;
}
OUTPUT :

56
B.FRIEND CLASS
SYNTAX :
class ClassB;

class ClassA {
// ClassB is a friend class of
ClassA friend class ClassB;
... ..
... }

class ClassB {
... .. ...
}
PROGRAM:
// C++ program to demonstrate the working of friend class

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

// forward declaration
class ClassB;

class ClassA {
private:
int numA;

// friend class declaration


friend class ClassB;

57
public:
// constructor to initialize numA to 12
ClassA() : numA(12) {}
};

class ClassB {
private:
int numB;

public:
// constructor to initialize numB to 1
ClassB() : numB(1) {}

// member function to add numA


// from ClassA and numB from ClassB
int add() {
ClassA objectA;
return objectA.numA + numB;
}
};

int main() {
ClassB objectB;
cout << "Sum: " << objectB.add();
return 0;
}

58
OUTPUT :

RESULT :Thus the program is executed and the output is verified.

59
EX.NO:13 IMPLEMENTATION OF CLASS TEMPLATE
DATE:
AIM:
To write c++ program to implement Class template.
SYNTAX :
1. template<class Ttype>
2. class class_name
3. {
4. .
5. .
6. }
PROGRAM:
#include<iostream.h>
#include<conio.h>
templateclass <T>
class A
{
public:
T num1 = 5;

T num2 = 6;

void add()

{
}
std::cout << "Addition of num1 and num2 : " << num1+num2<<std::endl;

};

60
int main()
{
A<int> d;
d.add();
return 0;
}
OUTPUT :

RESULT : Thus the program is executed and the output is verified.

61

You might also like