LAB
REPORT 05
Object Oriented Programming
MUHAMMAD Rehan Tabassum
FA19-BEE_139
EEE-3
Submitted To
Madam Nayab Gogosh
5. Lab Tasks
5.1.
Area of a circle is π × r2 where r = radius
Area of a triangle is ½ × b × h where b = base, h = height
Area of a rectangle is w × h w = width, h = height
Area of a square is a2 where a = length of side
Write four different functions with same name that is Area to calculate the area of circle, triangle,
rectangle and square.
code
#include <iostream>
using namespace std;
const float pi=3.14;
//the function calculate area of circle
void area(float r)
{
float ar;
ar=pi*r*r;
cout<<"area of circle = "<<ar<<"\n";
}
//the function calculate area of triangle
void area(float n,float b,float h)
{
float ar;
ar=n*b*h;
cout<<"area of triangle = "<<ar<<"\n";
}
//the function calculate area of rectangle
void area(float w,float h)
{
float ar;
ar=w*h;
cout<<"area of rectangle = "<<ar<<"\n";
}
//the function calculate area of square
void area(double s)
{
float ar;
ar=s*s;
cout<<"area of square = "<<ar<<"\n";
}
int main()
{
float b,h,w,r;
double s;
cout<<"Enter the radius of circle : ";
cin>>r;
area(r);
cout<<"Enter the base and height of triangle : ";
cin>>b>>h;
area(0.5,b,h);
cout<<"Enter the width and height of rectangle : ";
cin>>w>>h;
area(w,h);
cout<<"Enter the side of square : ";
cin>>s;
area(s);
return 0;
}
Output
5.2.
Write a definition of a Counter class having one private data member count of integer type. This
class has following functions
• void inc_count( ); // will increment the value of count by 1
• int get_count ( ); // will return the value of count this class has two contructor
• Counter( ); // that initialize count by 0
• Counter (int i); // that initialize the count by i
Code
#include <iostream>
using namespace std;
class Counter{
int count;
public:
Counter(){
count=0;
cout<<"default constructor"<<endl;
}
Counter(int i)
{
count=i;
cout<<"constructor with parameter"<<endl;
}
void inc_count()
{
count=count+1;
}
int get_count()
{
return count; }};int main()
{
Counter c1;
cout<<"count from object1 :"<<c1.get_count()<<endl;
c1.inc_count();
c1.inc_count();
c1.inc_count();
cout<<"count from object1 after calling inc_count() three times :"<<c1.get_count()<<endl;
Counter c2(10);
cout<<"count from object2 :"<<c2.get_count()<<endl;
c2.inc_count();
c2.inc_count();
c2.inc_count();
c2.inc_count();
cout<<"count from object2 after calling inc_count() four times :"<<c2.get_count()<<endl;
return 0;
}
5.3
Write a definition of class named Race. It has following private data member
• carNo (int)
• driverID (int)
• carModel (int)
Code
#include<iostream>
using namespace std;
class Race{
private:
int carNo;
int driverID;
int carModel;
public:
Race(int carNo,int driverID,int carModel){
this->carNo=carNo;
this->driverID=driverID;
this->carModel=carModel;
}
void InputValues(){
int carNo;
cout<<"Enter car number: ";
cin>>carNo;
this->carNo=carNo;
int driverID;
cout<<"Enter driver id: ";
cin>>driverID;
this->driverID=driverID;
int carModel;
cout<<"Enter car Model: ";
cin>>carModel;
this->carModel=carModel;
void setValues(int cn,int di,int cm){
this->carNo=cn;
this->driverID=di;
this->carModel=cm;
void display(){
cout<<"\n***Car Information***"<<endl;
cout<<"Car Number: "<<carNo<<endl;
cout<<"Car DriverId: "<<driverID<<endl;
cout<<"Car model: "<<carModel<<endl;
}
};
int main(){
Race r1(567,8797,7800);
Race r2(765,5578,7900);
r1.InputValues();
r2.setValues(809767,98889,7800);
r1.display();
r2.display();
}
5.4 Write a definition of a distance class as shown in the example 4.2 above. Make all the
appropriate function constant. Include a constant data member called id of integer type.
Create two object constant and non-constant. Assign values and display them. Also check
what happens
• If you try to modify private data member of an object from the definition of const
function
• If you try to modify the private data member of const object from the definition of non-
constant function.
Code
#include<iostream>
using namespace std;
class Distance{
private:
int feet;
float inches;
const int id = 101;
public:
Distance(){
cout << "default constructor" << endl;
feet = 0;inches = 0;
}
Distance(float mtrs){
cout << "one argument constructor" << endl;
float ft = mtrs*3.28084;
feet = (int)ft;
inches = (ft-feet)*12;
}
Distance(int f,float i){
cout << "two argument constructor" << endl;
feet = f;inches = i;
}
void setdist(int ft,float in){
feet = ft;
inches = in;
}
void getdist(){
cout << "\nEnter feet: ";cin >> feet;
cout << "Enter inches: ";cin >> inches;
}
void initialize(){
feet = 0;
inches = 0;
}
int getId() const{
return id;
}
void showdist() const{
cout << "feet = " << feet << "\tinches = " << inches << endl;
}
};
int main(){
Distance dist1;
dist1.setdist(11,6.75);
const Distance dist2(10,3.4);
dist1.showdist();
dist2.showdist();
cout << dist1.getId() << endl;
cout << dist2.getId() << endl;
return 0;
}
6. Home Tasks
Write a definition of class named Date that contains three elements the month, the day of the
month, and the year, all of type int.
• Write two constructors, a default constructor (that initialize each data element of object with
zero) and a constructor that takes three parameters (the month, the day of the month, and the
year) and initialize the data member of the object with these parameters.
• Write a function void printDate() that displays the data elements of the object.
• Write a function void setDate(int, int, int) that takes three parameters (he month, the day of
the month, and the year) and initialize the data member of the object with these parameters.
Write a main function create two object of class Date, the data member of one object is initialized
with zero through default constructor. The data member of second object is initialized with some
values using a constructor that takes three parameters.
Prompt the user to input date (the month, the day of the month, and the year) in a main function,
assign these values to the first object (using function setDate) and then display the value of the data
members of two objects using function printDate().
Code
#include <iostream>
#include <string>
using namespace std;
class Date{
private:
int month;
int day;
int year;
public:
Date(int m,int d,int y){
if(d>0 || d<=31){
day = d;
}
if(m>0 || m<=12){
month = m;
}
if(y>=1900){
year = y;
}
}
void setDate(int m,int d,int y){
if(d>0 || d<=31){
day = d;
}
if(m>0 || m<=12){
month = m;
}
if(y>=1900){
year = y;
}
}
Date(){
month = 1;
year = 1900;
day = 1;
}
int getDay() const
{
return day;
}
int getMonth() const
{
return month;
}
int getYear() const
{
return year;
}
void setDay(int day)
{
if(day>0 || day<=31)
this->day = day;
}
void setMonth(int month)
{
if(month>0 || month<=31)
this->month = month;
}
void setYear(int year)
{
if(year>1900)
this->year = year;
}
void printDate(){
cout<<year<<"/";
if(month>9){
cout<<month<<"/";
}else{
cout<<"0"<<month<<"/";
}
if(day>9){
cout<<day;
}else{
cout<<"0"<<day;
}
}
};
int main() {
Date dateDefault;
cout<<"Default date print : ";
dateDefault.printDate();
cout<<"\nParameter date print : ";
Date date(12, 25, 2020);
date.printDate();
int m,y,d;
cout<<"\nEnter month : ";
cin>>m;
cout<<"Enter day : ";
cin>>d;
cout<<"Enter year : ";
cin>>y;
dateDefault.setDate(m,d,y);
cout<<"Entered date is : ";
dateDefault.printDate();
}
Critcle analysis
In this lab we have learnt the concept and utility of constructors, how they are helpful in object
initialization, different forms of a constructor, method overloading as well as const keyword is also
covered in the lab. After completion of this Lab we know what is a constructor, when it is called and
how it can be called and get benefit of method overloading where ever required. Student will also
know what are a constant function, constant member and constant objects. Initializing the object
private data member can be performed with a help of a public member function. With this
approach you have to call that function after creating each object of that class. It will be a
convenient approach if and object’s private data members can be initialize itself when it is created,
without requiring a separate call to a member function. Automatic initialization is carried out using a
special member function called a constructor. A constructor is a member function that is executed
automatically whenever an object is created. A constructor having no argument is called a default
constructor. Function overloading is a concept where you can have multiple functions with same
name. When a call to one of such function take place the compiler can differentiate as follow