Charotar University of Science & Technology & Devang Patel Institute of Advance Technology & Reserch CE144 Object Oriented Programming With C++
Charotar University of Science & Technology & Devang Patel Institute of Advance Technology & Reserch CE144 Object Oriented Programming With C++
SET-1
PRACTICAL-1.1
AIM: Write a C++ program that will print output in the following form. Make
sure, your output looks exactly as shown here (including spacing, line breaks,
punctuation, and the title and author).
Code:
#include<iostream>
using namespace std;
int main ()
{
for (int i=1;i<=41;i++)
cout<<"*";
Output:
Question:
Differentiate between \n and endl in two points in below given tabular format:
\n endl
It is a escape sequence It is a manipulator
It takes some space No space is required
Can be used in C and C++ both Can be used in only C++
PRACTICAL-1.2
AIM: Write a program to create the following table by making use of endl and
setw manipulator.
Code:
#include<iostream>
#include<iomanip>
using namespace std;
int main ()
{
for (int i=1;i<=4;i++)
{
for (int j=1;j<=4;j++)
{
cout<<left<<setw(3)<<i*j;
}
cout<<endl;
}
cout<<"\n@22dcs085";
cout<<"Patel Yash C.";
return 0;
}
Output:
Question:
1. Explain any three manipulators in the below given tabular format.
Sr. no Manipulator Description
1 Setw This manipulator sets the minimum field width on
output.
2 Setprecison It is used to set the number of digits printed to the right
of the decimal point.
3 setfill This manipulator used to set the ios library fill
character based on the character specified as the
parameter to this method.
PRACTICAL-1.3
Aim: Write a C++ program to add two floating numbers using pointer. The
result should contain only two digits after the decimal.
1) Fill the following table based on the outcome you get by executing the
functions in given sequence- fixed, scientific and setprecision(). Also
attach the screenshot of output.
Code:
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
float a,b,*p1,*p2;
p1=&a;
p2=&b;
cout<<"\n add two no.: ";
cin>>a>>b;
float sum=*p1+*p2;
cout<<"fixed:"<<fixed<<setprecision(2)<<sum<<endl;
cout<<"scientific: "<<scientific<<setprecision(2)<<sum<<endl;
cout<<"Setprecision(2): "<<defaultfloat<<setprecision(2)<<sum<<endl;
cout<<"\n@22dcs085 Patel Yash C.";
}
.
.
.
DEPSTAR (CSE) Page 6
Object Oriented Programming with C++ [CE144] 22DCS085
Output:
2) Fill the following table based on the outcome you get by executing the
functions in given sequence- scientific, fixed and setprecision(). Also
attach the screenshot of output.
Code:
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
float a,b,*p1,*p2;
p1=&a;
p2=&b;
cout<<"\n add two no. : ";
cin>>a>>b;
float sum=*p1+*p2;
cout<<"scientific: "<<scientific<<setprecision(2)<<sum<<endl;
cout<<"fixed :"<<fixed<<setprecision(2)<<sum<<endl;
cout<<"Setprecision(2): "<<defaultfloat<<setprecision(2)<<sum<<endl;
cout<<"\n@22dcs085 Patel Yash C.";
}
Output:
Question:
Which ios class function will be responsible for setting the number
of decimal places?
The setprecision method of ios class in C++ is used to set the ios library floating point
precision based on the precision specified as the parameter to this method .
Set-2
PRACTICAL-2.1
Code 1(struct):
#include<iostream>
#include<iomanip>
using namespace std;
struct student
{
char college_name[10],college_code[10],department[5];
int intake;
}s1;
int main()
{
cout<<"\n+++++ Enter the College Information +++++";
cout<<"\nName of the college: ";
cin>>s1.college_name;
cout<<"College Code: ";
cin>>s1.college_code;
cout<<"Department: ";
cin>>s1.department;
cout<<"Department In-take: ";
DEPSTAR (CSE) Page 10
Object Oriented Programming with C++ [CE144] 22DCS085
cin>>s1.intake;
cout<<"\n\n********College information********";
cout<<"\n\nName of the college: "<<s1.college_name;
cout<<"\nCollege University code: "<<s1.college_code;
cout<<"\nName of Department:"<<s1.department;
cout<<"\nThe department of"<<s1.department<<"has in-take:"<<s1.intake;
cout<<"\n@22dcs085 Patel Yash C.";
}
Output-1(struct):
Code-2:
#include<iostream>
#include<iomanip>
using namespace std;
class student
{
public:
char college_name[10],college_code[10],department[5];
int intake;
}s1;
int main()
{
cout<<"\n+++++ Enter the College Information +++++";
cout<<"\nName of the college: ";
cin>>s1.college_name;
cout<<"College Code: ";
cin>>s1.college_code;
cout<<"Department: ";
cin>>s1.department;
cout<<"Department In-take: ";
cin>>s1.intake;
cout<<"\n\n********College information********";
cout<<"\n\nName of the college: "<<s1.college_name;
cout<<"\nCollege University code: "<<s1.college_code;
cout<<"\nName of Department:"<<s1.department;
cout<<"\nThe department of"<<s1.department<<"has in-take:"<<s1.intake;
cout<<"\n@22dcs085 Patel Yash C.";
}
Output-2(class):
s1.read();
s1.print();
cout<<"\n@22dcs085 Patel Yash C.";
}
Output-3:charusat
Questions:
1. Are you able to find the concept of struct that you studied in C Programming, similar to
class? If yes, then in which ways?
In C, we have used struct and classes in c++ there are some similarities between them, we
have to declare a struct variable in struct in same way we have to declre a object, we have to
use dot operator and arrow operator to access member variables.
PRACTICAL-2.2
DEPSTAR (CSE) Page 15
Object Oriented Programming with C++ [CE144] 22DCS085
AIM: Write a C++ program to collect the details of student like roll_no, name,
class and division(A/B) and display the same of 5 students. Declare the
following data members in class as public: roll_no, name, class and
division(A/B). Make use of two functions read and display as public for
collecting information and displaying it respectively.
Code:
#include<iostream>
using namespace std;
class student
{
public:
int roll_no;
char name[20],classes[5];
char div;
void read()
{
cout<<"Add Name: ";
cin>>name;
cout<<"add roll no: ";
cin>>roll_no;
cout<<"add class: ";
cin>>classes;
cout<<"add divison[a/b]: ";
cin>>div;
}
void display()
{
cout<<"Name: "<<name<<endl;
cout<<"Roll no: "<<roll_no<<endl;
cout<<"class: "<<classes<<endl;
cout<<"div: "<<div<<endl;
}
}s1[5];
int main()
{
for(int i=0;i<5;i++)
{
cout<<"Add details for "<<i+1<<endl;
s1[i].read();
cout<<endl;
}
for(int i=0;i<5;i++)
{
cout<<"\ndetails of"<<i+1;
s1[i].display();
}
cout<<"@22DCS085 Patel Yash c.";
return 0;
}
Output:
DEPSTAR (CSE) Page 17
Object Oriented Programming with C++ [CE144] 22DCS085
1 Dev 1 CSE A
2 Kirtan 2 CSE A
3 Joy 3 CSE A
4 Siddharth 4 CSE B
5 Shivang 5 CSE B
Questions:
1. State the reason for creating object of class.
Ans: Objects are required in OOPs because they can be created to call a non-static
function which are not present inside the Main Method but present inside the Class and
also provide the name to the space which is being used to store the data.
PRACTICAL-2.3
AIM: Write a C++ program to swap two numbers without using third variable,
using the concept of class and object. Display the values of the variables before
and after swapping.
Code:
#include<iostream>
using namespace std;
class swap0
{
public:
int a,b;
void read()
{
cout<<"Add two no: ";
cin>>a>>b;
}
void swap1()
{
a=a+b;
b=a-b;
a=a-b;
cout<<a<<" "<<b<<endl;
}
void swap2(int n1, int n2)
{
n1=n1+n2;
n2=n1-n2;
n1=n1-n2;
cout<<n1<<" " <<n2<<endl;
}
int swap3(int x,int y,int *m1,int *m2)
{
DEPSTAR (CSE) Page 20
Object Oriented Programming with C++ [CE144] 22DCS085
x=x+y;
y=x-y;
x=x-y;
*m1=x;
*m2=y;
}
};
int main()
{
swap0 s1;
int f,e,s,t,m,n,*p1,*p2;
s1.read();
s1.swap1();
cout<<"\n add two no: ";
cin>>e>>f;
s1.swap2(e,f);
cout<<"\n add two no: ";
cin>>s>>t;
s1.swap3(s,t,&m,&n);
cout<<m<<" "<<n;
cout<<"\n @22DCS085 Patel Yash C.";
return 0;
}
Output:
After Swapping 4 3
2 Before Swapping 5 98
After Swapping 98 5
3 Before Swapping 67 3
After Swapping 3 67
SET-3
PRACTICAL-3.1
AIM: Find error in the following code and give reasons for each error:
#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;
}
Errors:
#include<iostream>
using namespace std;
int main()
{
int no1=10, no2=12;
int & x=no1;
int & r;
//you cannot initialize a reference variable without referencing to a variable
PRACTICAL-3.2
Output:
Questions:
1. Explain how scope Resolution operator is used to access global version of a
variable.
Ans: If the global variable name is same as local variable name, the scope resolution
operator will be used to call the global variable. It is also used to define a function outside the
class and used to access the static variables of class.
PRACTICAL-3.3
AIM: Write a program to enter a size of array. Create an array of size given by
user using “new” Dynamic memory management operator (free store operator).
Enter the data to store in array and display the data after adding 2 to each
element in the array. Delete the array by using “delete” memory management
operator.
Code:
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"add size";
cin>>n;
int *p1=new int[n];
for(int i=0;i<n;i++)
{
cout<<"add no: ";
cin>>*(p1+i);
}
cout<<endl;
for(int i=0;i<n;i++)
{
cout<<"no: "<<*(p1+i)+2<<endl;
}
delete[] p1;
cout<<"@22DCS085 Patel Yash C.";
}
Output:
Size of Array: 5 6
Questions:
1. Where the new operator does allocate memory in system?
Ans: The new operator allocates memory in the heap of the memory in system.
SET-4
PRACTICAL-4.1
AIM: Define three functions named divide (). First function takes numerator
and denominator as an input argument and checks it is divisible or not, second
function takes one integer numbers as input argument and checks whether the
number is prime or not and Third function takes 3 float number as argument and
finds out average of the numbers.
CODE 1:
#include<iostream>
using namespace std;
class demo
{
public:
void display(int a,int b)
{
cout<<"a: "<<a<<" b:"<<" "<<b<<endl;
}
void display(float c)
{
cout<<"c: "<<c<<" "<<endl;
}
void display(int d,int f)
{
cout<<"d: "<<d<<" f:"<<f<<endl;
}
void display(int c,int b,float a)
{
cout<<" c: "<<c;
cout<<" b: "<<b;
cout<<" a: "<<c<<endl;
}
};
int main()
{
int a1=4,b1=5,c1=6,d1=7,e1=8;
float g1=4.3,f1=2.3;
demo de1;
de1.display(a1,b1);
de1.display(f1);
cout<<"@22DCS085 Patel Yash C.";
}
Output:
Solution: we cannot overload a function with same no. of same data type’s arguments.
Therefore, overload a function with different no. of arguments or different data types.
Code 2:
#include<iostream>
using namespace std;
class math
{
public:
void divide(int a,int b)
{
if(a%b==0)
cout<<"a is divisible by b"<<endl;
else
cout<<"a is not divisible by b"<<endl;
}
void divide(int a)
{
int f1=0;
for(int i=2;i<a/2;i++)
{
if(a%i==0)
{
f1=1;
break;
}
}
if(f1==1)
cout<<"A is Non prime"<<endl;
else
cout<<"A is prime"<<endl;
}
void divide(float a,float b,float c)
{
float avg=(a+b+c)/3;
cout<<"Average: "<<avg<<endl;
}
};
int main()
{
math m;
int a1,b1;
float m1,n1,o1;
cout<<"\n Add a b: ";
cin>>a1>>b1;
m.divide(a1,b1);
m.divide(a1);
cout<<"ADD m,n,o: ";
cin>>m1>>n1>>o1;
m.divide(m1,n1,o1);
cout<<"@22DCS085 Patel Yash C.";
}
Output:
Questions:
1. State the benefits of using function overloading
Ans: in object oriented programming there are so many examples where we have to take
data of multiple numbers and many times data type and no of argument is not fixed. So we
can use function overloading in such cases.
It can save the memory
It can make code simpler and easier to understand
PRACTICAL-4.2
AIM: Write a function called tonLarge () that takes two integer arguments call
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 1:
#include<iostream>
using namespace std;
class dem3
{
public:
void tonLarge(int &a,int &b)
{
if(a>b)
{
a=100;
cout<<"a ="<<a<<endl;
}
else
{
b=100;
cout<<"b="<<b<<endl;
}
}
};
int main()
{
dem3 d1;
int a,b;
cout<<"Add a,b:";
cin>>a>>b;
d1.tonLarge(a,b);
cout<<"@22DCS085 Patel Yash C.";
DEPSTAR (CSE) Page 34
Object Oriented Programming with C++ [CE144] 22DCS085
}
Output:
Code 2:
#include<iostream>
using namespace std;
class dem3
{
public:
int tonLarge(int &c,int &d)
{
if(c>d)
{
return c=100;
}
else
{
return d=100;
}
}
};
int main()
{
dem3 d1;
int c,d;
cout<<"Add c,d:";
cin>>c>>d;
int e=d1.tonLarge(c,d);
cout<<"\n c: "<<c<<"d: "<<d<<endl;
cout<<"@22DCS085 Patel Yash C.";
}
Output:
Code 3:
#include<iostream>
using namespace std;
class dem3
{
public:
int& tonLarge(int &c,int &d)
{
if(c>d)
{
return c=100;
}
else
{
return d=100;
}
}
};
int main()
{
dem3 d1;
int c,d;
cout<<"Add c,d:";
cin>>c>>d;
int e=d1.tonLarge(c,d);
cout<<"\n c: "<<c<<"d: "<<d<<endl;
cout<<"@22DCS085 Patel Yash C.";
}
Output:
Code 4:
#include<iostream>
using namespace std;
class dem3
{
public:
int& tonLarge(int &c,int &d)
{
if(c>d)
{
return c=100;
}
else
{
return d=100;
}
}
};
int main()
{
dem3 d1;
int c,d;
cout<<"Add c,d:";
cin>>c>>d;
int &e=d1.tonLarge(c,d);
cout<<"\n c: "<<c<<"d: "<<d<<endl;
cout<<"@22DCS085 Patel Yash C.";
}
Output:
Code 5:
#include<iostream>
using namespace std;
class dem3
{
public:
int tonLarge(int &c,int &d)
{
if(c>d)
{
return c=100;
}
else
{
return d=100;
}
}
};
int main()
{
dem3 d1;
int c,d;
cout<<"Add c,d:";
cin>>c>>d;
int &e=d1.tonLarge(c,d);
cout<<"\n c: "<<c<<"d: "<<d<<endl;
cout<<"@22DCS085 Patel Yash C.";
}
Output:
Solution:
To solve this error, Just use a const keyword where e is declared. Here the function does not
return a constant value so we can’t assign it with reference variable. Therefore, we use const
keyword.
Questions:
1.Explain the difference of call by reference and return by reference, each in
two points.
Ans:
Call by reference Return by reference
In the Call by value method original value is in the Call by reference method, the original
not modified. value is modified.
In Call by value, a copy of the variable is in Call by reference, a variable itself is
passed. passed.
PRACTICAL-4.3
AIM: Write a inline function called power () that takes two arguments: a double
value for Base and an integer for Power, and returns the result as double value.
Use default argument as 2 for Base, 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 1:
#include<iostream>.
using namespace std;
class demo1
{
void display(int a,int b,int c=9)
{
cout<<"a: "<<a<<"b: "<<b<<"c: "<<c<<endl;
}
void display(int a,int b)
{
cout<<"a: "<<a<<"b: "<<b<<endl;
}
};
int main()
{
demo1 d1;
int a=2,b=4,c=6;
d1.display(a,b);
d1.display(a,b,c);
cout<<"@22DCS085 Patel Yash";
}
Output:
Solution:
We cannot overload a function which contain a default argument. Here the program contains
the default argument. Here, if we pass 2 arguments the 3rd value is automatically assigned. So
we cannot overload this function for 2 int arguments.
Code 2:
DEPSTAR (CSE) Page 45
Object Oriented Programming with C++ [CE144] 22DCS085
#include<iostream>
#include<math.h>
using namespace std;
class dem1
{
public:
void power(int a,int b=2)
{
cout<<"Ans is: "<<pow(a,b)<<endl;
}
};
int main()
{
int a,b;
cout<<"add base: ";
cin>>a;
cout<<"Add Power: ";
cin>>b;
dem1 d1;
//default argument
cout<<"Default arg 2 "<<endl;
d1.power(a);
cout<<"power is "<<b<<endl;
d1.power(a,b);
cout<<"@22DCS085 Patel Yash";
}
Output:
2. 12 - 144
Code 3:
//inline
#include<iostream>
#include<math.h>
using namespace std;
class dem1
{
public:
void power(int a,int b=2);
};
inline void dem1:: power(int a,int b)
{
cout<<"Ans is: "<<pow(a,b)<<endl;
}
int main()
{
int a,b;
cout<<"add base: ";
cin>>a;
cout<<"Add Power: ";
cin>>b;
dem1 d1;
//default argument
cout<<"Default arg 2 "<<endl;
d1.power(a);
cout<<"power is "<<b<<endl;
d1.power(a,b);
cout<<"@22DCS085 Patel Yash";
}
Output:
DEPSTAR (CSE) Page 48
Object Oriented Programming with C++ [CE144] 22DCS085
2. 4 - 16
Question:
1. Explain the situations where inline function cannot work?
Ans. If the function is recursive then in that case, we cannot use inline function. And if
the function is too big or too complex then we cannot use inline function
SET-5
DEPSTAR (CSE) Page 49
Object Oriented Programming with C++ [CE144] 22DCS085
PRACTICAL-5.1
Code 1:
//c code
#include<stdio.h>
struct rect1
{
int length,width;
}r1;
void getvalue()
{
printf("Add values of length and width: ");
scanf("%d %d",&r1.length,&r1.width);
}
void area()
{
printf("\n area: %d",r1.length*r1.width);
}
int main()
{
//using struct
getvalue();
area();
printf("\n@22DCS085 Patel Yash C.");
}
Output:
Inputs Outputs
Height Width Area of Rectangle
45 38 1710
Code 2:
DEPSTAR (CSE) Page 51
Object Oriented Programming with C++ [CE144] 22DCS085
//c++
#include<iostream>
using namespace std;
class rec2
{
public:
int l,w;
void getvalue1()
{
cout<<"Add values: ";
cin>>l>>w;
}
void area1()
{
cout<<"Area: "<<l*w<<endl;
}
};
int main()
{
rec2 r2;
cout<<endl;
r2.getvalue1();
r2.area1();
cout<<"@22DCS085 Patel Yash C.";
}
Output:
Inputs Outputs
Height Width Area of Rectangle
56 4 224
Questions:
1. Illustrate the difference between C Structure and C++ Class.
Ans.
C structure C++ classes
It cannot have static member. It can have static member
Constructor/ destructor are not allowed Constructor/ destructor are allowed
All members are public by default All members are private by default
It can used in c and c++. It is not supported in c.
PRACTICAL-5.2
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.
Code:
#include<iostream>
using namespace std;
class Batsman
{
char bm_name[20];
int bcode,innings,not_out,runs;
float b_average;
void calcavg();
public:
void getdata();
void putdata();
};
{
cout<<"Add Batsman Name: ";
cin>>bm_name;
cout<<"Add bcode: ";
cin>>bcode;
cout<<"Add innings: ";
cin>>innings;
cout<<"Add no of not outs :";
cin>>not_out;
cout<<"Add runs: ";
cin>>runs;
}
inline void Batsman:: putdata()
{
cout<<"Batsman name: "<<bm_name<<endl;
cout<<"Bcode: "<<bcode<<endl;
cout<<"Innings: "<<innings<<endl;
cout<<"No of Not out: "<<not_out<<endl;
cout<<"Runs: "<<runs<<endl;
calcavg();
}
int main()
{
Batsman b1;
b1.getdata();
b1.putdata();
cout<<"\n@22DCS085 Patel Yash C.";
}
Output:
PRACTICAL-5.3
DEPSTAR (CSE) Page 56
Object Oriented Programming with C++ [CE144] 22DCS085
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.
Code 1:
#include<iostream>
using namespace std;
//function argument as int
class demo
{
public:
int a;
void disp(int b)
{
cout<<"b: "<<b<<endl;
}
};
int main()
{
int b;
cout<<"Add b: ";
cin>>b;
demo d;
d.disp(b);
cout<<"@22DCS085 Patel Yash C.";
}
Output:
Code2:
#include<iostream>
using namespace std;
//function argument as an object
class demo
{
public:
int a;
void disp(demo d1)
{
cout<<"d.a: "<<d1.a<<endl;
}
};
int main()
{
int b;
demo d;
cout<<"Add d.a: ";
cin>>d.a;
d.disp(d);
cout<<"@22DCS085 Patel Yash C.";
}
Output:
Code 3:
#include<iostream>
using namespace std;
//function returning value and argument as call by value
class demo
{
public:
int a;
int disp(int b)
{
return b;
}
};
int main()
{
demo d;
int b;
cout<<"Add b: ";
cin>>b;
int c=d.disp(b);
cout<<"C= "<<c<<endl;
cout<<"\n@22DCS085 Patel Yash C.";
}
Output:
Code 4:
#include<iostream>
using namespace std;
DEPSTAR (CSE) Page 59
Object Oriented Programming with C++ [CE144] 22DCS085
class demo
{
public:
int a;
demo disp(demo d1)
{
return d1;
}
};
int main()
{
demo d,d2;
int b;
cout<<"Add d.a: ";
cin>>d.a;
d2=d.disp(d);
cout<<"d1.a= "<<d2.a<<endl;
cout<<"\n@22DCS085 Patel Yash C.";
}
Output:
Code 5 (main):
#include<iostream>
using namespace std;
class currency
{
public:
int rp,ps;
void read()
{
int s;
cout<<"\n Add rupees: ";
cin>>rp;
cout<<" Add paisas: ";
cin>>ps;
if(ps>=100)
{
for(int i=1;ps>=100;i++)
{
rp++;
ps=ps-100;
}
}
display();
}
void display()
{
cout<<"Rupees: "<<rp<<endl<<"Paisa: "<<ps<<endl;
}
currency add(currency c1, currency c2)
{
float s2;
currency c3;
c3.rp=c1.rp+c2.rp;
c3.ps=c1.ps+c2.ps;
if(c3.ps>=100)
{
for(int i=1;c3.ps>=100;i++)
{
DEPSTAR (CSE) Page 61
Object Oriented Programming with C++ [CE144] 22DCS085
c3.rp++;
c3.ps=c3.ps-100;
}
}
return c3;
}
currency sum(currency c1)
{
currency c2;
float s1;
c2.rp=c1.rp+rp;
c2.ps=c1.ps+ps;
if(c2.ps>=100)
{
for(int i=1;c2.ps>=100;i++)
{
c2.rp++;
c2.ps=c2.ps-100;
}
}
return c2;
}
};
int main()
{
currency c1,c2,c3,c4;
c1.read();
c2.read();
c3=c3.add(c1,c2);
c3.display();
cout<<endl<<endl;
c4=c1.sum(c2);
c4.display();
cout<<"@22DCS085 Patel Yash C.";
}
DEPSTAR (CSE) Page 62
Object Oriented Programming with C++ [CE144] 22DCS085
Output:
Using sum()
Rupees Paisa Total
34 760 87 rupees 89 paisa
45 129
Using add()
34 760 87 rupees 89 paisa
45 129
{
c3.rp++;
c3.ps=c3.ps-100;
}
}
return c3;
}
currency& sum(currency c1)
{
currency &c2=c1;
float s1;
c2.rp=c1.rp+rp;
c2.ps=c1.ps+ps;
if(c2.ps>=100)
{
for(int i=1;c2.ps>=100;i++)
{
c2.rp++;
c2.ps=c2.ps-100;
}
}
return c2;
}
};
int main()
{
currency c1,c2;
c1.read();
c2.read();
currency c3=c3.add(c1,c2);
c3.display();
cout<<endl;
currency &c4=c1.sum(c2);
cout<<"Rupees: "<<c4.rp<<"\npaisa: "<<c4.ps<<endl;
cout<<"@22DCS085 Patel Yash C.";
DEPSTAR (CSE) Page 65
Object Oriented Programming with C++ [CE144] 22DCS085
Output:
Using sum()
Rupees Paisa Total
45 234 116 rupees 24 paisa
58 1090
Using add()
45 234 116 rupees 24 paisa
58 1090
PRACTICAL-5.4
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 ( )
DEPSTAR (CSE) Page 66
Object Oriented Programming with C++ [CE144] 22DCS085
function that takes object by reference and scale factor in float as an input
argument. The function will scale the distance accordingly.
Code:
#include<iostream>
using namespace std;
class Dist
{
public:
int feet;
float inches,sf;
void read()
{
cout<<"Add feet: ";
cin>>feet;
cout<<"Add inches: ";
cin>>inches;
}
void scale(Dist &s1, float sf)
{
cout<<"\n Values after using scale factor: ";
cout<<sf*s1.feet<<"'"<<" "<<sf*s1.inches<<"\"";
}
void display()
{
cout<<"\n Values before using scale factor: ";
cout<<feet<<"'"<<" "<<inches<<"\"";
}
};
int main()
{
Dist d1;
float s;
d1.read();
d1.display();
cout<<"\n add scale factor: ";
cin>>s;
Dist d2;
d2.scale(d1,s);
cout<<"\n@22DCS085 Patel Yash C.";
}
Output:
PRACTICAL-5.5
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.
Code 1:
#include<iostream>
using namespace std;
class demo
{
public:
int a;
static int b;
void read()
{
cout<<"\n add A: ";
cin>>a;
cout<<"add b: ";
cin>>b;
}
void display()
{
cout<<"\n a: "<<a<<" b: "<<b<<endl;
}
};
int demo::b=10;
int main()
{
demo d1;
int n;
cout<<"\n Add no of objects: ";
cin>>n;
demo d[n];
for(int i=0;i<n;i++)
{
d[i].read();
}
for(int i=0;i<n;i++)
{
d[i].display();
}
cout<<"@22DCS085 Patel Yash C.";
}
Output:
Code 2:
#include<iostream>
DEPSTAR (CSE) Page 70
Object Oriented Programming with C++ [CE144] 22DCS085
for(int i=0;i<n;i++)
{
d[i].read();
}for(int i=0;i<n;i++)
{
d[i].display();
}
cout<<"@22DCS085 Patel Yash C.";
DEPSTAR (CSE) Page 71
Object Oriented Programming with C++ [CE144] 22DCS085
}
Output:
Solution:
We can only use static data members in static function. Here we cannot use a. or we can
declare a as a static data member.
Code 3:
#include<iostream>
using namespace std;
DEPSTAR (CSE) Page 72
Object Oriented Programming with C++ [CE144] 22DCS085
class gate
{
public:
int rg_no;
char name[20];
char Ec;
static int ECV_cnt, ECS_cnt, ECA_cnt;
void getadata()
{
cout<<"\n Add name: ";
cin>>name;
cout<<"Add registration no. : ";
cin>>rg_no;
try1:
cout<<"Add Examination center[A/S/V]: ";
cin>>Ec;
if(Ec=='A' || Ec=='a')
ECA_cnt++;
else if(Ec=='V'|| Ec=='v')
ECV_cnt++;
else if(Ec=='S'|| Ec=='s')
ECS_cnt++;
else
goto try1;
}
void display()
{
cout<<"\n Name: "<<name;
cout<<"\n Registration no: "<<rg_no;
cout<<"\n Examination center: "<<Ec;
}
static void getcount()
{
cout<<"\n\n Total Students(centerwise): ";
cout<<"\n Ahemadabad: "<<ECA_cnt;
cout<<"\n Vadodara: "<<ECV_cnt;
cout<<"\n Surat: "<<ECS_cnt;
}
};
int gate::ECA_cnt=0;
int gate::ECS_cnt=0;
int gate::ECV_cnt=0;
int main()
{
int n;
cout<<"\n add no. of students: ";
cin>>n;
gate g[n];
for(int i=0;i<n;i++)
{
g[i].getadata();
}
for(int i=0;i<n;i++)
{
g[i].display();
}
g[n-1].getcount();
cout<<”@22DCS085 Patel Yash C.”;
}
Output:
Input Output
Sr. Registration Name Initials V S A
no. Number of city
1. 11 Ansh S
2. 12 Kavish A
3. 13 Yash V 1 1 3
4. 14 Siddharth A
5. 15 Shivang A
PRACTICAL-5.6
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.
Code:
#include<iostream>
using namespace std;
class demo
{
private:
int a;
protected:
int b;
public:
void read()
{
cout<<"\n add A: ";
cin>>a;
cout<<"\n add B: ";
cin>>b;
}
friend class demo1;
};
class demo1
{
public:
void display(demo d)
{
cout<<"\n A: "<<d.a<<"\n B: "<<d.b<<endl;
}
};
int main()
{
demo d;
demo1 d1;
d.read();
d1.display(d);
cout<<”\n @22DCS085 Patel Yash C.”;
}
Output:
Code 2:
#include<iostream>
using namespace std;
class date
{
private:
int d,m,y;
public:
void read()
{
tre1:
cout<<"\n Add day: ";
cin>>d;
if(d>31)
goto tre1;
tre2:
cout<<"\n Add Month: ";
cin>>m;
if(m>12)
goto tre2;
cout<<"\n Add Year: ";
cin>>y;
}
friend class date1;
};
class date1
{
public:
void swap1(date &d1, date &d2)
{
date d3;
cout<<"\n before swaping ";
d3.m=d1.m;
d1.m=d2.m;
d2.m=d3.m;
d3.y=d1.y;
d1.y=d2.y;
d2.y=d3.y;
int main()
{
date d1,d2;
date1 d3;
d1.read();
d2.read();
d3.swap1(d1,d2);
Output:
DEPSTAR (CSE) Page 79
Object Oriented Programming with C++ [CE144] 22DCS085
PRACTICAL-5.7
AIM: Create a class LAND having data members: length, width, area1. Write
member functions to read and display the data of land. Also, calculates the area
of the land. Create another class TILES having data members: l, w, area2. Write
a member function to get the data of tile. Calculate the area of one tile. Class
TILE has a member function named number_of_tiles() which is a friend of class
LAND and takes the object of class LAND by reference which calculates the
number of tiles which can be put over the land area. Write the main function to
test all the functions. Use the concept of member function of one class can be a
friend function of another class.
Code:
#include<iostream>
using namespace std;
class land;
class tile
{
public:
int l,w,area2;
void read()
{
cout<<"Enter the lenght of tile: ";
cin>>l;
cout<<"Enter the width of tile: ";
cin>>w;
}
void dis()
{
cout<<"lenght is:"<<l<<endl;
cout<<"width is:"<<w<<endl;
area2= l*w;
cout<<"area2 is:"<<area2<<endl;
}
void number_of_tiles(land &la);
};
class land{
DEPSTAR (CSE) Page 81
Object Oriented Programming with C++ [CE144] 22DCS085
int length;
int width;
int area1;
public:
void read()
{
cout<<"Enter the lenght: ";
cin>>length;
cout<<"Enter the width: ";
cin>>width;
}
void display()
{
cout<<"lenght is:"<<length<<endl;
cout<<"width is:"<<width<<endl;
area1=length*width;
cout<<"area is:"<<area1<<endl;
}
friend void tile::number_of_tiles(land &la);
}la;
void tile::number_of_tiles(land &la)
{
float totatiles;
totatiles=la.area1/area2;
cout<<"total tiles fit:"<<totatiles;
}
int main()
{
tile ti;
la.read();
la.display();
ti.read();
ti.dis();
ti.number_of_tiles(la);
cout<<"\n@22DCS085 Patel Yash C.";
DEPSTAR (CSE) Page 82
Object Oriented Programming with C++ [CE144] 22DCS085
Output:
PRACTICAL-5.8
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
{
public:
char name[20];
char gender[10];
void get()
{
cout<<"\n Add Name: ";
cin>>name;
cout<<"\n Add gender: ";
cin>>gender;
}
void put()
{
cout<<"\n Name: "<<name;
cout<<"\n gender: "<<gender;
}
friend class parent;
};
class parent
{
public:
void ReadChildData(child& c1)
{
cout<<"\n Add Name: ";
cin>>c1.name;
cout<<"\n Add gender: ";
cin>>c1.gender;
}
void DisplayChildData(child c1)
{
cout<<"\n Name: "<<c1.name;
cout<<"\n gender: "<<c1.gender;
}
};
int main()
{
child c1;
parent p1;
p1.ReadChildData(c1);
p1.DisplayChildData(c1);
cout<<"\n@22DCS085 Patel Yash C.";
}
Output:
Input Output
Name Gender Name Gender
Arya Female Arya Female
Yash Male Yash Male
PRACTICAL-6.1
AIM: Write a C++ program having class time with data members: hr, min and
sec. Define following member functions.
1) getdata() to enter hour, minute and second values
2) putdata() to print the time in the format 11:59:59 4 1,2,5,6 Page 12 of 22
3) default constructor
4) parameterized constructor Use 52 as default value for sec in parameterized
constructor.
5) copy constructor
6) Destructor.
Code 1:
#include<iostream>
using namespace std;
class demo{
public:
int a,b;
demo (){
}
demo(int a1,int a2){
a=a1;
b=a2;
}
demo(const demo &d1){
a=d1.a;
}
};
int main(){
int c,d;
cout<<"\n Add two no. :";
cin>>c>>d;
//demo d;
demo d1(c,d);
DEPSTAR (CSE) Page 87
Object Oriented Programming with C++ [CE144] 22DCS085
demo d2(d1);
cout<<d1.a<<endl;
cout<<d1.b<<endl;
cout<<"\n @22DCS085 Patel Yash C. ";
}
Output:
Code 2:
#include<iostream>
int main(){
int c,d;
cout<<"\n Add two no. :";
cin>>c>>d;
demo d1(c,d);
demo d2(d1);
cout<<d1.a<<endl;
cout<<d1.b<<endl;
cout<<"\n @22DCS085 Patel Yash C. ";
}
Output:
Code 3:
//declare outside
#include<iostream>
DEPSTAR (CSE) Page 89
Object Oriented Programming with C++ [CE144] 22DCS085
Output:
Code 4:
#include<iostream>
using namespace std;
class demo{
public:
int a,b;
demo(int a1,int a2);
demo(const demo &d1);
};
demo::demo(int a1,int a2){
a=a1;
b=a2;
}
demo::demo(const demo &d1){
a=d1.a;
}
int main(){
int c,d;
cin>>c>>d;
demo d1(c,d);
demo d2(d1);
cout<<d1.a<<endl;
cout<<d1.b<<endl;
cout<<"\n @22DCS085 Patel Yash C. ";
}
Output:
Code 5:
#include<iostream>
using namespace std;
DEPSTAR (CSE) Page 91
Object Oriented Programming with C++ [CE144] 22DCS085
class time
{
public:
int hr,sec,mn;
void getdata()
{
tr11:
cout<<"\n Add hours: ";
cin>>hr;
if(hr>12)
goto tr11;
tr12:
cout<<" Add Minutes: ";
cin>>mn;
if(mn>=60)
goto tr12;
tr13:
cout<<" Add seconds: ";
cin>>sec;
if(sec>=60)
goto tr13;
}
void putdata()
{
cout<<"\n "<<hr<<":"<<mn<<":"<<sec<<endl;
}
time(){}
time(int a1, int b1,int c1=52)
{
hr=a1;
mn=b1;
sec=c1;
}
time(time& t)
{
DEPSTAR (CSE) Page 92
Object Oriented Programming with C++ [CE144] 22DCS085
hr=t.hr;
mn=t.mn;
sec=t.sec;
}
~time()
{
cout<<"\n Destructor executed";
}
};
int main()
{
time t1;
int h,m,s;
t1.getdata();
tr11:
cout<<"\n Add hours: ";
cin>>h;
if(h>12)
goto tr11;
tr12:
cout<<" Add Minutes: ";
cin>>m;
if(m>=60)
goto tr12;
tr13:
cout<<" Add seconds: ";
cin>>s;
if(s>=60)
goto tr13;
time t2(h,m,s);
time t3(h,m);
time t4(t1);
cout<<"\n with getdata() ";
t1.putdata();
cout<<"\n with parametarized constructor ";
DEPSTAR (CSE) Page 93
Object Oriented Programming with C++ [CE144] 22DCS085
t2.putdata();
cout<<"\n with parametarized constructor (default argument)";
t3.putdata();
cout<<"\n with copy constructor ";
t4.putdata();
cout<<"\n @22DCS085 Patel Yash C. ";
}
Output:
Default - - - -
Parameterized 4 8 23 4:8:23
Parameterized 4 8 - 4:8:52
(With default arg)
Copy 2 56 56 2:56:56
Question:
1. Differentiate Default, Parameterized and Copy constructor.
Ans: Default constructor is created by compiler when the programmer doesn't write any
constructor in the class. Parameterized constructor is specifically written by the
programmer while creating a class. Copy constructor copies the values of data members
of object given in argument to new object.
SET – 7
PRACTICAL – 7.1
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. Use the
concept of Overloading Unary Operators.
Code 1:
//operator overloading with void type
#include<iostream>
using namespace std;
class Number
{
int num;
public:
void read()
{
cout<<"\n add num: ";
cin>>num;
}
void display()
{
cout<<"\n num:"<<num<<endl;
}
Number(){}
Number(int n)
{
num=n;
}
Number(Number& n1)
{
num=n1.num;
}
//pre increment
void operator ++ ()
{
++num;
}
//post increment
void operator ++(int)
{
num++;
}
//pre decrement
void operator --()
{
--num;
}
//post decrement
void operator --(int)
{
num--;
}
void operator - ()
{
num=-(num);
}
};
int main()
{
Number n1;
n1.read();
n1++;
cout<<"\n num++"<<endl;
n1.display();
DEPSTAR (CSE) Page 97
Object Oriented Programming with C++ [CE144] 22DCS085
++n1;
cout<<"\n ++num"<<endl;
n1.display();
--n1;
cout<<"\n --num"<<endl;
n1.display();
n1--;
cout<<"\n num--"<<endl;
n1.display();
-n1;
cout<<"\n -num"<<endl;
n1.display();
cout<<"\n @22DCS085 Patel Yash C. ";
}
Output:
Code 2:
#include<iostream>
using namespace std;
class Number
{
int num;
public:
void read(){
cout<<"\n add num: ";
cin>>num;
}
void display(){
cout<<"\n num:"<<num<<endl;
}
Number()
{}
Number(int n) {
num=n;
}
Number(Number& n1){
num=n1.num;
}
Number operator ++ ()
{
Number t;
t.num=++num;
return t;
}
Number operator ++(int)
{
Number t;
t.num=num++;
return t;
DEPSTAR (CSE) Page 99
Object Oriented Programming with C++ [CE144] 22DCS085
}
Number operator --()
{
Number t;
t.num=--num;
return t;
}
Number operator --(int)
{
Number t;
t.num=num--;
return t;
}
Number operator - () {
Number t;
t.num=-num;
return t;
}
};
int main()
{
Number n1,nt;
n1.read();
nt=n1++;
cout<<"\n num++"<<endl;
nt.display();
nt=++n1;
cout<<"\n ++num"<<endl;
nt.display();
nt=--n1;
cout<<"\n --num"<<endl;
nt.display();
nt=n1--;
cout<<"\n num--"<<endl;
DEPSTAR (CSE) Page 100
Object Oriented Programming with C++ [CE144] 22DCS085
nt.display();
nt=-n1;
cout<<"\n -num"<<endl;
nt.display();
cout<<"\n @22DCS085 Patel Yash C. ";
}
Output:
Inputs Outputs
Number Unary (++) Unary (++) Unary ( - )
N1=N2++ N3=++N1 N3=-N3
85 86 87 -87
56 57 58 -58
Question:
1. Also explain use of nameless object in operator overloading
Sometimes to reduce the code size, we create nameless temporary object of class. When
we want to return an object from member function of class without creating an object, for
this: we just call the constructor of class and return it to calling function and there is an
object to hold the reference returned by constructor. This concept is known as nameless
temporary objects
PRACTICAL-7.2
Aim: Create a class complex having data members int real, img and member
function to print data. Overload Unary operator (-) using friend function such
that it supports – C1 where C1 is the object of class complex. Also define
default and parameterized constructor for the class. Use the concept of
Overloading Unary Operators with friend function.
Code 1:
//friend void global function
#include<iostream>
using namespace std;
class complex1
{
int real,img;
public:
void read()
{
cout<<"\n add complex no: ";
cin>>real>>img;
}
void display()
{
if(img<0)
cout<<"\n complex no: "<<real<<""<<img<<"i"<<endl;
else
cout<<"\n complex no: "<<real<<"+"<<img<<"i"<<endl;
}
friend complex1 operator - (complex1& c1);
};
complex1 operator - (complex1& c1)
{
c1.img=-c1.img;
}
int main()
DEPSTAR (CSE) Page 102
Object Oriented Programming with C++ [CE144] 22DCS085
{
complex1 c1;
c1.read();
-c1;
c1.display();
cout<<"\n @22DCS085 Patel Yash C. ";
}
Output:
Code 2:
#include<iostream>
using namespace std;
class complex1
{
int real,img;
public:
void read()
{
cout<<"\n add complex no: ";
cin>>real>>img;
}
void display()
{
if(img<0)
cout<<"\n complex no: "<<real<<""<<img<<"i"<<endl;
else
cout<<"\n complex no: "<<real<<"+"<<img<<"i"<<endl;
}
friend void operator - (complex1& c1);
};
void operator - (complex1& c1)
{
c1.img=-c1.img;
}
int main()
{
complex1 c1;
c1.read();
-c1;
c1.display();
cout<<"\n @22DCS085 Patel Yash C. ";
}
Output:
PRACTICAL-7.3
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 operators.
Code 1:
#include<iostream>
using namespace std;
class string1
{
string s1;
public:
void read() {
cout<"\n add string: ";
cin>>s1;
}
void display() {
cout<<"\n string: "<<s1<<endl;
}
string1 operator + (string1& s3)
{
string1 s2;
s2.s1=s1+s3.s1;
return s2;
}
int operator == (string1& s3) {
if(s1==s3.s1)
return 1;
else if(s1<s3.s1)
return -1;
else
return 0;
}
void operator += (string1& s3){
s1+=s3.s1;
}
};
int main()
{
string1 so,st;
so.read();
st.read();
string1 sth;
sth=so+st;
sth.display();
if((so==st)==1)
cout<<"\n They are equal";
else
cout<<"\n they are not equal";
so+=st;
so.display();
cout<<"\n @22DCS085 Patel Yash C. ";
}
Output:
Code 2:
#include<iostream>
#include<cstring>
using namespace std;
class string1
{
char s1[20];
public:
void read()
{
cout<<"\n add string: ";
cin>>s1;
}
void display()
{
cout<<"\n string: "<<s1<<endl;
}
string1 operator + (string1& s3)
{
string1 t;
char sm[20];
strcpy(sm,s1);
strcpy(t.s1,strcat(sm,s3.s1));
return t;
}
int operator == (string1& s3)
{
if(strcmp(s1,s3.s1)==0)
return 1;
else if(strcmp(s1,s3.s1)<0)
return -1;
else
return 0;
}
DEPSTAR (CSE) Page 108
Object Oriented Programming with C++ [CE144] 22DCS085
int main()
{
string1 so,st;
so.read();
st.read();
//second overloaded operator
if((so==st)==1)
cout<<"\n They are equal";
else
cout<<"\n they are not equal";
//first overloaded operator
string1 sth;
sth=so+st;
sth.display();
//third overloaded operator;
so+=st;
so.display();
cout<<"\n @22DCS085 Patel Yash C. ";
}
Output:
Inputs Outputs
String_1 String_2 Concatenation String_1 Add
and String_2 to
String_2 String_1
equal or not
hello world helloworld Not equal helloworld
use use useuse Equal useuse
PRACTICAL- 7.4
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 1:
#include<iostream>
using namespace std;
class time
{
int h,m;
public:
time(int d)
{
h=d/60;
m=d%60;
}
void display()
{
cout<<"\n Hours: "<<h<<" Minutes: "<<m<<endl;
}
};
int main()
{
int dn;
cout<<"\n add duration: ";
cin>>dn;
time t1=dn;
t1.display();
cout<<"\n @22DCS085 Patel Yash C. ";
}
Output:
Code 2:
#include<iostream>
using namespace std;
class time
{
int h,m;
public:
void operator = (int d)
{
h=d/60;
m=d%60;
}
void display()
{
cout<<"\n Hours: "<<h<<" Minutes: "<<m<<endl;
}
};
int main()
{
int dn;
cout<<"\n add duration: ";
cin>>dn;
time t1;
t1=dn;
t1.display();
cout<<"\n @22DCS085 Patel Yash C. ";
}
Output:
Code 3:
#include<iostream>
using namespace std;
class time
{
int d;
int h,m;
public:
time(int h1,int m1)
{
h=h1;
m=m1;
}
operator int()
{
return h*60+m;
}
void display()
{
time t1(h,m);
(int)t1;
d=t1;
cout<<"\n "<<d;
cout<<"\n @22DCS085 Patel Yash C. ";
}
Output:
Note: we can not convert class type to basic type with using constructor
Main Code 1:
DEPSTAR (CSE) Page 114
Object Oriented Programming with C++ [CE144] 22DCS085
#include<iostream>
using namespace std;
class Celsius
{
float C1,C2;
public:
Celsius (float f)
{
C1=f;
}
void display()
{
cout<<"\n celcius: "<<C1<<endl;
}
};
int main()
{
float f;
cout<<"\n Add f: ";
cin>>f;
Celsius c=f;
c.display();
cout<<"\n @22DCS085 Patel Yash C. ";
}
Output:
Code 2:
#include<iostream>
using namespace std;
class Celsius
{
float C1;
public:
void operator = (float f){
C1=f;
}
void display(){
cout<<"\n celsius "<<C1;
}
};
int main()
{
float c1;
cout<<"\n Add c: ";
cin>>c1;
Celsius c;
c=c1;
c.display();
cout<<"\n @22DCS085 Patel Yash C. ";
}
Output:
Code 3:
DEPSTAR (CSE) Page 116
Object Oriented Programming with C++ [CE144] 22DCS085
#include<iostream>
using namespace std;
class Celsius
{
float temp,C;
public:
Celsius (float c2){
temp=c2;
}
operator float(){
return temp;
}
void display(){
cout<<"\n celcius: "<<temp<<endl;
}
};
int main()
{
float c1;
cout<<"\n Add f: ";
cin>>c1;
Celsius c(c1);
(float)c;
cout<<"\n "<<c;
cout<<"\n @22DCS085 Patel Yash C. ";
}
Output:
PRACTICAL- 7.5
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 1:
#include<iostream>
using namespace std;
class inventory1{
int qty,ino;
float rate,amt;
public:
void read(){
cout<<"\n Add Ino: ";
cin>>ino;
cout<<"\n Add qty: ";
cin>>qty;
cout<<"\n Add rate: ";
cin>>rate;
amt=qty*rate;
}
int getqty(){
return qty;
}
int getino(){
return ino;
}
float getamt(){
amt= qty*rate;
return amt;
}
void display(){
i2=i1;
i2.display();
}
Output:
Code 2:
#include<iostream>
using namespace std;
class fahrenheit
{
float f1;
public:
float getf()
{
return f1;
}
fahrenheit(float c)
{
f1=((9*c)/5)+32;
}
void display()
{
cout<<"\n fahrenheit: "<<f1;
}
};
class celcius
{
float c1;
public:
celcius(float f)
{
c1=((f-32)*5)/9;
}
void display()
{
cout<<"\n Celcius: "<<c1;
}
void operator = (fahrenheit& f11)
{
DEPSTAR (CSE) Page 120
Object Oriented Programming with C++ [CE144] 22DCS085
float temp=f11.getf();
c1=((temp-32)*5)/9;
}
};
int main()
{
float a;
cout<<"\n add celcius: ";
cin>>a;
celcius c1(a);
fahrenheit f1(a);
c1=f1;
c1.display();
f1.display();
cout<<"\n @22DCS085 Patel Yash C." ;
}
Output:
Code 3:
DEPSTAR (CSE) Page 121
Object Oriented Programming with C++ [CE144] 22DCS085
#include<iostream>
using namespace std;
class fahrenheit;
class celcius{
float c1;
public:
celcius(float f) {
c1=((f-32)*5)/9;
}
void display(){
cout<<"\n Celcius: "<<c1;
}
float getc1() {
return c1;
}
};
class fahrenheit
{
float f1;
public:
fahrenheit()
{}
fahrenheit(float c)
{
f1=((9*c)/5)+32;
}
void operator = (celcius c11) {
float t=c11.getc1();
f1=((9*t)/5)+32;
}
void display(){
cout<<"\n fahrenheit: "<<f1;
}
};
int main()
DEPSTAR (CSE) Page 122
Object Oriented Programming with C++ [CE144] 22DCS085
{
float a;
cout<<"\n add celcius: ";
cin>>a;
celcius c1(a);
fahrenheit f1;
f1=c1;
c1.display();
f1.display();
cout<<"\n @22DCS085 Patel Yash C." ;
}
Output: