PRACTICAL – 5.1
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. Write a main function to create objects of DISTANCE class. Input two distances and output the sum.
#include <iostream>using namespace std;class Distance{int feet, inch;public:Distance(int a = 0, int b = 0){feet = a;inch = b;}void Oputput(){cout << "Feet = " << feet << endl<< "Inch = " << inch << endl;}void addiction(Distance &a, Distance &b){cout << "Feet = " << (a.feet + b.feet) << endl<< "Inch = " << (a.inch + b.inch) << endl;}};int main(){Distance r(10, 13), j(11, 12);r.addiction(r, j);j.Oputput();return 0;}
PRACTICAL – 5.2
Write a function that creates a vector of user given size M using new operator. Demonstrate the use of the function.
#include <iostream>using namespace std;class Complex{int real, imaginary;public:void getData(){cout << "The real part is " << real << endl;cout << "The imaginary part is " << imaginary << endl;}void setData(int a, int b){real = a;imaginary = b;}};int main(){Complex *p = new Complex;p->setData(11, 12);p->getData();return 0;}
PRACTICAL – 5.3
Write a C++ program to swap two number by both call by value and call by reference mechanism, using two functions swap_value() and swap_reference respectively , by getting the choice from the user and executing the user’s choice by switch-case.
#include <iostream>using namespace std;class Swap{public:void swap_value(int a, int b){int temp = a;a = b;b = temp;cout << a << endl<< b;}void swap_refrance(int *a, int *b){int temp = *a;*a = *b;*b = temp;}};int main(){Swap r;int num1, num2, selaction;cout << "Enter two Numbers : ";cin >> num1 >> num2;cout << "Enter Selaction " << endl<< "1. Value swap " << endl<< " 2. Refrance swap " << endl;cin >> selaction;switch (selaction){case 1:r.swap_value(num1, num2);break;case 2:r.swap_refrance(&num1, &num2);cout << num1 << endl<< num2;break;default:cout << "Wrong Input";break;}return 0;}
0 Comments