Array objects and const Member function in oop(object oriented programming)
Array objects and const Member function
Task 1:
Create a class called time that has separate int member data
for hours, minutes, and seconds.
One constructor should initialize this data to 0, and
another should initialize it to fixed values.
Another member function should display
it, in 11:59:59 format.
#include<iostream>
#include<string>
using
namespace std;
class time
{
int h, m, s;
public:
time();
time(int hr, int min, int sec);
void show() const;
};
int main()
{
time t, t1(11, 69, 59);
t1.show();
getchar();
getchar();
return 0;
}
time::time()
{
h = m = s = 0;
}
time::time(int
hr, int min, int sec)
{
if (hr >= 1 && hr <=
12)
{
h = hr;
}
if (min >= 0 && min <=
59)
{
m = min;
}
if (sec >= 0 && sec <=
59)
{
s = sec;
}
else
{
cout <<
"Enter hours between 1 and 12" << endl;
cout <<
"Enter the minute between 0 and 59" << endl;
cout <<
"Enter the seconds between 0 and 59" << endl;
}
}
void
time::show() const
{
cout << h <<
":" << m << ":" << s << endl;
}
Task 2:
Write a program with a class that contains following data
members
Id
name
marks
Create and array of objects of size 10. Create a function
read() to take input from user. Create
a function to calculate percentage of each student. Then
create a function that displays the
student id and name of the student with
the lowest percentage.
#include<iostream>
#include<string>
using
namespace std;
class
Student{
int id;
string name;
int marks;
public:
void read();
void eachper();
void lowper(int max);
};
int main()
{
Student t[10];
for (int i = 0; i < 3; i++)
{
t[i].read();
}
for (int i = 0; i < 3; i++)
{
t[i].eachper();
}
for (int i = 0; i < 3; i++)
{
t[i].lowper(0);
}
getchar();
getchar();
return 0;
}
void
Student::read()
{
cout << "Enter the id of
the student:";
cin >> id;
cin.ignore();
cout << "Enter the name
of the student:";
getline(cin, name);
cout << "Enter the marks
of the student:";
cin >> marks;
}
void
Student::eachper()
{
cout << "Obtain marks
are:" << marks << endl;
}
void
Student::lowper(int max)
{
cout << endl << endl
<< endl;
int temp = 0;
for (int i = 0; i < 3; i++)
{
if (max > marks)
{
max = marks;
temp = max;
marks =
temp;
}}
cout << "Student lowest
marks are:"<<marks<< endl;
}
Task3:
Create a class personAge with following data members: name,
dob, age. Calculate age by
taking name and dob as input from user.
Make appropriate constant functions and variables.
#include<iostream>
#include<string>
using
namespace std;
class
Personage{
string name;
int dob;
int age;
public:
void input();
void set();
void show() const;
};
int main()
{
Personage p1;
p1.input();
p1.show();
getchar();
getchar();
return 0;
}
void
Personage::input()
{
cout << "Enter the
name:";
getline(cin, name);
cout << "Enter the date
of birth:";
cin >> dob;
cin.ignore();
age = (2020 - dob);
}
void
Personage::show() const
{
cout << "Your name
is:" << name << endl;
cout << "Your ware born
in:" << dob << endl;
cout << "Your age
is:" << age << endl;
}
Comments
Post a Comment