JNTUK R20 2-1 Object-Oriented Programming in C++ Lab
1 a)
Cpp program to create objects of distance class and add them
In this article we will learn to implement a Cpp program to create objects of distance class and add them. A C++ program is provided below that adds objects of distance class.
We create a distance class with the following:
- feet and inches as data members
- member function to input distance
- member function to output distance
- member function to add two distance objects of distance class
#include <iostream>
using namespace std;
class Distance
{
private:
int feet;
int inches;
public:
void set_distance()
{
cout<<"Enter feet: ";
cin>>feet;
cout<<"Enter inches: ";
cin>>inches;
}
void get_distance()
{cout<<"Distance is feet= "<<feet<<", inches= "<<inches<<endl;
}
void add(Distance d1, Distance d2)
{
feet = d1.feet + d2.feet;
inches = d1.inches + d2.inches;
feet = feet + (inches / 12);
inches = inches % 12;
}
};
int main()
{
Distance d1, d2, d3;
d1.set_distance();
d2.set_distance();
d3.add(d1, d2);
d3.get_distance();
return 0;
}
Cpp program to illustrate the use of constructor and destructor
#include <iostream>
using namespace std;
class Distance
{
private:
int feet;
int inches;
public:
Distance() {}
Distance(int f, int i)
{
feet = f;
inches = i;
}
void get_distance()
{
cout<<"Distance is feet= "<<feet<<", inches= "<<inches<<endl;
}
void add(Distance &d1, Distance &d2)
{
feet = d1.feet + d2.feet;
inches = d1.inches + d2.inches;
feet = feet + (inches / 12);
inches = inches % 12;
}
~Distance()
{
cout<<"Distance object destroyed"<<endl;
}
};
int main()
{
int f1, in1, f2, in2;
cout<<"Enter feet: ";
cin>>f1;
cout<<"Enter inches: ";
cin>>in1;
cout<<"Enter feet: ";
cin>>f2;
cout<<"Enter inches: ";
cin>>in2;
Distance d1(f1, in1);
Distance d2(f2, in2);
Distance d3;
d3.add(d1, d2);
d3.get_distance();
return 0;
}
CPP program for illustrating function overloading in adding the distance between objects
In this article we will learn to implement a CPP program for illustrating function overloading in adding the distance between objects. A C++ program is provided below for adding the distance between objects using function overloading.
We create a distance class with the following:
- feet and inches as data members
- member function to input distance
- member function to output distance
- member function to add two distance objects
Now we overload the add function for adding two distance objects. Program is as follows:
#include <iostream>
using namespace std;
class Distance
{
private:
int feet;
int inches;
public:
void set_distance()
{
cout<<"Enter feet: ";
cin>>feet;
cout<<"Enter inches: ";
cin>>inches;
}
void get_distance()
{
cout<<"Distance is feet= "<<feet<<", inches= "<<inches<<endl;
}
void add(Distance d1, Distance d2)
{
feet = d1.feet + d2.feet;
inches = d1.inches + d2.inches;
feet = feet + (inches / 12);
inches = inches % 12;
}
void add(Distance *d1, Distance *d2)
{
feet = d1->feet + d2->feet;
inches = d1->inches + d2->inches;
feet = feet + (inches / 12);
inches = inches % 12;
}
};
int main()
{
Distance d1, d2, d3;
d1.set_distance();
d2.set_distance();
d3.add(d1, d2);
d3.get_distance();
d3.add(&d1, &d2);
d3.get_distance();
return 0;
}
Input and output for the above program are a follows:
2 a) CPP program using friend function
2. b) CPP program to illustrate this pointer
Output for the above program is as follows:
No comments