Object Oriented Programming through C++ LAB R19 (DS LAB)

                    Object Oriented Programming through C++ LAB R19 (DS LAB)

                                                  DS-LAB Exercise -1 to Exercise -5

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 

a) Write a main function to create objects of DISTANCE class. Input two distances and output the sum. 

b) Write a C++ Program to illustrate the use of Constructors and Destructors (use the above program.) 

c) Write a program for illustrating function overloading in adding the distance between objects (use the above problem)

Exercise -1 a):-

#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;

}

Input and output for the above program are as follows:

Enter feet: 3

Enter inches: 8

Enter feet: 4

Enter inches: 9

Distance is feet= 8, inches= 5


Exercise -1 b):-

Write a C++ Program to illustrate the use of Constructors and Destructors (use the above program.) 

#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;

}

Input and output for the above program are as follows:

Enter feet: 3

Enter inches: 8

Enter feet: 4

Enter inches: 9

Distance is feet= 8, inches= 5

Distance object destroyed

Distance object destroyed

Distance object destroyed

Exercise -1 c):-

Write a program for illustrating function overloading in adding the distance between objects (use the above problem)

#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:

Enter feet: 3

Enter inches: 4

Enter feet: 4

Enter inches: 9

Distance is feet= 8, inches= 1

Distance is feet= 8, inches= 1

No comments

JavaFX Scene Builder

  This is an article about the JavaFX Scene Builder. You will get a short introduction about the installation and usage of the software. The...