Composition Multi-File STEP 1: Create a Multifile Project for the Composition La
ID: 3550769 • Letter: C
Question
Composition Multi-File
STEP 1: Create a Multifile Project for the Composition Lab
1. Add three classes to the project: a Filter class, a Capacitor class, and a Resistor class.
2. You may use the Resistor class files from the Week 2 lab for this lab.
3. The Capacitor class should be modeled after the Resistor class for class members and operation.
STEP 2: Filter-Class Definition
The Filter class should have, at a minimum, the following capabilities.
STEP 3: Test-Program Operation
//Resistor.cpp
//Resistor class implementation
#include "Resistor.h"
#include <iostream>
using namespace std;
Resistor::Resistor()
{
nominalValue=0;
tolerance=0;
}
/*Resistor::Resistor(double v, double t)
{
nominalValue=v;
tolerance=t;
}*/
void Resistor::setNomValue(double v)
{
nominalValue=v;
}
void Resistor::setTolerance(double t)
{
tolerance=t;
}
double Resistor::getNomValue()
{
return nominalValue;
}
double Resistor::getTolerance()
{
return tolerance;
}
void Resistor::printValue()
{
if (getNomValue() >= 1000000) {
cout << endl << "Resistor value: " << getNomValue()/1000000 << "M ohms with a tolerance of " << getTolerance() << "%" << endl << endl;
} else if (getNomValue() >= 1000) {
cout << endl << "Resistor value: " << getNomValue()/1000 << "K ohms with a tolerance of " << getTolerance() << "%" << endl << endl;
} else {
cout << endl << "Resistor value: " << getNomValue() << " ohms with a tolerance of " << getTolerance() << "%" << endl << endl;
}
}
*****************************************************************************
/*
Resistor.h
Multifile Week 2 Lab header file
*/
class Resistor
{
private:
double tolerance;
double nominalValue;
public:
Resistor();
void setTolerance(double t);
void setNomValue(double v);
double getNomValue();
double getTolerance();
void printValue();
};
STEP 1: Create a Multifile Project for the Composition Lab
Explanation / Answer
I've EDITTED my Answer, It won't give an error Now....
The Format of the data saved in the file (as asked in the question ) is :
resistance_nom_val resistance_tolerance capacitor_nom_val capacitor_tolerance filter_type max_cutoff min_cutoff " "
Here goes all the files in the order :
resistor.h
resistor.cpp
capacitor.h
capacitor.cpp
filter.h
filter.cpp
test.cpp
Feel free to ask if you still have any queries..
____________________________________
// resistor.h
#ifndef RESISTOR_H
#define RESISTOR_H
class Resistor
{
private:
double tolerance;
double nominalValue;
public:
Resistor();
Resistor(double v, double t);
void setTolerance(double t);
void setNomValue(double v);
double getNomValue();
double getTolerance();
void printValue();
};
#endif
___________________________________
// resistor.cpp
#include "resistor.h"
#include <iostream>
using namespace std;
Resistor::Resistor()
{
nominalValue=0;
tolerance=0;
}
Resistor::Resistor(double v, double t)
{
nominalValue=v;
tolerance=t;
}
void Resistor::setNomValue(double v)
{
nominalValue=v;
}
void Resistor::setTolerance(double t)
{
tolerance=t;
}
double Resistor::getNomValue()
{
return nominalValue;
}
double Resistor::getTolerance()
{
return tolerance;
}
void Resistor::printValue()
{
if (getNomValue() >= 1000000)
cout << endl << "Resistor value: " << getNomValue()/1000000 << "M ohms with a tolerance of " << getTolerance() << "%" << endl << endl;
else if (getNomValue() >= 1000)
cout << endl << "Resistor value: " << getNomValue()/1000 << "K ohms with a tolerance of " << getTolerance() << "%" << endl << endl;
else
cout << endl << "Resistor value: " << getNomValue() << " ohms with a tolerance of " << getTolerance() << "%" << endl << endl;
}
_________________________________________________________
// capacitor.h
#ifndef CAPACITOR_H
#define CAPACITOR_H
class Capacitor
{
private:
double tolerance;
double nominalValue;
public:
Capacitor();
Capacitor(double v, double t);
void setTolerance(double t);
void setNomValue(double v);
double getNomValue();
double getTolerance();
void printValue();
};
#endif
_________________________________________________________
// capacitor.cpp
#include "capacitor.h"
#include <iostream>
using namespace std;
Capacitor::Capacitor()
{
nominalValue=0;
tolerance=0;
}
Capacitor::Capacitor(double v, double t)
{
nominalValue=v;
tolerance=t;
}
void Capacitor::setNomValue(double v)
{
nominalValue=v;
}
void Capacitor::setTolerance(double t)
{
tolerance=t;
}
double Capacitor::getNomValue()
{
return nominalValue;
}
double Capacitor::getTolerance()
{
return tolerance;
}
void Capacitor::printValue()
{
if (getNomValue() >= 1000000)
cout << endl << "Capacitor value: " << getNomValue()/1000000 << "M ohms with a tolerance of " << getTolerance() << "%" << endl << endl;
else if (getNomValue() >= 1000)
cout << endl << "Capacitor value: " << getNomValue()/1000 << "K ohms with a tolerance of " << getTolerance() << "%" << endl << endl;
else
cout << endl << "Capacitor value: " << getNomValue() << " ohms with a tolerance of " << getTolerance() << "%" << endl << endl;
}
_____________________________________________________
// filter.h
#ifndef FILTER_H
#define FILTER_H
#include "capacitor.h"
#include "resistor.h"
const double PI =3.141592653589793238463;
const double INF = 99999999999.99999;
class Filter
{
private:
Resistor resist;
Capacitor capac;
int filter_type; // 0 - low pass filter , 1- high pass filter
double max_cutoff;
double min_cutoff;
public:
Filter();
void set_values();
void write_data_in_file();
void print_values_from_file();
void calculate();
double getMaxCutoff();
double getMinCutoff();
int getFilterType();
void display();
};
#endif
________________________________________________________________
// filter.cpp
#include<iostream>
#include<cmath>
#include<fstream>
#include "filter.h"
#include "resistor.h"
#include "capacitor.h"
using namespace std;
Filter::Filter()
{
filter_type = 0;
max_cutoff = 0.0;
min_cutoff = 0.0;
}
void Filter::set_values()
{
double val;
int type;
cout<<" Enter the Nominal Resistance :";
cin>>val;
resist.setNomValue(val);
cout<<" Enter the Resistance Tolerance :";
cin>>val;
resist.setTolerance(val);
cout<<" Enter the Capacitor Resistance :";
cin>>val;
capac.setNomValue(val);
cout<<" Enter the Capacitor Tolerance :";
cin>>val;
capac.setTolerance(val);
cout<<" Enter Type of Filter (0 - low pass / 1- high pass) : ";
cin>>type;
filter_type = type;
}
void Filter::write_data_in_file()
{
char file[100];
cout<<" Enter Name of text file to write into (file_name.txt) ";
cin>>file;
ofstream out;
out.open(file,ofstream::in);
out<<resist.getNomValue()<<" "<<resist.getTolerance()<<" "<<capac.getNomValue()<<" "<<capac.getTolerance()<<" "<<getFilterType()<<" "<<getMaxCutoff()<<" "<<getMinCutoff()<<" ";
out.close();
}
void Filter::print_values_from_file()
{
char file[100];
cout<<" Enter the Name of the Text File To Read Values of the Filters : ";
cin>>file;
ifstream in(file);
if(in.fail())
printf(" ERROR : File Does not Exist ! ");
else
{
double val;
int type;
in>>val;
resist.setNomValue(val);
in>>val;
resist.setTolerance(val);
in>>val;
capac.setNomValue(val);
in>>val;
capac.setTolerance(val);
in>>type;
filter_type = type;
in>>val;
max_cutoff = val;
in>>val;
min_cutoff = val;
cout<<" Values written from File into the Filter Object ";
}
in.close();
}
double Filter::getMaxCutoff()
{
return max_cutoff;
}
double Filter::getMinCutoff()
{
return min_cutoff;
}
int Filter::getFilterType()
{
return filter_type;
}
void Filter::calculate()
{
max_cutoff = 2*PI*(resist.getNomValue() - resist.getTolerance()*resist.getNomValue()/100)*(capac.getNomValue()-capac.getTolerance()*capac.getNomValue()/100);
if(max_cutoff!=0)
max_cutoff = 1/max_cutoff;
else
max_cutoff = INF;
min_cutoff = 2*PI*(resist.getNomValue() + resist.getTolerance()*resist.getNomValue()/100)*(capac.getNomValue()+capac.getTolerance()*capac.getNomValue()/100);
if(min_cutoff!=0)
min_cutoff = 1/min_cutoff;
else
min_cutoff = -1*INF;
cout<<" Maximum Cutoff Frequency : "<<max_cutoff;
cout<<" Minimum Cutoff Frequency : "<<min_cutoff;
}
void Filter::display()
{
cout<<" Resistor's Resistance : "<<resist.getNomValue();
cout<<" Resistor's Tolerance : "<<resist.getTolerance();
cout<<" Capacitor Resistance : "<<capac.getNomValue();
cout<<" Capacitor Tolerance : "<<capac.getTolerance();
if(getFilterType()==0)
cout<<" Filter Type : Low Pass Filter";
else
cout<<" Filter Type : High Pass Filter";
cout<<" Maximum Cutoff Frequency : "<<getMaxCutoff();
cout<<" Minimum Cutoff Frequency : "<<getMinCutoff();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.