Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
120 views

C Lab Assignment

The document outlines 8 lab assignments on object oriented programming concepts for a course. It covers topics like classes, objects, functions, constructors, destructor, function overloading, inheritance, virtual functions, templates, and exception handling. Detailed concepts and problems are provided for each assignment along with sample code solutions for some problems.

Uploaded by

Anurag Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
120 views

C Lab Assignment

The document outlines 8 lab assignments on object oriented programming concepts for a course. It covers topics like classes, objects, functions, constructors, destructor, function overloading, inheritance, virtual functions, templates, and exception handling. Detailed concepts and problems are provided for each assignment along with sample code solutions for some problems.

Uploaded by

Anurag Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

​ ​JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE

​Object Oriented Programming

TABLE OF CONTENTS

LAB ASSIGNMENT 1 2
Concept based on “cout, cin” 2
Concept based on “Functions” 2
Concept based on “Class & Object” (Pass arguments in functions) 2
Viva Questions based on Lab Assignment 1 3

LAB ASSIGNMENT 2 4
Concept based on “Constructor, Destructor” 4
Concept based on “Function Overloading” 4
Viva Questions based on Lab Assignment 2 5

LAB ASSIGNMENT 3 5
Concept based on “Passing objects as Function Arguments” 5
Viva Questions based on Lab Assignment 3 6
Solution to selected problems of Lab Assignment 3 6

LAB ASSIGNMENT 4 13
Concept based on “Friend Function” 13
Viva Questions based on Lab Assignment 4 14
Solution to selected problems of Lab Assignment - 4 14

LAB ASSIGNMENT 5 20
Concept based on “Inheritance and access specifiers (visibility modes)” 21
Concept based on “Pointer to object” 21
Viva Questions based on Lab Assignment 5 21

LAB ASSIGNMENT 6 22
Concept based on “Inheritance” 22
Concept based on “Virtual Function” 22
Concept based on “Pure Virtual Function” 23
Viva Questions based on Lab Assignment 6 23

LAB ASSIGNMENT 7 23
Concept based on “Operator Overloading” 23
Concept based on “Overloading Binary Operators using Friend Function” 24
Viva Questions based on Lab Assignment 7 24
Solution to selected problems of Lab Assignment 7 25
Amit Mithal, Department of Computer Science and Engineering Page no: 1 of 27
​ ​JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
​Object Oriented Programming

LAB ASSIGNMENT 8 26
Concept based on “Template” 26
Concept based on “Exception Handling” 26
Viva Questions based on Lab Assignment 8 27

LAB ASSIGNMENT 1
Solve the following problems by programming. Use C++ as the programming language.

Concept based on “cout, cin”

1a) Print a message.


1b) Add three integers without taking input from the user.
1c) Add three integers taking input from user.

Concept based on “Functions”

1d) Add two numbers using function: don’t pass argument; pass argument.
1e) Find area of rectangle by passing argument to function.
1f) Add two numbers by returning value.

Concept based on “Class & Object” ​(Pass arguments in


functions)

1g) Print a message


1h) Find area of rectangle.
1i) Find area of square.

Amit Mithal, Department of Computer Science and Engineering Page no: 2 of 27


​ ​JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
​Object Oriented Programming

1j) Show that the public members of a class(data and functions) can be accessed both inside
the class and outside the class (i.e in main) but the private members can be accessed inside the
class but NOT outside the class.
1k) Show that a compiler error comes when the member data of a class is ​initialized at the time
of declaration in a class.

1l) Declare a class fruit. Define 2 functions: setprice( ) & showprice ( ) to set the price and show
the price of fruit respectively. Make an object of class fruit as apple and write the main function.
Make the members of class as: Data- price; Functions: setprice(int) and showprice().
1m) Modify the above program for class distance. Data: feet, inches
1n) Modify the above program for class box. Data: l, b, h for 2 objects.

Viva Questions based on Lab Assignment 1

Q1. What is the use of cout and cin?


Q2. Write one instruction in C++ to print the value of a float variable, say v1.
Q3. Write one instruction in C++ to input the value of an integer, say v2.
Q4. Write the syntax to declare, define and call a zero argument function, say f1.

Q5. Write the syntax to declare, define and call a two argument (first an int, then a float)
function, say f2.
Q6. Write C++ instructions to declare a class c, having private data member m and public
member function f. Declare object o of c. Call f using o.

Amit Mithal, Department of Computer Science and Engineering Page no: 3 of 27


​ ​JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
​Object Oriented Programming

LAB ASSIGNMENT 2
Solve the following problems by programming. Use C++ as the programming language.

Concept based on “Constructor, Destructor”

2a) Initialize an integer using default constructor and print its value.
2b) Use parameterized constructors to print the dimensions(length, breadth and height) of two
Box objects using: i) Implicit call ii) Explicit call

2c) Define multiple constructors in a class and hence implement constructor overloading.
2d) Implement destructor.

Concept based on “Function Overloading”

2e) Use function overloading using different number of arguments to add some integers.
2f) Use function overloading to add 2 integers & 2 double numbers.

2g) Display an integer value in a function named f1(int). Display a double value in another
function which has same name as f1(double) using function overloading.
2h) Find the area of rectangle & square using function overloading. (Use different number of
arguments)

Amit Mithal, Department of Computer Science and Engineering Page no: 4 of 27


​ ​JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
​Object Oriented Programming

Viva Questions based on Lab Assignment 2

Q1. Name and explain the features of object oriented programming.


Q1. What is constructor?
Q2. What are the properties of constructor?
Q3. What is destructor?
Q4. What are the properties of destructor?
Q5. Explain default constructor and parameterized constructor.
Q6. What is constructor overloading and function overloading?
Q7. What is polymorphism? How compile time polymorphism is achieved in C++ (using function
overloading)?

LAB ASSIGNMENT 3
Solve the following problems by programming. Use C++ as the programming language.

Concept based on “Passing objects as Function Arguments”


3a) Implement the following situation: ob1.a = 10, ob2.a = 20. Here ob1 and ob2 are objects of
some class abc and a is the private data member of the class. Your program should add the
data member of both the objects and assign the result to the private data of ob1. Member
functions of class abc are set(int), show(), add(abc).

3b) Copy the price of one fruit object into another. Functions: set (int), show ( ), copy (fruit) to set
the price, show the price and copy the price to another object.

3c) Copy the length, breadth, height of one box object of box class into another. Functions:
show ( ), copy (box). Use constructors to initialize the dimensions.

3d) Add the two distances objects d1, d2 (feet, inches) of class distance. Functions: set ( ),
show (), add (distance, distance).

3e) Do question 3c) by returning object from copy (box) function.

Amit Mithal, Department of Computer Science and Engineering Page no: 5 of 27


​ ​JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
​Object Oriented Programming

3f) Do question 3d) by returning object from add (distance, distance).

3g) Do question 3d) by passing only one object argument as add (distance).

3h) Write a program to declare a class with two integers. Read values using member functions.
Pass the object to another member function and display the difference between the two
integers.

Viva Questions based on Lab Assignment 3

Q1. How object can be passed as an argument to a function and what is its significance?

Solution to selected problems of Lab Assignment 3

Concept based on “Passing objects as Function Arguments”

/*3a) Implement the following situation: ob1.a = 10, ob2.a = 20. Here ob1 and ob2 are objects of
some class and a is the private data member of the class. Your program should add the data
member of both the objects and assign the result to the private data of ob1. Member functions
of class abc are set(int), show(), add(abc).

Solution:*/
#include<iostream.h>
#include<conio.h>
class abc
{
private:
int a;
public:
void set(int a1)
{
a=a1;
}

Amit Mithal, Department of Computer Science and Engineering Page no: 6 of 27


​ ​JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
​Object Oriented Programming

void show()
{
cout<<a<<endl;
}

void add(abc ob3) //passing objects as function argument


{
a=a+ob3.a;
/*calling object ob1's a = calling object ob1's a + ob2's a*/
}
};

void main()
{
clrscr();
abc ob1,ob2;
ob1.set(10);
ob2.set(20);
ob1.show();
ob2.show();
ob1.add(ob2);
/*passing objects as function argument,calling object is ob1*/
ob1.show();
getch();
}

/*Output:
10
20
30*/

/*3b) Copy the price of one fruit object into another. Functions: set (int), show ( ), copy (fruit) to
set the price, show the price and copy the price to another object.
Solution:*/

#include<iostream.h>
#include<conio.h>

Amit Mithal, Department of Computer Science and Engineering Page no: 7 of 27


​ ​JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
​Object Oriented Programming

class fruit
{
private:
int price;
public:
void set(int price1)
{
price=price1;
}
void show()
{
cout<<price<<endl;
}
void copy(fruit mango1)
{
price=mango1.price;
}
};

void main()
{
clrscr();
fruit mango,apple;
mango.set(100);
mango.show();
apple.copy(mango); //passing object as function argument
apple.show();
getch();
}

/*Output:
100
100*/

/*3c) Copy the length, breadth, height of one box object of box class into another. Functions:
show ( ), copy (box). Use constructors to initialize the dimensions.
Solution:*/

Amit Mithal, Department of Computer Science and Engineering Page no: 8 of 27


​ ​JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
​Object Oriented Programming

#include<iostream.h>
#include<conio.h>

class box
{
private:
int l,b,h;
public:
box() //default constructor
{
l = 0;
b = 0;
h = 0;
}
box(int l1,int b1,int h1)
{ //constructor is initializing the dimensions of box object
l=l1;
b=b1;
h=h1;
}
void show()
{
cout<<l<<"\t"<<b<<"\t"<<h<<endl;
}
void copy(box ob)
{
l = ob.l;
b = ob.b;
h = ob.h;
/*LHS l,b,h are of calling object's i.e. ob2's l,b,h */
}
};

void main()
{
clrscr();
box ob1(1,2,3); //parameterized constructor called
box ob2; //this will expect calling of default constructor

Amit Mithal, Department of Computer Science and Engineering Page no: 9 of 27


​ ​JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
​Object Oriented Programming

ob1.show();
ob2.show();
ob2.copy(ob1);
ob2.show();
getch();
}

/*Output:
1 2 3
0 0 0
1 2 3*/

/*3d) Add the two distances objects d1, d2 (feet, inches) of class distance. Functions: set ( ),
show (), add (distance, distance).
Solution:*/

#include<iostream.h>
#include<conio.h>

class distance
{
private:
int feet,inches;
public:
void set(int f,int i)
{
feet=f;
inches=i;
}
void show()
{
cout<<feet<<"\t"<<inches<<endl;
}
void add(distance d1,distance d2)
{
feet = d1.feet + d2.feet;
inches = d1.inches + d2.inches;
/*LHS feet and inches is of that object who is calling this add function*/

Amit Mithal, Department of Computer Science and Engineering Page no: 10 of 27


​ ​JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
​Object Oriented Programming

}
};

void main()
{
clrscr();
distance ob1,ob2,ob3;
ob1.set(1,2);
ob2.set(3,4);
ob1.show();
ob2.show();
ob3.add(ob1,ob2);
ob3.show();
getch();
}

/*Output:
1 2
3 4
4 6*/

/*3e) Do question 3c) by returning object from copy (box) function.


3c) Copy the length, breadth, height of one box object of box class into another. Functions:
show ( ), copy (box). Use constructors to initialize the dimensions.
Solution: funarg5.cpp*/

#include<iostream.h>
#include<conio.h>

class box
{
private:
int l,b,h;
public:
box() //default constructor
{
l = 0;
b = 0;

Amit Mithal, Department of Computer Science and Engineering Page no: 11 of 27


​ ​JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
​Object Oriented Programming

h = 0;
}
box(int l1,int b1,int h1)
{ //constructor is initializing the dimensions of box object
l=l1;
b=b1;
h=h1;
}
void show()
{
cout<<l<<"\t"<<b<<"\t"<<h<<endl;
}
box copy(box ob) /*return type of copy function is class, as this function is returning
object of class box*/
{
box ob3;
ob3.l = ob.l;
ob3.b = ob.b;
ob3.h = ob.h;
/*LHS l,b,h are of calling object's i.e. ob3's l,b,h */
return ob3; //object is returned
}
};

void main()
{
clrscr();
box ob1(1,2,3); //parameterized constructor called
box ob2,ob3; //this will expect calling of default constructor
ob1.show();
ob2.show();
ob3 = ob2.copy(ob1); /*copy function is returning an object, so LHS is an object. No
effect on ob2's l,b,h */
ob2.show();
ob3.show();
getch();
}

Amit Mithal, Department of Computer Science and Engineering Page no: 12 of 27


​ ​JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
​Object Oriented Programming

/*Output:
1 2 3
0 0 0
0 0 0
1 2 3*/

LAB ASSIGNMENT 4
Solve the following problems by programming. Use C++ as the programming language.

Concept based on “Friend Function”


4a) Access the private data of a class using friend function without passing object as argument.

4b) Access the private data of a class using friend function by passing object as argument.

4c) Prove that friend function cannot access the private data member directly, but has to use
object name and dot operator to access private data member.

4d) Prove that friend function can be declared either in public or private section of a class.

4e) Create a class abc and find the mean of two numbers using friend function. Member
functions: set (int, int), mean (abc) as a friend function which takes object as argument.

4f) Create a class distance and add two distance objects (feet, inch) using friend function.

4g) Create a class complex to add two complex numbers. Define function add(complex,
complex) as a friend function such that it returns an object of type complex.

4h) Create a class time to add three time objects (hours, min, sec) using friend function. Use
constructors to set the values of all the time objects. You may or may not choose to return the

Amit Mithal, Department of Computer Science and Engineering Page no: 13 of 27


​ ​JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
​Object Oriented Programming

result object from the friend function. You may also choose the signature of friend function to
take 2 or 3 object arguments, depending on your wish.

Viva Questions based on Lab Assignment 4


Q1. What is a friend function?
Q2. What are the properties of friend function?

Solution to selected problems of Lab Assignment - 4

Concept based on “Friend Function”

/* 4a) Access the private data of a class using friend function without passing object as
argument.

Solution: */

#include<iostream.h>

#include<conio.h>

class abc

private:

int x;

public:

friend void fun(); //friend function declared; non-member function

Amit Mithal, Department of Computer Science and Engineering Page no: 14 of 27


​ ​JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
​Object Oriented Programming

};

void fun() //friend function defined, without :: operator

abc ob;

ob.x = 10; /* friend function accessing private data member x of class abc

Must use object to access data member*/

// x=10; will give error- undefined symbol x

cout<<ob.x;

void main()

clrscr();

fun(); //friend function called, without object name and dot operator

getch();

/*Output: 10 */

/* 4b) Access the private data of a class using friend function by passing object as argument.

Solution: */

#include<iostream.h>

#include<conio.h>

Amit Mithal, Department of Computer Science and Engineering Page no: 15 of 27


​ ​JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
​Object Oriented Programming

class abc

private:

int x;

public:

void set(int x1)

x=x1;

friend void fun(abc); //friend function declared; non-member function

};

void fun(abc ob1) //friend function defined, without :: operator

cout<<ob1.x;

/* friend function accessing private data member x of class abc

Must use object to access data member*/

void main()

clrscr();

abc ob;

Amit Mithal, Department of Computer Science and Engineering Page no: 16 of 27


​ ​JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
​Object Oriented Programming

ob.set(20);

fun(ob); /*friend function called, passing object as function argument, without object

name and dot operator */

getch();

/*Output: 20 */

/* 4e) Create a class abc and find the mean of two numbers using friend function. Member
functions: set (int, int), mean (abc) as a friend function which takes object as argument.

Solution: */

#include<iostream.h>

#include<conio.h>

class abc

private:

int x,y;

public:

void set(int x1,int y1)

x=x1;

y=y1;

Amit Mithal, Department of Computer Science and Engineering Page no: 17 of 27


​ ​JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
​Object Oriented Programming

friend void fun(abc); //friend function declared; non-member function

};

void fun(abc ob1) //friend function defined, without :: operator

cout<<(ob1.x+ob1.y)/2.0;

/* friend function accessing private data member x and y of class abc

Must use object to access data member*/

void main()

clrscr();

abc ob;

ob.set(3,4);

fun(ob); //friend function called, without object name and dot operator

getch();

/*Output: 3.5 */

/* 4g) Create a class complex to add two complex numbers. Define function add(complex,
complex) as a friend function such that it returns an object of type complex.

Solution: */

Amit Mithal, Department of Computer Science and Engineering Page no: 18 of 27


​ ​JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
​Object Oriented Programming

#include<iostream.h>

#include<conio.h>

class complex

int real,imag;

public:

void set(int real1,int imag1)

real=real1;

imag=imag1;

void show()

cout<<real<<"\t"<<imag<<endl;

friend complex add(complex,complex); /*friend function will take 2 object arguments and
will return an object of class type as complex */

};

complex add(complex ob1,complex ob2)

complex ob3;

ob3.real = ob1.real + ob2.real;

Amit Mithal, Department of Computer Science and Engineering Page no: 19 of 27


​ ​JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
​Object Oriented Programming

ob3.imag = ob1.imag + ob2.imag;

return ob3; //object is returning from function add

void main()

clrscr();

complex ob1,ob2,ob3;

ob1.set(1,2);

ob2.set(3,4);

ob1.show();

ob2.show();

ob3 = add(ob1,ob2);

ob3.show();

getch();

/* Output:

1 2

3 4

4 6 */

Amit Mithal, Department of Computer Science and Engineering Page no: 20 of 27


​ ​JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
​Object Oriented Programming

LAB ASSIGNMENT 5
Solve the following problems by programming. Use C++ as the programming language.

Concept based on “Inheritance and access specifiers (visibility


modes)”
5a) Create a class ‘a’. Define a member function fun of class a to print the message “hello”.
Derive a class ‘b’ from class a. Inside main function, use the object of class b to call the member
function fun of class a and hence prove single inheritance.

5b) Prove multiple inheritance.

5c) Justify multilevel inheritance.

5d) Use hierarchical inheritance to derive class b, c and d from class a using public, private and
protected inheritance respectively. What are the differences observed?

5e) Show that a protected class member can be accessed in immediately derived class.

Concept based on “Pointer to object”

5f) Call the member function of a class using pointer to object.

Viva Questions based on Lab Assignment 5

Q1. Define inheritance and types of inheritance.


Q2. Differentiate between public, private and protected as i) class members ii) way to derive one
class from another.

Q3. What is the use of pointer to object? How class member function is called by pointer to
object?

Amit Mithal, Department of Computer Science and Engineering Page no: 21 of 27


​ ​JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
​Object Oriented Programming

LAB ASSIGNMENT 6
Concept based on “Inheritance”
6a) Assume that a bank maintains two kinds of accounts for customers, one called as savings
account and the other as current account. The savings account provides compound interest and
withdrawal facilities but no cheque book facility. The current account provides cheque book
facility but no interest. Current account holders should also maintain a minimum balance and if
the balance falls below this level, a service charge is imposed.

Create a class account that stores customer name, account number and type of account. From
this, derive the classes cur_acct and sav_acct to make them more specific to their requirements.
Include necessary member functions in order to achieve the following tasks:
i) Accept deposit from a customer and update the balance.
ii) Display the balance.
iii) Compute and deposit interest.
iv) Permit withdrawal and update the balance.
v) Check for the minimum balance, impose penalty, necessary, and update the
balance.

Do not use any constructors. Use member functions to initialize the class members.

Concept based on “Virtual Function”


6b) Create a base class called shape. Use this class to store two double type values that could
be used to compute the area of figures. Derive two specific classes called triangle and rectangle
from the base class shape. Add to the base class, a member function get_data( ) to initialize
base class data members and another member function display_area( ) to compute and display
the area of figures. Make display area( ) as a virtual function and redefine this function in the
derived classes to suit their requirements.

Using these three classes, design a program that will accept dimensions of a triangle or a
rectangle interactively, and display the area. Remember the two values given as input will be
treated as lengths of two sides in the case of rectangles, and as base and height in the case of
triangles.

6c) Extend the above program to display the area of circles. This requires addition of a new
derived class ‘circle’ that computes the area of a circle. Remember, for a circle we need only
Amit Mithal, Department of Computer Science and Engineering Page no: 22 of 27
​ ​JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
​Object Oriented Programming

one value, its radius, but the get_data( ) function in the base class requires two values to be
passed. (Hint: Make the second argument of get_data( ) function as a default one with zero
value.)

Concept based on “Pure Virtual Function”


6d) Run the above program with the following modifications:
i) Remove the definition of display_area( ) from one of the derived classes.
ii) In addition to the above change, declare the display_area( ) as virtual in the base
class shape.

Comment on the output in each case.

Viva Questions based on Lab Assignment 6

Q1. What do you mean by virtual function?


Q2. What do you mean by pure virtual function?

LAB ASSIGNMENT 7
Solve the following problems by programming. Use C++ as the programming language.

Concept based on “Operator Overloading”


7a) Overload unary operator ++.

7b) Overload unary operator --.

7c) Create a class distance. Make data members as: feet, inches. Define functions as set (int,
int), show ( ). Make 2 objects of class distance. Set and show the values of data members in set
(int, int) and show ( ) functions respectively. Now overload the binary operator + to add these 2
objects using the concept of operator overloading.

7d) Modify the above question 7c) to add two complex numbers.

Amit Mithal, Department of Computer Science and Engineering Page no: 23 of 27


​ ​JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
​Object Oriented Programming

7e) Add two time objects in the format: hrs, min, sec.

7f) Create a class point and specify x & y co-ordinates. Make two objects of class point pt1 (x1,
y1), pt2 (x2, y2). Your program should make the co-ordinates of pt1 equal to pt2 on specifying
the statement pt2 = pt1 by overloading the binary operator =.

7g) Overload + operator to add two matrices of size m*n.

7h) Concatenate 2 strings.

Concept based on “Overloading Binary Operators using Friend


Function”
Suppose A, B and C are objects of the same class. Perform the following operations by
overloading binary operator using friend function:

7i) A = B + 5;

7j) A = 5 * B;

7k) A = B + C;

7l) Overload the overloaded operator function - in the following manner. Here abc is the name of
class.

friend abc operator-(int,abc);

friend abc operator-(abc,int);

friend abc operator-(abc,abc);

Viva Questions based on Lab Assignment 7


Q1. Define operator overloading.
Q2. What are the rules for operator overloading?
Q3. What are the operators which cannot be overloaded.
Q4. What do you mean by unary, binary and ternary operators and give their examples.

Amit Mithal, Department of Computer Science and Engineering Page no: 24 of 27


​ ​JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
​Object Oriented Programming

Q5. What is the syntax of unary and binary operator overloading?


Q6. Which feature of C++ does operator overloading supports and how?
Q7. How friend function can be used to overload binary operators?

Solution to selected problems of Lab Assignment 7


7f) Create a class point and specify x & y co-ordinates. Make two objects of class point pt1 (x1,
y1), pt2 (x2, y2). Your program should make the co-ordinates of pt1 equal to pt2 on specifying
the statement pt2 = pt1 by overloading the binary operator =.

Solution:
class point
{
int x,y;
void set(int x1,int y1)
{
x = x1;
y = y1;
}
void show()
{
cout<<x<<”\t”<<y<<endl;
}
void operator +(point pt) //overloaded operator function
{
x = pt.x;
y = pt.y;
}
};
void main()
{
clrscr();
point pt1,pt2;
pt1.set(1,2);
pt1.show();
pt2 = pt1; //means pt2.=(pt1); This will call overloaded operator function
pt2.show();
getch();

Amit Mithal, Department of Computer Science and Engineering Page no: 25 of 27


​ ​JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
​Object Oriented Programming

Output:
1 2
1 2

LAB ASSIGNMENT 8
Solve the following problems by programming. Use C++ as the programming language.

Concept based on “Template”


8a) Define a function template to add two numbers. The numbers being 2 integers at the first
function call and 2 floats when the function is called for the second time.

8b) Define a function template to swap two numbers. The numbers being 2 integers at the first
function call and 2 floats when the function is called for the second time.

8c) Define a function template to print an array of 5 numbers. When the function template is
called for the first time, an integer array of size 5 is passed in the function call. When the
function template is called for the second time, a float array of size 5 is passed in the function
call.

8d) There are three function calls for a single function having one argument. An int, a float and a
character is passed during the consecutive function calls. Create a function template to display
the argument passed each time.

8e) A function, having two arguments, is called 3 times. The arguments that are passed are two
integers, two floats and two characters during each consecutive call. Design a function template
to determine the greater among the two arguments for each function call.

Concept based on “Exception Handling”


8f) Handle the exception “divide by zero” using try, throw, catch keywords of exception handling
mechanism.

Amit Mithal, Department of Computer Science and Engineering Page no: 26 of 27


​ ​JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
​Object Oriented Programming

8g) Show how an exception can be thrown by a function that is invoked from within a try block.

8h) Show how multiple catch statements are used to handle various types of exceptions.

8i) Catch and handle various types of exceptions using catch(…)

8j) Show how an exception can be rethrown and caught.

8k) Show how to restrict a function to throw only an int type of exception.

8l) Show how to prevent a function from throwing any exception (throw nil exception).

Viva Questions based on Lab Assignment 8


Q1. What is template, function template and what are their advantages?
Q2. Name and explain the various types of errors.
Q3. Name and explain the various types of exceptions.
Q3. How various types of exceptions can be caught and handled?
Q4. How an exception can be rethrown and caught?
Q5. How can we restrict a function to throw only certain exception or nil exception?

Amit Mithal, Department of Computer Science and Engineering Page no: 27 of 27

You might also like