0% found this document useful (0 votes)
6 views

22CST202-OOPS Lab Record

The document outlines the structure and objectives of the 22CST202 Object Oriented Programming Laboratory course for the academic year 2023-2024 at the Department of Computer Science and Engineering. It includes details about the course outcomes, program outcomes, and specific experiments to be conducted, emphasizing the application of C++ programming concepts. Additionally, it highlights the vision and mission of the institute and department, aiming to prepare students for industry readiness and innovation.

Uploaded by

balamuruganp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

22CST202-OOPS Lab Record

The document outlines the structure and objectives of the 22CST202 Object Oriented Programming Laboratory course for the academic year 2023-2024 at the Department of Computer Science and Engineering. It includes details about the course outcomes, program outcomes, and specific experiments to be conducted, emphasizing the application of C++ programming concepts. Additionally, it highlights the vision and mission of the institute and department, aiming to prepare students for industry readiness and innovation.

Uploaded by

balamuruganp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 59

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

22CST202 OBJECT ORIENTED PROGRAMMING


LABORATORY (REGULATION 2022)

NAME :
REGISTER NO. :
YEAR/ SEMESTER : I Year / II Sem
ACADEMIC YEAR : 2023-2024
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

LABORATORY RECORD

BONAFIDE CERTIFICATE

This is to certify that bonafide record of practical work done by

Mr. /Ms Register Number of the Year 2023-2024 B.E. Department of Computer
Science and Engineering in the 22CST202 - Object Oriented Programming
Laboratory for the Second Semester University Examination held on .

Signature of the Lab Course In-charge Signature of Head of the Department

Signature of Examiners:

Internal Examiner External Examiner


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

INSTITUTE VISION & MISSION


Vision of JNNIE:
Lead the transformation of engineering and management learning experience to educate the
next generation of innovators and entrepreneurs who want to make the world a better place.

Mission of JNNIE:
1. To develop the required resources and infrastructure and to establish a
conducive ambience for the teaching-learning process.

2. To nurture professional and ethical values in the students and to instils in them a
spirit of innovation and entrepreneurship.

3. To encourage a desire for higher learning and research in the students and to
equip them to face global challenges.

4. To provide opportunities for students to learn job-relevant skills to make


them industry ready.

5. To interact with industries and other organizations to facilitate transfer of


knowledge and know-how.

DEPARTMENT VISION & MISSION


Department Vision: To produce globally competent, quality computer professionals and to
inculcate the spirit of moral values for the cause of development of our nation

Department of Mission:
M1. Establish closer relationship with IT industries and expose the students to the cutting
edge technological advancements.

M2. Provide impetus and importance to beyond curriculum learning and thereby provide
an opportunity for the student community to keep them updated with latest and socially
relevant technology.

M3. To impart interpersonal skills and ethical responsibilities to the students


PROGRAM EDUCATIONAL OBJECTIVES (PEOs):
PEO Statements M1 M2 M3

Our graduates to pursue higher education and research, or have a 2 3 3


successful career in industries associated with Computer Science and
Engineering, or as entrepreneurs.

Our graduates will have the ability and attitude to adapt to emerging 3 3 2
technological changes.

Our graduates shall adapt to the changing career opportunities, 2 3 3


assimilate new technologies and work in multi-disciplinary areas with
strong focus on innovation and entrepreneurship.

3- High; 2-Medium; 1-Low


POs PROGRAM OUTCOMES

PO1 Engineering knowledge: Apply the knowledge of mathematics, science, engineering


fundamentals and an engineering specialization to the solution of complex engineering
problems.

PO2 Problem analysis: Identify, formulate, review research literature, and analyze
complex engineering problems reaching substantiated conclusions using first
principles of mathematics, natural sciences, and engineering sciences.

PO3 Design/development of solutions: Design solutions for complex engineering problems


and design system components or processes that meet the specified needs with
appropriate consideration for the public health and safety, and the cultural, societal, and
environmental considerations.

PO4 Conduct investigations of complex problems: Use research-based knowledge and


research methods including design of experiments, analysis and interpretation of data,
and synthesis of the information to provide valid conclusions.

PO5 Modern tool usage: Create, select, and apply appropriate techniques, resources, and
modern engineering and IT tools including prediction and modeling to complex
engineering activities with an understanding of the limitations.

PO6 The engineer and society: Apply reasoning informed by the contextual knowledge to
assess societal, health, safety, legal and cultural issues and the consequent
responsibilities relevant to the professional engineering practice.
PO7 Environment and sustainability: Understand the impact of the professional
engineering solutions in societal and environmental contexts, and demonstrate the
knowledge of, and need for sustainable development.

PO8 Ethics: Apply ethical principles and commit to professional ethics and
responsibilities and norms of the engineering practice.

PO9 Individual and team work: Function effectively as an individual, and as a member
or leader in diverse teams, and in multidisciplinary settings.

PO10 Communication: Communicate effectively on complex engineering activities with


the engineering community and with society at large, such as, being able to
comprehend and write effective reports and design documentation, make effective
presentations, and give and receive clear instructions.

PO11 Project management and finance: Demonstrate knowledge and understanding of


the engineering and management principles and apply these to one’s own work, as a
member and leader in a team, to manage projects and in multidisciplinary
environments.

PO12 Life-long learning: Recognize the need for, and have the preparation and ability
to engage in independent and life-long learning in the broadest context of
technological change.

PROGRAM SPECIFIC OUTCOMES:


PSO1 To apply software engineering principles and practices for developing quality software for
scientific and business applications.

PSO2 To adapt to emerging Information and Communication Technologies (ICT) to innovate


ideas and solutions to existing/novel problems.

22CST202 OBJECT ORIENTED PROGRAMMING LAB L T P C

Total Contact Hours - 30 0 0 2 2

Prerequisite –Fundamental of Computing and Programming,


Object Oriented Programming using C++,C.

Lab Manual Prepared by – Dept. of Computer Science & Engineering

OBJECTIVES: This lab manual demonstrates familiarity with various concepts of OOPS.

COURSE OUTCOMES (COs)

CO1 Demonstrate class object concepts by using C++.

CO2 Develop programs using inheritance and polymorphism.

CO3 Demonstrate the significance of constructors and destructor.

CO4 Implement function and operator overloading using C++.

CO5 Construct generic classes using template concepts.

CO6 Implement the concept of file handling.

MAPPING BETWEEN COURSE OUTCOMES & PROGRAM OUTCOMES


(3 /2/1 INDICATES STRENGTH OF CORRELATION) 3- High, 2- Medium, 1-
Low

COs PO1 PO2 PO3


PO4 PO5 PO6
PO7 PO8 PO9
PO10 PO11 PO12
PSO1 PSO2

CO 2 3 3 3 2 2 2 3 3 3 3 3
1
CO 2 3
2

CO 3 3 2 2 3 3 3 3 3
3

CO 2 1 2 2 3
4

CO 2 3 2 2 2 2 2 3
5

CO 1 2 2 2 2 1 2 2 3 3
6

22CST202 - OBJECT ORIENTED PROGRAMMING LABORATORY

LIST OF EXPERIMENTS

PROGRAMMING IN C++

1. Program using Functions


• Functions with Default arguments, Friend Function, Inline Function •
Implementation of Call by Value, Address, Reference

2. Simple classes for understanding objects, member functions, constructors, destructor,


copy constructor.
• Classes with Primitive Data Members
• Classes with Arrays as Data Members
• Classes with Pointers as Data Members
• Classes with Constant Data Members
• Classes with Static Member Functions
3. Compile Time Polymorphism
• Operator Overloading
• Function Overloading
4. Run Time Polymorphism
• Various Forms of Inheritance
• Virtual Functions
• Virtual Base Classes
• Templates
CONTENT
S.NO DATE NAME OF THE EXPERIMENT SIGNATURE

1 a) Variables using function with default arguments

b) Function with Default Arguments

c) Implementation of Call By Value

d) Implementation of Call By Address

e) Implementation of Call By Reference

f) Inline Function

2 a) Classes With Primitive Data Members

b) Classes With Arrays As Data Members

c) Classes With Pointers As Data Members

d) Classes With Constant Data Member

e) Classes With Static Member Function

f) Friend Function

3 a) Unary Operator Overloading

b) Binary Operator Overloading

c) Function Overloading

4 a) Single Inheritance

b) Multiple Inheritance

c) Multilevel Inheritance

d) Virtual Function

e) Virtual Base Class

f) Class Templates
g) Function Template

EX. NO.1A VARIABLES USING FUNCTION WITH DEFAULT ARGUMENTS DATE:

AIM:
To write a C++ program to find the sum of the given variables using function with
default arguments.

ALGORITHM:

1. Start the program


2. Include the iostream library for input/output operations.
3. Define a function with default arguments to calculate the area of a rectangle. 4.
Inside the function, calculate the area using the provided length and width. 4. In the
main() function:
5. Declare variables for length and width of the rectangle.
6. Prompt the user to enter the length and width or provide default values. 7. Call
the function with the provided or default arguments to calculate the area. 8.
Compile and execute the program to observe the output of each function call.
SOURCE CODE:

#include <iostream>
using namespace std;
// Function with default arguments to calculate the area of a rectangle
double calculateArea(double length = 1.0, double width = 1.0)
{
return length * width;
}
int main()
{
double length, width;
// Prompting the user to enter length and width or providing default
values cout << "Enter length of rectangle (default is 1.0): ";
cin >> length;
cout << "Enter width of rectangle (default is 1.0): ";
cin >> width;
// Calculate area using function with default arguments
double area = calculateArea(length, width);
// Displaying the area of the rectangle
cout << "Area of rectangle: " << area << endl;
return 0;
}
Output:
Enter length ofrectangle (default is 1.0): 4.5
Enter width of rectangle (default is 1.0): 2.5
Area of rectangle: 11.25

RESULT:
The program to implement the variables using function with default arguments has
been executed successfully using C++.
EX. NO. 1B FUNCTIONS WITH DEFAULT ARGUMENTS DATE:

AIM:
To implement the concept of function with default arguments.

ALGORITHM:
1. Start the program.
2. Include the iostream library for input/output operations.
3. Define a function named printLine with two parameters: ch (a character, defaulted to '_')
and Repeatcount (an integer, defaulted to 70).
4. Inside the function, print a newline character (endl) to start a new line.
5. Use a for loop to iterate Repeatcount times.
6. Inside the loop, print the character ch.
7. Define the main function, the entry point of the program.
8. Call printLine function multiple times with different arguments to print lines of
characters with specified repetitions:
▪ Call printLine with no arguments to print a line of default characters ('_')
repeated 70 times.
▪ Call printLine with the character '/' to print a line of '/' repeated 70 times. ▪
Call printLine with the character '' and repeat count 40 to print a line of ''
repeated 40 times.
▪ Call printLine with the character 'R' and repeat count 55 to print a line of 'R'
repeated 55 times.
9. Implement the printLine function according to its definition, providing the logic to print
lines of characters with specified repetitions.
10. Compile and execute the program to observe its behavior and output.

SOURCE CODE:

#include <iostream>
using namespace std;
void printLine(char ='_', int =40);
int main() {
printLine();
printLine('/');
printLine('*', 30);
printLine('S', 35);
return 0;
}
void printLine(char ch, int Repeatcount)
{
int i;
cout << endl;
for(i = 0; i < Repeatcount; i++)
cout << ch;
}

OUTPUT:

////////////////////////////////////////
******************************
SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS

RESULT:
The program to implement the concept of function with default arguments
has been executed successfully using C++.
EX NO . 1C IMPLEMENTATION OF CALL BY VALUE DATE:

AIM:
To write a C++ program to find the value of a number raised to its power using call
by value.

ALGORITHM:
1. Start the program.
2. Declare two integer type variables X and Y.
3. Get the values for the variables X and Y.
4. Call the function power() to which a copy of the two variables is passed. 5. The function
power() is defined to calculate the value of x raised to power y and result is saved in p.
6. Return the value of p to the main function.
7. Display the result.
8. Stop the program.
SOURCE CODE:
#include <iostream>
using namespace std;
double power(int x, int y);
int main()
{
int x, y;
cout << "Enter x, y:" << endl;
cin >> x >> y;
cout << x << " to the power " << y << " is " << power(x, y);
return 0;
}
double power(int x, int y)
{
if (x == 0 && y < 0)

{
cout << "Error: Cannot raise 0 to a negative power." <<
endl; exit(1); // Exit the program with an error code
}
double p = 1.0;
if (y >= 0)
{
while (y--)
p *= x;
}
else
{
while (y++)
p /= x;
}
return p;
}

OUTPUT:
Enter x, y:
23
2 to the power 3 is 8

RESULT:
The program to implement the concept of Call by Value has been executed
successfully using C++.
EX. NO. 1D IMPLEMENTATION OF CALL BY ADDRESS DATE:

AIM:
To write a C++ program to implement the concept of Call by

Address. ALGORITHM:

1. Start the program.


2. Include suitable header file.
3. Declare a function swap() with two pointer variables *X and *Y as arguments.
4. Declare and initialize the value for the two alias variables i and j in main(). 5.
Print the value of two variables i and j before swapping.
6. Call the swap() function by passing address of the two variables as arguments.
7. Print the value of two variables i and j after swapping.

8. Stop the program.

SOURCE CODE:
#include <iostream>
using namespace std;
void swap(int *x, int *y); // Function prototype declaration
int main()
{
int i, j;
i = 10;
j = 20;
cout << "\nThe value of i before swapping is: " << i;
cout << "\nThe value of j before swapping is: " << j;
swap(&i, &j); // Call the swap function
cout << "\nThe value of i after swapping is: " << i;
cout << "\nThe value of j after swapping is: " << j;
return 0;
}
void swap(int *x, int *y)
{
// Function definition
int temp = *x;
*x = *y;
*y = temp;
}

OUTPUT:
The value of i before swapping is: 10
The value of j before swapping is: 20
The value of i after swapping is: 20
The value of j after swapping is: 10

RESULT:
The program to implement the concept of Call by Address has been executed
successfully using C++.
EX.NO:1E IMPLEMENTATION OF CALL BY REFERENCE DATE:

AIM:
To write a program in C++ to implement the concept of call by

reference. ALGORITHM:

1. Start the program.


2. Declare the variables x,y,m and n.
3. Define the function refer(int&a, int&b) to print the values of a and b. 4. Define the
function refer(float &a, float &b) to print the values of a and b. 5. Call the function by
reference ie, the variables a and b are alias variables for x,y,m and n. 6. Display the result.
7. Stop the program.
SOURCE CODE:

#include <iostream>
using namespace std;
void refer(int &a, int &b
{
cout << "\n\n The value of integer is: " << a;
cout << "\n\n The value of integer is: " <<
b; }
void refer(float a, float b)
{
cout << "\n\n Value of floating is: " << a;
cout << "\n\n Value of floating is: " << b;
}
int main()
{
int x, y;
float n, m;
cout << "\n Enter the Integer number of two numbers\
n"; cin >> x >> y;

cout << "\n Enter the Floating point of two numbers\


n"; cin >> n >> m;

refer(x, y);
refer(n, m);

return 0;
}
OUTPUT:

Enter the Integer number of two numbers


58
Enter the Floating point of two numbers
4.5
6.2
The value of integer is: 5
The value of integer is: 8
Value of floating is: 4.5
Value of floating is: 6.2

RESULT:
The program to implement Call by Reference has been executed successfully using C++.
EX.NO:1F INLINE FUNCTION
DATE:

AIM:
To write C++ program to implement inline function.
ALGORITHM:

1. Start the program.


2. Declare and initialize the variables x and y.
3. Define the inline member function mul() using the keyword inline.
4. The parameterized member function mul () is used to get the values of two floating point
variables x and y and their product is reurned.
5. The parameterized member function div() is used to get the values of two variables p and
q of double data type.
6. Invoke the member function mul().
7. Display the result.
8. Stop the program.
SOURCE CODE:

//inline function
#include <iostream>
using namespace std;
inline float mul(float x, float y)
{
return (x * y);
}

inline double div(double x, double y)


{
return (x / y);
}

int main() {
float x ;
float y ;

cout << "\nINLINE FUNCTION\n";


cout << "Enter the value x: " "\n";
cin >> x;
cout << "Enter the value y: " "\n";
cin >> y;
cout << "Multiplication: " << mul(x, y) << "\
n"; cout << "Division: " << div(x,y) << "\n";

return 0;
}
OUTPUT:
INLINE FUNCTION
Enter the value x:
25.2
Enter the value y:
35.3
Multiplication: 889.56
Division: 0.713881

RESULT:
The program to implement the Inline Function has been executed successfully using C++.
EX.NO.2A CLASSES WITH PRIMITIVE DATA MEMBERS DATE:

AIM:
To write a program in C++ to prepare a student record using classes with primitive
data members.

ALGORITHM:

1. Start the program.


2. Create a class record.
3. Declare the variables name,regno,marks,m1,m2,m3,m4,m5 and avg. 4. Define the
member function getdata() to read the values for name, Regno,mark1,mark2, mark3,
mark4, mark5 of the student.
5. Define the member function calculate() to calculate the average of mark as
Avg=(mark1+mark2+mark3+mark4+mark5)/5.
6. Display the student record using the member function display().
7. Stop the program.
SOURCE CODE:

#include<iostream>
using namespace std;
class record
{
public:
char name[20];
int regno;
int marks,m1,m2,m3,m4,m5;
float avg;
void getdata()
{
cout<<"Enter the name:\n";
cin>>name;
cout<<"Enter the regno:";
cin>>regno;
cout<<"Enter the Subject marks m1,m2,m3,m4,m5:\
n"; cin>>m1>>m2>>m3>>m4>>m5;

void calculate()
{
avg=(m1+m2+m3+m4+m5)/5;
}
void display()
{
void display() {
cout << "******************\n";
cout << "\nName: " << name;
cout << "\nRegno: " <<
regno; cout << "\nMark1: "
<< m1;
cout << "\nMark2: " << m2;
cout << "\nMark3: " << m3;
cout << "\nMark4: " << m4;
cout << "\nMark5: " << m5;
cout << "\nAvg: " << avg;
cout << "\n******************\
n"; }};
int main()
{
record r;
r.getdata();
r.calculate();
r.display();
return 0;
}
OUTPUT:
Enter the name: lokesh
Enter the regno: 1107
Enter the Subject marks m1,m2,m3,m4,m5:
58
96
78
96
98
******************
Name: Lokesh
Regno: 11072404001
Mark1: 58
Mark2: 96
Mark3: 78
Mark4: 96
Mark5: 98
Avg: 85
******************

RESULT:
The program in C++ to prepare a student Record using classes with primitive
data members was verified and executed successfully.
EX.NO.2B CLASSES WITH ARRAYS AS DATA MEMBERS DATE:

AIM:
To write a program in C++ to display product detail using classes with array as
data members.

ALGORITHM:

1. Start the program.


2. Create a class product.
3. Declare the variables pro_code, pro_price and count.
4. Define the member function getproduct() to read the values of product code and product
cost.
5. Define the member function displaysum() to calculate the sum of product cost.
6. Define the member function displayproduct() to display product details. 7.
Declare the object obj and invoke the member functions.

8. Display the output.


9. Stop the program.
SOURCE CODE:
#include<iostream>
using namespace std;
class product
{
int pro_code[50];
float pro_price[50];
int count;
public:
void cnt()
{
count=0;
}
void getproduct();
void displaysum();
void displayproduct();
};
void product::getproduct()
{
cout<<"Enter product Code:";
cin>>pro_code[count];
cout<<"Enter product Cost:";
cin>>pro_price[count];
count++;
}
void product::displaysum()
{
float sum=0;
for(int i=0;i<count;i++)
sum=sum+pro_price[i];
cout<<"Total Value:"<<sum<<"\
n"; }
void product::displayproduct()
{
cout<<" \nCode Price\n";
for(int i=0;i<count;i++)
{
cout<<"\n"<<pro_code[i];
cout<<" "<<pro_price[i]; }
cout<<"\n";
}
int main()
{
product obj;
obj.cnt();
int x; do }
cout<<"Enter your choice";
{
cout<<"\n------------ ";
cout<<"\n1.Add a product";
cout<<"\n2.Display a product total value"; cout<<"\
n3.Display all products"; cout<<"\n4.Quit \n";
cin>>x;
switch(x)
{
case 1:
obj.getproduct();
case 2:
obj.displaysum();
case 3:
obj.displayproduct();
case 4:break;
default:
cout<<"\n Invalid choice";
}

while(x!=4);
}
OUTPUT:

Enter your choice

1.Add a product
2.Display a product total
value 3.Display all products
4.Quit
1
Enter product
Code:4002 Enter
product Cost:2500 Total
Value:2500

Code Price
4002 2500

Enter your choice

1.Add a product
2.Display a product total
value 3.Display all products
4.Quit
1
Enter product
Code:4005 Enter
product Cost:6500 Total
Value:9000
Code Price
4002 2500
4005 6500

Enter your choice

1.Add a product
2.Display a product total
value 3.Display all products
4.Quit
2
Total Value:9000
Code Price
4002 2500
4005 6500
Enter your choice

1.Add a product
2.Display a product total value
3.Display all products
4.Quit
3

Code Price
4002 2500
4005 6500
Enter your choice

1.Add a product
2.Display a product total value
3.Display all products
4.Quit
4

RESULT:
The program in C++ to implement arrays as data members was created and
executed successfully.
EX.NO.2C CLASSES WITH POINTERS AS DATA MEMBERS DATE:

AIM:
Write a program in C++ to implement the classes with pointers as data
members. ALGORITHM:

1. Start the program.


2. Include <iostream> to enable input and output operations.
3. Declare a public integer member variable a.
4. Define a public member function print() to print the value of a.
5. Define the main() function:Create an instance of the data class, d.
6. Declare a pointer to a data object, dp.
7. Assign the address of d to dp.
8. Declare a pointer to an integer member of data, ptr, and initialize it with the
address of data::a.
9. Assign the value 10 to the member variable a of d using the pointer-to-member
syntax d.*ptr = 10.
10. Print the value of a using the print() method of d.
11. Assign the value 20 to the member variable a of *dp (which is d) using
the pointer-to-member syntax dp->*ptr = 20.
12. Print the value of a using the print() method of *dp (which is d).
13. Exit the program with a return value of 0.
14. Display the output.
15. Stop the program.
SOURCE CODE:
#include<iostream>
Using namespace std;
class data
{
public:
int a;
void print()
{
cout << "a: " << a;
}
};
int main()
{
data d, *dp;
dp = &d;
int data::*ptr = &data::a;
d.*ptr = 10;
d.print();
dp->*ptr = 20;
dp->print();
return 0;
}
OUTPUT:

a:10 a:20

RESULT:
The program in C++ to implement classes with pointers as data members was
verified and executed successfully.
EX.NO.2D CLASSES WITH CONSTANT DATA MEMBER DATE:

AIM:
To write a program in C++ implements the concept of class with constant data member.

ALGORITHM:

1. Start the program.


2. Create the class test.
3. Declare the integer const variable t.
4. The parameterized constructor test (int t) was created and Initialized. 5. Create two
objects t1 and t2 and the value for the variable t is initialized. (Since t is a constant data
member every object has an independent copy of t)
6. Display the result.
7. Stop the program.
//constant data member
#include<iostream>
using namespace std;
class Test
{
const int t;
public:
Test(int t): t(t)
{ // Initialize 't' in the constructor initializer list
}
int getT()
{
return t;
}
};
int main() {
cout << "constant data member";
Test t1(10);
cout << "\nDefault t1: " << t1.getT();
Test t2(20);
cout << "\nDefault t2: " << t2.getT();
return 0;
}
OUTPUT:
Default t1:10
Default t2:20

RESULT:
The program in C++ to implement classes with constant data members was verified
and executed successfully.
EX.NO.2E CLASSES WITH STATIC MEMBER FUNCTION DATE:

AIM:
To write a program in C++ to implement the concept of class with static member

functions. ALGORITHM:

1. Start the program.


2. Create the class test.
3. Declare the variable code and static integer variable count.
4. Define the member function setcode() to autoincrement the value of count. 5. Define
the member function showcode() and showcount() to display the values of code and count.
6. Create the objects t1,t2 and t3 and invoke the functions setcode() and showcount().
7. Display the output

8. Stop the program.


//STATIC MEMBER FUNCTION
#include<iostream>
using namespace std;
class test
{
int code;
static int count;
public:
void setcode(void)
{
code = ++count;
}
void showcode(void)
{
cout << "object number:" << code << "\n";
}
static void showcount(void)
{
cout << "count:" << count << "\n";
}
};
int test::count = 0; // Initialize the static member variable
count int main() {

test t1, t2;


t1.setcode();
t2.setcode();
test::showcount();
test t3;
t3.setcode();
test::showcount();
t1.showcode();
t2.showcode();
t3.showcode();
return 0;
}

OUTPUT:
count:2
count:3
object number:1
object number:2
object number:3

RESULT:
The program in C++ to implement classes with static member function was verified and
executed successfully.
EX.NO:2F FRIEND FUNCTION
DATE:

AIM:
To write a C++ program to implement the friend function concept.
ALGORITHM:
1. Start the program.
2. Declare the class sample
3. Declare the variables a and b.
4. Define the member function setvalue() to assign the values for a and b.
5. Declare a friend function mean() using the keyword friend.

6. Define a parameterized function mean() with object s as a argument variable.


7. The object s is used to access the private data variables a and b. 8. Create the
object x for the class sample.

9. Invoke the member function setvalue using x.


10. Display the value for mean.
11. Stop the program
SOURCE CODE:
#include<iostream>
using namespace std;
class sample
{
int a;
int b;
public:
void setvalue()
{
a = 25;
b = 40;
}
friend float mean(sample s); // Friend function
declaration };

// Definition of the friend function mean


float mean(sample s)
{
return float(s.a + s.b) / 2.0;
}
int main() {
sample x;
x.setvalue();
cout << "FRIEND FUNCTION:\n";
cout << "Mean value = " << mean(x) << "\
n"; return 0;

}
OUTPUT:
FRIEND FUNCTION:
Mean value =32.5

RESULT:
The program in C++ to implement the Friend Function was verified and
executed successfully.
EX.NO.3A UNARY OPERATOR OVERLOADING DATE:

AIM:
To implement the concept of unary operator overloading using c++.

ALGORITHM:

1. Start the Program.


2. Create a class space and declare necessary data members and member functions and
operator function as member function.
3. The operator unary minus is overloaded to perform the operation of changing sign
4. Define member function getdata(), to get three values that is passed as arguments.
5. Define the operator overloading function to change the sign of the values. 6.
Define the function display() to display the values before and after sign change. 7.
Stop the program.
SOURCE CODE:
#include<iostream>
using namespace std;
class space
{
int x,y,z;
public: void getdata(int a, int b, int
c); void display(void);

void operator-();
};
void space::getdata(int a,int b,int
c) {

x=a; y=b; z=c;


}
void space::display(void)
{
cout<<x<<" ";
cout<<y<<" ";
cout<<z<<"\n";
}
void space::operator-()
{
x = -x;
y = -y;
z = -z;
}
int main()
{
space s;
s.getdata(10,-20,30);
cout<<"s:";
s.display();
-s;
cout<<"s:";
s.display();
return 0;
}

OUTPUT:
s: 10 -20 30
s: -10 20 -30

RESULT:
The program in C++ to implement unary operator overloading concept was verified
and executed successfully.
EX.No. 3B BINARY OPERATOR OVERLAODING DATE:

AIM :
To write a C++ program to implement the concept of Binary operator

overloading. ALGORITHM:

1. Start the program.


2. Declare the class complex.
3. Declare the variables and its member function.
4. Declare the default constructor complex() and a parameterized constructor complex(float
real,float imag).
5. Define a binary operator overloading function complex operator+(complex c) which has
one object in the argument list.
6. Create the objects c1,c2 and c3 and invoke the constructors and the binary operator
overloading function.
7. Invoke the display() function to display the result.
8. Stop the program
SOURCE CODE:
#include<iostream>
using namespace std;
class complex
{
float x;
float y;
public:
complex()
{
}
complex(float real, float imag)
{
x=real;
y=imag;
}
complex operator+(complex
c); void display(void);
};
complex complex::operator+(complex c)
{

complex temp;
temp.x = x+c.x;
temp.y = y+c.y;
return(temp);
}
void complex::display(void)
{
cout<<x<<"+j"<<y<<"\n";
}
int main()
{
complex c1,c2,c3;
c1=complex(2.5,3.5);
c2=complex(1.6,2.7);
c3=c1+c2;
cout<<"c1="; c1.display(); cout<<"c2=";
c2.display();
cout<<"c3="; c3.display();
return 0;
}

OUTPUT:

c1=2.5+j3.5
c2=1.6+j2.7
c3=4.1+j6.2

RESULT:
The program in C++ to implement binary operator overloading concept was verified and
executed successfully.
EX.NO.3C FUNCTION OVERLOADING
DATE:

AIM:
To write a C++ program to implement the concept of Function

Overloading. ALGORITHM:

1. Start the program.


2. Create the class
3. Declare the necessary variables.
4. Define three member functions with the same name volume with different number of
variables in the argument list.
5. Invoke the member functions and pass the value in the argument list. 6. According to
the number of values passed in the argument list the appropriate member function is
invoked.
7. Stop the program.
SOURCE CODE:
#include<iostream>
using namespace std;

int volume(int s)
{
return (s * s * s);
}

double volume(double r, int


h) {
return (3.14 * r * r * h);
}

long volume(long l, int b, int


h) {
return (l * b * h);
}

int main()
{
int s, l, b, h;
double r;

cout << "!!!VOLUME!!!\n";


cout << "Enter the value of s:
"; cin >> s;
cout << "Volume of a Cube: " << volume(s) << endl;

cout << "Enter the value of radius and height: ";


cin >> r >> h;
cout << "Volume of a Cylinder: " << volume(r, h) << endl;

cout << "Enter the value of length, breadth and height: ";
cin >> l >> b >> h;
cout << "Volume of a Rectangle: " << volume(l, b, h) << endl;

return 0;
}
OUTPUT:

!!VOLUME!!!
Enter the value of s: 5
Volume of a Cube: 125
Enter the value of radius and height: 5 10
Volume of a Cylinder: 785
Enter the value of length, breadth and height: 12 6 4
Volume of a Rectangle: 288

RESULT:
The program in C++ to implement function overloading concept was verified
and executed successfully.
EX.NO:4A SINGLE INHERITANCE
DATE:

AIM:
To write a C++ program to implement single inheritance using c++.

ALGORITHM:
1. Start the program.
2. Declare the base class emp.
3. Define and declare the function get() to get the employee details.
4. Declare the derived class salary.
5. Declare and define the function get1() to get the salary details.
6. Define the function calculate() as the member of class salary to find the net pay. 7.
Define the function display() as the member of class salary to display the details of the
employee.
8. Create the object s for the derived class.
9. Read the number of employees.
10. Invoke the member functions get(),get1() and calculate().
11. Invoke the member function display().
12. Stop the program.

SOURCE CODE:
#include<iostream>
using namespace std;
class emp
{
public:
int eno;
char name[20],des[20];
void get()
{
cout<<"Enter the Employee Id : ";
cin>>eno;
cout<<"Enter the employee name:";
cin>>name;
cout<<"Enter the designation:";
cin>>des;
}
};
class salary:public emp
{
float bp,hra,da,pf,np;
public: void get1()
{
cout<<"Enter the basic pay:";
cin>>bp;
cout<<"Enter the House Rent
Allowance:"; cin>>hra;

cout<<"Enter the Dearness


Allowance :"; cin>>da;

cout<<"Enter the Provident Fund:";


cin>>pf;
}
void calculate()
{
np=bp+hra+da-pf;
}
void display()
{
cout<<eno<<"\t"<<name<<"\t"<<des<<"\t "<<bp<<"\t "<<hra<<"\t "
<<da<<"\t"<<pf<<"\t"<<np<<"\n";

}
};
int main()
{
int i,n;
char ch;
salary s[10];
cout<<"\t EMPLOYEE DETAILS\n";
cout<<"\t \n";
cout<<"Enter the number of records :";
cin>>n; for(i=0;i<n;i++)
{
s[i].get();
s[i].get1();
s[i].calculate();
}
cout<<"EmpId EmpName Designation BasicPay HRA\tDA\tPF\tNetPay\n";
for(i=0;i<n;i++)
{ s[i].display();
}
return 0;
}
EMPLOYEE DETAILS

Enter the number of records :2


Enter the Employee Id : 1234
Enter the employee name:Rani
Enter the designation:AP
Enter the basic pay:25000
Enter the House Rent Allowance:4000
Enter the Dearness Allowance :2000
Enter the Provident Fund:1000

Enter the Employee Id : 2345


Enter the employee name:Raja
Enter the designation:AP
Enter the basic pay:30000
Enter the House Rent Allowance:6000
Enter the Dearness Allowance :2000
Enter the Provident Fund:2000

EmpId EmpName Designation BasicPay HRA DA PF NetPay


1234 Rani AP 25000 4000 2000 1000 30000
2345 Raja AP 30000 6000 2000 2000 36000

RESULT:
The program in C++ to implement Single Inheritance concept was verified and executed
successfully.
EX.NO:4B MULTIPLE INHERITANCE DATE:

AIM:
To write a C++ program to implement multiple inheritance.

ALGORITHM:
1. Start the program.
2. Declare the base class student.
3. Declare and define the function get() to get the student details.
4. Declare the base class sports.

5. Declare and define the function getsm() to read the sports mark. 6. Declare the
class statement derived from class student and class sports. 7. Declare and define
the function display() to find out the total and average. 8. Declare the derived
class object, call the functions get(),getsm() and display(). 9. Stop the program.

SOURCE CODE:
#include<iostream>
using namespace std;
class student
{
protected:
int rollno, m1, m2;
public:
void get()
{
cout << "Enter the Roll Number :";
cin >> rollno;
cout << "Enter mark1 :";
cin >> m1;
cout << "Enter mark2 :";
cin >> m2;
}
int getMark1() const
{
return m1;
}
int getMark2() const
{
return m2;
}
};
class sports
{
protected:
int sm;
public:
void getsm()
{
cout << "Enter the sports
mark :"; cin >> sm;

}
int getSportsMark() const
{
return sm;
}
};
class report : public student, public sports
{
int tot;
float avg;

public:
void display() {
tot = (getMark1() + getMark2() +
getSportsMark()); avg = static_cast<float>(tot) / 3;
cout << "\n\tTotal : " << tot;
cout << "\n\tAverage : " << avg;
}
};

int main()
{
report obj;
cout << "\t STUDENT DETAILS\n";
cout << "\t \n";
obj.get();
obj.getsm();
obj.display();
return 0;
}
OUTPUT:

STUDENT DETAILS

Enter the Roll Number : 11072404001


Enter the mark1 : 80
Enter the mark2 : 90
Enter the sports mark :78

Total 248
Average 82

RESULT:
The Program in C++ to implement the Multiple Inheritance has been verified
and executed successfully
EX.NO:4C MULTILEVEL INHERITANCE
DATE:

AIM:
To write a C++ program to implement multilevel inheritance.

ALGORITHM:
1. Start the program.
2. Declare the base class student.
3. Declare and define the function getdata() to get the student details.
4. Declare and define the function putdata() to display the student details.
5. Declare the class test derived from student.

6. Declare and define the function gettest() to read the marks.


7. Declare and define the function puttest() to display the marks.
8. Declare the class result derived from test.
9. Declare and define the function displayresult() to find out the total marks. 10. Declare
the derived class object, call the functions getdata(),gettest() and displayresult(). 11. Stop
the program.

SOURCE CODE:

#include<iostream>
using namespace std;
class student
{
protected: int
roll_number;
public: void get_number(int a)
{
roll_number=a;
}
void put_number(void)
{
cout<<"Roll Number :"<<roll_number<<"\
n"; }

};
class test :public student
{
protected: float
sub1,sub2;
public: void get_marks(float x,float y)
{
sub1=x;
sub2=y;
}
void put_marks(void)
{
cout<<"\nMarks in Subject 1 ="<<sub1;
cout<<"\nMarks in Subject 2 ="<<sub2<<"\n"; }

};
class result: public test
{
protected:
float total;
public:
void display(void)
{
total=sub1+sub2;
put_number();
put_marks();
cout<<"Total :"<<total<<"\n";
}
};
int main()
{
result s;
cout<<"\t MULTILEVEL INHERITANCE";
cout<<"\n\t \n";
s.get_number(111);
s.get_marks(80.5,75.5);
s.display();
getch();
}

OUTPUT:

MULTILEVEL INHERITANCE
Roll Number :111
Marks in Subject 1 =80.5
Marks in Subject 2 =75.5
Total :156

RESULT:
The Program to implement the Multilevel Inheritance has been verified and
executed successfully using C++.
EX. NO. 4D VIRTUAL FUNCTION
DATE:

AIM:
To write a C++ program to implement the concept of Virtual functions

ALGORITHM:
1. Start the program.
2. Define the base class base.
3. Define the base class virtual function display() using the keyword virtual. 4. Derive the
classes sub1, sub2 from base class base and define the function display() in the respective
classes.
5. Declare a base class pointer in main function.
6. Declare objects for d1 and d2 for the classes sub1 and sub2.
7. Assign the objects to the base pointer *bptr.
8. Invoke the display() function using -> pointer.
9. Depending upon the object in the bptr the appropriate display() function is displayed.
SOURCE CODE:

#include<iostream>
using namespace std;

class base {
public:
virtual void display() {
cout << "Base class display is called\
n"; }
};

class sub1 : public base {


public:
void display() override {
base::display(); // Call base class display
cout << "Derived class1 display is called\
n"; }

};

class sub2: public base {


public:
void display() override {
base::display(); // Call base class display
cout << "Derived class2 display is called\
n"; }
};

int main() {
base *bptr, b;
sub1 d1;
sub2 d2;
cout << "\t VIRTUAL FUNCTION\n";
cout << "\t \n";
bptr = &b;
bptr->display();
bptr = &d1;
bptr->display();
bptr = &d2;
bptr->display();
return 0;
}

OUTPUT:

VIRTUAL FUNCTION

Base class display is called

Derived class1 display is called

Derived class2 display is called

RESULT:
The program to implement the virtual function has been verified and executed
successfully using C++.
EX. NO:4E VIRTUAL BASE CLASS
DATE:

AIM:
To write a C++ program to implement the concept of virtual base class.

ALGORITHM:
1. Start the program
2. Include suitable header files 3.
Create a base class student.
4. In the base class student define the function get_number() and put_number().
5. In the class sports define the function get_score() and put_score(). 6. Derive a
class test form base student and define the function get_mark() and put_mark().

7. Derive a class result from test and sports class and define function display().
8. Create the object s for the class result using the object invoke the functions
get_number(), get_score(), get_mark() and display().
9. Stop the program
SOURCE CODE:

#include<iostream>
using namespace std;

class student
{
protected:
int roll_number;
public:
void get_number(int a)
{
roll_number = a;
}
void put_number(void)
{
cout << "ROLL NO: " << roll_number << "\
n"; }
};

class test : public student


{
protected:
float part1, part2;
public:
void get_marks(float x, float y)
{
part1 = x;
part2 = y;
}
void put_marks(void)
{
cout << "Marks Obtained:\n";
cout << "Part 1 = " << part1 << "\
n"; cout << "Part 2 = " << part2 << "\
n"; }
};
class sports : public student
{
protected:
float score;
public:
void get_score(float s)

score = s;
}
void put_score(void)
{
cout << "Sports wt : " << score << "\
n"; }
};
class result : public test, public
sports {
float total;
public:
void display(void);
};
void result::display(void)
{
total = part1 + part2 + score;
put_number(); // Call base class member function first
put_marks(); // Call test class member function next
put_score(); // Call sports class member function last
cout << "Total Score : " << total << "\n";
}
int main()
{
result s;
cout << "\tVIRTUAL BASECLASS";
cout << "\n\t -----------------\n";
s.get_number(678);
s.get_marks(30.5, 25.5);
s.get_score(7.0);
s.display();
return 0;
}
OUTPUT:

VIRTUAL BASECLASS

ROLL NO:678
Marks Obtained:
Part 1 = 30.5
Part 2 = 25.5
Sports wt : 7
Total Score : 63

RESULT:
The Program to implement the virtual base class has been verified and executed
successfully using C++.
EX.NO:4F CLASS TEMPLATES
Date:

AIM:
To write a C++ program to implement the concept of class template.

ALGORITHM:
1. Start the program.
2. Create the class template test using the keyword template.
3. Declare the two member variables a and b of type T.
4. Define the parameterized constructor with two variables x and y in the argument list of
type T1 and T2.
5. The variables a and b acts as alias variable for x and y.
6. Objects test1,test2,test3 and test4 are created and the different type of data values are
passed to the constructor test. 7. Display the vales of a and b 8. Stop the process.
SOURCE CODE:
#include <iostream.h>
using namespace std;
template <class T1, class
T2> class test
{
T1 a;
T2 b;
public :
test (T1 x, T2 y)
{
a= x;
b= y;
cout << "\n\nThe value of a is :
"<<a; cout << "\nThe value of b is :
"<<b; cout <<" \n Sum is :" <<
a+b; }
};
int main()
{
cout<< " CLASS
TEMPLATES"; cout<<"\n ";

test <int,int> test1(10,20);


test <float,float>
test2(15.5,25.5);
test <int,float>
test3(20,25.5);
test <float,int>
test4(15.5,30); return 0;

}
OUTPUT:

CLASS TEMPLATES

The value of a is : 10
The value of b is : 20
Sum is :30

The value of a is : 15.5


The value of b is : 25.5
Sum is :41

The value of a is : 20
The value of b is : 25.5
Sum is :45.5

The value of a is : 15.5


The value of b is : 30
Sum is :45.5

RESULT:
The Program to implement the class templates has been verified and executed
successfully using C++.
EX. NO:4G FUNCTION TEMPLATE
DATE:

AIM:
To write a C++ program for swapping two values using function templates

ALGORITHM:
1. Start the program.
2. Create the template class X using the keyword template.
3. Create the parameterized member function bubble() with two variables in the argument
list to perform bubble sort.
4. Define the member function swap() to swap the values of the variables a and b.
5. Display the values in the array before and after sorting.
6. Stop the program.

SOURCE CODE:
#include <iostream.h>
using namespace std;
template <class T> void
bubble(T a[],int n)
{ for(int i=0;i<n-1;i++)
for(int j=n-1;i<j;j--)
if ( a[j]<a[j-1]) swap(a[j],a[j-1]);
}
emplate <class X> void
swap(X &a, X &b)
{
X temp=a;
a=b;
b=temp;
}
int main()
{
int x[5]={6,4,8,2,1};
float y[5]={1.1,3.5,4.5,2.2,3.3};
cout<< " FUNCTION
TEMPLATES"; cout<<"\n ";
cout <<"\nGiven X Array";
cout <<"\n ------------ \n";
for (int i=0;i<5;i++)
cout <<x[i]<<"\t";
bubble(x,5);
cout <<"\nSorted X Array";
cout <<"\n------------ \n";
for ( i=0;i<5;i++)
cout <<x[i]<<"\t";
cout <<"\nGiven Y Array";
cout <<"\n-------------\n";
for (int j=0;j<5;j++)
cout <<y[j]<<"\t";
bubble(y,5);
cout <<"\nSorted Y Array";
cout <<"\n------------ \n";
for ( j=0;j<5;j++)
cout <<y[j]<<"\t";
return;
}
OUTPUT:
FUNCTION TEMPLATES

Given X Array

64821
Sorted X Array

12468
Given Y Array

1.1 3.5 4.5 2.2 3.3


Sorted Y Array

1.1 2.2 3.3 3.5 4.5

RESULT:
The Program to implement the function templates has been verified and executed
successfully using C++.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy