FTL10 Experiment Wise Manual
FTL10 Experiment Wise Manual
List of Experiments:
Exercise -1 (Classes Objects) Create a Distance class with: •feet and inches as data members •member function to
input distance •member function to output distance •member function to add two distance objects
1. Write a main function to create objects of DISTANCE class. Input two distances and output the sum.
2. Write a C++ Program to illustrate the use of Constructors and Destructors (use the above program.)
3. Write a program for illustrating function overloading in adding the distance between objects (use the above
problem)
Exercise – 2 (Access) Write a program for illustrating Access Specifiers public, private, protected 1. Write a
program implementing Friend Function
2. Write a program to illustrate this pointer
3. Write a Program to illustrate pointer to a class
Exercise -4 (Inheritance)
1. Write C++ Programs and incorporating various forms of Inheritance i) Single Inheritance ii) Hierarchical
Inheritance iii) Multiple Inheritances iv) Multi-level inheritance v) Hybrid inheritance 2. Also illustrate the order
of execution of constructors and destructors in inheritance
Exercise -5(Templates, Exception Handling)
1. a)Write a C++ Program to illustrate template class
2. b)Write a Program to illustrate member function templates
3. c) Write a Program for Exception Handling Divide by zero
4. d)Write a Program to rethrow an Exception
Exercise -6
1. Write a C++ program illustrating user defined string processing functions using pointers (string length, string
copy, string concatenation)
2. Write a C++ program illustrating Virtual classes & virtual functions.
3. Write C++ program that implement Bubble sort, to sort a given list of integers in ascending order
Exercise -1 (Classes Objects)
Create a Distance class with:
• feet and inches as data members
• member function to input distance
• member function to output distance
• member function to add two distance objects
1. Write a main function to create objects of DISTANCE class. Input two distances and output the sum.
#include<iostream.h>
#include<conio.h>
class Distance
private:
int feet;
int inches;
public:
void input_distance()
cout<<"Feets : ";
cin>>feet;
cout<<"Inches :";
cin>>inches;
}
void output_distance()
}
void add(Distance d1,Distance d2)
{
feet=d1.feet+d2.feet;
inches=d1.inches+d2.inches;
if(inches>=12)
{
feet=feet+(inches/12);
inches=inches%12;
}
}
};
int main()
{
Distance d1,d2,d3;
clrscr();
d1.input_distance();
d2.input_distance();
d3.add(d1,d2);
d3.output_distance();
getch();
return 0;
Output:
2. Write a C++ Program to illustrate the use of Constructors and Destructors (use the above program.)
#include<iostream.h>
#include<conio.h>
class Distance
private:
int feet;
int inches;
public:
Distance(){}
Distance(int f,int i)
feet=f;
inches=i;
void get_distance()
feet=d1.feet+d2.feet;
inches=d1.inches+d2.inches;
if(inches>=12)
feet=feet+(inches/12);
inches=inches%12;
}
}
~Distance()
};
int main()
int f1,i1,f2,i2;
clrscr();
cout<<"Distance 1 "<<endl;
cout<<"Feets : ";
cin>>f1;
cout<<"Inches : ";
cin>>i1;
cout<<"Distance 2 "<<endl;
cout<<"Feets : ";
cin>>f2;
cout<<"Inches : ";
cin>>i2;
Distance d1(f1,i1);
Distance d2(f2,i2);
Distance d3;
d3.add(d1,d2);
d3.get_distance();
getch();
return 0;
}
Output:
3. Write a program for illustrating function overloading in adding the distance between objects (use the
above problem)
#include<iostream.h>
#include<conio.h>
class Distance
private:
int feet;
int inches;
public:
void set_distance()
cout<<"Feets : ";
cin>>feet;
cout<<"Inches : ";
cin>>inches;
void get_distance()
feet=d1.feet+d2.feet;
inches=d1.inches+d2.inches;
if(inches>=12)
feet=feet+(inches/12);
inches=inches%12;
feet=d1->feet+d2->feet;
inches=d1->inches+d2->inches;
if(inches>=12)
feet=feet+(inches/12);
inches=inches%12;
};
int main()
Distance d1,d2,d3;
clrscr();
cout<<"Distance 1 "<<endl;
d1.set_distance();
cout<<"Distance 2 "<<endl;
d2.set_distance();
d3.add(d1,d2);
d3.get_distance();
d3.add(&d1,&d2);
d3.get_distance();
getch();
return 0;
Output:
Exercise – 2 (Access)
#include<iostream>
class Box
private:
double length;
protected:
double width;
public:
double height;
void inputdata();
};
void Box::inputdata()
cin>>length;
cin>>width;
cin>>height;
}
double volume(Box b)
return b.length*b.width*b.height;
int main()
Box b;
b.inputdata();
cout<<"Volume = "<<volume(b)<<endl;
return 0;
Output:
2. Write a program to illustrate this pointer
#include<iostream>
class Box
private:
double length;
protected:
double width;
public:
double height;
this->length=length;
this->width=width;
this->height=height;
double volume()
return length*width*height;
};
int main()
double length,width,height;
cout<<"Enter length : \n";
cin>>length;
cin>>width;
cin>>height;
Box b(length,width,height);
cout<<"Volume = "<<b.volume()<<endl;
return 0;
Output:
3. Write a Program to illustrate pointer to a class
#include<iostream>
class Box
private:
double length;
protected:
double breadth;
public:
double height;
length=l;
breadth=b;
height=h;
double volume()
return length*breadth*height;
};
int main()
{
double length,breadth,height;
cin>>length;
cin>>breadth;
cin>>height;
Box b(length,breadth,height),*ptr;
ptr=&b;
return 0;
Output:
Exercise -3 (Operator Overloading)
Source Code:
//Unary operator as member function
#include<iostream>
using namespace std;
class Unary
{
private:
int num1,num2;
public:
Unary(int n1,int n2)
{
num1=n1;
num2=n2;
}
void display()
{
cout<<"num1 = "<<num1<<endl;
cout<<"num2 = "<<num2<<endl;
}
void operator ++()
{
++num1;
++num2;
}
};
int main()
{
int n1,n2;
cout<<"Enter num1 : ";
cin>>n1;
cout<<"Enter num2 : ";
cin>>n2;
Unary u(n1,n2);
cout<<"Before increment numbers are "<<endl;
u.display();
++u;
cout<<"Afterincrement numbers are "<<endl;
u.display();
return 0;
}
Output:
//Binary operator as non member function
#include<iostream>
using namespace std;
class Complex
{
private:
float real;
float imaginary;
public:
Complex(){}
Complex(float r,float i)
{
real=r;
imaginary=i;
}
void display()
{
cout<<real<<"+i"<<imaginary<<endl;
}
friend Complex operator +(Complex &,Complex &);
};
#include<iostream>
using namespace std;
class AssignOp
{
private:
int a;
double b;
public:
void input(int n1,double n2)
{
a=n1;
b=n2;
}
void display()
{
cout<<"Given values are : "<<endl;
cout<<a<<"\t"<<b<<endl;
}
void operator =(AssignOp ao)
{
a=ao.a;
b=ao.b;
}
};
int main()
{
int n1;
double n2;
cout<<"Enter integer and double values : "<<endl;
cin>>n1>>n2;
AssignOp a1,a2;
a1.input(n1,n2);
a1.display();
a2.operator=(a1);
a2.display();
return 0;
}
Output:
Exercise -4 (Inheritance)
i) Single Inheritance
v) Hybrid inheritance
//Single inheritance
#include<iostream>
class Base
protected:
char name[30];
int age;
};
public:
int height,weight;
void getdata()
cin>>name>>age;
cout<<"Enter height and weight : ";
cin>>height>>weight;
void display()
cout<<"name = "<<name<<endl;
cout<<"age = "<<age<<endl;
cout<<"height = "<<height<<endl;
cout<<"weight = "<<weight<<endl;
};
int main()
Derived d;
d.getdata();
d.display();
return 0;
}
Output:
name = sai
age = 35
height = 165
weight = 55
//hierarchical inheritance
#include<iostream>
class vehicle
protected:
vehicle()
};
public:
car()
};
public:
bike()
};
int main()
car c;
bike b;
return 0;
}
Output:
#include<iostream>
class A
protected:
int a;
};
class B
protected:
int b;
};
class C
protected:
int c;
};
public:
int d;
void getdata()
cin>>a>>b>>c>>d;
}
void display()
cout<<"A = "<<a<<endl;
cout<<"B = "<<b<<endl;
cout<<"C = "<<c<<endl;
cout<<"D = "<<d<<endl;
};
int main()
D obj;
obj.getdata();
obj.display();
return 0;
}
Output:
10 20 30 40
A = 10
B = 20
C = 30
D = 40
//Multi level inheritance
#include<iostream>
class A
protected:
char name[30];
int age;
};
class B:public A
protected:
float height;
float weight;
};
class C:public B
protected:
char gender;
public:
void getdata()
cin>>name;
cin>>height;
cin>>weight;
cin>>gender;
void displaydata()
};
int main()
C obj;
obj.getdata();
obj.displaydata();
return 0;
}
Output:
Enter age : 25
Enter weight : 55
Enter gender : m
name = sai
age = 25
height = 5.7
weight = 55
gender = m
//Hybrid inheritance
#include<iostream>
class player
protected:
char name[30];
char gender;
int age;
};
protected:
float height;
float weight;
};
class location
protected:
char city[10];
int pin;
};
protected:
char game[10];
public:
void getdata()
cout<<"Enter name, age, gender, height, weight, city, pin and game "<<endl;
cin>>name>>age>>gender>>height>>weight>>city>>pin>>game;
void display()
cout<<"Name = "<<name<<endl;
cout<<"Age = "<<age<<endl;
cout<<"Gender = "<<gender<<endl;
cout<<"Height = "<<height<<endl;
cout<<"Weight = "<<weight<<endl;
cout<<"City = "<<city<<endl;
cout<<"Pin = "<<pin<<endl;
cout<<"Game = "<<game<<endl;
};
int main()
game g;
g.getdata();
g.display();
return 0;
}
Output:
Enter name, age, gender, height, weight, city, pin and game
Name = sai
Age = 25
Gender = m
Height = 5.8
Weight = 55
City = hyderabad
Pin = 500082
Game = cricket
2. Also illustrate the order of execution of constructors and destructors in inheritance
#include<iostream>
class A
public:
A()
~A()
};
class B:public A
public:
B()
~B()
{
};
class C:public B
{
public:
C()
{
cout<<"C's constructor "<<endl;
}
~C()
{
cout<<"C's Destructor "<<endl;
}
};
int main()
C c;
return 0;
Output:
A's constructor
B's constructor
C's constructor
C's Destructor
B's Destructor
A's destructor
Exercise -5(Templates, Exception Handling)
#include<iostream>
template<class T>
class Data
{
public:
Data(T v)
{
cout<<"Value = "<<v<<endl;
Data <int>i(15);
Data <double>f(25.6563);
return 0;
}
Output:
Value = A
Size = 1 byte(s)
Value = 15
Size = 4 byte(s)
Value = 25.6563
Size = 8 byte(s)
//2. b)Write a Program to illustrate member function templates
#include<iostream>
using namespace std;
template <class T>
class data
{
T x,y;
public:
T maximum(T a,T b)
{
x=a;
y=b;
cout<<"Maximum ("<<x<<" and "<<y<<") is : ";
return (x>y)?x:y;
}
};
int main()
{
data <int>d1;
cout<<d1.maximum(15,20)<<endl;
data <double>d2;
cout<<d2.maximum(25.243,11.742)<<endl;
data <char>d3;
cout<<d3.maximum('S','M')<<endl;
return 0;
}
Output:
Maximum (15 and 20) is : 20
Maximum (25.243 and 11.742) is : 25.243
Maximum (S and M) is : S
#include<iostream>
using namespace std;
int main()
{
int a,b;
cout<<"Enter values of a and b : "<<endl;
cin>>a>>b;
try
{
if(b!=0)
cout<<"Result of (a/b) is : "<<(a/b)<<endl;
else
throw b;
}
catch(int e)
{
cout<<"Divide by error due to b = "<<b<<endl;
}
return 0;
}
Output1:
Enter values of a and b :
25 0
Divide by error due to b = 0
Output2:
Enter values of a and b :
150 10
Result of (a/b) is : 15
//4. d)Write a Program to rethrow an Exception
#include<iostream>
using namespace std;
int main()
{
try
{
int a,b;
cout<<"Enter a and b values : ";
cin>>a>>b;
try
{
if(b==0)
throw b;
else
cout<<"(a/b) = "<<a/b<<endl;
}
catch(int)
{
throw;
} //rethrowing the exception
}
catch(int)
{
cout<<"Second value b can not be zero "<<endl;
}
return 0;
}
Output:
Enter a and b values : 95 5
(a/b) = 19
Output2:
Enter a and b values : 15 0
Second value b can not be zero
Exercise -6
1. Write a C++ program illustrating user defined string processing functions using pointers (string
length, string copy, string concatenation)
2. Write a C++ program illustrating Virtual classes & virtual functions.
3. Write C++ program that implement Bubble sort, to sort a given list of integers in ascending order
1. Write a C++ program illustrating user defined string processing functions using pointers (string
length, string copy, string concatenation)
a) Strlen()
This is the string length function. It returns the length of the string passed to it as the argument.
Syntax:
strnlen(string1)
The parameter string1 is the name of the string whose length is to be determined.
The above function will return the length of the string string1.
b) strcpy()
This is the string copy function. It copies one string into another string.
Syntax:
strcpy(string1, string2);
The two parameters to the function, string1 and string2, are strings.
The function will copy the string string1 into the string 1
c) strcat()
Syntax:
strcat(string1, string2);
The two parameters to the function, string1 and string2 are the strings to be concatenated.
The above function will concatenate the string string2 to the end of the string string1.
//C++ program illustrating user defined string processing functions using pointers (string length, string
copy, string concatenation)
#include<iostream>
#include<cstring>
int main()
char name1[20];
char name2[20];
char name3[20];
//clrscr();
cin>>name1;
cin>>name2;
int len;
strcpy(name3, name1);
strcat(name1, name2);
len = strlen(name2);
return 0;
}
Output:
strlen(name2) : 5
Virtual base classes are used in virtual inheritance in a way of preventing multiple “instances” of a given
class appearing in an inheritance hierarchy when using multiple inheritances.
Both these classes are inherited into another in a new class D as shown in figure below.
As we can see from the figure that data members/function of class A are inherited twice to class D.
When any data / function member of class A is accessed by an object of class D, ambiguity arises as to
which data/function member would be called?
One inherited through B or the other inherited through C. This confuses compiler and it displays error.
Syntax:
#include<iostream>
class A1
protected:
int a1;
};
protected:
int a2;
};
protected:
int a3;
};
class A4 : public A2,A3 // virtual declaration
{
int a4;
public:
void get()
{
cout<<"Enter values for a1, a2,a3 and a4 : ";
cin>>a1>>a2>>a3>>a4;
void put()
{
cout<<"a1="<<a1 <<"\na2="<<a2 <<"\na3="<<a3 <<"\na4="<<a4;
}
};
int main()
//clrscr();
A4 a;
return 0;
}
Output:
a1=10
a2=20
a3=30
a4=40
Virtual function
A virtual function is a member function which is declared within a base class and is redefined(
Overridden) by a derived class. When you refer to a derived class object using a pointer or a
reference to the base class, you can call a virtual function for that object and execute the derived
• Virtual functions ensure that the correct function is called for an object, regardless of
Syntax:
Source Code:
#include <iostream>
class base {
public:
void show()
};
{
public:
void print()
void show()
};
int main()
base* bptr;
derived d;
bptr = &d;
// virtual function, binded at runtime
bptr->print();
bptr->show();
Output:
/* 3. Write C++ program that implement Bubble sort, to sort a given list of integers in ascending
order. */
Bubble Sort:
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements
Example:
First Pass:
( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1.
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5),
Second Pass:
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
Now, the array is already sorted, but our algorithm does not know if it is completed.
The algorithm needs one whole pass without any swap to know it is sorted.
Third Pass:
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
//Source Code
#include<iostream>
int main()
cin>>num;
cout<<" ";
cin>>arr[i];
if(arr[j]>arr[j+1])
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
cout<<" ";
cout<<arr[i]<<" ";
return 0;
Output:
Enter 10 Elements :
25 36 85 15 45 62 90 55 11 77
11 15 25 36 45 55 62 77 85 90