Description
In
this topic, we will see how to make class reusable by separating it into another
files.
Header File:
#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
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:
Source.cpp
Rational::Rational():numerator(0), denominator(1)
}
Rational::Rational(int n, int d)
:numerator(n), denominator(d)
return t;
return t;
}
if (numerator < 0)
cout << denominator << endl;
void Rational::printRational()
{
Test.cpp
#include "pch.h"
#include "gtest/gtest.h"
x = c.addition(d);
x = c.subtraction(d);
}
TEST(TestCaseName3, mult) {
}
x = c.division(d);
}
ASSERT_EQ(10, x.numerator); ASSERT_EQ(3, x.denominator);
}
ASSERT_EQ(-8, x.numerator); ASSERT_EQ(3, x.denominator);
}
ASSERT_EQ(1, x.numerator); ASSERT_EQ(1, x.denominator);
}
ASSERT_EQ(1, x.numerator); ASSERT_EQ(9, x.denominator);
}
CLICK HERE TO RUN THE CODE:
OUTPUT:
No comments:
Post a Comment