Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Lab 3: Classes and Objects II: Constructors and their Overloading,

Copy Constructor, Destructor


Student: Mian Sayaf Ali Shah, 18PWMCT0612, A
(Write your name, registration number and section here.)

Friday, 12 June 2020

Contents
1 Introduction

2 Experimental Setup

3 Data and Methods

4 Results and Analysis

5 Conclusion
Main file:
#include <iostream>
#include "ourclasses.h"
using namespace std;
int main()
{
mytime t1,t7;
mytime t2(2);
mytime t3(2,10);
mytime t4(2,10,20);
mytime t5=t4;
mytime t6(t4);
t7=t4.add(t5);

mycomplex c1,c6;
mycomplex c2(2);
mycomplex c3(2,10);
mycomplex c4=c3;
mycomplex c5(c4);
c6=c4.add(c5);

fraction f1,f6;
fraction f2(1);
fraction f3(1,2);
fraction f4=f3;
fraction f5(f4);
f6=f4.add(f5);

t1.print();
t2.print();
t3.print();
t4.print();
t5.print();
t6.print();
t7.print();

c1.print();
c2.print();
c3.print();
c4.print();
c5.print();
c6.print();

f1.print();
f2.print();
f3.print();
f4.print();
f5.print();
f6.print();

return 0;
}
Header file:

#ifndef OURCLASSES_H_INCLUDED
#define OURCLASSES_H_INCLUDED
class mytime
{
int hours;
int minutes;
int seconds;
public:
mytime(); //default constructor
~mytime();
mytime(int h) ; //overloaded constructor
mytime(int h, int m); //overloaded constructor
mytime(int h,int m,int s); //overloaded constructor
mytime(mytime &mt); //copy constructor
void reset();
void sethours(int h) ;
void setminutes(int m) ;
void setseconds (int s) ;
int gethours();
int getminutes();
int getseconds();
mytime add (mytime t);
mytime subtract (mytime t);
void printmytime();
};
class mycomplex
{
double real;
double imaginary;
public:
mycomplex();
~mycomplex();
mycomplex(double r);
mycomplex(double r,double i);
mycomplex(mycomplex &c);
void setreal(double r);
void setimaginary(double i);
double getreal();
double getimaginary();
mycomplex add(mycomplex c);
void printmycomplex();
};
class fraction
{
int numerator;
int denumerator;
public:
fraction();
~fraction();
fraction(int n);
fraction(int n,int d);
fraction(int &f);
void setnumerator(int n);
void setdenumerator(int d);
int getnumerator();
int getdenumerator();
fraction add(fraction f1);
void printmyfraction();
};
#endif // OURCLASSES_H_INCLUDED

Source file:
#include <iostream>
#include "ourclasses.h"
#include <iomanip>
using namespace std;
mytime::mytime ()
{
hours= 0;
minutes = 0;
seconds = 0;
cout << "Default constructor has been invoked" << endl ;
}
mytime::mytime(int h)
{
minutes = 0;
seconds = 0;
sethours(h);
cout << "Override constructor has been invoked" << endl ;
}
mytime::mytime(int h , int m)
{
seconds = 0;
sethours(h);
setminutes(m);

cout << "Override constructor has been invoked" << endl ;


}
mytime::mytime(int h , int m , int s)
{
setseconds(s);
setminutes(m);
sethours(h);
cout << "Override constructor has been invoked" << endl ;
}
mytime::mytime(mytime &mt)
{
hours=mt.hours;
minutes=mt.minutes;
seconds=mt.seconds;
cout << "copy constructor has been invoked" << endl ;
}
void mytime::sethours(int h){
if(h > 0) hours = h;
else hours = 0;
}
void mytime::setminutes(int m){
if(m < 0) minutes = 0;
else{
minutes = m % 60;
hours = hours + m / 60;
}
}
void mytime::setseconds(int s){
if(s < 0) seconds = 0;
else{
seconds = s % 60;
setminutes(getminutes() + s / 60);
}
}
int mytime::gethours()
{
return hours ;
}
int mytime::getminutes()
{
return minutes ;
}
int mytime::getseconds()
{
return seconds ;
}
void mytime::reset(){
hours = minutes = seconds = 0;
}
mytime::~mytime()
{
cout << "Destructor has been invoked" << endl ;
}
void mytime::printmytime()
{
cout<<setfill('0') << setw(2)<< hours << ":" << setw(2)<<minutes << ": "<< setw(2)<< seconds << endl ;
}
mytime mytime::add (mytime m2)
{
mytime sum;
sum.reset();
sum.setseconds(seconds + m2.getseconds());
sum.setminutes(minutes + m2.getminutes());
sum.sethours(hours + m2.gethours());
return sum;
}
#include <iostream>
#include "ourclasses.h"
using namespace std;

mycomplex::mycomplex ()
{
real= 0;
imaginary = 0;
cout << "Default constructor has been invoked" << endl ;
}
mycomplex::mycomplex(double r)
{
setreal(r);
imaginary = 0;
cout << "Override constructor has been invoked" << endl ;
}
mycomplex::mycomplex(double r , double i)
{
setreal(r);
setimaginary(i);
cout << "Override constructor has been invoked" << endl ;
}
void mycomplex::setreal(double r)
{
real = r;
}
void mycomplex::setimaginary(double i)
{
imaginary = i;
}
double mycomplex::getreal ()
{
return real;
}
double mycomplex::getimaginary ()
{
return imaginary;
}
mycomplex::mycomplex(mycomplex &c)
{
real=c.real;
imaginary=c.imaginary;
cout << "copy constructor has been invoked" << endl ;
}
mycomplex mycomplex::add(mycomplex c)
{
mycomplex sum;
sum.setreal(real+c.getreal());
sum.setimaginary(imaginary+c.getimaginary());
return sum;
}
mycomplex::~mycomplex()
{
cout << "Destructor has been invoked" << endl ;
}
void mycomplex::printmycomplex()
{
if (imaginary<0)
cout << real << "" <<imaginary <<"j" << endl ;
else
cout << real << "+" <<imaginary <<"j" << endl ;
}
#include <iostream>
#include "ourclasses.h"
using namespace std;

fraction::fraction ()
{
numerator= 0;
denumerator = 0;
cout << "Default constructor has been invoked" << endl ;
}
fraction::fraction(int n)
{
setnumerator(n);
denumerator = 0;
cout << "Override constructor has been invoked" << endl ;
}
fraction::fraction(int n , int d)
{
setnumerator(n);
setdenumerator(d);
cout << "Override constructor has been invoked" << endl ;
}
fraction::~fraction()
{
cout << "Destructor has been invoked" << endl ;
}
void fraction::setnumerator(int n)
{
numerator = n;
}
void fraction::setdenumerator(int d)
{
denumerator = d;
}
int fraction::getnumerator()
{
return numerator;
}
int fraction::getdenumerator()
{
return denumerator;
}
fraction fraction::add (fraction f1)
{
fraction sum;
if (denumerator == f1.getdenumerator())
{
sum.setdenumerator(denumerator);
sum.setdenumerator(numerator+f1.getnumerator());
}
else if (denumerator%f1.getdenumerator() ==0 || f1.getdenumerator()%denumerator==0)
{
if (denumerator < f1.getdenumerator())
{
sum.setdenumerator(f1.getdenumerator());
sum.setnumerator(numerator*(f1.getdenumerator()/denumerator)+f1.getnumerator());
}
else
{
sum.setdenumerator(denumerator);
sum.setnumerator(numerator+f1.getnumerator()*(denumerator/f1.getdenumerator()));
}
}
}
void fraction::printmyfraction ()
{

cout << numerator << "/" << denumerator << endl;


}
Console Window
Task 1,2 and 3
Task 4
Task 5

You might also like