Categories
Uncategorised

Constructors – Time

/* Copyright © 2007 Ankur Banerjee. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. http://www.fsf.org/licensing/licenses/lgpl.html /

/ Constructors – Time /

#include <iostream.h>
#include <conio.h>

class time
{
int h, m, s;
public:
time()
{
h = m = s = 0;
}
time (int hh, int mm, int ss)
{
h = hh;
m = mm;
s = ss;
}
void disp()
{
cout<<endl<<“Added time duration is “<<h<<” : “
<<m<<” : “<<s;
}
time add(time time1, time time2)
{
time time3;
time3.s = (time1.s + time2.s) % 60;
time3.m = ((time1.m + time2.m) % 60) + ((time1.s + time2.s) / 60);
time3.h = (time1.h + time2.h) + ((time1.m + time2.m) / 60);
return time3;
}
} ;

void main()
{
clrscr();
int h, m, s;
cout<<“Enter time durations in HH MM SS format”<<endl
<<“Enter time duration 1 : “<<endl;
cin>>h>>m>>s;
time t1(h, m, s);
cout<<“Enter time duration 2 : “<<endl;
cin>>h>>m>>s;
time t2(h, m, s);
time t3 = t1.add(t1, t2);
t3.disp();
getch();
}

/ Output */

Enter time durations in HH MM SS format
Enter time duration 1 : 5 14 9
Enter time duration 2 : 4 6 53

Added time duration is 9 : 21 : 2

Categories
Uncategorised

Constructors – Circle

/* Copyright © 2007 Ankur Banerjee. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. http://www.fsf.org/licensing/licenses/lgpl.html /

/ Constructors – Circle /

#include <iostream.h>
#include <conio.h>

class circle
{
float r, a;
public:
circle(float rad) // parameterized constructor
{
r = rad;
a = 3.14 * r * r;
}
circle(circle &c1) // copy constructor
{
r = c1.r + 1;
a = 3.14 * r * r;
}
void disp()
{
cout<<“Radius of circle : “<<r<<endl
<<“Area of circle : “<<a<<endl;
}
} ;

void main()
{
clrscr();
int rad;
cout<<endl<<“Enter radius of circle : “;
cin>>rad;
circle c1(rad); // use of parameterized constructor
c1.disp();
cout<<endl<<“Increasing radius of circle by 1 unit”<<endl;
circle c2(c1); // invoking copy constructor
c2.disp();
getch();
}

/ Output */

Enter radius of circle : 6
Radius of circle : 6
Area of circle : 113.04

Increasing radius of circle by 1 unit
Radius of circle : 7
Area of circle : 153.86