Question

Q1. Write a C++ class called Time24h that describes a time of the day in hours, minutes, and seconds. It should contain the f
0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER1

#include <bits/stdc++.h>

using namespace std;

class Time24h
{
private:
int hh,mm,ss;
public:
Time24h()
{
hh=mm=ss=0;
}
Time24h(int num)
{
hh=mm=0;
ss=num;
if(!(ss>=0 && ss<=59))
ss=0;
}
Time24h(int num1,int num2,int num3)
{
hh=num1;
mm=num2;
ss=num3;
if(!(hh>=0 && hh<=23)||!(mm>=0 && mm<=59)||!(ss>=0 && ss<=59))
{
hh=mm=ss=0;
}
}
void display()
{
if(hh>=0 && hh<=9)
cout<<"0"<<hh;
else
cout<<hh;
cout<<":";
if(mm>=0 && mm<=99)
cout<<"0"<<mm;
else
cout<<mm;
cout<<":";
if(ss>=0 && ss<=9)
cout<<"0"<<ss;
else
cout<<ss;
cout<<endl;
}
};
int main()
{
Time24h obj1(21,4,4);
obj1.display();
return 0;
}


output:
21:04:04

ANSWER 2

#include <bits/stdc++.h>

using namespace std;

class Time24h
{
private:
int hh,mm,ss;
public:
Time24h()
{
hh=mm=ss=0;
}
Time24h(int num)
{
hh=mm=0;
ss=num;
if(!(ss>=0 && ss<=59))
ss=0;
}
Time24h(int num1,int num2,int num3)
{
hh=num1;
mm=num2;
ss=num3;
if(!(hh>=0 && hh<=23)||!(mm>=0 && mm<=59)||!(ss>=0 && ss<=59))
{
hh=mm=ss=0;
}
}
void display()
{
if(hh>=0 && hh<=9)
cout<<"0"<<hh;
else
cout<<hh;
cout<<":";
if(mm>=0 && mm<=99)
cout<<"0"<<mm;
else
cout<<mm;
cout<<":";
if(ss>=0 && ss<=9)
cout<<"0"<<ss;
else
cout<<ss;
cout<<endl;
}
int totalclosest()
{
int num=hh*60 + mm;
return num;
}
int compare(Time24h t)
{
if(hh>t.hh)
return 1;
else if(hh == t.hh)
{
if(mm >t.mm)
return 1;
else if(mm == t.mm)
{
if(ss > t.ss)
return 1;
else if(ss == t.ss)
return 0;
}
}
return -1;
}
};
int main()
{
Time24h obj1(21,4,4);
cout<<"The current Time is : ";
obj1.display();
int close=obj1.totalclosest();
cout<<"The number of minutes passed since midnight : "<<close<<endl;
Time24h t(21,8,8);
int res=obj1.compare(t);
if(res==1)
cout<<"obj1 has greater time than t ";
else if(res==0)
cout<<"obj1 has equal time with t ";
else
cout<<"obj1 has smaller time with t ";
cout<<endl;
return 0;
}

output:

The current Time is : 21:04:04
The number of minutes passed since midnight : 1264
obj1 has smaller time with t


ANSWER 4


#include <bits/stdc++.h>

using namespace std;

class DataArray
{
private:
int len;
double *arr=new double;
public:
DataArray(int num)
{
len=num;
for(int i=0;i<num;i++)
arr[i] = 0;
}
DataArray(double *res,int num)
{
len=num;
for(int i=0;i<num;i++)
arr[i]=res[i];
}
void display()
{
cout<<"The array : ";
for(int i=0;i<len;i++)
cout<<arr[i]<<" ";
cout<<endl;
}
~DataArray()
{
len=0;
delete[] arr;
}

};
int main()
{
DataArray obj1(5);
obj1.display();
double *res=new double;
for(int i=0;i<7;i++)
res[i]=i+1;
DataArray obj2(res,7);
obj2.display();
return 0;
}

output:
The array : 0 0 0 0 0
The array : 1 2 3 4 5 6 7

ANSWER 5

#include <bits/stdc++.h>

using namespace std;

class DataArray
{
private:
int len;
double *arr=new double;
public:
DataArray(int num)
{
len=num;
for(int i=0;i<num;i++)
arr[i] = 0;
}
DataArray(double *res,int num)
{
len=num;
for(int i=0;i<num;i++)
arr[i]=res[i];
}
void display()
{
cout<<"The array : ";
for(int i=0;i<len;i++)
cout<<arr[i]<<" ";
cout<<endl;
}
double min()
{
double minn = INT_MAX;
for(int i=0;i<len;i++)
{
if(arr[i]<minn)
minn=arr[i];
}
return minn;
}
double max()
{
double maxx = INT_MIN;
for(int i=0;i<len;i++)
{
if(arr[i]>maxx)
maxx=arr[i];
}
return maxx;
}
DataArray scaleby(double factor)
{
DataArray obj(arr,len);
for(int i=0;i<obj.len;i++)
obj.arr[i] = arr[i]*factor;
return obj;
}
~DataArray()
{
len=0;
delete[] arr;
}

};
int main()
{
DataArray obj1(5);
obj1.display();
double *res=new double;
for(int i=0;i<7;i++)
res[i]=i+1;
DataArray obj2(res,7);
obj2.display();
cout<<obj2.max()<<" is the maximum element in the array "<<endl;
cout<<obj2.min()<<" is the minimum element in the array "<<endl;
DataArray obj3 = obj2.scaleby(2);
obj3.display();
return 0;
}


output:

The array : 0 0 0 0 0
The array : 1 2 3 4 5 6 7
7 is the maximum element in the array
1 is the minimum element in the array
The array : 2 4 6 8 10 12 14

ANSWER 6


#include <bits/stdc++.h>

using namespace std;

class DataArray
{
public:
int num=0;
DataArray()
{
cout<<"Constructor is called "<<endl;
}
~DataArray()
{
cout<<"Destructor is called "<<endl;
}
};
void callbyvalue(DataArray obj)
{
obj.num = 10;
}
void callbyreferene(DataArray &obj)
{
obj.num = 10;
}

int main()
{
DataArray obj;
cout<<"Initial value of obj.num : "<<obj.num<<endl;
callbyvalue(obj);
cout<<"After calling callbyback function , value of obj.num : "<<obj.num<<endl;
callbyreferene(obj);
cout<<"After calling callbyreference function , value of obj.num : "<<obj.num<<endl;
return 0;
}

/*
output:
Constructor is called
Initial value of obj.num : 0
Destructor is called
After calling callbyback function , value of obj.num : 0
After calling callbyreference function , value of obj.num : 10
Destructor is called


*/


/*
callbyvalue - The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function. ... In general, it means the code within a function cannot alter the arguments used to call the function.

callbyreference - The call by reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. It means the changes made to the parameter affect the passed argument.

Constructor - it is called when the object is first created.It invoked automatically.
Desetructor - it is called whent the object goes out of scope. It also invoked automatically.
*/


Add a comment
Know the answer?
Add Answer to:
Q1. Write a C++ class called Time24h that describes a time of the day in hours,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Q1. Write a recursive function in C++ void printNumberedChars(string str, int i=0) { ... } which...

    Q1. Write a recursive function in C++ void printNumberedChars(string str, int i=0) { ... } which prints each character of the given string str on a new line, with each line numbered 1, 2, 3, …. For example calling the function with printNumberedChars("hello"); should output the following: 1. h 2. e 3. l 4. l 5. o Q2. Write a recursive function in C++ int sumArray(const int* arr, unsigned int size) { ... } which takes an array of integers,...

  • In C++ Write a program that contains a class called VideoGame. The class should contain the...

    In C++ Write a program that contains a class called VideoGame. The class should contain the member variables: Name price rating Specifications: Dynamically allocate all member variables. Write get/set methods for all member variables. Write a constructor that takes three parameters and initializes the member variables. Write a destructor. Add code to the destructor. In addition to any other code you may put in the destructor you should also add a cout statement that will print the message “Destructor Called”....

  • Write a Java class called BankAccount (Parts of the code is given below), which has two...

    Write a Java class called BankAccount (Parts of the code is given below), which has two fields name (String) and balance (double), two constructors and five methods getName(), getBalance(), deposit (double amount), withdraw(double amount) and toString(). The first constructor should initialize name to null and balance to 0. The second constructor initializes name and balance to the parameters passed. deposit method deposits the amount to the account causing the current balance to increase, withdraw method withdraws the amount causing the...

  • Write a Java class called BankAccount (Parts of the code is given below), which has two...

    Write a Java class called BankAccount (Parts of the code is given below), which has two fields name (String) and balance (double), two constructors and five methods getName(), getBalance(), deposit (double amount), withdraw(double amount) and toString(). The first constructor should initialize name to null and balance to 0. The second constructor initializes name and balance to the parameters passed. deposit method deposits the amount to the account causing the current balance to increase, withdraw method withdraws the amount causing the...

  • C++, entry level no pointers or vectors. Write a class called Person that has two data...

    C++, entry level no pointers or vectors. Write a class called Person that has two data members - a string variable called name and a double variable called age. It should have a constructor that takes two values and uses them to initialize the data members. It should have get methods for both data members (getName and getAge), but doesn't need any set methods. Write a separate function (not part of the Person class) called stdDev that takes two parameters...

  • . Q7. Write a C++ class called Point2D that describes a 2-dimensional (x,y) point. It should...

    . Q7. Write a C++ class called Point2D that describes a 2-dimensional (x,y) point. It should have the following features: • A constructor that accepts two double values (x,y) to create a point object. A default constructor. • A void print() method that prints out the points coordinates. • A void set(Point2D p) method that allows you to set the point using the given object of Point2D. • A Point2D midPoint(Point2D p1) method that returns a Point2D object that is...

  • Write a class called Point that contains two doubles that represent its x- and y-coordinates. It...

    Write a class called Point that contains two doubles that represent its x- and y-coordinates. It should have get and set methods for both fields. It should have a constructor that takes two double parameters and initializes its coordinates with those values. It should have a default constructor that initializes both coordinates to zero. It should also contain a method called distanceTo that takes as a parameter another Point and returns the distance from the Point that was passed as...

  • java code Write a class called FractionObject that represents a fraction with an integer numerator and...

    java code Write a class called FractionObject that represents a fraction with an integer numerator and denominator as fields. A Fraction Object object should have the following methods: A constructor with int numerator, int denominator as parameters - Constructs a new fraction object to represent the ratio (numerator/denominator). The denominator cannot be 0, so output a message if O is passed and set the numerator and denominator to 1. A constructor with no parameters - Constructs a new FractionObject to...

  • Implement a Java class that is called RainFall that uses a double array to store the...

    Implement a Java class that is called RainFall that uses a double array to store the amount of rainfall for each month of the year. The class should have only one instance variable of type double[]. Implement the following methods in the RainFall class: 1. A constructor that creates and initializes all entries in the array to be -1. 2. toString: a method that returns a one-line String representation of the object that includes 12 double numbers that represent the...

  • Create a class called Student. This class will hold the first name, last name, and test...

    Create a class called Student. This class will hold the first name, last name, and test grades for a student. Use separate files to create the class (.h and .cpp) Private Variables Two strings for the first and last name A float pointer to hold the starting address for an array of grades An integer for the number of grades Constructor This is to be a default constructor It takes as input the first and last name, and the number...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT