OOP LAB 2 Cs
OOP LAB 2 Cs
Lab #02
Task 1
Create a class “Add” to calculate the sum of two decimal values by simply using
class syntax and access member functions through objects. Take both the values
from user at run time and store it in a variable named “result”.
#include <iostream>
using namespace std;
class add
private:
double a , b, result;
public:
void inputdata();
void displaydata();
};
void add::inputdata()
cin>>a>>b;
void add::displaydata()
result= a+b;
int main()
add obj1;
obj1.inputdata();
obj1.displaydata();
Output
Task 2
Create a class “Rectangle” to calculate the area of rectangle by simply
using class syntax and access member functions through objects.
#include <iostream>
using namespace std;
class rectangle
{
private:
int area,len,wid;
public:
void inputarea();
void displayarea();
};
void rectangle::inputarea()
{
cout<<"Enter the length and width of rectangle"<<endl;
cin>>len>>wid;
}
void rectangle::displayarea()
{
area=len*wid;
cout<<" Area of rectangle is "<<area<<endl;
}
int main()
{
rectangle obj1;
obj1.inputarea();
obj1.displayarea();
}
Output
Task 3
Create a class called Date that has separate int member data for date, month
and year. One member function should take the input from the user. Another
member function should contain fixed values. Another member function should
display the date, in 15-03-2021 format.
#include <iostream>
class Date
private:
int date,month,year;
public:
void inputdata();
void fixdata();
void displaydata();
};
void Date::inputdata()
void Date::fixdata()
year=2002;
void Date::displaydata()
int main()
Date obj1;
obj1.inputdata();
obj1.fixdata();
obj1.displaydata();
Output