OOP Lab Report 03
OOP Lab Report 03
Lab #: 03
Lab Title: Class Scope and Accessing Class Members
Submitted by:
Name Registration #
AMMAR FA19-BCE-001
MUHAMMAD KALEEM ULLAH FA19-BCE-007
In-Lab Post-Lab
Post- Lab
3.6. Encapsulation
The process of providing a public interface to interact with the object while hiding other
information inside the object is called encapsulation.
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().
Code:
#include <iostream>
using namespace std;
class Subtract
{
private:
int Data_1;
int Data_2;
public:
void Assigner(int,int);
void Display_Result();
int Subtractor() ;
};
int Subtract::Subtractor()
{
return(Data_1-Data_2);
}
void Subtract::Display_Result()
{
cout<<"\n\t The Result From Subtraction Of Data-1 And
Data-2 Is : "<<Subtractor();
}
int main()
{
Subtract Alpha;
int Data1,Data2;
cout<<"\n\t Input The Number 1 Data : ";cin>>Data1;
cout<<"\n\t Input The Number 2 Data : ";cin>>Data2;
Alpha.Assigner(Data1,Data2);
Alpha.Subtractor();
Alpha.Display_Result();
Output:
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.
Code:
#include <iostream>
using namespace std;
class Employee
{
private:
int Number;
float Compen;
public:
void Input()
{
cout<<"\n\t Input
The Employee Number : ";cin>>Number;
cout<<"\n\t Input
The Employee Compensation : ";cin>>Compen;
}
void Display()
{
cout<<"\n\n\t Employee
Number : "<<Number<<"\n\t Compensation Is : "<<Compen<<endl;
}
};
int main()
{
Employee Alpha[3];
for(int i=0;i<3;i++)
{
Alpha[i].Input();
}
system("cls");
for(int j=0;j<3;j++)
{
Alpha[j].Display();
}
}
Output:
------------------------------------------------------------------------------------------------------------
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.
Code:
#include <iostream>
using namespace std;
class Time
{
public:
int Hours;
int Mins;
int Secs;
char Meri;
Time() // Constructor No 1
{
Hours=0;
Mins=0;
Secs=0;
}
void Display_12()
{
if(Mins>60)
{
Hours++;
Mins=Mins-60;
}
if(Secs>60)
{
Mins++;
Secs=Secs-60;
}
if(Hours > 12)
{
Hours=Hours-12;
Meri='P';
}
if(Meri=='P')
{
cout<<"\n\n\t Time In Standered 12 Hour Format
is :\t "<<Hours<<" Hour : "<<Mins<<" Miuntes : "<<Secs<<"
Seconds\tPm";
}else
{
cout<<"\n\n\t Time In Standered 12 Hour Format
is :\t "<<Hours<<" Hour : "<<Mins<<" Miuntes : "<<Secs<<"
Seconds\tAm";
}
}
void Display_Final()
{
if(Mins>60)
{
Hours++;
Mins=Mins-60;
}
if(Secs>60)
{
Mins++;
Secs=Secs-60;
}
cout<<"\n\n\t The Sum Of Both Entered Time is :\t
"<<Hours<<" Hour : "<<Mins<<" Miuntes : "<<Secs<<" Seconds";
}
};
int main()
{
Time Alpha_1;
// Time No 1
int Ho,Mi,Se;
cout<<"\n\n\t Please Enter The Hours: "; cin>>Ho;
cout<<"\n\t Please Enter The Minutes: "; cin>>Mi;
cout<<"\n\t Please Enter The Seconds: "; cin>>Se;
Time Alpha_2(Ho,Mi,Se);
// Time No 2
int Ho2,Mi2,Se2;
cout<<"\n\n\t Please Enter The Hours: "; cin>>Ho2;
cout<<"\n\t Please Enter The Minutes: "; cin>>Mi2;
cout<<"\n\t Please Enter The Seconds: "; cin>>Se2;
Time Alpha_3(Ho2,Mi2,Se2);
// Displayer
Alpha_2.Display_12();
Alpha_3.Display_12();
Alpha_1.Adder(Alpha_2,Alpha_3);
Alpha_1.Display_Final();
}
Output:
------------------------------------------------------------------------------------------------------------
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.
Code:
#include <iostream>
using namespace std;
class Fraction
{
private:
int Nomi;
int Denom;
public:
void Inputer(int A,int B)
{
Nomi=A;
Denom=B;
}
void Display()
{
cout<<"\n\n\t Value = "<<Nomi<<"/"<<Denom;
}
B.Nomi=B.Nomi*X;
B.Denom=B.Denom*X;
}
}
};
int main()
{
Fraction Alpha[3];
int Nomii,Denomm;
for(int i=0;i<2;i++)
{
cout<<"\n\t"<<"("<<i+1<<")-"<<" Input The Values Of
Nominator : ";cin>>Nomii;
cout<<"\n\t Input The Values Of Denominator :
";cin>>Denomm;
Alpha[i].Inputer(Nomii,Denomm);
}
system("cls");
// Displayer
for(int i=0;i<2;i++)
{
Alpha[i].Display();
}
// Adder Caller
Alpha[2].Adder(Alpha[0],Alpha[1]);
}
Output:
class Date
{
private:
int Year;
int Month;
int Date;
public:
void Get_Data(int D,int M,int Y);
void Display() const;
};
}
Output:
----------------------------------------------------------------------------------------------------------------
6.2. Create a class of subtraction having two private data members. Create member
functions to get data from users and for subtraction of data members. Use
appropriate access modifiers for class methods.
Code:
#include <iostream>
using namespace std;
class Subtract
{
private:
int Data_1;
int Data_2;
public:
void Assigner(int,int);
void Display_Result();
int Subtractor() ;
};
int Subtract::Subtractor()
{
return(Data_1-Data_2);
}
void Subtract::Display_Result()
{
cout<<"\n\t The Result From Subtraction Of Data-1 And
Data-2 Is : "<<Subtractor();
}
int main()
{
Subtract Alpha;
int Data1,Data2;
cout<<"\n\t Input The Number 1 Data : ";cin>>Data1;
cout<<"\n\t Input The Number 2 Data : ";cin>>Data2;
Alpha.Assigner(Data1,Data2);
Alpha.Subtractor();
Alpha.Display_Result();
}
Output:
Conclusions:
One of the biggest advantages of C++ is the feature of object-
oriented programming which includes concepts like classes,
inheritance, polymorphism, data abstraction, and encapsulation that
allow code reusability and makes a program even more reliable.
Access modifier provides you the authority to control your data
depending upon the scenarios. ...
All base class public member becomes public members of the derived
class. ...
In private inheritance scenario, all base class public member becomes
private members of the derived class.