0% found this document useful (0 votes)
79 views9 pages

CS-212 Object Oriented Programing: Lab Manual # 03

The document describes a lab assignment to practice separating a class definition into header and implementation files. Students are asked to create a Rational class to represent fractions with member functions for arithmetic operations and converting to a float. The header file declares the class and functions. The implementation file defines the functions, including reducing fractions to lowest terms and printing outputs. The client code tests the class by calling its member functions on sample objects.

Uploaded by

Shahab Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views9 pages

CS-212 Object Oriented Programing: Lab Manual # 03

The document describes a lab assignment to practice separating a class definition into header and implementation files. Students are asked to create a Rational class to represent fractions with member functions for arithmetic operations and converting to a float. The header file declares the class and functions. The implementation file defines the functions, including reducing fractions to lowest terms and printing outputs. The client code tests the class by calling its member functions on sample objects.

Uploaded by

Shahab Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

CS-212 Object Oriented Programing

LAB MANUAL # 03

Course Instructor: Dr. Farhan Hussain

Lab Instructor: Syed Abubakar Bukhari

Group Members:
• Shahab Khan
• M.Suleiman Faisal
• Soban Qasim

Degree: 42

Syndicate: “C”

Object Oriented Programing DE-42


LAB # 03: Separate Header and Implementation Files

Tool used: Microsoft Visual Studio

Description

In this lab, we will see how to make class reusable by separating it into another files.
Header File
Class declarations are stored in a separate file. A file that contains a class declaration is called
header file. The name of the class is usually the same as the name of the class, with .h extension.
For example, the Time class would be declared in the file Time .h.

#pragma once
class Time
{
private :
int hour;
int minute;
int second;
public :
//with default value
Time(const int h = 0, const int m = 0, const int s = 0);
// setter function
void setTime(const int h, const int m, const int s);
// Print a description of object in " hh:mm:ss"
void print() const;
};

Implementation File
The member function definitions for a class are stored in a separate .cpp file, which is called the
class implementation file. The file usually has the same name as the class, with the .cpp extension.
For example the Time class member functions would be defined in the file Time.cpp.

#include <iostream>
#include <iomanip>
#include "Time.h"
using namespace std;

Time :: Time(const int h, const int m, const int s)


: hour(h), minute (m), second(s)
{}

void Time :: setTime(const int h, const int m, const int s)

Object Oriented Programing DE-42


{
hour = h;
minute = m;
second = s;
}
void Time :: print() const
{
cout << hour << ":" << minute << ":" << second << "\n";

Client Code
Client code is the one that includes the main function. This file should be stored by the name
main.cpp

#include <iostream>
using namespace std;
#include "Time.h"

int main()
{
Time t1(10, 50, 59);
t1.print(); // 10:50:59
Time t2;
t2.print(); // 06:39:09
t2.setTime(6, 39, 9);
t2.print(); // 06:39:09

return 0;
}

The advantages of storing class definition in separate file are


1. The class is reusable
2. The clients of the class know what member functions the class provides, how to call them and
what return types to expect
3. The clients do not know how the class's member functions are implemented.

Constructors:
A constructor is a special type of member function of a class which initializes objects of a class.
In C++, Constructor is automatically called when object(instance of class) create. It is special
member function of the class because it does not have any return type.

Object Oriented Programing DE-42


Lab Task:

1. Create a class Rational that stores a fraction in its original form (i.e. without finding
the equivalent floating pointing result). This class models a fraction by using two data
members: an integer for numerator and an integer for denominator. For this class,
provide the following functions:

a) A two-argument constructor that initializes the numerator and denominator(of


Private: integer types) to the values sent from calling function. This constructor
should prevent a 0 denominator in a fraction.

2. A function addition for addition of two rational numbers which takes an object of a
Rational as a parameter.
3. A function subtraction for subtraction of two rational numbers which takes an object
of a Rational as a parameter.
4. A function multiplication for multiplication of two rational numbers which takes an
object of a Rational as a parameter.
5. A function division for division of two rational numbers which takes an object of a
Rational as a parameter.
6. A function reduction for reduced form of rational number, with return type void.
7. A function printRationalAsFloating for converting rational to decimal of two rational
numbers with return type float.

Note: Reduce or simplify fractions that are not in reduced form.

Object Oriented Programing DE-42


Coding:

Header File:
#include<iostream>
#pragma once
using namespace std;
class Rational
{
int p, q;

public:

Rational(int a, int b)
{
p = a;

if (b != 0)
{
q = b;
}
else
{
cout << " Denominatior Is Zero........Error";
}
};

int sum(Rational a);


int sub(Rational a);
int mult(Rational a);
int div(Rational a);
void reduction(Rational a);
float printRationalAsFloat(Rational a);
};

Implementation file:
#include<iostream>
#include "Rational.h"
using namespace std;

int hcffunc(int a, int b);

int Rational::sum(Rational a)//for sum................


{
int num, den, hcf = 1;
num = (a.p * q) + (p * a.q);
den = q * a.q;
hcf = hcffunc(num, den);
num = num / hcf, den = den / hcf;
cout << "Sum of the two Rational numbers is: " << num << "/" << den;

Object Oriented Programing DE-42


return 0;
}

int Rational::sub(Rational a)//for substractiuon.........


{
int num, den, hcf = 1;
num = (p * a.q) - (a.p * q);
den = q * a.q;
hcf = hcffunc(num, den);
num = num / hcf, den = den / hcf;
cout << "Subtraction of the two Rational numbers is: " << num << "/" << den;
return 0;
}

int Rational::mult(Rational a)//for multiplication................


{
int num, den, hcf = 1;
num = (a.p * p);
den = q * a.q;
hcf = hcffunc(num, den);
num = num / hcf, den = den / hcf;
cout << "Multiplication of the two Rational numbers is: " << num << "/" << den;
return 0;
}

int Rational::div(Rational a)//for division...................


{
int num, den, hcf = 1;
num = a.q * p;
den = q * a.p;
hcf = hcffunc(num, den);
num = num / hcf, den = den / hcf;
cout << "Division of the two Rational numbers is: " << num << "/" << den;
return 0;
}

void Rational::reduction(Rational a)//for reduction..................


{
int hcf;
hcf = hcffunc(p, q);
p = p / hcf, q = q / hcf;
cout << "\nThe Reduced Rational number 1 is : " << p << "/" << q;

hcf = hcffunc(a.p, a.q);


a.p = a.p / hcf, a.q= a.q / hcf;
cout << "\nThe Reduced Rational number 2 is : " << a.p << "/" << a.q;
}

float Rational::printRationalAsFloat(Rational a)//for rational as floating........


{
float pop1, pop2;
pop1 = (float)p / q;
pop2 = (float)a.p / a.q;

cout << "\nRational number 1 as float is: " << pop1;

return pop2;
}

Object Oriented Programing DE-42


int hcffunc(int x, int y)
{
if (y < 0)
y = -y;
if (x < 0)
x= -x;

int hcf = 1, i, lab;


if (y > x)
{
lab= y;
y = x;
x= lab;
}
for (int i = 1; i <= y; i++)
{
if (x % i == 0 && y % i == 0)
hcf = i;
}
return hcf;
}

Client Code:
#include<iostream>
#include "Rational.h"
using namespace std;

int main()
{
Rational r1(1, 4), r2(3, 8);
r1.sum(r2);
cout << "\n";
r1.sub(r2);
cout << "\n";
r1.mult(r2);
cout << "\n";
r1.div(r2);
r1.reduction(r2);
float newrat2 = r1.printRationalAsFloat(r2);
cout << "\nRational number 2 as float is: " << newrat2;
getchar();
return 0;
}

Output:

Object Oriented Programing DE-42


Object Oriented Programing DE-42
Object Oriented Programing DE-42

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy