CS-212 Object Oriented Programing: Lab Manual # 03
CS-212 Object Oriented Programing: Lab Manual # 03
LAB MANUAL # 03
Group Members:
• Shahab Khan
• M.Suleiman Faisal
• Soban Qasim
Degree: 42
Syndicate: “C”
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;
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;
}
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.
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:
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.
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";
}
};
Implementation file:
#include<iostream>
#include "Rational.h"
using namespace std;
return pop2;
}
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: