The c++ program class called Time will have 3 integer data members named hrs, mi
ID: 3638446 • Letter: T
Question
The c++ program class called Time will have 3 integer data members named hrs, mins & secs which would be used to store hours, minutes and seconds. Member function will contain a constructor that supply’s default values “zero” for each data member, a display function that prints an object’s data values and lastly, an assignment operator that execute a member intelligent assignment between “2 Time objects”. This what I have come up with, why am receiving some many errors???#include <iostream>
using namespace std;
int main()
{
class Time
{
int hrs,mins,secs;
public:
Time(int Hrs=0,int Mins=0,int Secs=0;)
{
hrs=Hrs;
mins=Mins;
secs=Secs;
}
void display()
{
cout<<"hrs = "<<hrs<<endl;
cout<<"mins= "<<mins<<endl;
cout<<"secs= "<<secs<<endl;
}
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
class Time{
//variable declarations
//Functions
};
int main(){
Time t;
// t.functionName(parameters...);
}
class Time
{
int hrs,mins,secs;
public:
Time(){hrs=mins=secs=0;}
void display()
{
cout<<"hrs = "<<hrs<<" mins= "<<mins<<" secs= "<<secs<<endl;
}
Time operator=(Time& obj)
{
if(this != &obj)
{
hrs=obj.hrs;
mins=obj.mins;
secs=obj.secs;
}
}
};
int main()
{
int seconds=35800;
int hours = seconds/3600;
int tempminutes = seconds%3600;
int minutes = tempminutes/60;
seconds = tempminutes%60;
cout<<hours<<" "<<minutes<<" "<<seconds<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.