Practical
Practical
Practical
Practical-1
Aim:
Write a C++ program that will output this passage by Deepak Chopra. Make sure your output
looks exactly as shown here (including spacing, line breaks, punctuation, and the title and
author). Use cout and cin objects and endl manipulator.
Code:
#include<iostream>
using namespace std;
int main()
{
cout<<"*******************************"<<endl;
cout<<"* Programming assignment 1 *"<<endl;
cout<<"* Computer Programing 1 *"<<endl;
cout<<"* Author : Utsav *"<<endl;
cout<<"* Due Date: Thursday, Dec. 20 *"<<endl;
cout<<"*******************************"<<endl;
cout<<"Patel Utsav"<<endl;
cout<<"Id:18CE089";
return 0;
}
Output:
Conclusion:
We studied about cout and cin ad endl manipulator.
1
Object oriented programming in c++ 18CE089
Practical-2
Aim:
Write a program to create the following table. Use endl and setw manipulator.
1234
2468
3 6 9 12
4 8 12 16
Code:
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int i,j;
for(i=1;i<=4;i++)
{
for(j=1;j<=4;j++)
{
cout<<left<<setw(3)<<i*j;
}
cout<<endl;
}
cout<<"Patel Utsav"<<endl;
cout<<"Id:18CE089";
}
2
Object oriented programming in c++ 18CE089
Output:
Conclusion:
We learned about setw manipulator.
3
Object oriented programming in c++ 18CE089
Practical-3
Aim:
Write a C++ program to add two floating numbers using pointer. The result should contain
only two digits after the decimal.Use fixed, scientific and setprecision () manipulators for
controlling the precision of floating point numbers.
Code:
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
float i,j,sum;
float *p,*q;
p=&i;
q=&j;
cout<<"Enter i:";
cin>>*p;
cout<<"Enter j:";
cin>>j;
sum=*p+*q;
cout<<"Result="<<sum<<endl;
cout<<"Result of setprecision(2)="<<setprecision(2)<<sum<<endl;
cout<<"Result of scientific setprecision(2)="<<setprecision(2)<<fixed<<sum<<endl;
cout<<"Result of fixed setprecision(2)="<<setprecision(2)<<fixed<<sum<<endl;
cout<<"Patel Utsav\nId-18CE089";
}
Output:
4
Object oriented programming in c++ 18CE089
Conclusion:
We have studied about Use fixed, scientific and setprecision() controlling the precision of
floating point numbers.
5
Object oriented programming in c++ 18CE089
Practical-4
Aim:
Write a C++ program to find out sum of array element using Recursion.
Code:
#include<iostream>
using namespace std;
int sum(int b[],int);
int main()
{
int n,i,a[10],ans;
cout<<"Enter size:";
cin>>n;
for(i=0;i<n;i++)
{
cin>>a[i];
}
ans=sum(a,n-1);
cout<<"sum="<<ans<<endl;
cout<<"Patel Utsav"<<endl;
cout<<"Id:18CE089";
return 0;
}
int sum(int b[],int m)
{
static int add=0;
add=add+b[m];
m--;
if(m>=0)
{
sum(b,m);
6
Object oriented programming in c++ 18CE089
}
return add;
}
Output:
Conclusion:
We studied about recursion in c+
7
Object oriented programming in c++ 18CE089
Practical-5
Aim:
Write a C++ program to find the number of vowels present in the given character array using
pointer arithmetic.
Code:
#include<iostream>
using namespace std;
int main()
{
char str[50],*p;
int i,count1=0;
cout<<"Enter your string";
cin>>str;
p=str;
for(i=0;*p!='\0';i++,p++)
{
if(*p=='a'||*p=='e'||*p=='i'||*p=='o'||*p=='u'
||*p=='u'||*p=='A'||*p=='E'||*p=='I'||*p=='O'||*p=='U')
{
count1++;
}
}
cout<<"there is " <<count1<< " vowels in string";
cout<<"Patel Utsav"<<endl;
cout<<"Id:18CE089";
}
8
Object oriented programming in c++ 18CE089
Output:
Conclusion:
We learnt about pointers in c++.
9
Object oriented programming in c++ 18CE089
Practical-6
Aim:
Find error in the following code and give reasons for each error: Can we declare an array of
references? Can we assign NULL value to reference variable? Is Reference variable a pointer
variable? Can we declare a reference variable without initializing it? Does Reference
Variable change the original value of variable?
#include<iostream>
using namespace std;
int main()
{
int no1=10, no2=12;
int & x=no1;
int & r;
int & c = NULL;
int & d[2] = {no1,no2};
cout<<"x = "<< x+20;
cout<<"no1="<< no1+10;
return 0;
}
Code:
#include<iostream>
using namespace std;
int main()
{
int no1=10, no2=12;
int & x=no1;
int & r;
int & c = NULL;
int & d[2] = {no1,no2};
cout<<"x = "<< x+20;
cout<<"no1="<< no1+10;
10
Object oriented programming in c++ 18CE089
return 0;
}
Conclusion:
1)We can’t assign NULL to a reference variable.
2)Reference is not a pointer.
3)No we can’t declare a reference variable without initializing it.
4)Yes Reference Variable change the original value of variable.
11
Object oriented programming in c++ 18CE089
Practical-7
Aim:
Find output of the following code: Explain how scope Resolution operator is used to access
global version of a variable.
Code:
#include<iostream.h>
#include<conio.h>
int m=30;
int main()
{
int m=20;
{
int m=10;
cout<<”we are in inner block”<<endl;
cout<<”value of m=”<<m<<”\n”;
cout<<”value of ::m=”<<::m<<”\n”;
}
cout<<”we are in outer block”<<endl;
cout<<”value of m=”<<m<<”\n”;
cout<<”value of ::m=”<<::m<<”\n”;
getch();
return 0;}
Output:
Conclusion:
We can access global variable by scope resolution and different blocks can have different
variable with same name.
12
Object oriented programming in c++ 18CE089
Practical-8
Aim:
Find Error in the following code of a program and give explanation why these errors exist.
Code (1):
#include <iostream>
using namespace std;
int main()
{
int var1 = 35,var2 = 20;
int *const ptr = &var1;
ptr = &var2;
cout<<"var1= "<<*ptr;
return 0; }
Conclusion:
Constant pointer can only point to one address. After assigning an address we can not assign
second address.
Code (2):
#include <iostream>
using namespace std;
int main() {
int var1 = 43;
const int* ptr = &var1;
*ptr = 1;
var1=34;
cout<<"var1 = "<< *ptr;
return 0; }
Conclusion:
13
Object oriented programming in c++ 18CE089
Code (3):
#include <iostream>
using namespace std;
int main()
{
int var1 = 0,var2 = 0;
const int* const ptr = &var1;
*ptr = 1;
ptr = &var2;
cout<<"Var1 = "<<*ptr;
return 0; }
Conclusion:
This is the combination of first two code.
14
Object oriented programming in c++ 18CE089
Practical-9
Aim:
Find the output of following program. Explain the use of bool data type.
Code:
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter the size of array:";
cin>>n;
int *a=new int[n];
int i;
cout<<"Enter values of array"<<endl;
for(i=0;i<n;i++)
{
cin>>*(a+i);
}
for(i=0;i<n;i++)
{
*(a+i)=*(a+i)+2;
}
cout<<"After adding 2 to each element"<<endl;
for(i=0;i<n;i++)
{
cout<<a[i]<<endl;;
}
delete a;
cout<<"Patel Utsav\nId18CE089";
}
15
Object oriented programming in c++ 18CE089
Output:
Conclusion:
We learnt how to allocate dynamic memory.
16
Object oriented programming in c++ 18CE089
Practical-10
Aim:
Find the output of following program. Explain the use of bool data type.
Code:
#include<iostream>
using namespace std;
int main()
{
bool a = 321, b;
cout << "Bool a Contains : " << a<<endl;
int c = true;
int d = false;
cout<<"c = "<<c <<endl<<"d = "<<d;
c = a + a;
cout << "\nInteger c contain : " << c; b = c + a;
cout << "\nBool b contain : " <<b;
return 0;}
Output:
Conclusion:
We studied about bool data type.
17
Object oriented programming in c++ 18CE089
Practical-11
Aim:
Define three functions named divide(). First function takes numerator and denominator as an
input argument and checks it‟s divisible or not, Second function takes one int number as
input argument and checks whether the number is prime or not and Third function takes 3
float number as arguments, divide it with 3 and print average of the numbers Use concept of
Function Overloading / static binding.
Code:
#include<iostream>
using namespace std;
int divide(int,int);
int divide(int);
float divide(float,float,float);
int main(){
int a,b;
float x,y,z;
cout<<"Enter a:";
cin>>a;
cout<<"Enter b:";
cin>>b;
cout<<"Enter x,y,z:";
cin>>x>>y>>z;
divide(a,b);
divide(a);
divide(x,y,z);
cout<<endl<<"Patel Utsav \nId-18CE089";
return 0;
}
int divide(int a,int b)
{
cout<<endl<<"division:"<<a/b<<endl;
18
Object oriented programming in c++ 18CE089
return 0;
}
int divide(int a)
{
int i,countt=0;
for(i=1;i<=a;i++)
{
if(a%i==0)
countt++;
}
if(countt==2)
{
cout<<"Number is prime"<<endl;
}
else
{
cout<<"Number is not prime"<<endl;
}
}
float divide(float x,float y,float z)
{
float sum=0,avg;
sum=x+y+z;
avg=sum/3;
cout<<"Avg:"<<avg;
}
Output:
19
Object oriented programming in c++ 18CE089
Conclusion:
We studied the concept function overloading.
20
Object oriented programming in c++ 18CE089
Practical-12
Aim:
Define four function void swap () which accepts two arguments by reference and swap the
values. First function swaps two characters, second function swaps two integers, third
function swaps two floats values and fourth function swaps two double values. Use the
concept of call by reference in all four functions and function overloading and inline
function.
Code:
#include<iostream>
using namespace std;
inline void swapi(int &,int &);
inline void swapf(float &,float &);
inline void swapc(char &,char &);
inline void swapd(double &,double &);
int main()
{
int n1,n2;
cout<<"Enter first int no to swap a=";
cin>>n1;
cout<<"Enter second int no to swap b=";
cin>>n2;
swapi(n1,n2);
cout<<"after swap a="<<n1<<" b="<<n2<<endl;
float a,b;
cout<<"enter first float to swap a=";
cin>>a;
cout<<"Enter second float no to swap b=";
cin>>b;
swapf(a,b);
cout<<"after swap a="<<a<<" b="<<b<<endl;
char c,d;
21
Object oriented programming in c++ 18CE089
22
Object oriented programming in c++ 18CE089
{
char temp;
temp=a;
a=b;
b=temp;
}
inline void swapd(double &a,double &b)
{
double temp;
temp=a;
a=b;
b=temp;
}
Output:
Conclusion:
We learnt the concept of call by reference and inline function.
23
Object oriented programming in c++ 18CE089
Practical-13
Aim:
Write a function called tonLarge( ) that takes two integer arguments by reference and then
sets the larger of the two numbers to 100 using Return by reference. Write a main( ) program
to exercise this function.
Code:
#include<iostream>
using namespace std;
int a;
int &tonLarge(int &,int &);
int main()
{
int n,m,ans;
cout<<"Enter first no:";
cin>>n;
cout<<"Enter second no:";
cin>>m;
ans=tonLarge(n,m);
ans=100;
if(a==1)
{
cout<<"new values are="<<m<<","<<ans;
}
else
{
cout<<"new values are="<<n<<","<<ans;
}
cout<<"\nPatel Utsav\nId18CE089";
}
24
Object oriented programming in c++ 18CE089
Output:
Conclusion:
We learnt how to return reference of a variable.
25
Object oriented programming in c++ 18CE089
Practical-14
Aim:
Write a function called power ( ) that takes two arguments: a double value for n and an int for
p, and returns the result as double value. Use default argument of 2 for p, so that if this
argument is omitted, the number will be squared. Write a main ( ) function that gets values
from the user to test this function.
Code:
#include<iostream>
#include<math.h>
using namespace std;
double power(double n,int p=2);
int main()
{
int p;
char ans;
double no,answer;
cout<<"Enter no:";
cin>>no;
cout<<"Do u want to give power y/n?:";
cin>>ans;
if(ans=='y')
{
cout<<"Enter it's power:";
cin>>p;
answer=power(no,p);
cout<<"your ans="<<answer;
}
else
{
answer=power(no);
26
Object oriented programming in c++ 18CE089
cout<<"your ans="<<answer;
}
cout<<"\nPatel Utsav\nId-18CE089"
return 0;
}
double power(double n,int p)
{
return pow(n,p);
}
Output:
Conclusion:
We learnt how to give default argument to any function.
27
Object oriented programming in c++ 18CE089
Practical-15(a)
Aim:
Define a C++ Structure Rectangle with data member‟
s width and height. It has get_values( ) member functions to get the data from user and area( )
member functions to print the area of rectangle.
Code:
#include<iostream>
using namespace std;
struct rectangle
{
float width,height;
void get_values()
{
cout<<"Enter width:";
cin>>width;
cout<<"Enter height:";
cin>>height;
}
float area()
{
return height*width;
}
}v1;
int main()
{
float ans;
v1.get_values();
ans=v1.area();
cout<<"\narea="<<ans<<endl;
cout<<"\nPatel Utsav \nId-18CE089";
return 0;
}
Output:
Conclusion:
We studied about structure in c++.
28
Object oriented programming in c++ 18CE089
Practical-15(b)
Aim:
create a C++ Class for the above program. Define both functions inside the class. Member
function defined inside the class behaves like an inline function and illustrate the difference
between C++ Structure and C++ Class.
Code:
#include<iostream>
using namespace std;
class rectangle
{
public:
float width,height;
public:
void get_values()
{
cout<<"Enter width:";
cin>>width;
cout<<"Enter height:";
cin>>height;
}
float area()
{
return height*width;
}
};
int main()
{
class rectangle v1;
float ans;
v1.get_values();
ans=v1.area();
cout<<"\narea="<<ans<<endl;
cout<<"\nPatel Utsav \nId-18CE089";
return 0;
}
Output:
Conclusion:
We studied about class in c++.
29
Object oriented programming in c++ 18CE089
Practical-16
Aim:
Write a C++ program having class Batsman. It has private data members: batsman name,
bcode (4 Digit Code Number), innings, not out, runs, batting average. Innings, not out and
runs are in integer and batting average is in float. Define following function outside the class
using scope resolution operator. 1) Public member function getdata()to read values of data
members. 2) Public member function putdata()to display values of data members. 3)
Private member function calcavg() which calculates the batting average of a batsman. Also
make this outside function inline.
Code:
#include<iostream>
using namespace std;
class batsman
{
int bcode,innings,notout,runs;
char name[50];
float avg;
public:
void getdata();
void putdata();
private:
float calcavg();
};
void batsman::getdata()
{
cout<<"Enter name:";
cin.get(name,50);
cout<<"Enter bcode:";
cin>>bcode;
cout<<"Enter innings:";
cin>>innings;
cout<<"Enter runs:";
30
Object oriented programming in c++ 18CE089
cin>>runs;
cout<<"Enter no. of not out innings;";
cin>>notout;
}
inline float batsman::calcavg()
{
return runs/(innings-notout);
}
void batsman::putdata()
{
cout<<"name:"<<name<<endl;
cout<<"bcode:"<<bcode<<endl;
cout<<"runs:"<<runs<<endl;
cout<<"innings:"<<innings<<endl;
cout<<"Average:"<<calcavg()<<endl;
}
int main()
{
class batsman v1;
v1.getdata();
v1.putdata();
cout<<"Patel Utsav\nId-18CE089";
return 0;
}
Output:
31
Object oriented programming in c++ 18CE089
Conclusion:
We learnt how to create private function and how to use them.
32
Object oriented programming in c++ 18CE089
Practical-17
Aim:
Define class Digit having int „n‟ as data member. Define member function enter() to enter
the data and show() to print the data. A class has member function compare() that displays
whether the first object is smaller, greater or same as compared to second object. (Function
compare() should support: int x = d1.compare(d2); where d1 and d2 are objects of class
Digit). Use Concept of Object as Function Arguments.
Code:
#include<iostream>
using namespace std;
class digit
{
int m;
public:
int comp(digit);
void show();
void enter()
{
cin>>m;
}
};
digit d1,d2;
int main()
{
int ans;
cout<<"Enter first no:";
d1.enter();
cout<<"Enter second no:";
d2.enter();
d1.show();
cout<<"\nPatel Utsav\nId-18CE089";
33
Object oriented programming in c++ 18CE089
}
int digit::comp(digit d3)
{
if(d3.m>m)
{
return 1;
}
else if(d3.m<m)
{
return 2;
}
else
{
return 3;
}
}
void digit::show()
{
int ans;
ans=d2.comp(d1);
if(ans==1)
{
cout<<"first no is bigger";
}
else if(ans==2)
{
cout<<"second no is bigger";
}
else
{
34
Object oriented programming in c++ 18CE089
Output:
Conclusion:
We learnt how to send object as an argument.
35
Object oriented programming in c++ 18CE089
Practical-18
Aim:
Define class Currency having two integer data members rupee and paisa. A class has member
functions enter() to get the data and show() to print the amount in 22.50 format. Define one
member function sum() that adds two objects of the class and stores answer in the third object
i.e. c3=c1.sum (c2). The second member function should add two objects of type currency
passed as arguments such that it supports c3.add(c1,c2); where c1, c2 and c3 are objects of
class Currency. Also Validate your answer if paisa >100. Write a main( )program to test all
the functions. Use concepts of Object as Function Arguments, function returning object and
function overloading.
Code:
#include<iostream>
using namespace std;
class currency
{
int ru,pa;
public:
void enter()
{
cout<<"Enter rupee:";
cin>>ru;
cout<<"Enter paisa:";
cin>>pa;
}
void show()
{
cout<<"Amount="<<ru<<"."<<pa<<endl;
}
currency sum(currency);
void add(currency,currency);
}c1,c2,c3;
int main()
36
Object oriented programming in c++ 18CE089
{
cout<<"Enter data 1"<<endl;
c1.enter();
cout<<"Enter data 2"<<endl;
c2.enter();
c3=c1.sum(c2);
cout<<"Sum of two data with return type of object"<<endl;
c3.show();
c3.add(c1,c2);
cout<<"Sum of two data without return type of object"<<endl;
c3.show();
cout<<"\nPatel Utsav\nId-18CE089";
return 0;
}
currency currency::sum(currency c)
{
currency temp;
temp.pa=pa+c.pa;
if(temp.pa>=100)
{
temp.pa=temp.pa-100;
temp.ru=ru+c.ru+1;
}
else
{
temp.ru=ru+c.ru;
}
return temp;
}
void currency::add(currency a,currency b)
37
Object oriented programming in c++ 18CE089
{
pa=a.pa+b.pa;
if(pa>=100)
{
pa=pa-100;
ru=a.ru+b.ru+1;
}
else
{
ru=a.ru+b.ru;
}
}
Output:
Conclusion:
We learn how to add two object using two different methods.
38
Object oriented programming in c++ 18CE089
Practical-19
Aim:
Define a class Dist with int feet and float inches. Define member function that displays
distance in 1‟-2.5” format. Also define member function scale ( ) function that takes object
by reference and scale factor in float as an input argument. The function will scale the
distance accordingly. For example, 20‟-5.5” and Scale Factor is 0.5 then answer is 10‟-2.75”
Code:
#include<iostream>
using namespace std;
class dist
{
float inches;
int feet;
public:
void getdata()
{
cout<<"Enter feet:";
cin>>feet;
cout<<"Enter inches:";
cin>>inches;
}
void display()
{
cout<<feet<<"\'-"<<inches<<"\""<<endl;
}
void scale(dist &d,float sf)
{
feet=d.feet*sf;
inches=d.inches*sf;
}
}d1;
39
Object oriented programming in c++ 18CE089
int main()
{
d1.getdata();
float a;
cout<<"Enter scale factor:";
cin>>a;
d1.scale(d1,a);
d1.display();
cout<<"\nPatel Utsav\nId-!18CE089";
return 0;
}
Output:
Conclusion:
We learnt how to send object’s reference as an argument.
40
Object oriented programming in c++ 18CE089
Practical-20
Aim:
Create a Class Gate for students appearing in Gate (Graduate Aptitude test for
Engineering) exam. There are three examination center Vadodara, Surat, and Ahmedabad
where Gate exams are conducted. A class has data members: Registration number, Name
of student, Examination center. Class also Contains static data member ECV_Cnt,
ECS_Cnt and ECA_Cnt which counts the number of students in Vadodara, Surat and
Ahmedabad exam center respectively. Class Contains two Member function getdata ()
which gets all information of students and counts total students in each exam center and
pudata () which prints all information about the students. Class also contains one static
member function getcount () which displays the total number of students in each
examination center. Write a program for 5 students and display the total number of
students in each examination center. Use static data member, static member function and
Array of Objects.
Code:
#include<iostream>
#include<string.h>
using namespace std;
class GATE
{
int Reg_No;
char name[15],Ex_center[15];
static int ECV_Cnt;
static int ECS_Cnt;
static int ECA_Cnt;
public:
void getdata();
void putdata();
static void getcount();
}d[5];
int GATE::ECV_Cnt;
int GATE::ECS_Cnt;
int GATE::ECA_Cnt;
void GATE::getdata()
{
leb:
cout<<"Enter Name :";
cin>>name;
cout<<"Enter Reg_No.";
cin>>Reg_No;
cout<<"Enter Center Name : ";
cin>>Ex_center;
if(strcmp(Ex_center,"vadodara")==0)
{
ECV_Cnt++;
41
Object oriented programming in c++ 18CE089
}
else if(strcmp(Ex_center,"surat")==0)
{
ECS_Cnt++;
}
else if(strcmp(Ex_center,"Ahemdabad")==0)
{
ECA_Cnt++;
}
else
{
cout<<"Enter valid Data";
goto leb;
}
}
void GATE::getcount()
{
cout<<"No.Of students in Vadodara are : "<<ECV_Cnt<<endl;
cout<<"No.Of students in Surat are : "<<ECS_Cnt<<endl;
cout<<"No.Of students in Ahemdabad are : "<<ECA_Cnt<<endl;
}
void GATE::putdata()
{
cout<<"Name :"<<name<<endl;
cout<<"Reg No."<<Reg_No<<endl;
cout<<"\n Center :"<<Ex_center<<endl;
}
int main()
{
int i;
for(i=0;i<5;i++)
{
cout<<"**Enter data of student No."<<i+1<<endl;
d[i].getdata();
}
GATE::getcount();
for(i=0;i<5;i++)
{
cout<<"**Data of student No."<<i+1<<":"<<endl;
d[i].putdata();
}
cout<<"Patel Utsav\nId-18CE089";
}
42
Object oriented programming in c++ 18CE089
Output:
43
Object oriented programming in c++ 18CE089
Conclusion:
We learnt about static data members and static data function in c++.
44
Object oriented programming in c++ 18CE089
Practical-21
Aim:
Define a class Fahrenheit with float temp as data member. Define another class Celsius with
float temperature as data member. Both classes have member functions to input and print
data. Write a non-member function that receives objects of both the classes and declare which
one is higher than another according to their values. Also define main() to test the function.
Define all member functions outside the class. (Formula for converting Celsius to Fahrenheit
is F = (9C/5) + 32). Use the concept of friend function.
Code:
#include<iostream>
using namespace std;
class celsius;
class fahrenheit
{
float temp;
public:
void getdata();
friend void compare(fahrenheit , celsius);
}f;
void fahrenheit::getdata()
{
cout<<"Enter temperature in fahrenheit:";
cin>>temp;
}
class celsius
{
float t;
public:
void get();
friend void compare(fahrenheit , celsius);
}c;
void celsius::get()
{
cout<<endl<<"Enter temperature in celcius:";
cin>>t;
}
{
cout<<endl<<"\nTemperature 1 higher";
}
else
{
cout<<endl<<"\nTemperature 2 higher";
}
int main()
{
f.getdata();
c.get();
compare(f,c);
cout<<"\nPatel Utsav\nId-18CE089";
return 0;
}
Output:
Conclusion:
We learnt about friend function in c++.
46
Object oriented programming in c++ 18CE089
Practical-22
Aim:
Create a Class Date having data members: int dd, mm, yyyy. Class has one member function
to input the dates and another member function which prints the dates. Write a main() function
which takes two dates as input. Write a friend function swapdates() which takes two objects
by reference of type Date and swaps both the dates. Use the concept of Friend function which
takes objects by reference
Code:
#include<iostream>
using namespace std;
class date
{
int dd,mm,yyyy;
public:
void get()
{
cin>>dd>>mm>>yyyy;
}
void print()
{
cout<<dd<<"\\"<<mm<<"\\"<<yyyy<<endl;
}
friend int& swapdate(date &d1,date &d2);
}d1,d2;
int& swapdate(date &d1,date &d2)
{
int t;
t=d1.dd;
d1.dd=d2.dd;
d2.dd=t;
t=d1.mm;
d1.mm=d2.mm;
d2.mm=t;
t=d1.yyyy;
d1.yyyy=d2.yyyy;
d2.yyyy=t;
}
int main()
{
d1.get();
d2.get();
d1.print();
d2.print();
swapdate(d1,d2);
d1.print();
47
Object oriented programming in c++ 18CE089
d2.print();
Output:
Conclusion:
We have studied about the concept of friend function which takes objects by reference.
48
Object oriented programming in c++ 18CE089
Practical-23
Aim:
Create a class Customer having data members: name of the customer and custome number in
integer and member function to get customer data. Create another class Manager having data
members: name of manager and employee id in integer and member function to get managers
data. Class Manager also have member function get_cust_data () which takes objects of class
Customer as input and prints the customers details and is a friend function of class Customer .
Write a main () function to test all this function. Use the concepts of Member function of
one class can be a Friend Function of another class.
Code:
#include<iostream>
using namespace std;
class customer;
class manager
{
string name;
int id;
public:
void getdata()
{
cout<<" Name Of Manager : ";
cin>>name;
cout<<"Employee Id : ";
cin>>id;
}
void get_cust_data (customer);
};
class customer
{
string name_of_customer;
int customer_id;
public:
49
Object oriented programming in c++ 18CE089
void getdata()
{
cout<<" Name : ";
cin>>name_of_customer;
cout<<"Customer id : ";
cin>>customer_id;
}
friend void manager::get_cust_data (customer);
}c1,c2;
void manager::get_cust_data (customer c)
{
cout<<"Customer Name is : "<<c.name_of_customer<<endl;
cout<<"Id : "<<c.customer_id<<endl;
}
int main()
{
manager m;
cout<<"Enter data Of customer 1 :"<<endl;
c1.getdata();
cout<<"Enter data Of customer 2:"<<endl;
c2.getdata();
cout<<"Enter data Of Manager :"<<endl;
m.getdata();
m.get_cust_data (c1);
m.get_cust_data (c2);
cout<<"Utsav A Ptael\n Id-18CE089";
}
50
Object oriented programming in c++ 18CE089
Output:
Conclusion:
We have studied about Member function of one class can be a Friend Function of
another class.
51
Object oriented programming in c++ 18CE089
Practical-24
Aim:
Create a class Child having data members: name of the child and gender and a member
function to get and print child data. Create another class Parent which is a friend class of
child class. Class Parent have member function ReadChildData() which takes child‟s object
by reference as input argument and Reads the childs data and DisplayChildData() which
takes childs object as argument and displays childs data. Use the concepts of Friend Class.
Code:
#include<iostream>
using namespace std;
class child;
class parent
{
public:
void ReadchildData(child &);
void DisplaychildData(child &);
}p;
class child
{
string name,gender;
friend class parent;
}c1,c2;
void parent::ReadchildData(child &c3)
{
cout<<"Enter Child Name : ";
cin>>c3.name;
cout<<"Enter Gender : ";
cin>>c3.gender;
}
void parent::DisplaychildData(child &c4)
{
52
Object oriented programming in c++ 18CE089
cout<<"Name : "<<c4.name<<endl;
cout<<"Gender : "<<c4.gender<<endl;
}
int main()
{
cout<<"Enter Data Of First Child : "<<endl;
p.ReadchildData(c1);
cout<<"Enter Data Of Second Child : "<<endl;
p.ReadchildData(c2);
cout<<"---------------------"<<endl;
cout<<"Data Of First Child : "<<endl;
cout<<"---------------------"<<endl;
p.DisplaychildData(c1);
cout<<"---------------------"<<endl;
cout<<"Data Of Second Child : "<<endl;
cout<<"---------------------"<<endl;
p.DisplaychildData(c2);
cout<<"Utsav Patel\nId-18CE080";
}
Output:
53
Object oriented programming in c++ 18CE089
Conclusion:
We have studied about the concepts of Friend Class.
54
Object oriented programming in c++ 18CE089
Practical-25
Aim:
Check the following C++ code and find if there is any error in code, give justification for
the error, correct the code and write the output:
Code (1):
#include<iostream>
using namespace std;
class sample
{
int m, n;
public:
void getdata();
void putdata() const;
};
void sample::getdata()
{
cout<< "Enter m & n";
cin>>m>>n;
}
void sample::putdata() const
{
m=12;
n=34;
cout<< " m = "<<m<<"n= "<<n;
}
int main()
{
sample s1;
s1.getdata();
s1.putdata();
return 0;
}
Output:
55
Object oriented programming in c++ 18CE089
Conclusion:
As m and n are constant members then can be only treated as Read only objects. And
their value cannot be changed.
1. Example of (a)Pointer to data members, (b)Pointer to member functions
(a) #include<iostream>
using namespace std;
class student
{
public: int roll_no;
};
int main()
{
// declaring pointer to data member
int student :: *p1 = &student::roll_no;
student s;
student *optr = &s;
s->*p1 = 42;
cout<<"Roll no is "<<s->*p1<<endl;
optr.*p1 = 45;
cout<<"Roll no is"<<optr.*p1<<endl;
return 0;
56
Object oriented programming in c++ 18CE089
}
(b)
#include<iostream>
class employee
{
public:
void hello()
{
Prepared By: Kruti Dhyani
cout<<"Hi hello"<<endl;
}
};
int main()
{
// declaring pointer to member function hello
void (employee ::*fp)() = &employee::hello;
employee e;
employee *optr = &e;
(e->*fp)();
(optr.*fp)();
return 0;
}
OUTPUT:(A)
OUTPUT:(B)
CONCLUSION:
Mismatch use of operators. The (.) operator is use to access member of structure and
class by value. While (->) operator is used with refrence.
We have studied about Pointer to data members & Pointer to member functions.
57
Object oriented programming in c++ 18CE089
Conclusion:
The class inside a function works as a local class.in the local class all functions inside the
class should be inline. This condition is mandatory to run the function.
58
Object oriented programming in c++ 18CE089
Practical-26
Aim:
Write a C++ program having class time with data members: hr, min and sec. Definefollowing
member functions.
1) getdata() to enter hour, minute and second values
2) putdata() to print the time in the format 11:59:59
3) default constructor
4) parameterized constructor
5) copy constructor
6) Destructor.
Use 52 as default value for sec in parameterized constructor.
Use the concepts of default constructor, parameterized constructor, Copy
constructor,
constructor with default arguments and destructor.
Code:
#include<iostream>
using namespace std;
class time
{
int hr,mini,sec;
public:
getdata()
{
cin>>hr>>mini>>sec;
}
print()
{
if(sec>60)
{
sec=sec%60;
59
Object oriented programming in c++ 18CE089
mini=mini/60;
}
if(mini>60)
{
mini=mini%60;
hr=hr+mini/60;
}
cout<<hr<<":"<<mini<<":"<<sec<<"\n";
}
time ()
{
getdata();
}
time(int a,int b,int c=52)
{
hr=a;
mini=b;
sec=c;
}
time(time &x)
{
hr=x.hr;
mini=x.mini;
sec=x.sec;
}
~time(){};
};
int main()
{
60
Object oriented programming in c++ 18CE089
time t1;
time t2(11,45,30),t4(06,22);
time t3(t2);
t1.print();
t2.print();
t3.print();
t4.print();
}
Output:
Conclusion:
We have studied about the concepts of default constructor, parameterized constructor,
Copy constructor,constructor with default arguments and destructor.
61
Object oriented programming in c++ 18CE089
Practical-27
Aim:
Create a class Number having int num as member. The class has input and output
functions. Overload unary operator (++) such that it supports N1=N2++ and N3=++N1
and Overload unary (-) such that it supports
N3 = - N3. Also define default, parameterized and copy constructor for the class. Also
explain use of nameless object in operator overloading. Use the concept of Overloading
Unary Operators. Operator overloading is also known as Compile Time
Polymorphism or static binding.
Code:
#include <iostream>
using namespace std;
class num
{
int n;
public:
void put()
{
cout<<"\nYour ans is: "<<n;
}
num operator ++(int)
{
//num t; t.n=n++; return t;
return num(n++);
}
num operator ++()
{
num t;
t.n=++n;
return t;
}
num operator -()
{
62
Object oriented programming in c++ 18CE089
return num(-n);
}
num()
{
n=0;
}
num(int x)
{
n=x;
}
~num()
{}
};
int main()
{
num n2(0);
num n1,n3;
n1=n2++;
n1.put();
n3=++n1;
n3.put();
n3=-n3;
n3.put();
cout<<"\nUtsav Patel\n Id-18CE089";
return 0;
}
Output:
63
Object oriented programming in c++ 18CE089
Conclusion:
We have studied about the concept of OverloadingUnary Operators. Operator
overloading is also known as Compile Time Polymorphism or static binding.
64
Object oriented programming in c++ 18CE089
Practical : 27
AIM :
Create a class Number having int num as member. The class has input and output
functions. Overload unary operator (++) such that it supports N1=N2++ and N3=++N1
and Overload unary (-) such that it supports
N3 = - N3. Also define default, parameterized and copy constructor for the class. Also
explain use of nameless object in operator overloading. Use the concept of Overloading
Unary Operators. Operator overloading is also known as Compile Time
Polymorphism or static binding.
CODE :
#include <iostream>
using namespace std;
class num
{
int n;
public:
void put()
{
cout<<"\nYour ans is: "<<n;
}
num operator ++(int)
{
//num t; t.n=n++; return t;
return num(n++);
}
num operator ++()
{
num t;
t.n=++n;
return t;
}
num operator -()
65
Object oriented programming in c++ 18CE089
{
return num(-n);
}
num()
{
n=0;
}
num(int x)
{
n=x;
}
~num()
{}
};
int main()
{
num n2(0);
num n1,n3;
n1=n2++;
n1.put();
n3=++n1;
n3.put();
n3=-n3;
n3.put();
cout<<"\nName : Meet D. Patel\n Id : 18CE080";
return 0;
}
OUTPUT:
66
Object oriented programming in c++ 18CE089
CONCLUSION:
We have studied about the concept of OverloadingUnary Operators. Operator
overloading is also known as Compile Time Polymorphism or static binding.
Practical : 28
AIM :
Create a class Number having int num as member. The class has input and output functions.
Overload unary operator (++) such that it supports N1=N2++ and N3=++N1 and Overload unary
(-) such that it supports
N3 = - N3. Also define default, parameterized and copy constructor for the class. Also explain
use of nameless object in operator overloading. Use the concept of Overloading Unary
Operators. Operator overloading is also known as Compile Time Polymorphism or static
binding
CODE :
#include<iostream>
using namespace std;
class complex
{
67
Object oriented programming in c++ 18CE089
int real,img;
public:
void getdata()
{
cout<<"Enter A real part Of No. : ";
cin>>real;
cout<<"Enter A Imaginary part Of No. : ";
cin>>img;
}
friend void operator -(complex &);
void putdata()
{
cout<<"Real Part Of No is : "<<real<<"\n Imaginary Part Of No is : "<<img<<endl;
}
}c1;
void operator -(complex &t )
{
t.real= -t.real;
t.img= -t.img;
}
int main()
{
c1.getdata();
-c1;
c1.putdata();
cout<<"Utsav Patel\nId-18CE080";}
OUTPUT:
68
Object oriented programming in c++ 18CE089
CONCLUSION:
We have studied about Overloading Unary Operators.
Practical : 29
AIM :
Create a class String having character array. Class includes constructor and required
member functions to get and display the object. Overload the operators +(s3=s1+s2),
==(s1<s2), +=(s1+=s2) for the class. Use the concept of Overloading Binary Operator.
CODE :
#include<iostream>
using namespace std;
class String
{
string arr;
public:
String()
{
arr[0]='0';
}
void getdata()
{
cout<<"Enter A String : ";
69
Object oriented programming in c++ 18CE089
cin>>arr;
}
void putdata()
{
cout<<"Your String Is : "<<arr<<endl;
}
String operator + (String s)
{
String t;
t.arr=arr+" "+s.arr;
return t;
}
int operator == (String s)
{
if(arr==s.arr)
return 1;
else
return 0;
}
void operator += (String s)
{
arr=arr+s.arr;
}
}s1,s2,s3;
int main()
{
70
Object oriented programming in c++ 18CE089
OUTPUT:
71
Object oriented programming in c++ 18CE089
CONCLUSION:
We have studied about the concept of Overloading Binary Operator.
72
Object oriented programming in c++ 18CE089
Practical : 30
AIM :
Create a class Measure having members: meter and cm. The class has get( ) and put( )
functions. Overload operator + and – such that they support M1=M2+15 and M3=M1 –
4.5. Also overload + and – such that they support M1=5.0+M2 and M3=2.0 – M4. Write a
main( ) to test the class.
Use the concept of Overloading Binary Operators with friend function.
CODE :
#include<iostream>
#include<iomanip>
using namespace std;
class Measure
{
float m,cm;
public:
void getdata()
{
cout<<"Enter a length in m & cm : ";
cin>>m>>cm;
}
void putdata()
{
cout<<"Answer "<<setw(4)<<m<<" "<<setw(4)<<cm<<endl;
}
Measure operator +(float t)
{
Measure temp;
temp.m = m + t;
temp.cm = cm + t;
return temp;
}
73
Object oriented programming in c++ 18CE089
74
Object oriented programming in c++ 18CE089
m4=m1-4.5;
m5=2-m1;
m2.putdata();
m3.putdata();
m4.putdata();
m5.putdata();
cout<<"Utsav Patel\nId-18CE080";}
OUTPUT:
CONCLUSION:
We have studied about the concept of Overloading Binary Operators with friend
function.
75
Object oriented programming in c++ 18CE089
Practical 31
Aim: Create a class Celsius with float. Define appropriate member functions such that it support
the statements: C1=30.5F; float temperature; temperature=C2;
Use the concept of Type conversion from basic type to class type and class type to basic type.
CODE:
#include<iostream>
using namespace std;
class celcius
{
float temp;
public:
celcius ()
{
temp=0;
}
celcius (float t)
{
temp=5*(t-32)/9;
void get()
{
cout<<"Enter the temprature: ";
cin>>temp;
}
void print(){ cout<<"Temprature after conversion: "<<temp<<endl;}
operator float()
{return(((temp*9)/5)+32); }};
int main()
{ celcius c1,c2;
float a;
c1=30.5;
c1.print();
c2.get();
a=c2;
cout<<a;
}
OUTPUT:
76
Object oriented programming in c++ 18CE089
Practical 32
Aim: Create classes Celsius and Fahrenheit with float. Define appropriate member functions such
that they support the statements in main( ): Celsius C1, C2=5.0; Fahrenheit F1, F2; F1=C2; C1=F2;
Use the concepts of Type conversion from class type to class type.
Write this Program in two ways.
Define appropriate member function in class Celsius.
Define appropriate member function in class Fahrenheit.
CODE:
#include<iostream>
using namespace std;
class celcius
{
float temp1;
public:
celcius()
{
temp1=0;
}
celcius(float t)
{
temp1=5*(t-32)/9;
}
int get()
{
cout<<"Enter temprature in celcius: ";
cin>>temp1;
return temp1;
}
void print_c()
{
cout<<"celcius: "<<temp1<<endl;
}
};
class farhenite
{
float temp2;
public:
farhenite(){temp2=0;}
farhenite(celcius ce)
{
temp2=((ce.get()*9)/5)+32;
}
operator celcius()
{
return((5*(temp2-32)/9));
77
Object oriented programming in c++ 18CE089
void print_f()
{
cout<<"farhenite: "<<temp2<<endl;
}
void get_f()
{
cout<<"Enter temprature in farhenite: ";
cin>>temp2;
}
};
int main()
{
celcius c1,c2;
c2=5.0;
c2.print_c();
farhenite f1,f2;
f1=c2;
f1.print_f();
f2.get_f();
c1=f2;
c1.print_c();
cout<<endl<<"Patel Utsav"<<endl<<"Id:18CE089";
}
OUTPUT:
78
Object oriented programming in c++ 18CE089
Practical 34
Aim: Define a Base Class Vegetable having data member Color and member function getdata()
which takes color as an input and putdata() which print the color as an output. Vegetable Class has
one subclass named Tomato having data members weight and size and member function gtdata()
which takes weight and size as an input and ptdata() which prints weight and size as output. Write
a C++ Program which inherits the data of Vegetable class in Tomato class using Single Inheritance.
CODE:
#include<iostream>
using namespace std;
class Vegetable{
protected:
char color[15];
public:
void get() {
cout<<"What is the colour of vegetable:"<<endl;
cin>>color;}
void put(){
cout<<"The colour of vegetable is:"<<color<<endl;}};
class Tomato:public Vegetable
{protected:
int w,s;
public:
void getws(){
cout<<"Enter weight in grams and size in cm:"<<endl;
cin>>w>>s; }
void putws()
{cout<<"Weight:"<<w<<" grams"<<endl;
cout<<"Size:"<<s<<" cm"<<endl; }};
int main(){
Tomato t1;
t1.get();
t1.put();
t1.getws();
t1.putws();
cout<<endl<<"Patel Utsav"<<endl<<"Id:18CE089"; return 0; }
OUTPUT:
79
Object oriented programming in c++ 18CE089
Practical 35
Aim: Write a program to create a class Medicine which stores type of medicine, name of company,
date of manufacturing. Class Tablet is inherited from Medicine. Tablet class has name of tablet,
quantity per pack, price of one tablet as members. Class Syrup is also inherited from Medicine and
it has quantity per bottle, dosage unit as members. Both the classes contain necessary member
functions for input and output data. Write a main( ) that enter data for tablet and syrup, also
display the data. Use the concepts of Hierarchical Inheritance.
CODE:
#include<iostream>
using namespace std;
class medicine
{ protected: char cname[20],type[10],date[30];
public: void mget() {
cout<<"Enter type of medicine,company name and date of manf. : ";
cin>>type;
cin>>cname;
cin>>date; }
void mprint() { cout<<"\n"<<type<<" , "<<cname<<" and "<<date<<"\n";}};
class tablet : public medicine{
protected: char tname[20]; int qty,p;
public : void tget()
{ cout<<"Enter tablet name , quantity per pack and price : "; cin>>tname; cin>>qty;
cin>>p; } void tprint()
{ cout<<"\n"<<tname<<" , "<<qty<<" and "<<p<<"\n"; }};
class syrup : public medicine{
protected:
int sqty,dos;
public :
void sget() {
cout<<"Enter quantiy per bottle and dosage : ";
cin>>sqty;
cin>>dos; }
void sprint() {
cout<<"\n"<<sqty<<" and "<<dos<<"\n"; }};
int main(){ syrup s; tablet t;
t.mget(); t.mprint(); t.tget(); t.tprint(); s.sget();
s.sprint();cout<<endl<<"Patel Utsav"<<endl<<"Id:18CE089"; return 0;}
OUTPUT:
80
Object oriented programming in c++ 18CE089
81
Object oriented programming in c++ 18CE089
Practical 36
Aim: Create a class shape having data member shape_name and member function to get and print
shape_name. Derive a Class Circle which is inherited publicly from class shape and having data
members radius of a circle and member function to get and print radius of a circle. Derive a Class
Area which is inherited publicly from Class Circle and having data members area_of_circle and
member function display () which displays area of a circle. Use object of class Area in main ()
function and get and display all the information. Use the concepts of Multilevel Inheritance
CODE:
#include<iostream>
#define PI 3.14
using namespace std;
class Shape
{ public:
char s_name[10];
void sget()
{ cin>>s_name; }
void sprint() { cout<<s_name<<endl; }};
class Circle : public Shape
{ public:
float rad;
void cget() {
cout<<"enter radius: ";
cin>>rad; }
void cprint() {
cout<<"radius is: ";
cout<<rad<<endl; }};
class Area : public Circle
{ public:
float area_of_circle;
void display() {
area_of_circle=PI*rad*rad;
cout<<"Area of Circle : "<<area_of_circle<<endl;; }};
int main()
{ Area a;
a.sget(); a.cget(); a.sprint(); a.cprint(); a.display();
cout<<endl<<"Patel Utsav"<<endl<<"Id:18CE089”;
return 0;}
OUTPUT:
82
Object oriented programming in c++ 18CE089
Practical 37
Aim: Define a class Hospital having rollno and name as data members and member function to get
and print data. Derive a class Ward from class Hospital having data members: ward number and
member function to get and print data. Derive another class Room from Hospital having data
member bed number and nature of illness and member function to get and print data. Derive class
Patient from Class Ward and Class Room. In main () declare 5 object of Class Patient and get and
display all the information. Use the concept of Virtual Base Class and Hybrid Inheritance.
CODE:
#include<iostream>
using namespace std;
class Hospital
{public:
char h_name[10];
int rollno;
void hget()
{
cout<<"Enter hospital name and no."<<endl;
cin>>h_name>>rollno; }
void hprint()
{
cout<<h_name<<" "<<rollno<<endl; }};
class Ward : virtual public Hospital{
public: int wardno;
void wget()
{
cout<<"Enter ward no."<<endl;
cin>>wardno;
}
void wprint()
{
cout<<wardno<<endl;
}};
class Room : virtual public Hospital{
public:
int bedno;
char ill[25];
void rget() {
cin>>ill>>bedno; }
void rprint() {
83
Object oriented programming in c++ 18CE089
OUTPUT:
84
Object oriented programming in c++ 18CE089
85
Object oriented programming in c++ 18CE089
Practical 38
Aim: Create a Class alpha having data member: int x and one argument constructor which
initializes the value of x. It also has member function which displays the value of x. Create another
class beta which contains data member: float y and one argument constructor which initializes the
value of y. It also has member function which displays the value of y. Create a Class Gamma which
publicly inherits from class alpha and class beta and has two data members: int m, n and a
constructor which passes argument to the base class constructor as well as initializes its own data
members. Class Gamma also has member function to print the values of m and n. Write main
function which creates object of class Gamma which passes values of base class constructor as
well as derived class constructor. Use the concept of Multiple Inheritance and Constructor in
Derived Class.
CODE:
#include<iostream>
using namespace std;
class Alpha
{protected: int x;
public:
Alpha(int i)
{ x=i; } void putdata()
{ cout<<"The value of x is: "<<x; }};
class Beta
{protected: float y;
public:
Beta (float i) {
y=i; }
void putdata()
{ cout<<"The value of y is: "<<y; }};
class Gamma:public Alpha,public Beta
{protected:
int m,n;
public:
Gamma(int i,int j,int p,int q):Alpha(p),Beta(q)
{ m=i; n=j;
cout<<"m= "<<m<<"\nn= "<<n<<"\np= "<<p<<"\nq= "<<q; }};
int main()
{ Gamma g(1,2,3,4);
cout<<endl<<"Patel Utsav"<<endl<<"Id:18CE089";
return 0;
}
OUTPUT:
86
Object oriented programming in c++ 18CE089
Practical 39
void putdata()
{
cout<<"University name:"<<uni_name<<endl;
cout<<"University type:"<<type<<endl;
}
};
char clg_name[30],dep_name[25];
void gtdata()
{
cout<<"Enter college Name:";
cin.getline(clg_name,30);
cout<<"Enter Department name:";
cin.getline(dep_name,25);
}
void ptdata()
{
cout<<"College Name:"<<clg_name<<endl;
cout<<"Department Name:"<<dep_name<<endl;
}
};
class Student :public College
{public:
87
Object oriented programming in c++ 18CE089
string id,mo,email;
char name[20];
void gdata()
{ cout<<"Enter student name:";
cin.getline(name,20);
cout<<"Enter student id:";
cin>>id;
cout<<"Enter student mobile no.";
cin>>mo;
cout<<"Enter email id:";
cin>>email;
cout<<endl; }
void pdata()
{ cout<<"Student Name:"<<name<<endl;
cout<<"Student id:"<<id<<endl;
cout<<"Mobile No.:"<<mo<<endl;
cout<<"Email:"<<email<<endl; }};
int main()
{ Student s;
s.getdata();
s.gtdata();
s.gdata();
cout<<"*******STUDENT DETAILS********"<<endl;
s.putdata();
s.ptdata();
s.pdata();
cout<<endl<<"Patel Utsav"<<endl<<"Id:18CE089";
return 0;
}
OUTPUT:
88
Object oriented programming in c++ 18CE089
89
Object oriented programming in c++ 18CE089
Practical 40
Aim:Out of Following Codes
A) #include<iostream>
using namespace std;
class product {
int code;
float price;
public:
void getdata(int a, float b) {
code=a;
price=b; } void show() {
cout<<"Code: "<<code<<endl;
cout<<"Price: "<<price<<endl; } };
int main() {
product * p = new product;
product *d = p;
int x,i;
float y;
cout<<"Input code and price for product: ";
cin>>x>>y;
p->getdata(x,y);
d->show();
cout<<endl<<"Patel Utsav"<<endl<<"Id:18CE089"; }
OUTPUT:
B) #include<iostream>
using namespace std;
class student
{ int roll_no;
float age;
public:
student(int r, float a) {
roll_no = r;
age = a; }
student & greater (student & x) {
if(x.age>=age)
return x;
else
return *this; }
void display() {
90
Object oriented programming in c++ 18CE089
cout<<"Roll No "<<roll_no<<endl;
cout<<"Age "<<age<<endl; } };
int main() {
student s1 (23,18),s2 (30,20),s3 (45,16);
student s = s1.greater(s3);
cout<<"Elder Person is :"<<endl;
s.display();cout<<endl<<"Patel Utsav"<<endl<<"Id:18CE089"; return 0;}
OUTPUT:
C) #include<iostream>
using namespace std;
class BC {
public:
int b;
void show() {
cout<<"b = "<<b<<endl; } };
class DC : public BC {
public:
int d;
void show() { cout<<"b = "<<b<<endl;
cout<<"d = "<<d<<endl; } };
int main() {
BC *bptr;
BC base;
bptr = &base;
bptr->b = 100;
cout<<"bptr poins to base objects"<<endl;
bptr->show();
DC derived;
bptr = &derived;
bptr->b = 200;
/*bptr->b = 300;*/ // wont work
cout<<"bptr now points to derived object"<<endl;
bptr->show();
DC *dptr;
dptr=&derived;
dptr->d=300;
cout<<"Dptr is derived type pointer"<<endl;
dptr->show();cout<<endl<<"Patel Utsav"<<endl<<"Id:18CE089";
return 0; }
OUTPUT:
91
Object oriented programming in c++ 18CE089
92
Object oriented programming in c++ 18CE089
Practical 41
Aim: Create a class Media that stores the title (a string) and price (float). Class Media has two
argument constructor which initializes data members of class Media. Also declare a virtual function
display () in Class Media. From the class Media derive two classes: Class book, which contains data
member page count (int): and Class tape, which contains data member playing time in minutes
(float). Both Class book and Class tape should have a constructor which initializes base class
constructor as well as its own data members and display ( ) function which displays book details and
tape details respectively. Write a main ( ) to test book and tape classes by creating instances of
them, asking the user to fill data and displaying them. Use the concept of Virtual function and
Constructor in Derived Class. Virtual function is also known as Runtime Polymorphism or Dynamic
Binding.
Code:
#include<iostream>
#include<string.h>
using namespace std;
class media
{ protected:
char title[10];
float price;
public: media(float t_price,char t_title[])
{ price=t_price;
strcpy(title,t_title); }
virtual void display() {
cout<<"Title is: "<<title<<endl<<"price is: "<<price<<endl; }};
class book :public media{
int pg;
public:
book(float price,char title[]):media(price,title) {
pg=100; }
void display() {
cout<<"page: "<<pg<<endl; }};
class tape:public media{
float mins;
public:
tape(float price,char title[],int time):media(price,title) {
mins=time; }
void display() { cout<<"time: "<<mins<<endl; }};
int main()
{ media m(50,"Zeeshan"); book b(100,"pt");
tape t(75,"c++",60); media *p;
p=&m; p->display(); book *pb;
pb=&b; pb->display(); tape *pt;
pt=&t; pt->display();cout<<endl<<"Patel Utsav"<<endl<<"Id:18CE089"; return 0;}
OUTPUT:
93
Object oriented programming in c++ 18CE089
94
Object oriented programming in c++ 18CE089
Practical 42
Aim: Create a Abstract class vehicle having average as data and pure virtual function getdata() and
putdata(). Derive class car and truck from class vehicle having data members: fuel type (petrol,
diesel, CNG) and no of wheels respectively. Write a main ( ) that enters the data of two cars and a
truck and display the details of them. Use the concept of Abstract Base class and Pure Virtual
functions.
Code:
#include<iostream>
using namespace std;
class veh
{protected:
float avg;
public: virtual void get()=0;
virtual void put()=0;};
class car:public veh
{protected:
char ft[10];
int nof;
void get() {
cout<<"\n\ncar class\n\nEnter Fule type: ";
cin>>ft;
cout<<"\nNo of tyre: ";
cin>>nof; }
void put() {
cout<<"\n\ncar class\n\nFule type: "<<ft<<"\nNO of wheels: "<<nof<<"\n"; }};
class truck:public veh{
protected:
char ft[10];
int nof;
void get() {
cout<<"\n\ntruck class\n\nEnter Fule type: ";
cin>>ft;
cout<<"\nNo of tyre: ";
cin>>nof; } void put() {
cout<<"\n\ntruck class\n\nFule type: "<<ft<<"\nNO of wheels: "<<nof<<"\n"; }};
int main(){ car obc; veh *v; v=&obc;
v->get(); v->put();
truck obt; v=&obt;
v->get(); v->put();cout<<endl<<"Patel Utsav"<<endl<<"Id:18CE089";
return 0;}
OUTPUT:
95
Object oriented programming in c++ 18CE089
96
Object oriented programming in c++ 18CE089
Practical-43
Aim:
Write a program that creates a text file that contains ABC…Z. A program should print the
file in reverse order on the screen. i.e. ZYX…BA. Use concept ofOpening the file using
constructor and open () function. Use all error handling functions like eof() , fail() , bad() ,
good() and functions for manipulation of file pointer like seekg() and tellg().
Code:
#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
int main()
{
fstream fp;
char str[27]="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
cout<<"Open file and store characters"<<endl;
int l=strlen(str);
fp.open("ABC.txt",ios::out);
for(int i=0;i<l;i++)
fp.put(str[i]);
fp.close();
fp.open("ABC.txt",ios::in);
cout<<"reading from the file:"<<endl;
fp.seekg(-1,ios::end);
cout<<"FP:"<<fp.tellg()<<endl;
char ch;
cout<<"\ngood()="<<fp.good();
cout<<"\nbad()="<<fp.bad();
cout<<"\nof()="<<fp.eof();
cout<<"\nfail()="<<fp.fail()<<endl;
while(fp.tellg()>=0)
{
97
Object oriented programming in c++ 18CE089
fp.get(ch);
cout<<ch;
fp.seekg(-2,ios::cur);
}
cout<<endl;
fp.close();
return 0;
Output:
Conclusion:
We studied about file handling in c++.
98