Академический Документы
Профессиональный Документы
Культура Документы
#include<math.h>
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()
vec.displayVector();
return 0;
#include<iostream>
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 .
objective : To convert Time class with hour and minute as attributes into
duration in minutes of type integer .
#include<iostream>
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 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 .
#include <iostream>
#include <string>
class
DayOfYear{ private:
int day;
static string m[12];
int month = 0;
public:
DayOfYear(int d){
if(d < 1 || d > 365){
else
day = d;
}
void print(){
for(int counter = 0; counter < 12; counter++)
break;
else{
}
cout << m[month] << day;
}
};
string DayOfYear::m[12] = {"January ", "February ", "March ", "April ",
"May ", "June ", "July ", "August ",
int main()
{
int x;
dayN.print();
return 0;
#include<iostream>
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){
void show(){
cout << f;
};
int main()
{ Celsius
celsius;
Fahrenheit fahrenheit;
fahrenheit = Fahrenheit(celsius);
cout<<"Temperature in Fahrenheit:";
fahrenheit.show();
return 0;
#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 .
#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 –
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
-- 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 .
#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);
}
}
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 << obj1.getHours() << " hours = " <<obj1.getDays() << " days.\n";
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";
obj1++;
cout << obj1.getHours() << " hours = " <<obj1.getDays() << " days.\n";
obj2--;
cout << obj2.getHours() << " hours = " <<obj2.getDays() << " days.\n";
return 0;