Exercise -1
(Classes Objects)
AIM:
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)
Solutions:
1. Write a main function to create objects of DISTANCE class. Input two distances and output the sum.
#include <iostream.h>
class Distance
{
private:
int feet,inches;
public:
void inputDistance()
{
cout<<"Enter feet
& inches?";
cin>>feet>>inches;
}
void outputDistance()
{
cout<<"feet="<<feet<<"\t
Inches="<<inches<<endl;
}
void addDistances(Distance d1,Distance
d2)
{
int f,i;
f=d1.feet+d2.feet;
i=d1.inches+d2.inches;
if(i>=12){
f=f+i/12;}
i=i%12;
cout<<"feet="<<f<<"\t
Inches="<<i<<endl;
}
};
int main()
{
Distance d1,d2;
d1.inputDistance();
d2.inputDistance();
Distance d3;
d3.addDistances(d1,d2);
return 0;
}
Enter feet & inches?1 1
Enter feet & inches?1 12
feet=3 Inches=1
------------------------------------------------------------------------------------
2. Write a C++ Program to illustrate the use of Constructors and Destructors (use the above program.)
#include <iostream.h>
class Distance
{
private:
int feet,inches;
public:
Distance()
{
feet=0;
inches=0;
cout<<"default
constructor...."<<endl;
}
Distance(int f,inti)
{
feet=f;
inches=i;
cout<<"argument
constructor...."<<endl;
}
void outputDistance()
{
cout<<"feet="<<feet<<"\t
Inches="<<inches<<endl;
}
void addDistances(Distance d1,Distance
d2)
{
int f,i;
f=d1.feet+d2.feet;
i=d1.inches+d2.inches;
if(i>=12){
f=f+i/12;}
i=i%12;
cout<<"feet="<<f<<"\t
Inches="<<i<<endl;
}
~Distance()
{
feet=inches=0;
cout<<"distructor...."<<endl;
}
};
int main()
{
Distance d1(2,9),d2(2,9);
Distance d3;
d3.addDistances(d1,d2);
return 0;
}
argument constructor....
argument constructor....
default constructor....
feet=5 Inches=6
distructor....
distructor....
distructor....
distructor....
distructor....
3. Write a program for illustrating function overloading in adding the distance between objects (use the above problem)
class Distance
{
private:
int feet,inches;
public:
Distance()
{
feet=0;
inches=0;
cout<<"default
constructor...."<<endl;
}
Distance(int f,inti)
{
feet=f;
inches=i;
cout<<"argument
constructor...."<<endl;
}
void outputDistance()
{
cout<<"feet="<<feet<<"\t
Inches="<<inches<<endl;
}
void addDistances(Distance d2)
{
int f,i;
f=this->feet+d2.feet;
i=this->inches+d2.inches;
if(i>=12){
f=f+i/12;}
i=i%12;
cout<<"feet="<<f<<"\t
Inches="<<i<<endl;
}
void addDistances(Distance d1,Distance
d2)
{
int f,i;
f=d1.feet+d2.feet;
i=d1.inches+d2.inches;
if(i>=12){
f=f+i/12;}
i=i%12;
cout<<"feet="<<f<<"\t
Inches="<<i<<endl;
}
~Distance()
{
feet=inches=0;
cout<<"distructor...."<<endl;
}
};
int main()
{
Distance d1(2,9),d2(2,9);
Distance d3;
d1.addDistances(d2);
d3.addDistances(d1,d2);
return 0;
}
argument constructor....
argument constructor....
default constructor....
feet=5 Inches=6
distructor....
distructor....
distructor....
distructor....
distructor....
distructor....
Write a program for illustrating Access Specifiers
public, private, protected
1. Write a program implementing Friend Function
#include <iostream.h>
class Box {
double width;
public:
friend
void printWidth( Boxbox );
void
setWidth( doublewid );
};
// Member function definition
void Box::setWidth( double wid ) {
width = wid;
}
// Note: printWidth() is not a member function of any
class.
void printWidth( Boxbox ) {
/* Because
printWidth() is a friend of Box, it can
directly
access any member of this class */
cout<< "Width of box : "
<<box.width<<endl;
}
// Main function for the program
int main() {
Box box;
// set box
width without member function
box.setWidth(10.0);
// Use friend
function to print the wdith.
printWidth( box );
return 0;
}
Width of box : 10
2. Write a program to illustrate this pointer
#include <iostream.h>
class Box {
public:
//
Constructor definition
Box(double l = 2.0, double b = 2.0, double h = 2.0) {
cout<<"Constructor called."
<<endl;
length
= l;
breadth
= b;
height
= h;
}
double
Volume() {
return
length * breadth * height;
}
int
compare(Box box) {
return
this->Volume() >box.Volume();
}
private:
double
length; // Length of a box
double
breadth; // Breadth of a box
double
height; // Height of a box
};
int main(void) {
Box Box1(3.3,
1.2, 1.5); // Declare box1
Box Box2(8.5,
6.0, 2.0); // Declare box2
if(Box1.compare(Box2)) {
cout<< "Box2 is smaller than Box1"
<<endl;
} else {
cout<< "Box2 is equal to or larger than
Box1" <<endl;
}
return 0;
}
When the above code is compiled and executed, it
produces the following result −
Constructor called.
Constructor called.
Box2 is equal to or larger than Box1
3. Write a Program to illustrate pointer to a class
public:
//
Constructor definition
Box(double l = 2.0, double b = 2.0, double h = 2.0) {
cout<<"Constructor called."
<<endl;
length
= l;
breadth
= b;
height
= h;
}
double
Volume() {
return
length * breadth * height;
}
private:
double
length; // Length of a box
double
breadth; // Breadth of a box
double
height; // Height of a box
};
int main(void) {
Box Box1(3.3,
1.2, 1.5); // Declare box1
Box Box2(8.5,
6.0, 2.0); // Declare box2
Box
*ptrBox; // Declare
pointer to a class.
// Save the
address of first object
ptrBox = &Box1;
// Now try to
access a member using member access operator
cout<< "Volume of Box1: "
<<ptrBox->Volume() <<endl;
// Save the
address of second object
ptrBox = &Box2;
// Now try to
access a member using member access operator
cout<< "Volume of Box2: "
<<ptrBox->Volume() <<endl;
return 0;
}
Constructor called.
Constructor called.
Volume of Box1: 5.94
Volume of Box2: 102