Gujarat University BCA Sem 3 C++ Practical Unit 3 | GU BCA Sem 3 OOP Practical Answer
Today Pubg
---
Gujarat University BCA Sem 3 C++ Practical Unit 3 | GU BCA Sem 3 OOP Practical Answer
Unit - 3 Virtual Functions And Operator Overloading
Q1. Create a class vehicle which stores the vehicleno and chassisno as a member. Define another class for scooter, which inherits the data members of the class vehicle and has a data member for a storing wheels and company. Define another class for which inherits the data member of the class vehicle and has a data member for storing price and company. Display the data from derived class. Use virtual function.
Ans :
GU BCA Sem 3 OOP Practical Answer
#include<iostream.h>
#include<conio.h>
class vehicle
{
protected:
int vehicleno,chassisno;
public:
virtual void set()
{
cout<<"Enter The Vehicle no:";
cin>>vehicleno;
cout<<"Enter The Chassis no:";
cin>>chassisno;
}
virtual void disp()
{
cout<<"Vehicle no:"<<vehicleno;
cout<<"\nChassis no:"<<chassisno;
}
};
class scooter:public vehicle
{
int wheels;
char company[20];
public:
virtual void set()
{
cout<<"\nEnetr The Wheels no:";
cin>>wheels;
cout<<"Enter The Company name:";
cin>>company;
}
virtual void disp()
{
cout<<"Wheels:"<<wheels;
cout<<"\nCompany:"<<company;
}
};
class price:public vehicle
{
int price;
char company;
public:
virtual void set()
{
cout<<"\nEnetr The Price:";
cin>>price;
cout<<"Enter The Company name:";
cin>>company;
}
virtual void disp()
{
cout<<"Price:"<<price;
cout<<"\nCompany:"<<company;
}
};
void main()
{
clrscr();
vehicle v1;
vehicle *p=&v1;
p->set();
p->disp();
scooter s1;
p=&s1;
p->set();
p->disp();
price p1;
p=&p1;
p->set();
p->disp();
getch();
}
Gujarat University BCA Sem 3 C++ Practical Unit 3
Q2. Create a base class shape. Use this class to store two double type values that could be used to compute the area of figures. Derive two specific classes called triangle and rectangle from the base shape. Add to the base class, a member function get_data() to initialize the base class data members and another member function display_area() to compute and display the area of figures. Make display_area() as a virtual function and redefine this function in the derived class to suit their requirements.
Ans :
GU BCA Sem 3 OOP Practical Answer
#include<iostream.h>
#include<conio.h>
class shape
{
protected:
double height;
double width;
public:
void get_data()
{
cout<<"\nEnter the Height:";
cin>>height;
cout<<"Enter the Width:";
cin>>width;
}
double virtual disp_area()=0;
};
class rectangle:public shape
{
public:
double disp_area()
{
cout<<"\nHeight:"<<height<<"\nWidth:"<<width;
return height*width;
}
};
class triangle:public shape
{
public:
double disp_area()
{
cout<<"\nHeight:"<<height<<"\nWidth:"<<width;
return 0.5*height*width;
}
};
void main()
{
clrscr();
shape *s;
rectangle r1;
s=&r1;
cout<<"RECTANGLE:";
r1.get_data();
cout<<"\nArea of Rectangle:"<<s->disp_area();
triangle t1;
cout<<"\nTRIANGLE:";
t1.get_data();
s=&t1;
cout<<"\nArea of Triangle:"<<s->disp_area();
getch();
}
Gujarat University BCA Sem 3 C++ Practical Unit 3
Q3. Write a program to demonstrate the use of pure virtual function.
Ans :
GU BCA Sem 3 OOP Practical Answer
#include<iostream.h>
#include<conio.h>
class A
{
public:
void virtual disp()=0;
};
class B:public A
{
public:
void disp()
{
cout<<"I am Class B,from DERIVED CLASS A";
}
};
void main()
{
A *a;
B b;
a=&b;
clrscr();
a->disp();
getch();
}
Gujarat University BCA Sem 3 C++ Practical Unit 3
Q4. For multiple inheritance, write a program to show the invocation of constructor and destructor.
Ans :
GU BCA Sem 3 OOP Practical Answer
#include<iostream.h>
#include<conio.h>
class A
{
public:
A()
{
cout<<"\nCONSTRUCTOR CLASS A";
}
~A()
{
cout<<"\nDESTRUCTOR CLASS A";
}
};
class B
{
public:
B()
{
cout<<"\nCONSTRUCTOR CLASS B";
}
~B()
{
cout<<"\nDESTRUCTOR CLASS B";
}
};
class C:public A,B
{
public:
C()
{
cout<<"\nCONSTRUCTOR CLASS C";
}
~C()
{
cout<<"\nDESTRUCTOR CLASS C";
}
};
void main()
{
clrscr();
C c;
getch();
}
Gujarat University BCA Sem 3 C++ Practical Unit 3
Q5. Create a class string with character array as a data member and write a program to add two strings with use of operator overloading concept.
Ans :
GU BCA Sem 3 OOP Practical Answer
#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
char s[50];
public:
void get_string()
{
cout<<"NAME :";
cin>>s;
}
void disp_string()
{
cout<<s;
}
string operator+(string a)
{
string n;
strcat(s," ");
strcat(s,a.s);
strcpy(n.s,s);
return n;
}
};
void main()
{
string s1,s2,s3,s4;
clrscr();
cout<<"FIRST ";s1.get_string();
cout<<"LAST ";s2.get_string();
s3=s1+s2;
cout<<"FULL NAME :";s3.disp_string();
getch();
}
Gujarat University BCA Sem 3 C++ Practical Unit 3
Q6. Create a class distance which contains feet and inch as a data member. Overhead = =, <and> operator for the same class. Create necessary functions and constructors too.
Ans :
GU BCA Sem 3 OOP Practical Answer
#include<iostream.h>
#include<conio.h>
class distance
{
float feet,inch;
public:
void get()
{
cout<<"Enter the Feet:";
cin>>feet;
cout<<"Enter the Inch:";
cin>>inch;
}
void disp()
{
cout<<"Enter the Feet:"<<feet;
cout<<"Enter the Inch:"<<inch;
}
void operator ==(distance &);
};
void distance::operator ==(distance &s1)
{
if(feet==s1.feet && inch==s1.inch)
cout<<"Both DISTANCE SAME.";
else
cout<<"DISTANCE ARE NOT SAME.";
}
void main()
{
float a,b;
distance o1,o2;
clrscr();
cout<<"FIRST DISTANCE:\n";o1.get();
cout<<"SECOND DISTANCE:\n";o2.get();
o1==o2;
getch();
}
Gujarat University BCA Sem 3 C++ Practical Unit 3
Q7. Create a class MATRIX of size mxn. Overload + and – operators for addition and subtraction of the MATRIX.
Ans :
GU BCA Sem 3 OOP Practical Answer
#include<iostream.h>
#include<conio.h>
class matrix
{
int r,c,m[30][30];
int i,j;
public:
matrix(){}
matrix(int a,int b)
{
r=a;c=b;
}
void get();
void disp();
matrix operator +(matrix );
matrix operator -(matrix);
};
void matrix::get()
{
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<'['<<i<<','<<j<<"]:";
cin>>m[i][j];
}
}
}
void matrix::disp()
{
for(i=0;i<r;i++)
{
cout<<endl;
for(j=0;j<c;j++)
{
cout<<m[i][j]<<"\t";
}
}
}
matrix matrix::operator +(matrix k)
{
matrix add(r,c);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
add.m[i][j]=m[i][j]+k.m[i][j];
}
}
return add;
}
matrix matrix::operator -(matrix k)
{
matrix sub(r,c);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
sub.m[i][j]=m[i][j]-k.m[i][j];
}
}
return sub;
}
void main()
{
int a,b;
clrscr();
cout<<"Enter the ROW SIZE of Matrix :";
cin>>a;
cout<<"Enter the COLUMN SIZE of Matrix:";
cin>>b;
matrix o1(a,b);
matrix o2(a,b);
cout<<"Enter the Number of MATRIX A:\n";o1.get();
cout<<"Enter the Number of MATRIX B:\n";o2.get();
matrix o3;
o3=o1+o2;
cout<<"MATRIX [A+B]:";o3.disp();
o3=o1-o2;
cout<<"\nMATRIX [A+B]:";o3.disp();
getch();
}
Gujarat University BCA Sem 3 C++ Practical Unit 3
Q8. Define a class Coord, which has x and y coordinates as its data members. Overload ++ and – operators for the Coord class. Create both its prefix and postfix forms.
Ans :
GU BCA Sem 3 OOP Practical Answer
#include<iostream.h>
#include<conio.h>
class coor
{
int x,y;
public:
void get(int a,int b)
{
x=a;
y=b;
}
void disp()
{
cout<<"X="<<x<<"\nY="<<y;
}
coor operator ++();
coor operator --();
};
coor coor::operator ++()
{
coor c;
c.x=++x;//prefix
c.y=++y;//prefix
return c;
};
coor coor::operator --()
{
coor c;
c.x=x--;//postfix
c.y=y--;//postfix
return c;
};
void main()
{
clrscr();
coor o1,o2;
o1.get(4,9);
cout<<"Class o1:\n";
o1.disp();
o2=++o1;
cout<<"\nPrefix Expression after value.\nClass o1:\n";
o1.disp();
cout<<"\nClass o2:\n";
o2.disp();
o2=--o1;
cout<<"\nPostfix Expression after value.\nClass o1:\n";
o1.disp();
cout<<"\nClass o2:\n";
o2.disp();
getch();
}
Gujarat University BCA Sem 3 C++ Practical Unit 3
Q9. Create one class called Rupees, which has one member data to store amount in rupee, and create another class called Paise which has member data to store amount in paise. Write a program to convert one amount to another amount with the use of type conversion.
Ans :
GU BCA Sem 3 OOP Practical Answer
#include<iostream.h>
#include<conio.h>
class paisa;
class rupee
{
int r;
public:
rupee(){}
void set(){cout<<"ENTER THE RUPEES :";cin>>r;}
void disp(){cout<<"CONVERT TO RUPEES:"<<r;}
int get_r(){return r;}
rupee(paisa p1);
};
class paisa
{
int p;
public:
paisa(){}
paisa(rupee r1)
{
p=r1.get_r()*100;
}
void set(){cout<<"\nENTER THE PAISA:";cin>>p;}
void disp(){cout<<"CONVERT TO PAISA:"<<p;}
int get_p(){return p;}
};
rupee::rupee(paisa p1)
{
r=p1.get_p()/100;
}
void main()
{
rupee r1;
paisa p1;
clrscr();
r1.set();
p1=r1;
p1.disp();
p1.set();
r1=p1;
r1.disp();
getch();
}
Gujarat University BCA Sem 3 C++ Practical Unit 3
Q10. Create two classes Celsius and Fahrenheit to store temperature in terms of Celsius and Fahrenheit respectively. Include necessary functions to read and display the values. Define conversion mechanism to convert Celsius object to Fahrenheit object and vice versa. Show both types of conversions in main function.
Ans :
GU BCA Sem 3 OOP Practical Answer
#include<iostream.h>
#include<conio.h>
class celsius;
class fehrenheit
{
int f;
public:
fehrenheit(){}
void set(){cout<<"ENTER THE FEHRENHEIT :";cin>>f;}
void disp(){cout<<"CONVERT TO FEHRENHEIT:"<<f;}
int get_f(){return f;}
fehrenheit(celsius c1);
};
class celsius:public fehrenheit
{
int c;
public:
celsius(){}
celsius(fehrenheit f1)
{
c=(f1.get_f()-32)/1.8;
}
void set(){cout<<"\nENTER THE CELSIUS:";cin>>c;}
void disp(){cout<<"CONVERT TO CELSIUS:"<<c;}
int get_c(){return c;}
};
fehrenheit::fehrenheit(celsius c1)
{
f=(c1.get_c()*1.8)+32;
}
void main()
{
fehrenheit f1;
celsius c1;
clrscr();
f1.set();
c1=f1;
c1.disp();
c1.set();
f1=c1;
f1.disp();
getch();
}
Thank You.
OOP Sem 3 Unit 1 Practical: Click Here
Unit 2 and 4 Coming Soon!
Post a Comment
Post a Comment