Lab 03 Converted 1
Lab 03 Converted 1
1. Objectives
The objective of this lab is to teach the students, the scope of class data members and its
member functions.
2. Outcome
At the end of this lab student will be familiar with the accessing rules of class data members
and member functions
3. Introduction
In object oriented programming, methods and variables have various scope. Scope means
that the method or variable may or may not be directly accessible to other objects or classes.
Classes that do not have instances may be accessible to the system.
The following program illustrates the usage of objects and classes in C++:
//The program uses public member functions to input two private numbers, add
two private numbers and display the result
#include<iostream>
5. In Lab Tasks
5.1. Code the example given above and check the errors if you try to access the private data
members in main() function. Also modify the above task by making the scope of public
member functions as private. Create access functions in public scope to access private
member functions from main().
5.2. Create an employee class, The member data should comprise an int for storing the
employee number and a float for storing the employee’s compensation. Member
functions should allow the user to enter this data and display it. Write a main() that
allows the user to enter data for three employees and display it.
5.3. 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. The
final member function should add two objects of type time passed as arguments.
A main() program should create two initialized time objects and
one that isn’t initialized. Then it should add the two initialized values together, leaving
the result in the third time variable. Finally it should display the value of this third
variable.
5.4. Create a fraction class. Member data is the fraction’s numerator and denominator.
Member functions should accept input from the user in the form 3/5, and output the
fraction’s value in the same format. Another member function should add two fraction
values. Write a main() program that allows the user to repeatedly input two fractions and
then displays their sum. After each operation, ask whether the user wants to continue.