0% found this document useful (0 votes)
88 views13 pages

Gaurab Oop 6 PDF

The document describes a NumDays class that converts a number of work hours to days. The class has private data members to store hours and days, and public methods to get/set hours and days. Operator overloading is used to add, subtract, and increment/decrement NumDays objects. The main() function demonstrates creating NumDays objects, performing operations on them, and outputting results.

Uploaded by

Pramod Shahi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views13 pages

Gaurab Oop 6 PDF

The document describes a NumDays class that converts a number of work hours to days. The class has private data members to store hours and days, and public methods to get/set hours and days. Operator overloading is used to add, subtract, and increment/decrement NumDays objects. The main() function demonstrates creating NumDays objects, performing operations on them, and outputting results.

Uploaded by

Pramod Shahi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

PROGRAMMER’S NAME: Gaurab Khatri

Date : 22th july


objective : To convert an array of integers into Vector class .

#include <iostream> programmer name :

#include<math.h>

using namespace std;

class Vector{

int arr[3];

public:

Vector(int

array[]){ for(int

i=0;i<3;i++)

arr[i]=array[i];

void

displayVector(){ for

(int i=0;i<3;i++)

cout<<arr[i]<<" ";

};

int main()

int array[]={1,2,3}; //basic data type array

Vector vec(array); //class data type vec

vec.displayVector();

return 0;

Date : 22th july


objective : To convert duration in minutes of type integer to Time class.

#include<iostream>
using namespace std;

class Time{

private:
float hour;

int m;

public:
Time(){}

Time(float

min){ float
hr=min/60;

hour=int(hr);

m=60*(hr-hour);

void
Display(){ cout<<hour<<"hour"<<m<<"minute
s";

}
};
int

main(){ float

min=254; Time x;

x=min;

x.Display();
return 0;

r.

Date : 22th july


objective : To convert Time class with hour and minute as attributes into
duration in minutes of type integer .
#include<iostream>

using namespace std;


class Time{

private:
float h, m;int minute,s;

public:

Time(){}
Time(float

sec){ float m=

sec/60; float
hour=m/60;

h=int(hour);

minute=60*(hour-h);

s=sec-(h*60*60+minute*60);
}

void
display(){ cout<<h<<"hour"<<minute<<"minute"<<s<<"seconds
";

}
};
int

main(){ float
sec=4000; Time

t;

t=sec;
t.display();

return 0;

the month. For example,

Day D
December 31.

The constructor for the class should take as parameter an integer representing the day

of the year, and the class should have a member function print() that prints the day

in the month–day format. The class should have an integer member variable to represent

the day, and should have static member variables holding strings that can be used
t.

Date : 22th july

Objective : To To Design a class called NumDays whose class’s purpose is to


store a value that represents a number of work hours and to convert it to
a number of days.

#include <iostream>

#include <string>
using namespace std;

class

DayOfYear{ private:

int day;
static string m[12];

static int numOfDays[12];


int month = 0;

public:
DayOfYear(int d){
if(d < 1 || d > 365){

cout << "Invalid Number.\n";


exit(0);

}
else
day = d;
}

void print(){
for(int counter = 0; counter < 12;

counter++){ if(day <= numOfDays[counter])

break;
else{

day -= numOfDays[counter];
month++;

}
}
cout << m[month] << day;
}

};
string DayOfYear::m[12] = {"January ", "February ", "March ", "April ",
"May ", "June ", "July ", "August ",

"September ", "October ", "November ",


"December "};
int DayOfYear::numOfDays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,
31};

int main()
{
int x;

cout << "Please enter a day number between 1-365:\n";


cin >> x;
DayOfYear dayN(x);
dayN.print();

return 0;

5. Convert Celcius class into Fahrenheit Class .

Date : 22th july


objective : To Convert Celcius class into Fahrenheit Class.

#include<iostream>

using namespace std;

class

Celsius{ priv

ate:

float cel;

public:

Celsius(){
cout<<"Enter temperature in Celsius:"; cin>>cel;

Celsius(float

c){ cel = c;

float getCel(){

return cel;

};

class

Fahrenheit{

private:

float f;

public:

Fahrenheit(){f = 0;}

Fahrenheit(Celsius cel){

f = (cel.getCel() * 9.0/5.0) + 32.0;

void show(){

cout << f;

};

int

main(){ Celsius

celsius;

Fahrenheit fahrenheit;

fahrenheit = Fahrenheit(celsius);

cout<<"Temperature in Fahrenheit:";

fahrenheit.show();

return 0;

}
.

Date : 22th july

objective : To convert polar x = r * cos(th)and y = r * sin(th) to


Rectangular P(x,y) Coordinate Conversion using Constructor .

#include<iostream>
#include<math.h>
using namespace std;
class polar

{
public:
float r,th;

polar(){}
polar(int a,int b)

{
r=a;

th=b;

}
void show()

{
cout<<"In polar form:\nr="<<r<<" and theta="<<th;

}
};
class rectangular

{
float x,y;
public:

rectangular(){}

rectangular(polar p)
{

x=p.r*cos(p.th);
y=p.r*sin(p.th);
}

void show()
{
cout<<"\nIn Rectangular form:\nx="<<x<<"and y="<<y;

}
};
int main()

{
polar p(5.5,3.2);
p.show();
rectangular r;
r=p;

r.show();

Date : 22th july


objective : To convert a class that stores date in long format 190206 (yymmdd)
to a class that stores date in year , month and day as separate attributes of
a class .

#include<iostream>
using namespace std;
class
LongDate{ private:
int yymmdd;
public:
LongDate(int
longdate){ yymmdd=longdate;

}
int
returnLongDate(){ retur
n yymmdd;

}
};
class Date{
private:
int year, month, day;

public:

Date(){}
Date(LongDate

ld){ int value;

value=ld.returnLongDate()/10000;
year= value;

year=year+2000;

value=ld.returnLongDate()%10000;
value=value/100; month=value;
day=ld.returnLongDate()%100;

}
void showDate(){
cout<<"The date in format is: "<< year<<" /" << month<<"/"<<day;
}

};
int main()
{

LongDate ld(151115);
Date d;
d=ld;

d.showDate();

return 0;

8.
rs.

s’

Date : 23rd july

objective : To Design a class called NumDays whose class’s purpose is to


store a value that represents a number of work hours and to convert it to
a number of days.

#include <iostream>
#include <string>
using namespace std;

class
NumDays{ private:

double hours;
double days;

public:
NumDays(double h =
0){ hours = h;

days = h/(8.00);
}

double

getHours(){ ret

urn hours;

}
double getDays(){
return days;
}

void setHours(double
h){ hours = h;

days = h/(8.00);
}
void setDays(double

d){ days = d;

hours = d*(8.00);
}

double operator+ (const NumDays


&right){ return hours+right.hours;

double operator- (const NumDays

&right){ if(hours < right.hours){

cout << "ERROR! Cannot subtract! Now terminating!\n";


exit(0);

}
return hours-right.hours;
}

NumDays operator++(){
++hours;

days = hours/(8.00);

return *this;
}

NumDays
operator++(int){ hours
++;
days = hours/(8.00);

return *this;
}

NumDays operator--(){
--hours;

days = hours/(8.00);

return *this;

NumDays operator--

(int){ hours--;

days = hours/(8.00);

return *this;
}

};
int main()
{

cout << "Creating object with 12 hours...\n";


NumDays obj1(12);

cout << obj1.getHours() << " hours = " <<obj1.getDays() << " days.\n";

cout << "\nCreating object with 18 hours...\n";


NumDays obj2(18);

cout << obj2.getHours() << " hours = " <<obj2.getDays() << " days.\n";

cout << endl << "Adding hours... " << obj1 + obj2 << " hours.\n";
cout << endl << "Subtracting hours... " << obj2 - obj1 << "
hours.\n\n";

cout << "Pre- and post-incrementing first object...\n";


++obj1;
cout << obj1.getHours() << " hours = " <<obj1.getDays() << " days.\n";

obj1++;

cout << obj1.getHours() << " hours = " <<obj1.getDays() << " days.\n";

cout << "\nPre- and post-decrementing second object...\n";


--obj2;
cout << obj2.getHours() << " hours = " <<obj2.getDays() << " days.\n";

obj2--;

cout << obj2.getHours() << " hours = " <<obj2.getDays() << " days.\n";
return 0;

You might also like