Вы находитесь на странице: 1из 13

Programmer's name : Bibek Bhardar

Date : 22th July

1. Convert an array of integers into Vector class .

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;

2. Convert duration in minutes of type integer to Time class.

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<<"minutes";

}
};
int main()

{ float

min=254; Time

x;

x=min;

x.Display();

return 0;

3. Convert Time class with hour and minute as attributes into duration in minutes of type integer .

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;

ys, write a class named DayOfYear that takes an integer representing a day of the year and translates it to a string consis

the month. For example,

Day 2 would be January 2. Day 32 would be February 1. Day 365 would be


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
to assist in the translation from the integer format to the month-day format .

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{ private:

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;

6. Polar x = r * cos(th)and y = r * sin(th) to Rectangular P(x,y) Coordinate Conversion using Constructor


.

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();

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

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()
{ return 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. Design a class called NumDays. The class’s purpose is to store a value that represents a

number of work hours and convert it to a number of days. For example, 8 hours

would be converted to 1 day, 12 hours would be converted to 1.5 days, and 18 hours

would be converted to 2.25 days. The class should have a constructor that accepts a
number of hours, as well as member functions for storing and retrieving the hours and

days. The class should also have the following overloaded operators:

+ Addition operator. When two NumDays objects are added together, the overloaded

+ operator should return the sum of the two objects’ hours members.

- Subtraction operator. When one NumDays object is subtracted from another,the overloaded –

operator should return the difference of the two objects’

hours members.

++ Prefix and postfix increment operators. These operators should increment the

number of hours stored in the object. When incremented, the number of days

should be automatically recalculated.

-- Prefix and postfix decrement operators. These operators should decrement the number of hours

stored in the object. When decremented, the number of days should be automatically

recalculated .

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(){

return 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;

Вам также может понравиться