FINAL Object Oriented Programming Lab Manual

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

LAB MANUAL

Lab Name : Object Oriented Programming Lab

Lab Code : 3AID4-22

Branch : Artificial Intelligence & Data Science

Year : 2nd Year

Department of Artificial Intelligence & Data Science


Jaipur Engineering College and Research Center, Jaipur
(Rajasthan Technical University, KOTA)

INDEX
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

S.No CONTENTS Page No

1 Vision and Mission of the Institute 3


2 Vision and Mission of the Department 4
3 Program Educational Objectives (PEOs) 5
4 Program Outcomes (POs) 6-7
5 RTU Syllabus with List of Experiments 8-9
6 Course Outcomes (COs) 10
7 CO/PO-PSO mapping 11-13
8 Introduction about Lab &its Applications 14

9 Instructions Sheet 15-16


Experiment List (As per RTU, Kota Syllabus)

Exp:- 1 Understand the basics of C++ library, variables, and data input-output.
Exp:- 2 C++ program using with the concept of structures.
Exp:-3 Implement class and object concepts and function overloading.
Write programs to understand dynamic memory allocation and array of
Exp:-4
objects.
Exp:-5 Program to understand different types of constructors and destructor.

Exp:-6 Implement friend function to access private data of a class and usage of this
pointer.
Exp:-7 Write programs to understand the usage of constant data member and member
function, static data member and member function in a class.
Exp:-8 Implement different types of inheritance, function overriding and virtual
function.
Exp:-9 Implement Operator overloading concepts.
Exp:-10 Write programs to understand function template and class template.
Exp:-11 Write programs to understand exception handling techniques.
Exp:-12 Write programs to understand file handling techniques.

Vision of the Institute


2
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

To become a renowned centre of outcome based learning and work toward academic,
professional, cultural and social enrichment of the lives of individuals and communities.

Mission of the Institute


M1: Focus on evaluation of learning outcomes and motivate students to inculcate research

aptitude by project based learning.

M2: Identify, based on informed perception of Indian, regional and global needs, the areas of

focus and provide platform to gain knowledge and solutions.

M3: Offer opportunities for interaction between academia and industry.

M4: Develop human potential to its fullest extent so that intellectually capable and imaginatively

gifted leaders can emerge in a range of professions.

Vision of the Department

3
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

To prepare students in the field of Artificial Intelligence and Data Science for competing with
the global perspective through outcome based education, research and innovation.

Mission of the Department


M1: To impart outcome based education in the area of AI&DS.

M2: To provide platform to the experts from institutions and industry of repute to transfer the

knowledge to students for providing competitive and sustainable solutions.

M3:To provide platform for innovation and research.

Program Educational Objectives (PEO)

4
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

PEO1: To provide students with the fundamentals of Engineering Sciences with more
emphasis in Artificial Intelligence and Data Science by way of analyzing and exploiting
engineering challenges.
PEO2:To train students with good scientific and engineering knowledge so as to
comprehend, analyze, design, and create novel products and solutions for the real life
problems in Artificial Intelligence and Data Science
PEO3: To inculcate professional and ethical attitude, effective communication skills,
teamwork skills, multidisciplinary approach, entrepreneurial thinking and an ability to relate
engineering issues with social issues for Artificial Intelligence & Data Science.
PEO4: To provide students with an academic environment aware of excellence, leadership,
written ethical codes and guidelines, and the self-motivated life-long learning needed for a
successful professional career in Artificial Intelligence & Data Science.
PEO5: To prepare students to excel in Industry and Higher education by Educating
Students along with High moral values and Knowledge in Artificial Intelligence & Data
Science.

5
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

Program Outcomes (PO)

1. Engineering knowledge: Apply the knowledge of mathematics, science,


engineering fundamentals, and Artificial Intelligence and Data Science specialization to
the solution of complex Artificial Intelligence & Data Science problems.
2. Problem analysis: Identify, formulate, research literature, and analyze complex
Artificial Intelligence and Data Science problems reaching substantiated conclusions
using first principles of mathematics, natural sciences, and engineering sciences.
3. Design/development of solutions: Design solutions for complex Artificial
Intelligence and Data Science 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.
4. Conduct investigations of complex problems: Use research-based knowledge and
research methods including design of Artificial Intelligence & Data Science
experiments, analysis and interpretation of data, and synthesis of the information to
provide valid conclusions.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and
modern engineering and IT tools including prediction and modeling to complex
Artificial Intelligence and Data Science activities with an understanding of the
limitations.
6. 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 Artificial Intelligence & Data Science
practice.
7. Environment and sustainability: Understand the impact of the professional
Artificial Intelligence & Data Science in societal and environmental contexts, and

6
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

demonstrate the knowledge of, and need for sustainable development.


8. Ethics: Apply ethical principles and commit to professional ethics and
responsibilities and norms of the Artificial Intelligence & Data Science practice.
9. Individual and team work: Function effectively as an individual, and as a member
or leader in diverse teams, and in multidisciplinary settings in Artificial Intelligence &
Data Science
10. Communication: Communicate effectively on complex Artificial Intelligence &
Data Science 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.
11. Project management and finance: Demonstrate knowledge and understanding of
the Artificial Intelligence & Data Science 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.
12. Life-long learning: Recognize the need for, and have the preparation andability to
engage in independent and life-long learning in the broadest contextof technological
change in Artificial Intelligence & Data Science.

7
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

RTU Syllabus with List of Experiments


Object Oriented Programming Lab Code: 3AID4-22

Class: 3th Sem. B. Tech. 2rd year Evaluation


Branch: Artificial Intelligence & Data Science Examination Time=Three (3) Hours
Credits: 1.5 Max. Marks : 100 (IA:60, ETE:40)
Schedule per week: 3 Hrs (Practical)

Experiment List (As per RTU, Kota Syllabus)

Exp:- 1 Understand the basics of C++ library, variables, and data input-output.
Exp:- 2 C++ program using with the concept of structures.
Exp:-3 Implement class and object concepts and function overloading.
Write programs to understand dynamic memory allocation and array of
Exp:-4
objects.
Exp:-5 Program to understand different types of constructors and destructor.

Exp:-6 Implement friend function to access private data of a class and usage of this
pointer.
Exp:-7 Write programs to understand the usage of constant data member and
member function, static data member and member function in a class.
Exp:-8 Implement different types of inheritance, function overriding and virtual
function.
Exp:-9 Implement Operator overloading concepts.
Exp:-10 Write programs to understand function template and class template.
Exp:-11 Write programs to understand exception handling techniques.
Exp:-12 Write programs to understand file handling techniques.
Perform the following experiment
using Virtual Lab :http://vlabs.iitb.ac.in/vlabs-dev/labs/oops/labs/exp1/index.php
To understand the concept of Object Oriented Programming in C++.
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

Perform the following experiment using Virtual Lab:


http://vlabs.iitb.ac.in/vlabs-dev/labs/oops/labs/exp4/index.php

To understand the concept of function overloading and understand the practical use of function
overloading in C++.

Perform the following experiment using Virtual Lab:


http://vlabs.iitb.ac.in/vlabs-dev/labs/oops/labs/exp5/index.php

To understand the concept of constructor and different characteristics of constructor. To understand


constructor in an object-oriented systems.
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

Course Outcomes

CO1: Understand the concept of C++ and using it in structure.


CO2: Apply object oriented programming concepts using class and objects and function
overloading
CO3: Implement various Inheritance and polymorphism techniques.
CO4: Apply exception handling and file handling techniques.
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

Mapping of Experiments with COs & BT Level


S. No. Contents COs BT*

1
Exp:- 1 Understand the basics of C++ library, variables, and data input-output. 2

Exp:- 2 C++ program using with the concept of structures. 1 2

Exp:-3 Implement class and object concepts and function overloading. 1 4

Exp:-4 Write programs to understand dynamic memory allocation and array of objects. 1 4

Exp:-5 Program to understand different types of constructors and destructor. 2 4

2
Exp:-6 Implement friend function to access private data of a class and usage of this pointer. 4

Exp:-7 Write programs to understand the usage of constant data member and member 2 4
function, static data member and member function in a class.
3
Exp:-8 Implement different types of inheritance, function overriding and virtual function. 4

Exp:-9 Implement Operator overloading concepts. 3 4


Exp:-10 Write programs to understand function template and class template. 4 4

Exp:-11 Write programs to understand exception handling techniques. 4 4


Exp:-12 Write programs to understand file handling techniques. 4 4
Perform the following experiment using Virtual Lab :http://vlabs.iitb.ac.in/vlabs-
dev/labs/oops/labs/exp1/index.php
1 To understand the concept of Object Oriented Programming in C++. 1 2

Perform the following experiment using Virtual Lab: http://vlabs.iitb.ac.in/vlabs-dev/labs/oops/labs/exp4/index.php


2 To understand the concept of function overloading and understand the practical use of 2 4
function overloading in C++.
Perform the following experiment using Virtual Lab: http://vlabs.iitb.ac.in/vlabs-dev/labs/oops/labs/exp5/index.php
2 4
3 To understand the concept of constructor and different characteristics of constructor.
To understand constructor in an object-oriented systems.
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

Mapping of Course Outcomes & POs/PSOs

Conduct Invest. of complex problems

Project Management and Finance


Environment and Sustainability
Design/Development of Solution

Individual and Team Work


The engineer and society
Engineering Knowledge

Modern Tool Usage

Life-long Learning
Problem analysis

Communication
Ethics

PO PO PO PO PO PO PO PO PO PO PO PO
1 2 3 4 5 6 7 8 9 10 11 12

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

CO-2 3 3 3 2 3 - - - - - 2 2

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

CO-4 3 3 3 2 1 - 1 1 - 2 1 2
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

INTRODUCTION ABOUT LABORATORY& APPLICATIONS


Direct Instructions:
• Black board presentation

Interactive Instruction:

• Coding

Indirect Instructions:

• Problem solving

LEARNING MATERIALS:-

Text/Lab Manual

ASSESSMENT OF OUTCOMES:-

• End term Practical exam (Conducted by RTU, KOTA)


• Daily Lab interaction.

OUTCOMES WILL BE ACHIEVED THROUGH FOLLOWING:-

1. Lab Teaching (through chalk and board).

2. Discussion on website work


Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

INSTRUCTIONS SHEET
INSTRUCTIONS OF LAB

DO’s
 Please switch off the Mobile/Cell phone before entering Lab.
 Enter the Lab with complete source code and data.
 Check whether all peripheral are available at your desktop before proceeding for
program.
 Intimate the lab in charge whenever you are incompatible in using the system or in case
software get corrupted/ infected by virus.
 Arrange all the peripheral and seats before leaving the lab.
 Properly shutdown the system before leaving the lab.
 Keep the bag outside in the racks.
 Enter the lab on time and leave at proper time.
 Maintain the decorum of the lab.
 Utilize lab hours in the corresponding experiment.
 Get your Cd / Pen drive checked by lab in charge before using it in the lab.

DON’TS

 No one is allowed to bring storage devices like Pan Drive /Floppy etc. in the lab.
 Don’t mishandle the system.
 Don’t leave the system on standing for long
 Don’t bring any external material in the lab.
 Don’t make noise in the lab.
 Don’t bring the mobile in the lab. If extremely necessary then keep ringers off.
 Don’t enter in the lab without permission of lab in charge.
 Don’t litter in the lab.
 Don’t delete or make any modification in system files.
 Don’t carry any lab equipment outside the lab.
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

INSTRUCTIONS FOR STUDENT

BEFORE ENTERING IN THE LAB

 All the students are supposed to prepare the theory regarding the next program.
 Students are supposed to bring the practical file and the lab copy.
 Previous programs should be written in the practical file.
 Any student not following these instructions will be denied entry in the lab.

WHILE WORKING IN THE LAB


• Adhere to experimental schedule as instructed by the lab in charge.
• Get the previously executed program signed by the instructor.
• Get the output of the current program checked by the instructor in the lab copy.
• Each student should work on his/her assigned computer at each turn of the lab.
• Take responsibility of valuable accessories.
• Concentrate on the assigned practical and do not
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

LAB-1
1.(A)AIM: WAP to Print a Hello world message.
PROGRAM CODE:
#include<iostream.h>
Void main ()
{
cout<<”Hello world”;
getch();
}
OUTPUT:
Hello world

2.(b)AIM: WAP to calculate Average of two numbers.


PROGRAM CODE:
#include<iostream.h>
Void main ()
{
Float n1,n1,Sum,Avg;
cout<<”enter the 2 numbers:”;
cin>>n1>>n2;
Sum= n1+n2;
Avg=(Sum/2);
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

Cout<<”Sum=”<<Sum;
Cout<<”Avg=”<<Avg;
getch();
}
OUTPUT:
Enter the 2 numbers
4 6
Sum=10
Avg=5

3. AIM: WAP to display Simple Interest Computation.


PROGRAM CODE:
#include<iostream.h>
#include<conio.h>
void main()
{ floatp,r,t,si,total;
cout<<"enter p,r,t,si,total"<<"\n";
cin>>p>>r>>t;
si=(p*t*r)/100;
total=p+si;
cout<<"si="<<si<<"\n";
cout<<"total="<<total<<"\n";
getch();
}
OUTPUT:
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

Enter p, r ,t, si, total


1000 2 5
Si=100
Total=1100

4.AIM: WAP to Calculate factorial of an number.


PROGRAM CODE:
#include<iostream.h>
#incliude<conio.h>
#include<stdio.h>
Void main ()
{ Intn,I,f=1;
Clrscr();
cout<<”enter no.”;
cin>>n;
If(n==0)
cout<<”factorialis”<<f<,endl;
Else
{ For(i=1;i<+n;i++)
F=f*I;
cout<<”factorial is”<<f;
}
getch();
}
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

OUTPUT:
Enter no.
5
Factorial: 120

5.AIM: WAP to Calculate Maximum of two numbers.


PROGRAM CODE:
#include<iostream.h>
#incliude<conio.h>
#include<stdio.h>
Void main ()
{ int x ,y;
cout<<”enter the two integers:”;
cin>>x>>y;
cout<<”maximum=”<< max(x,y);
return 0;
}
Int max (int a, int b)
{
if(a>b)
return a;
Else
return b;
}
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

OUTPUT:
ENTER THE TWO NUMBERS
34
23
MAXIMUM:34

6.AIM:WAP to Swap Integer value.


PROGRAM CODE:
#include<iostream.h>
#include<conio.h>
void main()
{ float n1,n2,s;
cout<<"n1,n2"<<"\n";
cin>>n1>>n2;
s=n1;
n1=n2;
n2=s;
cout<<"after swaping<a,b>;"<<"\n"<<n1<<"\n"<<n2;
getch();
}
OUTPUT:
n1:4 n2:6
After swapping: n1:6 n2:4

Viva voice:
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

1)WHAT IS CLASS.
2)HOW CAN WE ACCESS CLASS MEMBER FUNCTION.
3)WHAT IS LOGIC BEHIND FUNCTION SWAPING OF TWO VARIABLE.

LAB-2:
1. AIM: PROGRAMME TO PRINT THE NAME, PRICE, NO OF PAGES OF A
BOOK USING STRUCTURE

#include<conio.h> OUTPUT
#include<stdio.h> ENTER THE NAME PRICE PAGES OF BOOK
void main( ) OOP
{ 300
struct book 500
{ THE ENTERED DETAILS ARE
char name[10]; OOP
int price; 300
int pages; 500
}b;
clrscr();
printf("enter the name,price,pages of a book\n");
scanf("%s%d%d",&b.name,&b.price,&b.pages);
printf(“the entered details are \n”);
printf("%s\n%d\n%d",b.name,b.price,b.pages);
getch( );
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

2.AIM: PROGRAMME TO COPY THE VALUE OF ONE STRUCTRE INTO


ANOTHER STRUCTURE VARRIABLE
#include<stdio.h>
#include<conio.h>
void main( )
{
structemp
{
char name[10]; OUTPUT
int age; ENTER THE NAME AGE SALARY OF EMPLOYEE
int salary; RAMESH RAMESH
}e1,e2; 25 25
clrscr( ); 30000 30000
printf("enter the name ,age ,salary of an employee");
scanf("%s%d%d",&e1.name,&e1.age,&e1.salary);
e2=e1;
printf("\n %s \n %d\n %d",e2.name,e2.age,e2.salary);
getch( );
}

3 AIM:PROGRAMME TO DISPLAY 5 STUDENTS RECORD (ARRAY OF


STRUCTURE) .
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

#include<conio.h>
#include<stdio.h>
void main( )
{
typedefstruct student
{
charsname [10];
introllno;
charcname[10];
}student ;
studentsarr[5];
inti;
clrscr();
for(i=0;i<5;i++)
{
printf("enter name and class of student no %d:",i+1);
scanf("%s%s",sarr[i].sname,sarr[i].cname);
printf ("enter the roll no \n");
scanf("%d",&sarr[i].rollno);
}
printf("\n student detail");
printf("\n sname \t cname \t roll no \n");
for(i=0;i<5;i++)
{
printf("%s\t%s\t",sarr[i].sname,sarr[i].cname);
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

printf ("%5d\n",sarr[i].rollno);
}
getch();
}

4.AIM: PROGRAMME TO CALCULATE GROSS SALARY OF THE PERSON GIVEN


BASIC SALARY (BS) AS INPUT. IF BASIC SALARY IS >5000 DA=55% OF BS AND
HRA=15%OF BS ELSE DA=45%OF BS AND HRA=10% OF BS
#include<iostream.h>
#include<conio.h>
void main( )
{
floatbs,gs,hra,da;
cout<<"enter your basic salary\n";
cin>>bs;
if(bs<=5000)
{
da=(bs*45)/100;
hra=(bs*10)/100;
}
else
{
da=(bs*55)/100;
hra=(bs*15)/100;
}
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

gs=bs+da+hra;
cout<<"basic salary is"<<bs<<endl;
cout<<"hra is "<<hra<<endl;
cout<<"da is "<<da<<endl;
cout<<"gross salary is"<<gs<<endl;
getch();
}

5.AIM: SWAPPING OF TWO VALUES USING CALL BY REFFERENCE.


#include<iostream.h>
#include<conio.h>
void main()
{
void swap(int&,int&); OUTPUT
intx,y; ENTER TWO NUMBER
cout<<"enter the two no"; 12
cin>>x>>y; 14
cout<<"before calling swap\n"; BEFORE CALLING SWAP
cout<<"x="<<x<<"\t"; X=12 Y=14
cout<<"y="<<y<<"\t"<<endl; AFTER CALLING SWAP
swap(x,y); X=14 Y=12
cout<<"after calling swap\n";
cout<<"x="<<x<<"\t";
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

cout<<"y="<<y<<endl;
getch( );
}
void swap(int&rx,int&ry)
{
int t;
t=rx;
rx=ry;
ry=t;
}

6.AIM: SWAPPING OF TWO VALUES USING CALL BY VALUE


#include<iostream.h>
#include<conio.h> OUTPUT
void main() ENTER TWO NUMBER
{ 55
void swap(int,int); 66
intx,y; BEFORE CALLING SWAP
cout<<"enter the two no"; X=55 Y=66
cin>>x>>y; AFTER CALLING SWAP
cout<<"before calling swap\n"; X=66 Y=55
cout<<"x="<<x<<"\t";
cout<<"y="<<y<<"\t"<<endl;
swap(x,y);
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

cout<<"after calling swap\n";


cout<<"x="<<x<<"\t";
cout<<"y="<<y<<endl;
getch();
}
void swap(intvx,intvy)
{
int t;
t=vx;
vx=vy;
vy=t;
}
VIVA VOICE:
1) WHAT IS STRUCTURE?
2) HOW ONE STRUCTURE COPY INTO ANOTHER STRUCTURE.
3) WHAT IS DIFFERENCE BETWEEN CALL BY VALUE AND CALL BY
REFRENCE?
4) WHAT IS ARRAY?
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

LAB 3 & 4:
1. AIM: PROGRAM TO GENERATE FIBONACCI SERIES USING CLASS AND
OBJECT.
#include<conio.h>
#include<iostream.h>
classFibo
{
private: OUTPUT
int term; ENTER THE NO OF TERMS
public: 8
void input(int x) 0 1 1 2 3 5 8 13
{
term=x;
}
voidgen_ser( )
{
intc,a=0,b=1,t;
cout<<"fibbonaci series is \n";
cout<<a<<" "<<b;
for(t=1;t<=term-2;t++)
{
c=a+b;
a=b;
b=c;
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

cout<<" "<<c;
}
cout<<endl;
}
};
void main( )
{
Fiboobj;
int n;
cout<<"enter the no of terms \n";
cin>>n;
obj.input(n);
obj.gen_ser( );
getch( );
}
2.AIM: PROGRAM TO REVERSE ARRAY ELEMENT USING CLASS AND OBJECT.
#include<iostream.h>
#define s 5
#include<conio.h>
class A
{
intarr[s];
public:
void input( )
{
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

for(inti=0;i<s;i++) OUTPUT:
{ enterarr[0] element :23
cout<<"\n enter array["<<i<<"] element :"; enter arr[1] element :13
cin>>arr[i]; enter arr[2] element :33
} } enter arr[3] element :53
enter arr[4] element :5
void show( ) array elements are
{ arr[0]=23
cout<<"array element are \n"; arr[1]=13
for(inti=0;i<s;i++) arr[2]=33
cout<<"arr["<<i<<"]="<<arr[i]<<endl; arr[3]=53
} arr[4]=5
void reverse( ) reverse array elements are
{ arr[0]=5
inti,j; arr[1]=53
for(i=0;i<s;i++) arr[2]=33
for(j=i+1;j<s;j++) arr[3]=13
{ arr[4]=23
arr[i]=arr[i]+arr[j];
arr[j]=arr[i]-arr[j];
arr[i]=arr[i]-arr[j];
}
show( );
}
};
void main( )
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

{
Aobj;
obj.input( );
obj.show( );
cout<<"reverse";
obj.reverse( );
getch( );
}
3. AIM: AIM: PROGRAM TO SORT ARRAY ELEMENT BY USING CLASS AND
OBJECT.
#include<conio.h>
#include<iostream.h>
#define s 5
classAsort
{
intarr[s];
public:
void input( )
{
for(inti=0;i<s;i++)
{
cout<<"\n enter arr["<<i<<"] element :";
cin>>arr[i];
}
}
void show( )
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

{
cout<<"array element are\n" ;
for(inti=0;i<s;i++)
cout<<"arr["<<"]="<<arr[i]<<endl;
}
void sort()
{
inti,j,t;
for(i=0;i<s;i++)
for(j=i+1;j<s;j++)
if(arr[i]>arr[j])
{
t=arr[i];
arr[i]=arr[j];
arr[j]=t;
}
show( );
} };
void main( )
{
Asortobj;
clrscr( );
obj.input( );
obj.show( );
cout<<"sorted array is \n";
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

obj.sort( );
getch( );}
4.AIM: WAP to Create Class Vehicle and two member function setpart() and put part()
declare member function outside the class body
FUNCTION:
Class vehicle
{ Int cost;
Int model no;
Char name[12];
Public:
Void setpart(inta,intm,char b)
{ }
Void putpart()
{ }
5.AIM:WAP to Create Class student and two member function get() and put()declare
member function inside & outside the class body.
FUNCTION:

Class student
{
Int roll no;
Char *name[12];
Public:
Void get(inta,char*n)
{
}
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

Void put()
{
};
Void student:: get(inta,char*n)
{}

6.AIM:Make a class fruit. Make 2 functions: setprice( ) &showprice ( ) to set the price and
show the price of fruit. Make an object of class fruit: mango and make the
program.member function declare inside the class body.
FUNCTION:
Class fruit
{
Int cost;
Char name[12];
Public:
Void setprice()
{ }
Void putprice(){ }
VIVA VOICE:
1)WHAT IS CLASS?
2)HOW WE CAN DECLARE PUBLIC,PRIVATE AND PROTECTED MEMBER IN
CLASS.
3)WHAT IS OBJECT.
4)WHAT IS USE OF SCOPE RESOLUTION OPERATOR IN C++.
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

LAB 5:

1. AIM: PROGRAM TO CALCULATE MAXIMUM OF TWO NUMBERS USING


INLINE FUNCTION.
PROGRAM CODE:

#include<conio.h>
#include<iostream.h>
inlineint max(int x, int y)
{
return(x>y?x:y);
}
void main( )
{
int n1,n2;
cout<<"enter two number\n"; cin>>n1>>n2;
int m=max(n1,n2);
cout<<"max="<<m<<endl;
getch();
}

2 AIM: PROGRAM TO CALCULATE AREA OF SQUARE AND RECTANGLE


// USING FUNCTION OVERLOADING.
PROGRAM CODE:
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

#include<conio.h>
#include<iostream.h>
void area(int );
void area (int,int);
void main( )
{
clrscr( );
area(5);
area(4,8);
getch( );
}
void area(int s)
{ int a;
a=s*s;
cout<<"the area of square is= "<<a<<endl;
}
void area(int l ,int b) OUTPUT
{ int a; int b; a=l*b; The area of square is = 25

cout<<"the area of rectangle is ="<<a<<endl; The area of rectangle is =32


}

3.AIM: SWAPPING OF TWO CLASS DATA USING FRIEND FUNCTION.


PROGRAM CODE:
#include<conio.h>
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

#include<iostream.h>
class Second;
class First
{ intfx;
public:
voidinputf(int x)
{ fx=x; }
voidshowf( )
{ cout<<"fx="<<fx<<endl; }
friend void swap(First &,Second &);
};
class Second
{
intsx;
public:
void inputs(int x)
{ sx=x;}
void shows( )
{ cout<<"sx="<<sx<<endl;
}
friend void swap(First &,Second &);
};
void swap(First &A,Second& B)
{ int t;
t=A.fx;
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

A.fx=B.sx;
B.sx=t;}
void main( )
{ First F;
Second S;
clrscr();
F.inputf(20);
S.inputs(40);
cout<<"before swapping \n";
F.showf( );
S.shows( );
swap(F,S);
cout<<"After swapping \n";
F.showf( );
S.shows( );
getch();}

4.AIM: PROGRAM FOR ADDITION AND SUBTRACTION OF TWO COMPLEX


NUMBER USING FRIEND FUNCTION.

PROGRAM CODE:
#include<iostream.h> OUTPUT
#include<conio.h> C1 = 2+J3
class Complex C2 = 3+J2
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

{ SUM = 5+J5
float real; SUB = -1+J1
floatimag;
public:
Complex( )
{
real=imag=0;
}
Complex(float r,floati)
{
real=r;
imag=i; }
friend Complex sum(Complex,Complex);
friend Complex sub(Complex,Complex);
friend void show(Complex); };
Complex sum(Complex A,Complex B)
{
Complex temp;
temp.real=A.real+B.real;
temp.imag=A.imag+B.imag;
return temp;
}
Complex sub(Complex A,Complex B)
{
Complex temp;
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

temp.real=A.real-B.real;
temp.imag=A.imag-B.imag;
return temp;
}
void show(Complex C)
{
cout<<C.real<<"+j"<<C.imag<<endl;
}
void main( )
{
Complex C1=Complex(2.0,3.0);
Complex C2=Complex(3.0,2.0);
Complex C3,C4;C3=sum(C1,C2);C4=sub(C1,C2);
cout<<"C1=";
show(C1);
cout<<"C2=" ;
show(C2);
cout<<"sum=";
show(C3);
cout<<"sub=";
show(C4);
getch( );
VIVA VOICE:
1) WHAT IS INLINE FUNCTION?
2) WHAT DIFFERENCE BETWEEN FUNCTION OVERLOADING AND FUNCTION
OVERIDING.
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

3) WHAT IS FRIEND FUNCTION.WHAT LOGIC BEHIND SWAPING PF TWO


NUMBERS?
4) WHICH OPERATOR ARE NOT OVERLOAD IN C++.

LAB 6& 7

1.AIM:WAP TO CALCULATE SIMPLE INTREST USING CONSTRUCTOR.


PROGRAM CODE:
#include<iostream.h>
#include<conio.h>
class SI
{
float rate;
float time;
float principal;
public:
SI(float p,floatr,float t)
{
principal=p;
rate=r;
time=t;
}
floatcalc( )
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

{
float temp;
temp=(principal*rate*time/100);
return temp;
}
void show( )
{
cout<<"principal=" <<principal<<endl;
cout<<"rate="<<rate<<endl;
cout<<"time="<<time<<endl;
cout<<"simple intrest="<<calc()<<"\n";} };
void main( )
{
clrscr( );
SI obj1=SI(10000,10.0,3);
cout<<"\t object1\n\n";
obj1.show( );
SI obj2=SI(8000,12.5,2);
cout<<"\t object2\n\n";
obj2.show( );
SI obj3(15000,5,10);
cout<<"\t object3\n\n";
obj3.show( );
getch( );
}
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

2.AIM: PROGRAM FOR MULTIPLICATION AND DIVISION OF TWO COMPLEX


NUMBER USING CONSTRUCTOR.
PROGRAM CODE:
#include<iostream.h> OUTPUT
#include<conio.h> FIRST NUMBER
class Complex 2+J4
{ SECOND NUMBER
float real; 3+J6
floatimag; MULTIPLICATION
public: -18 + J 24
Complex( ) DIVISION
{ 0.666667 + J 0
real=imag=0;
}
Complex(float r,floati)
{
real=r;
imag=i;
}
voidmul(Complex,Complex);
void div(Complex,Complex);
void show(char*);
};
void Complex::mul(Complex A,Complex B)
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

{
real=A.real*B.real-A.imag*B.imag;
imag=A.real*B.imag+A.imag*B.real;
}
void Complex :: div(Complex A,Complex B)
{
double d;
d=B.real*B.real+B.imag*B.imag;
real=(A.real*B.real+A.imag*B.imag)/d;
imag=(B.real*A.imag-B.imag*A.real)/d;
}
void Complex::show(char*s)
{
cout<<s<<endl;
cout<<real<<"+j"<<imag<<endl;
}
void main( )
{
Complex c1=Complex(2.0,4.0);
Complex c2=Complex(3.0,6.0);
Complex c3,c4;
clrscr( );
c3.mul(c1,c2);
c4.div(c1,c2);
c1.show("first number");
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

c2.show("second number");
c3.show("multiplication");
c4.show("division");
getch( );
}

3. AIM:OVERLOADING BINARY OPERATOR + WITH CLASS OBJECT AS


ARGUMENT.

PROGRAM CODE:
#include<iostream.h> OUTPUT
#include<conio.h> ENTER THE NO FOR OBJECT1
#include<stdio.h> 12
class Sum THE NO IS 12
{ ENTER THE NO FOR OBJECT2
private: 35
intnum; THE NO IS 35
staticint count; SUM OF TWO IS 47
public:
void input()
{
cout<<"enter the number for "<<"object" <<++count<<"\n";
cin>>num;
}
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

void operator +(Sum temp)


{
int x;
x=num+temp.num;
cout<<"sum of two is "<<x<<endl;
}
void show()
{
cout<<"the num is"<<num<<endl;
}
};
int Sum::count;
void main()
{
Sum d1,d2;
d1.input( );
d1.show( );
d2.input( );
d2.show( );
d1+d2;
getch( );
}

4.AIM:OVERLOADING +,*,-,AND / ALL IN ONE.


Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

PROGRAM CODE:
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
classExp
{
private:
floatnum;
staticint count1,count2;
public:
void input( )
{
cout<<"Enter the no for object"<<++count1<<"\n";
cin>>num;
}
Exp operator * (Exp temp) OUTPUT
{ ENTER THE NO FOR OBJECT 1
Exp t; 5
t.num=num*temp.num; ENTER THE NO FOR OBJECT 2
return t; 8
} ENTER THE NO FOR OBJECT 3
Exp operator + (Exp temp) 12
{ ENTER THE NO FOR OBJECT 4
Exp t; 9
t.num=num+temp.num; THE NO FOR OBJECT 1 IS 5
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

return t; THE NO FOR OBJECT 2 IS 8


} THE NO FOR OBJECT 3 IS 12
Exp operator - (Exp temp) THE NO FOR OBJECT 4 IS 9
{ THE NO FOR OBJECT 5 IS 15.2
Exp t;
t.num=num-temp.num;
return t;
}
Exp operator / (Exp temp)
{
Exp t;
if(temp.num!=0)
t.num=num*temp.num;
else
{
cout<<"division by zero is not allowed\n";
exit(0);
}
return t;
}
void show( )
{
cout<<"the no for object "<<++count2<<"is"<<num<<endl;
}
};
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

intExp::count1;
intExp::count2;
void main()
{
Exp d1,d2,d3,d4,d5;
clrscr( );
d1.input( );
d2.input( );
d3.input( );
d4.input( );
d1.show( );
d2.show( );
d3.show( );
d4.show( );
d5=d1+d2*d3/d1-d4;
d5.show( );
getch( );
}

VIVA VOICE:

1) WHAT IS CONSTRUCTOR?
2) WHAT VARIOUS TYPES OF CONSTRUCTOR.
3) WHAT IS LOGIC BEHIND MULTIPLICATION AND DIVISION OF COMPLEX
NUMBERS?
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

4) WHAT IS BINARY OPERATOR OVERLOADING?


5) WHAT IS OPERATOR OVERLOADING.HOW IT DIFFERS FROM FUNCTION
OVERLOADING?

LAB 8

1.AIM: FINDING AREA AND VOLUME USING SINGLE LEVEL INHERITANCE.

PROGRAM CODE:
#include<iostream.h> OUTPUT
#include<conio.h> enter the length and width
#include<stdio.h> 4 5
class Area enter the height
{ 7
protected: length=4
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

int length, width; width=5


public: area=20
void input1( ) height=7
{ volume=140
cout<<"enter the length and width\n";
cin>>length>>width;
}
};
class Volume : public Area
{
int height;
public:
void input2( )
{
input1( );
cout<<"enter the height\n";
cin>>height;
}
void show( )
{
cout<<"length="<<length<<endl;
cout<<"width="<<width<<endl;
cout<<"area="<<length*width<<endl;
cout<<"height="<<height<<endl;
cout<<"volume="<<length*width*height<<endl;
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

} };
void main( )
{ Volume v;
v.input2( );
v.show( );
getch( );
}

2.AIM: FINDING MAXIMUM OF THREE CLASSES DATA MULTILEVEL


INHERITENCE.
PROGRAM CODE:
#include<conio.h>
#include<stdio.h>
#include<iostream.h> 0UTPUT
class first Enter the value for fa
{ 12
protected: Enter the value for sa
int fa; 34
public: Enter the value for ta
voidinput_f( ) 33
{ data of class first fa=12
cout<<"enter the value for fa\n"; data of class first sa=34
cin>>fa; data of class first ta=33
} max is 34
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

};

classsecond:public first
{
protected:
intsa;
public:
voidinput_s( )
{
input_f( );
cout<<"enter the value for sa\n";
cin>>sa;
}
};

classthird:public second
{
protected:
int ta;
public:
voidinput_t( )
{
input_s( );
cout<<"enter the value for ta\n";
cin>>ta;
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

}
void show( )
{
cout<<"data of class first fa="<<fa<<endl;
cout<<"data of class second sa="<<sa<<endl;
cout<<"data of class third ta="<<ta<<endl;
}

int max( )
{
int t1,t2;
t1=fa>sa?fa:sa;
t2=ta>t1?ta:t1;
return t2;
}
};

void main( )
{
third t;
t.input_t( );
t.show( );
cout<<"max is"<<t.max( )<<endl;
getch( );
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

3.AIM: PROGRAM TO FIND POWER FROM BASE AND EXPONENT USING


MULTIPLE INHERITENCE
PROGRAM CODE:
#include<iostream.h>
#include<conio.h> OUTPUT
class Base enter the base
{ 4
protected: enter the exponent
intba; 3
public: base=4
voidinput_base( ) exponent=3
{ power=64
cout<<"enter the base\n";
cin>>ba;
}
voidshow_base( )
{
cout<<"base="<<ba<<endl;
}
};

class Exponent
{
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

protected:
intexp;
public:
voidinput_exp( )
{
cout<<"enter the exponent\n";
cin>>exp;
}
voidshow_exp( )
{
cout<<"exponent="<<exp<<endl;
}
};

class Power : public Base,public Exponent


{
intpo;
public:
void input( )
{
input_base( );
input_exp( );
}
void show( )
{
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

show_base( );
show_exp( );
inti;
po=1;
for(i=1;i<=exp;i++)
po=po*ba;;
cout<<"power="<<po<<endl;
}
};

void main( )
{
Power obj;
obj.input( );
obj.show( );
getch( );
}

4.AIM: USES OF HIERARCHICAL INHERITANCE


PROGRAM CODE:

#include<conio.h> OUTPUT
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

#include<iostream.h> COLLEGE NAME=JECRC


#include<string.h> AFFILATED TO= rajasthan technical university
class University COLLEGE NAME=GIT
{ AFFILATED TO= rajasthan technical university
protected:
charuname[40];
public:
University( )
{
strcpy(uname,"rajasthan technical university");
}
};
class College1 : public University
{
charcname[50];
public:
College1( )
{
strcpy(cname,"JECRC" );
}
void show_college1( )
{
cout<<"college name="<<cname<<endl;
cout<<"affileted to="<<uname<<endl;
}};
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

class College2 : public University


{
charcname[50];
public:
College2( )
{
strcpy(cname,"GIT" );
}
void show_college2( )
{
cout<<"college name="<<cname<<endl;
cout<<"affileted to="<<uname<<endl;
}
};
void main( )
{
clrscr( );
College1 c1;
c1.show_college1( );
College2 c2;
c2.show_college2( );
getch( );
}
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

5.AIM:Write a program to illustrating how all the three classes are implemented in
multiple inheritance mode

ALGORITHM:
• Start the process
• Invoke the class M
• Invoke the another class N
• Invoke one more class,ie class P,which is inherited by both M and N
• Create an object for class p,ie P p
• Call p.get_m(),assign the value in to ‘m’
• Call p.get_n(),assign the value in to ‘n’
• Call display(), for dispay the result
• Stop the result

PROGRAM CODE:

#include<iosteram.h>
#include<conio.h>

Class M
{
Protected:
Int m;
Public :
Void get_M();
};
Class N
{
Protected:
Int n;
Public:
Void get_N();
};
Class p: public M, public N
{
Public:
Void disply(void);
};
Void M ::get_m(int x)
{
m=x;
}
Void N::get_n(int y)
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

{
n=y;
}
Void P:: disply(void)
{
Cout<<”m=”<<m<<endl;
Cout<<”n=”<<n<<endl;
Cout<<”m*n=”<<m*n<<endl;
}
int main()
{
P p;
p.get_m(10);
p.get_n(20);
p.display();
return 0;
}

OUTPUT

m=10
n=20
m*n=20

6.AIM: To find out the student details using multiple inheritance.

ALGORITHM:

Step 1: Start the program.


Step 2: Declare the base class student.
Step 3: Declare and define the function get() to get the student details.
Step 4: Declare the other class sports.
Step 5: Declare and define the function getsm() to read the sports mark.
Step 6: Create the class statement derived from student and sports.
Step 7: Declare and define the function display() to find out the total and average.
Step 8: Declare the derived class object,call the functions get(),getsm() and
display().
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

Step 9: Stop the program.

PROGRAM CODE:

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

class student
{
protected:
int rno,m1,m2;
public:
void get()
{
cout<<"Enter the Roll no :";
cin>>rno;
cout<<"Enter the two marks :";
cin>>m1>>m2;
}
};
class sports
{
protected:
intsm; // sm = Sports mark
public:
void getsm()
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

{
cout<<"\nEnter the sports mark :";
cin>>sm;

}
};
class statement:publicstudent,public sports
{
inttot,avg;
public:
void display()
{
tot=(m1+m2+sm);
avg=tot/3;
cout<<"\n\n\tRoll No : "<<rno<<"\n\tTotal : "<<tot;
cout<<"\n\tAverage : "<<avg;
}
};
void main()
{
clrscr();
statement obj;
obj.get();
obj.getsm();
obj.display();
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

getch();
}

Output:

Enter the Roll no: 100

Enter two marks

90
80

Enter the Sports Mark: 90

Roll No: 100


Total : 260
Average: 86.66

7.AIM:To calculate the total mark of a student using the concept of virtual base class.

ALGORITHM:

Step 1: Start the program.


Step 2: Declare the base class student.
Step 3: Declare and define the functions getnumber() and putnumber().
Step 4: Create the derived class test virtually derived from the base class student.
Step 5: Declare and define the function getmarks() and putmarks().
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

Step 6: Create the derived class sports virtually derived from the base class student.
Step 7: Declare and define the function getscore() and putscore().
Step 8: Create the derived class result derived from the class test and sports.
Step 9: Declare and define the function display() to calculate the total.
Step 10: Create the derived class object obj.
Step 11: Call the function get number(), getmarks(), getscore() and display().
Step 12: Stop the program.

PROGRAM CODE:

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

class student
{
Int rno;
public:
void getnumber()
{
cout<<"Enter Roll No:";
cin>>rno;
}
voidputnumber()
{
cout<<"\n\n\tRoll No:"<<rno<<"\n";
}
};
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

classtest:virtual public student


{

public:
int part1,part2;
voidgetmarks()
{
cout<<"Enter Marks\n";
cout<<"Part1:";
cin>>part1;
cout<<"Part2:";
cin>>part2;
}
voidputmarks()
{
cout<<"\tMarks Obtained\n";
cout<<"\n\tPart1:"<<part1;
cout<<"\n\tPart2:"<<part2;
}
};

Classs ports: public virtual student


{
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

public:
int score;
voidgetscore()
{
cout<<"Enter Sports Score:";
cin>>score;
}
voidputscore()
{
cout<<"\n\tSports Score is:"<<score;
}
};

Class result: public test, public sports


{
int total;
public:
void display()
{
total=part1+part2+score;
putnumber();
putmarks();
putscore();
cout<<"\n\tTotal Score:"<<total;
}
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

};

void main()
{
resultobj;
clrscr();
obj.getnumber();
obj.getmarks();
obj.getscore();
obj.display();
getch();
}

Output: Enter Roll No: 200

Enter Marks

Part1: 90
Part2: 80
Enter Sports Score: 80
Roll No: 200
Marks Obtained
Part1: 90
Part2: 80
Sports Score is: 80
Total Score is: 250
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

VIVA VOICE:
1) WHAT IS INHERITENCE?
2) WHAT ARE VARIOUS TYPES OF INHERITENCE?
3) WHAT IS DIRRERENCE BETWEEN MULTILEVEL AND MULTIPLE
INHERTENCE.
4) WHAT IS MULTIPATH INHERITENCE?

LAB 9
1.AIM:WAP to bonding pointer to base class’s objects to base or derived.

Program code:
Class father
{
Protected:
Intf_age;
Public: father(int n) : output: father’s age=45
{
f_age=n;
} son’s age=20
Virtual intgetage(void)
{
returnf_age;
}
};

Class son: public father


Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

{
protected:
Ints_age;
Public:
Son(int n, int m):father(n)
{s_age=m;}

Intgetage(void)
{ returns_age;}
};

Void main()
{
Father *basep ;
Basep=new father(45);
Cout<<”father’s age:”;
Cout<<basep->getage()<<endl;
Delete basep;
Basep=new son(45,20);
Cout<<”son’s age:”;
Cout<<basep->getage()<<endl;
Delete basep;
}
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

2.AIM:WAP to display base class pointer using virtual function.

Progran code:
Output:
Class father father’s name:sunil
{
char name[20]; son’s name:raj
public:
father(char*fname)
{
strcpy(name,fname);
}

Virtual void show()


{ cout<< “father name:”<< name<<endl;
}
};

Class son:Public father


{
Char name[20];
Public:
Son(char*name,char *fname); father(fname)
{
Strcpy(name,sname);
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

Void show()
{cout<<”son name:”<< name<<endl;
}
};

Void main()
{
Father *fp;
Father f1(“sunil”);
Fp=&f1;
Fp->show();
Son s1(“raj”,”sunil”);
Fp=&s1;
Fp->show();

3:AIM: PROGRAM TO CALCULATE MAXIMUM SALARY OF TWO EMPLOYEE


USING THIS POINTER.

PROGRAM CODE:

#include<conio.h>
#include<iostream.h>
classEmp
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

{
floatsal;
public:
Emp( ){}
Emp(float s)
{sal=s;}
Empcompare(Emp);
void show(char *s)
{
cout<<s<<" "<<sal<<endl;
}
};
EmpEmp :: compare(Emp E)
{
if(this->sal>E.sal)
return *this;
else
return E;
}
void main( )
{
EmpE1(9760),E2 (9700),E3;
E3=E1.compare(E2);
E1.show("sal is=" );
E2.show("sal is=");
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

E3.show("max is=");
getch( );
}

VIVA VOICE:
1)WHAT IS VIRTUAL FUNCTION.
2)WHAT IS VIRTUAL BASE CLASS.
3)WHAT IS THIS POINTER.
HOW THIS POINTER CAN IMPLEMENTED.

AIM:Using polymorphism to delete dynamically allocated objects by their base class


pointer: Declare base class destructor virtual so that the delete operator can be applied to
the base class pointer. The derived class destructor is called first, before the base class
destructor.

PROGRAM CODE:

#include <iostream>

using namespace std;

class Base

public:

Base(){};

virtual ~Base(){ cout<< "Base class destructor called" <<endl; }


Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

};

class Derived : public Base

public:

Derived(){};

~Derived(){ cout<< "Derived class destructor called" <<endl; }

};

main()

Base *ptr = new Derived( );

delete ptr;

OUTPUT:

Derived class destructor called


Base class destructor called

Note:

 If the delete operator is applied to the base class and the destructor is NOT virtual,
then this will cause a memory leak as only a portion of the memory is freed.
 Base class destructor is not pure virtual (set =0) or there would be no base class
implementation of the destructor..
 Class contructors can NOT be virtual.

LAB10,11
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

AIM: To swap the numbers using the concept of function template.

ALGORITHM:

STEP 1: Start the program.


STEP 2: Declare the template class.
STEP 3: Declare and define the functions to swap the values.
STEP 4: Declare and define the functions to get the values.
STEP 5: Read the values and call the corresponding functions.
STEP6: Display the results.
STEP 7: Stop the program.
PROGRAM CODE:
#include<iostream.h>
#include<conio.h>

template<class t>

void swap(t &x,t&y)


{
t temp=x;
x=y;
y=temp;
}

void fun(inta,intb,floatc,float d)
{
cout<<"\na and b before swaping :"<<a<<"\t"<<b;
swap(a,b);
cout<<"\na and b after swaping :"<<a<<"\t"<<b;
cout<<"\n\nc and d before swaping :"<<c<<"\t"<<d;
swap(c,d);
cout<<"\nc and d after swaping :"<<c<<"\t"<<d;
}

void main()
{ inta,b;
floatc,d;
clrscr();
cout<<"Enter A,B values(integer):";
cin>>a>>b;
cout<<"Enter C,D values(float):";
cin>>c>>d;
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

fun(a,b,c,d);
getch();
}

Output:

Enter A, B values (integer): 10 20


Enter C, D values (float): 2.50 10.80

A and B before swapping: 10 20


A and B after swapping: 20 10

C and D before swapping: 2.50 10.80


C and D after swapping: 10.80 2.50

AIM: To Find the maximum numbers using the concept of function template.
PROGRAM CODE:

#include <iostream>


using namespace std;


int min(inta,int b )


return (a < b )?a:b;
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

float min(float a,float b )


{

return (a < b )?a:b;


}


char min(char a,char b )


return (a < b )?a:b;


int main()


int a=10,b=5;

cout<<min(a,b )<<'\n';


char p='A',q='Z';

cout<<min(p,q)<<'\n';


float z=1.91f,x=3.98f;
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

cout<<min(z,x)<<'\n';


return 0;

OUTPUT:

5
A
1.91

VIVA VOICE:

Q1. What is function template and template function?

Q2. What is new and delete operator?

Q3. What is the order of destructor and constructor called?

Q4. What is class template?


Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]

Books
Reference Books:-

1. How to Program C++, Dietel, Pearson


2. 2. Mastering C++ By K.R.Venugopal, TMH

You might also like