oopm ex-4
oopm ex-4
Problem Statement: C++ program to read time in HH:MM:SS format and convert into total seconds
Objectives: Defining functions inside class in C++
Theory:
An object is an instance of a class. It represents a specific entity with its own state and behaviour.
Member Functions:
Member functions are functions that are defined within a class. They operate on the data
members of the class and define the object's behaviour.
Key Points:
1. Data Members:
o These are variables declared within a class that store data specific to an object of
that class.
o They can be of any data type, including primitive data types (like int, float, char)
and user-defined data types (like other classes).
2. Member Functions:
o These functions are defined within a class and are used to manipulate the data
members of the class.
o They can be used to:
Initialize data members (constructor)
Access and modify data members
Perform calculations or operations on data members
Interact with other objects
3. Access Specifiers:
o public: Members declared as public can be accessed from anywhere outside the
class.
o private: Members declared as private can only be accessed within the class.
1|Page
o protected: Members declared as protected can be accessed within the class and
its derived classes.
4. Member Function Calls:
o Member functions are called using the dot operator (.) on an object of the class.
Program:
#include<iostream>
using namespace std;
class Time
{
public:
int seconds;
int HH,MM,SS;
void gettime();
void convertIntoTime();
void display();
};
void Time::gettime()
{
cout<<"Enter hours: ";
cin>>HH;
cout<<"Enter minutes: ";
cin>>MM;
cout<<"Enter seconds:";
cin>>SS;
}
void Time::convertIntoTime()
{
seconds=HH*3600+MM*60+SS;
}
void Time::display(){
cout<<"Time in second is: "<<seconds;
}
int main(){
Time obj;
2|Page
obj.gettime();
obj.convertIntoTime();
obj.display();
}
Output:
Enter hours: 2
Enter minutes: 20
Enter seconds:10
Time in second is: 8410
3|Page