C Lab Assignment
C Lab Assignment
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.
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.
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.
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.
LAB ASSIGNMENT 2
Solve the following problems by programming. Use C++ as the programming language.
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.
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)
LAB ASSIGNMENT 3
Solve the following problems by programming. Use C++ as the programming language.
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).
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.
Q1. How object can be passed as an argument to a function and what is its significance?
/*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;
}
void show()
{
cout<<a<<endl;
}
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>
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:*/
#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
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*/
}
};
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*/
#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;
}
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();
}
/*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.
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
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.
/* 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:
};
abc ob;
ob.x = 10; /* friend function accessing private data member x of class abc
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>
class abc
private:
int x;
public:
x=x1;
};
cout<<ob1.x;
void main()
clrscr();
abc ob;
ob.set(20);
fun(ob); /*friend function called, passing object as function argument, without object
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:
x=x1;
y=y1;
};
cout<<(ob1.x+ob1.y)/2.0;
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: */
#include<iostream.h>
#include<conio.h>
class complex
int real,imag;
public:
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 ob3;
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 */
LAB ASSIGNMENT 5
Solve the following problems by programming. Use C++ as the programming language.
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.
Q3. What is the use of pointer to object? How class member function is called by pointer to
object?
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.
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.)
LAB ASSIGNMENT 7
Solve the following problems by programming. Use C++ as the programming language.
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.
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 =.
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.
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();
Output:
1 2
1 2
LAB ASSIGNMENT 8
Solve the following problems by programming. Use C++ as the programming language.
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.
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.
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).