0% found this document useful (0 votes)
150 views123 pages

Charotar University of Science & Technology & Devang Patel Institute of Advance Technology & Reserch CE144 Object Oriented Programming With C++

This document contains code snippets and output related to object oriented programming concepts in C++. It includes examples of using structs and classes to define a College_Details structure with attributes like college name, code, department, and intake. Code is provided to read data into struct and class objects from the keyboard and display the stored values. Additional examples demonstrate using class member functions to encapsulate the read and print logic. The document is formatted as a series of code examples with corresponding output for learning and practicing OOP concepts in C++.

Uploaded by

Agent minecraft
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
150 views123 pages

Charotar University of Science & Technology & Devang Patel Institute of Advance Technology & Reserch CE144 Object Oriented Programming With C++

This document contains code snippets and output related to object oriented programming concepts in C++. It includes examples of using structs and classes to define a College_Details structure with attributes like college name, code, department, and intake. Code is provided to read data into struct and class objects from the keyboard and display the stored values. Additional examples demonstrate using class member functions to encapsulate the read and print logic. The document is formatted as a series of code examples with corresponding output for learning and practicing OOP concepts in C++.

Uploaded by

Agent minecraft
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 123

Object Oriented Programming with C++ [CE144] 22DCS085

CHAROTAR UNIVERSITY OF SCIENCE & TECHNOLOGY


&
DEVANG PATEL INSTITUTE OF ADVANCE TECHNOLOGY &
RESERCH

CE144 Object Oriented Programming With C++

DEPSTAR (CSE) Page 1


Object Oriented Programming with C++ [CE144] 22DCS085

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<<"*";

cout<<"\n*\t Programming Assignment 1\t*";


cout<<"\n*\t Computer Programming \t\t*";
cout<<"\n*\t \tAuthor: ?\t\t*";
cout<<"\n*\t Due Date: Thursday, Dec 20\t*\n";
for (int i=1;i<=41;i++)
cout<<"*";
cout<<"\n@22dcs085";
cout<<"Patel Yash C.";
}

DEPSTAR (CSE) Page 2


Object Oriented Programming with C++ [CE144] 22DCS085

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++

DEPSTAR (CSE) Page 3


Object Oriented Programming with C++ [CE144] 22DCS085

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;
}

DEPSTAR (CSE) Page 4


Object Oriented Programming with C++ [CE144] 22DCS085

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.

DEPSTAR (CSE) Page 5


Object Oriented Programming with C++ [CE144] 22DCS085

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:

Sr. no Input 1 Input 2 Functions results


1 9.67 5.78 Fixed 15.45

2 9.67 5.78 scientific 1.55e+01

3 9.67 5.78 Setprecision(2) 15

DEPSTAR (CSE) Page 7


Object Oriented Programming with C++ [CE144] 22DCS085

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:

DEPSTAR (CSE) Page 8


Object Oriented Programming with C++ [CE144] 22DCS085

Sr. no Input 1 Input 2 Functions results


1 2.39 4.40 Scientific 6.79e+00

2 2.39 4.40 fixed 6.79

3 2.39 4.40 Setprecision(2) 6.8

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 .

Student’s Signature & Marks out of 30 Faculty Signature &


Date Date

DEPSTAR (CSE) Page 9


Object Oriented Programming with C++ [CE144] 22DCS085

Set-2
PRACTICAL-2.1

AIM: Write a C++ Program to declare the struct named College_Details, by


taking following data members:
char college_name[10]; (eg. CHARUSAT)
char college_code[10]; (eg. CSPIT/DEPSTAR)
char deparment[5]; (eg. CE/CS/IT)
int intake; (eg. 120)
Collect the data from keyboard and display the same in appropriate
view.

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):

DEPSTAR (CSE) Page 11


Object Oriented Programming with C++ [CE144] 22DCS085

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.";
}

DEPSTAR (CSE) Page 12


Object Oriented Programming with C++ [CE144] 22DCS085

Output-2(class):

DEPSTAR (CSE) Page 13


Object Oriented Programming with C++ [CE144] 22DCS085

Code-3(class with functions):


#include<iostream>
#include<iomanip>
using namespace std;
class student
{
public:
char college_name[10],college_code[10],department[5];
int intake;
void read()
{
cout<<"\n+++++ Enter the College Information +++++";
cout<<"\nName of the college: ";
cin>>college_name;
cout<<"College Code: ";
cin>>college_code;
cout<<"Department: ";
cin>>department;
cout<<"Department In-take: ";
cin>>intake;
}
void print()
{
cout<<"\n\n********College information********";
cout<<"\n\nName of the college: "<<college_name;
cout<<"\nCollege University code: "<<college_code;
cout<<"\nName of Department:"<<department;
cout<<"\nThe department of"<<department<<"has in-take:"<<intake;
}
}s1 ;
int main()
{

DEPSTAR (CSE) Page 14


Object Oriented Programming with C++ [CE144] 22DCS085

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;
}

DEPSTAR (CSE) Page 16


Object Oriented Programming with C++ [CE144] 22DCS085

}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

Sr.no Name Roll No class Division(A/B)


DEPSTAR (CSE) Page 18
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.

DEPSTAR (CSE) Page 19


Object Oriented Programming with C++ [CE144] 22DCS085

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:

DEPSTAR (CSE) Page 21


Object Oriented Programming with C++ [CE144] 22DCS085

Sr. No Outcome Variable_1 Variable_2


value value
1 Before Swapping 3 4

After Swapping 4 3

2 Before Swapping 5 98

After Swapping 98 5

3 Before Swapping 67 3

After Swapping 3 67

Student’s Signature & Marks out of 30 Faculty Signature &


Date Date

DEPSTAR (CSE) Page 22


Object Oriented Programming with C++ [CE144] 22DCS085

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

DEPSTAR (CSE) Page 23


Object Oriented Programming with C++ [CE144] 22DCS085

int & c = NULL;


//you cannot assign a null value to the reference variable
int & d[2] = {no1,no2};
//you cannot assign a variable to the reference variable
cout<<"x = "<< x+20;
cout<<"no1="<< no1+10;
return 0;
}

Output(after solving errprs):

Sr. No Questions Output Remarks


1 Can we declare an array of Error We can’t declare an
references? array of references
2 Can we assign NULL value Error We can’t assign a
to reference variable? null value to
reference variable
3 Is Reference variable a NO Reference variable is
pointer variable? not a pointer
variable
4 Can we declare a reference Error We have to assign a
variable without initializing value to reference
it? variable
5 Does Reference Variable Yes It can change the
change the original value of value of real variable
variable?

DEPSTAR (CSE) Page 24


Object Oriented Programming with C++ [CE144] 22DCS085

PRACTICAL-3.2

AIM: Find output of the following 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:

DEPSTAR (CSE) Page 25


Object Oriented Programming with C++ [CE144] 22DCS085

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.

DEPSTAR (CSE) Page 26


Object Oriented Programming with C++ [CE144] 22DCS085

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.";
}

DEPSTAR (CSE) Page 27


Object Oriented Programming with C++ [CE144] 22DCS085

Output:

Size of Array: 5 6

Array Elements: 12345 78 89 45 56 24 12

After adding two to 34567 80 91 47 58 26 14


elements:

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.

2. State two points on delete operator.


Ans:
 Delete can be used by either using Delete operator or Delete [] operator.
 Delete operator deallocates memory from heap.
 The delete operator has void return type does not return a value.

Student’s Signature & Marks out of 30 Faculty Signature &


Date Date

DEPSTAR (CSE) Page 28


Object Oriented Programming with C++ [CE144] 22DCS085

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;

DEPSTAR (CSE) Page 29


Object Oriented Programming with C++ [CE144] 22DCS085

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.

DEPSTAR (CSE) Page 30


Object Oriented Programming with C++ [CE144] 22DCS085

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;

DEPSTAR (CSE) Page 31


Object Oriented Programming with C++ [CE144] 22DCS085

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:

DEPSTAR (CSE) Page 32


Object Oriented Programming with C++ [CE144] 22DCS085

Display Input Output


Input two numbers to Number1=56 Divisible
check if it is divisible or Number2=7
not
Input a number to check Number=56 Non-Prime
if it is prime or not
Enter three float FNum1=3.3 Average=3.7
numbers to get average FNum2=3.3
of them
FNum3=4.5

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

DEPSTAR (CSE) Page 33


Object Oriented Programming with C++ [CE144] 22DCS085

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:

Display Inputs Larger Number Output


Enter two Number1= 3 Number2 Number1 = 3
numbers Number2= 67 Number2= 100

DEPSTAR (CSE) Page 35


Object Oriented Programming with C++ [CE144] 22DCS085

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.";
}

DEPSTAR (CSE) Page 36


Object Oriented Programming with C++ [CE144] 22DCS085

Output:

Display Inputs Larger Number Output


Enter two Number1= 56 Number2 Number1 = 56
numbers Number2=89 Number2= 100

DEPSTAR (CSE) Page 37


Object Oriented Programming with C++ [CE144] 22DCS085

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.";
}

DEPSTAR (CSE) Page 38


Object Oriented Programming with C++ [CE144] 22DCS085

Output:

Display Inputs Larger Number Output


Enter two Number1= 4 Number2 Number1 = 4
numbers Number2= 5 Number2= 100

DEPSTAR (CSE) Page 39


Object Oriented Programming with C++ [CE144] 22DCS085

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.";
}

DEPSTAR (CSE) Page 40


Object Oriented Programming with C++ [CE144] 22DCS085

Output:

Display Inputs Larger Number Output


Enter two Number1= 67 Number1 Number1 = 100
numbers Number2=2 Number2= 2

DEPSTAR (CSE) Page 41


Object Oriented Programming with C++ [CE144] 22DCS085

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.";
}

DEPSTAR (CSE) Page 42


Object Oriented Programming with C++ [CE144] 22DCS085

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.

DEPSTAR (CSE) Page 43


Object Oriented Programming with C++ [CE144] 22DCS085

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";
}

DEPSTAR (CSE) Page 44


Object Oriented Programming with C++ [CE144] 22DCS085

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:

DEPSTAR (CSE) Page 46


Object Oriented Programming with C++ [CE144] 22DCS085

Sr. No Input Output


Enter Base Enter Power Result
1. 12 3 1728

2. 12 - 144

Code 3:

DEPSTAR (CSE) Page 47


Object Oriented Programming with C++ [CE144] 22DCS085

//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

With explicit inline function (2.372s)

Without explicit inline function (2.870 s)

Sr. No Input Output


Enter Base Enter Power Result
1. 4 3 64

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

Student’s Signature & Marks out of 30 Faculty Signature &


Date Date

SET-5
DEPSTAR (CSE) Page 49
Object Oriented Programming with C++ [CE144] 22DCS085

PRACTICAL-5.1

AIM: Write a C program defining 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 the rectangle.
Also create a C++ Class for the above program. Define the data members and
both functions inside the class. Get the area of the rectangle as an output.

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:

DEPSTAR (CSE) Page 50


Object Oriented Programming with C++ [CE144] 22DCS085

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:

DEPSTAR (CSE) Page 52


Object Oriented Programming with C++ [CE144] 22DCS085

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

DEPSTAR (CSE) Page 53


Object Oriented Programming with C++ [CE144] 22DCS085

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
{
char bm_name[20];
int bcode,innings,not_out,runs;
float b_average;
void calcavg();
public:
void getdata();
void putdata();
};

inline void Batsman::calcavg()


{
b_average=runs/(innings-not_out);
cout<<"Batting average: "<<b_average<<endl;
}

inline void Batsman:: getdata()

DEPSTAR (CSE) Page 54


Object Oriented Programming with C++ [CE144] 22DCS085

{
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:

DEPSTAR (CSE) Page 55


Object Oriented Programming with C++ [CE144] 22DCS085

parameters Inputs Output


(Batting Average)
Name Yash
Bcode 85
Total innings 100 6666
Enter not_out_timings 97
Enter total runs 20000

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.";
}

DEPSTAR (CSE) Page 57


Object Oriented Programming with C++ [CE144] 22DCS085

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:

DEPSTAR (CSE) Page 58


Object Oriented Programming with C++ [CE144] 22DCS085

#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;

DEPSTAR (CSE) Page 60


Object Oriented Programming with C++ [CE144] 22DCS085

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

Code 6(main 2):


#include<iostream>
using namespace std;

DEPSTAR (CSE) Page 63


Object Oriented Programming with C++ [CE144] 22DCS085

//return an object by reference


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 64
Object Oriented Programming with C++ [CE144] 22DCS085

{
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;

DEPSTAR (CSE) Page 67


Object Oriented Programming with C++ [CE144] 22DCS085

d1.read();
d1.display();
cout<<"\n add scale factor: ";
cin>>s;
Dist d2;
d2.scale(d1,s);
cout<<"\n@22DCS085 Patel Yash C.";
}

Output:

Feet Inches Scale factor Output


6 2 2 12’ 4”
3 5 0 0’ 0”
7 0 3 21’ 0”

PRACTICAL-5.5

DEPSTAR (CSE) Page 68


Object Oriented Programming with C++ [CE144] 22DCS085

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()
{

DEPSTAR (CSE) Page 69


Object Oriented Programming with C++ [CE144] 22DCS085

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

using namespace std;


class demo
{
public:
int a;
static int b;
static 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.";
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()

DEPSTAR (CSE) Page 73


Object Oriented Programming with C++ [CE144] 22DCS085

{
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:

DEPSTAR (CSE) Page 74


Object Oriented Programming with C++ [CE144] 22DCS085

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

DEPSTAR (CSE) Page 75


Object Oriented Programming with C++ [CE144] 22DCS085

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;
}
};

DEPSTAR (CSE) Page 76


Object Oriented Programming with C++ [CE144] 22DCS085

int main()
{
demo d;
demo1 d1;
d.read();
d1.display(d);
cout<<”\n @22DCS085 Patel Yash C.”;
}

Output:

Code 2:

DEPSTAR (CSE) Page 77


Object Oriented Programming with C++ [CE144] 22DCS085

#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 ";

cout<<"\n Date 1: "<<d1.d<<"/"<<d1.m<<"/"<<d1.y;


DEPSTAR (CSE) Page 78
Object Oriented Programming with C++ [CE144] 22DCS085

cout<<"\n Date 2: "<<d2.d<<"/"<<d2.m<<"/"<<d2.y;


d3.d=d1.d;
d1.d=d2.d;
d2.d=d3.d;

d3.m=d1.m;
d1.m=d2.m;
d2.m=d3.m;

d3.y=d1.y;
d1.y=d2.y;
d2.y=d3.y;

cout<<"\n After swaping ";


cout<<"\n Date 1: "<<d1.d<<"/"<<d1.m<<"/"<<d1.y;
cout<<"\n Date 2: "<<d2.d<<"/"<<d2.m<<"/"<<d2.y;
}
};

int main()
{
date d1,d2;
date1 d3;
d1.read();
d2.read();
d3.swap1(d1,d2);

cout<<"\n @22DCS085 Patel Yash C.";


}

Output:
DEPSTAR (CSE) Page 79
Object Oriented Programming with C++ [CE144] 22DCS085

Sr. no. Date Month Year Before After


swapping swapping
1 12 3 2009 12/3/2009 13/5/1998
2 13 5 1998 13/5/1998 13/5/1998

PRACTICAL-5.7

DEPSTAR (CSE) Page 80


Object Oriented Programming with C++ [CE144] 22DCS085

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:

Input for land Input for tiles output


length width length width Area of No of
land required
tiles
100 200 10 20 20000 100
45 45 5 5 2025 81

PRACTICAL-5.8

DEPSTAR (CSE) Page 83


Object Oriented Programming with C++ [CE144] 22DCS085

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

DEPSTAR (CSE) Page 84


Object Oriented Programming with C++ [CE144] 22DCS085

{
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:

DEPSTAR (CSE) Page 85


Object Oriented Programming with C++ [CE144] 22DCS085

Input Output
Name Gender Name Gender
Arya Female Arya Female
Yash Male Yash Male

Student’s Signature & Marks out of 30 Faculty Signature &


Date Date

PRACTICAL-6.1

DEPSTAR (CSE) Page 86


Object Oriented Programming with C++ [CE144] 22DCS085

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>

DEPSTAR (CSE) Page 88


Object Oriented Programming with C++ [CE144] 22DCS085

using namespace std;


class demo{
public:
int a,b;
demo (){}
demo(int a1,int a2){
a=a1;
b=a2;
}
demo( demo d1){
a=d1.a;
}
};

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

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;
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 4:
#include<iostream>
using namespace std;

DEPSTAR (CSE) Page 90


Object Oriented Programming with C++ [CE144] 22DCS085

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:

Result for inputs outputs


constructor

DEPSTAR (CSE) Page 94


Object Oriented Programming with C++ [CE144] 22DCS085

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.

Student’s Signature & Marks out of 30 Faculty Signature &


Date Date

DEPSTAR (CSE) Page 95


Object Oriented Programming with C++ [CE144] 22DCS085

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)
{

DEPSTAR (CSE) Page 96


Object Oriented Programming with C++ [CE144] 22DCS085

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:

DEPSTAR (CSE) Page 98


Object Oriented Programming with C++ [CE144] 22DCS085

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

DEPSTAR (CSE) Page 101


Object Oriented Programming with C++ [CE144] 22DCS085

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:

DEPSTAR (CSE) Page 103


Object Oriented Programming with C++ [CE144] 22DCS085

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. ";
}

DEPSTAR (CSE) Page 104


Object Oriented Programming with C++ [CE144] 22DCS085

Output:

Real Number Imaginary Complex Number


Number -C1 C1
56 34 56 -34i 56+34i
85 29 85-29i 85+29i

DEPSTAR (CSE) Page 105


Object Oriented Programming with C++ [CE144] 22DCS085

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;

DEPSTAR (CSE) Page 106


Object Oriented Programming with C++ [CE144] 22DCS085

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:

DEPSTAR (CSE) Page 107


Object Oriented Programming with C++ [CE144] 22DCS085

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

void operator += (string1& s3)


{
strcat(s1,s3.s1);
}
};

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. ";
}

DEPSTAR (CSE) Page 109


Object Oriented Programming with C++ [CE144] 22DCS085

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

DEPSTAR (CSE) Page 110


Object Oriented Programming with C++ [CE144] 22DCS085

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. ";
}

DEPSTAR (CSE) Page 111


Object Oriented Programming with C++ [CE144] 22DCS085

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;

DEPSTAR (CSE) Page 112


Object Oriented Programming with C++ [CE144] 22DCS085

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()
{

DEPSTAR (CSE) Page 113


Object Oriented Programming with C++ [CE144] 22DCS085

cout<<"\n Duration: "<<d<<endl;


}
};
int main()
{
int h,m,d;
cout<<"\n add hours and minutes: ";
cin>>h>>m;

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:

DEPSTAR (CSE) Page 115


Object Oriented Programming with C++ [CE144] 22DCS085

#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

DEPSTAR (CSE) Page 117


Object Oriented Programming with C++ [CE144] 22DCS085

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(){

DEPSTAR (CSE) Page 118


Object Oriented Programming with C++ [CE144] 22DCS085

cout<<"\n inventory no: "<<ino<<" qty: "<<qty<<" amt: "<<amt<<endl;


}
};
class inventory2{
int ino;
float rate,amt;
public:
void operator = (inventory1 i1){
amt=i1.getamt();
ino=i1.getino();
}
void display(){
cout<<"\n inventory no: "<<ino<<" amt: "<<amt<<endl;
}
};
int main(){
inventory1 i1;
inventory2 i2;
i1.read();
i1.display();

i2=i1;
i2.display();
}
Output:

Code 2:

DEPSTAR (CSE) Page 119


Object Oriented Programming with C++ [CE144] 22DCS085

#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:

Student’s Signature & Marks out of 30 Faculty Signature &


Date Date

DEPSTAR (CSE) Page 123

You might also like