0% found this document useful (0 votes)
9 views6 pages

Programming For Problem Solving: Experiment - 12

The document outlines programming tasks focused on object-oriented concepts in C++. It includes creating a class for shapes to calculate areas, overloading the + operator for adding time and distance objects, and overloading a sum function for different types of inputs. Each task is accompanied by sample code demonstrating the implementation of these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

Programming For Problem Solving: Experiment - 12

The document outlines programming tasks focused on object-oriented concepts in C++. It includes creating a class for shapes to calculate areas, overloading the + operator for adding time and distance objects, and overloading a sum function for different types of inputs. Each task is accompanied by sample code demonstrating the implementation of these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

PROGRAMMING FOR PROBLEM SOLVING

EXPERIMENT – 12
Task 1: Create a class named “Shapes” with integer variable “Area”. Write
a member function “calArea” with two float parameters to calculate the
area of rectangle and overload the same function with having one float
parameter to calculate the area of square.

PROGRAM:

Task 2: Write a program to overload + operator to add two instances of


time (having hours and minutes).

PROGRAM:

#include <iostream>

using namespace std;

class Time {

private:

int HR, MIN, SEC;

public:

void setTime(int x, int y, int z)

HR = x;

MIN = y;

SEC = z;

void showTime()

{
cout << endl

<< HR << ":" << MIN << ":" << SEC;

void normalize()

MIN = MIN + SEC / 60;

SEC = SEC % 60;

HR = HR + MIN / 60;

MIN = MIN % 60;

Time operator+(Time t)

Time temp;

temp.SEC = SEC + t.SEC;

temp.MIN = MIN + t.MIN;

temp.HR = HR + t.HR;

temp.normalize();

return (temp);

};

int main()

Time t1, t2, t3;

t1.setTime(5, 50, 30);


t2.setTime(7, 20, 34);

t3 = t1 + t2;

t1.showTime();

t2.showTime();

t3.showTime();

return 0;

Task 3: write a program using class distance that creates an object and gets
value from user in feet and inches. It then adds these values with the values
of another object by overloading of + operator.

PROGRAM:

#include <iostream>

using namespace std;

class Distance {

private:

int feet, inches;

public:

void readDistance(void)

cout << "Enter feet: ";

cin >> feet;

cout << "Enter inches: ";

cin >> inches;

void dispDistance(void)
{

cout << "Feet:" << feet << "\t"

<< "Inches:" << inches << endl;

Distance operator+(Distance& dist1)

Distance tempD; // to add two distances

tempD.inches = inches + dist1.inches;

tempD.feet = feet + dist1.feet + (tempD.inches / 12);

tempD.inches = tempD.inches % 12;

return tempD;

};

int main()

Distance D1, D2, D3;

cout << "Enter first distance:" << endl;

D1.readDistance();

cout << endl;

cout << "Enter second distance:" << endl;

D2.readDistance();

cout << endl;

D3 = D1 + D2;

cout << "Total Distance:" << endl;


D3.dispDistance();

cout << endl;

return 0;

Task 4: Write a program to overload sum function to perform addition of


two integers, 3 integers and two floating point numbers.

PROGRAM:

#include<iostream>

using namespace std;

int sum(int x, int y)

return x+y;

double sum(double x, double y)

return x+y;

int sum (int x, int y, int z)

return x+y+z;

int main()

cout <<"The Sum of two integers: "<<sum(10, 20)<<endl;

cout <<"The Sum of two floats: "<<sum(10.5, 20.7)<<endl;


cout <<"The Sum of three integers: "<<sum(10, 20, 30)<<endl;

You might also like