Define a class Pairs with 2 integer data members, f & s, where f represents
the 1st value in the ordered pair and s represents the 2nd value. Objects of type Pairs can be used wherever ordered pairs are
needed.
The class must have a default constructor that initializes data members to
0, & 2overloaded constructors, 1 with one int parameter and the other with
2 int parameters. The 1-parameter constructor should initialise the 1st
member of the pair; the 2nd member of the pair must be 0. The 2-parameter
constructor must initialise both members of the ordered pair.
The class must have a destructor that outputs 'Bye'
Add accessor functions that return the values stored in each of the member
variables of an object of class Pairs & mutator functions to update each of the member variables of an object of class Pairs
respectively. The class must contain a void member
function reset() that resets the member variables of a Pairs to values
specified by parameters.
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
class Pairs{
public:
int f,s;
Pairs()
{
}
Pairs(int n)
{
f = n;
s = 0;
}
Pairs(int n, int m)
{
f = n;
s = m;
}
int get_f()
{
return f;
}
int get_s()
{
return s;
}
void update(int a, int b)
{
f = a;
s = b;
cout<<"Values of f and s updated"<<endl;
}
void reset(int n, int m)
{
f = n;
s = m;
cout<<"Values of f and s is reset"<<endl;
}
~Pairs()
{
cout<<"Bye"<<endl;
}
};
int main()
{
Pairs ob1(5);
Pairs ob2(10,20);
cout<<"Value of f "<<ob2.get_f()<<endl;
cout<<"Value of s "<<ob2.get_s()<<endl<<endl;
ob2.reset(0,0);
cout<<"Value of f "<<ob2.get_f()<<endl;
cout<<"Value of s "<<ob2.get_s()<<endl;
cout<<endl<<endl;
ob2.update(30,40);
cout<<"Value of f "<<ob2.get_f()<<endl;
cout<<"Value of s "<<ob2.get_s()<<endl;
cout<<endl<<endl;
}
Comments
Leave a comment