Breaking

Be Strong Let's Try!

Thursday, 16 September 2021

Separate Header and Implementation Files | Data structure

 

Description

In this topic, 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)
{
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

The clients do not know how the class's member functions are implemented.
*Here is the task we will try to solve it.

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, with return type Rational.

3.      A function subtraction for subtraction of two rational numbers which takes an object of  a Rational as a parameter, with return type Rational.

4.      A function multiplication for multiplication of two rational numbers which takes an object of  a Rational as a parameter, with return type Rational.

5.      A function division for division of two rational numbers which takes an object of  a Rational as a parameter, with return type Rational.

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.


Rational.h: 

#pragma once

 

#include "pch.h"

 

#include<iostream>

 

 

 

class Rational

{

private:

 public:

 int numerator; // as the the numertor is used in test file so i make them int denominator; // public to access them

 Rational();

 Rational(int n, int d);

 Rational addition(Rational q);

 Rational subtraction(Rational q);

 Rational multiplication(Rational q);

 Rational division(Rational q);

 void reduction();

 float printRationalAsFloating();

 void printRational();

 };

 

 Source.cpp

 #include "pch.h"

 #include "Rational.h"

 using namespace std;

  Rational::Rational():numerator(0), denominator(1)

 {

 

} 

Rational::Rational(int n, int d) :numerator(n), denominator(d)

 {

 if (d == 0)

 {

 cout << "Denominator cannot be zero" << endl;

 exit;

 }

 }

 Rational Rational::addition(Rational q)

 {

 Rational t;

 t.numerator = numerator * q.denominator + denominator * q.numerator; t.denominator = denominator * q.denominator; t.reduction();

return t;

 }

 Rational Rational::subtraction(Rational q)

 {

 Rational t;

 t.numerator = numerator * q.denominator - denominator * q.numerator; t.denominator = denominator * q.denominator; t.reduction();

return t;

 }

 Rational Rational::multiplication(Rational q)

 {

 Rational t;

 t.numerator = numerator * q.numerator;

 t.denominator = denominator * q.denominator;

 t.reduction();

 return t;

 

}

 Rational Rational::division(Rational q)

 {

 Rational t;

 t.numerator = numerator * q.denominator;

 t.denominator = denominator * q.numerator;

 t.reduction();

 return t;

 }

 void Rational::reduction()

 {

 int d,n, max = 1;

 n = numerator;

 

if (numerator < 0)

 n = -numerator;

 if (n > denominator)

 d = denominator;

 else

 }

 numerator = numerator / max;

 denominator = denominator / max;

 if (denominator == 1)

cout << denominator << endl;

 else

 cout <<numerator<<"/" <<denominator<< endl;

 }

 float Rational::printRationalAsFloating()

 {

 float r;

 r  = static_cast<float>(numerator) / static_cast<float>(denominator); return r;

 }

 

void Rational::printRational()

{

 if (denominator == 1)

 cout << denominator << endl;

 else

 cout << numerator << "/" << denominator << endl;

 }

Test.cpp

#include "pch.h"

 #include "gtest/gtest.h"

 Rational c(2, 6), d(9, 3), x;

 TEST(TestCaseName, add) {

 x = c.addition(d);

 x.printRational();

 float x1 = x.numerator, x2 = x.denominator;

 float r = x1 / x2;

 EXPECT_FLOAT_EQ(3.333333, x.printRationalAsFloating());

 }

 TEST(TestCaseName1, sub) {

x = c.subtraction(d);

 /*float x1 = x.numerator, x2 = x.denominator;

 float r = x1 / x2;*/

 EXPECT_FLOAT_EQ(-2.6666667, x.printRationalAsFloating());

 

}

TEST(TestCaseName3, mult) {

 x = c.multiplication(d);

 


} 

 TEST(TestCaseName4, div) {

x = c.division(d);

 /*float x1 = x.numerator, x2 = x.denominator;

 float r = x1 / x2;*/

 EXPECT_FLOAT_EQ(0.11111111, x.printRationalAsFloating());

 

}

 TEST(TestCaseName5, reducedformA) {

 x = c.addition(d);

 /*float x1 = x.numerator, x2 = x.denominator;*/

ASSERT_EQ(10, x.numerator); ASSERT_EQ(3, x.denominator);

}

 TEST(TestCaseName6, reducedformS) {

 x = c.subtraction(d);

 /*float x1 = x.numerator, x2 = x.denominator;*/ 

ASSERT_EQ(-8, x.numerator); ASSERT_EQ(3, x.denominator);

}

 TEST(TestCaseName7, reducedformM) {

 x = c.multiplication(d);

 /*float x1 = x.numerator, x2 = x.denominator;*/

ASSERT_EQ(1, x.numerator); ASSERT_EQ(1, x.denominator);

}

 TEST(TestCaseName8, reducedformD) {

 x = c.division(d);

 /*float x1 = x.numerator, x2 = x.denominator;*/ 

ASSERT_EQ(1, x.numerator); ASSERT_EQ(9, x.denominator);

}


CLICK HERE TO RUN THE CODE:

OUTPUT:

Separate Header and Implementation Files | Data structure




Separate Header and Implementation Files | Data structure




No comments:

Post a Comment

Pages