FINAL Object Oriented Programming Lab Manual
FINAL Object Oriented Programming Lab Manual
FINAL Object Oriented Programming Lab Manual
INDEX
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]
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.
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.
M2: Identify, based on informed perception of Indian, regional and global needs, the areas of
M4: Develop human potential to its fullest extent so that intellectually capable and imaginatively
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.
M2: To provide platform to the experts from institutions and industry of repute to transfer the
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]
6
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]
7
Jaipur Engineering College and Research Centre,Jaipur
Department of Artificial Intelligence & Data Science
Object Oriented Programming Lab [3AID4-22]
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]
To understand the concept of function overloading and understand the practical use of function
overloading in C++.
Course Outcomes
1
Exp:- 1 Understand the basics of C++ library, variables, and data input-output. 2
Exp:-4 Write programs to understand dynamic memory allocation and array of objects. 1 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
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]
Interactive Instruction:
• Coding
Indirect Instructions:
• Problem solving
LEARNING MATERIALS:-
Text/Lab Manual
ASSESSMENT OF OUTCOMES:-
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]
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.
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
Cout<<”Sum=”<<Sum;
Cout<<”Avg=”<<Avg;
getch();
}
OUTPUT:
Enter the 2 numbers
4 6
Sum=10
Avg=5
OUTPUT:
Enter no.
5
Factorial: 120
OUTPUT:
ENTER THE TWO NUMBERS
34
23
MAXIMUM:34
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]
#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();
}
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();
}
cout<<"y="<<y<<endl;
getch( );
}
void swap(int&rx,int&ry)
{
int t;
t=rx;
rx=ry;
ry=t;
}
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:
#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();
}
#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
#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();}
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]
LAB 6& 7
{
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]
{
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( );
}
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]
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]
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]
LAB 8
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]
} };
void main( )
{ Volume v;
v.input2( );
v.show( );
getch( );
}
};
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]
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;
}
};
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( );
}
#include<conio.h> OUTPUT
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
ALGORITHM:
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:
90
80
7.AIM:To calculate the total mark of a student using the concept of virtual base class.
ALGORITHM:
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]
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;
}
};
public:
int score;
voidgetscore()
{
cout<<"Enter Sports Score:";
cin>>score;
}
voidputscore()
{
cout<<"\n\tSports Score is:"<<score;
}
};
};
void main()
{
resultobj;
clrscr();
obj.getnumber();
obj.getmarks();
obj.getscore();
obj.display();
getch();
}
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;
}
};
{
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]
Progran code:
Output:
Class father father’s name:sunil
{
char name[20]; son’s name:raj
public:
father(char*fname)
{
strcpy(name,fname);
}
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();
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.
PROGRAM CODE:
#include <iostream>
class Base
public:
Base(){};
};
public:
Derived(){};
};
main()
delete ptr;
OUTPUT:
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]
ALGORITHM:
template<class t>
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:
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]
{
}
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:
Books
Reference Books:-