Modify the Book-Class Definition 1. Add a static data member of the class Book t
ID: 652080 • Letter: M
Question
Modify the Book-Class Definition
1. Add a static data member of the class Book to keep track of the number of Book objects that are dynamically created. In the Book constructor, use a random-number generator to randomly assign a Book id value to the object between 1,000 and 10,000.
Create the Test Function Main() and the Support Function
1. Create an array of pointers of type Book.
2. Use elements of the pointer array to allow the user to dynamically allocate memory and to instantiate objects of the Book class.
3. Use the indirect member-selection operator (->) in the test routine to access function members of the Book class.
4. Write a new, nonclass function called in function main() to display the Book objects.
5. Function main() should also ensure that there are no memory leaks when the program ends.
this is what i have so far...
#pragma once
#include
#include
using std::string;
class pointersbook
{
private:
void setBookID();
public:
pointersbook(void);
~pointersbook(void);
pointersbook(string title,int page,string Auther,bool hardCoverOrPaperBack);
string title;
int page;
string Auther;
bool hardCoverOrPaperBack;
int bookID;
static int bookNumberStuff;
string displayBookInfo;
};
#include "pointersbook.h"
#include
#include
using namespace std;
pointersbook::pointersbook(void)
{
title ="";
page =0;
Auther ="";
hardCoverOrPaperBack= true;
setBookID();
bookNumberStuff++;
}
pointersbook::pointersbook(string title,int page,string Auther,bool hardCoverOrPaperBack)
{
this->title= title;
this->page= page;
this->Auther =Auther;
this-> hardCoverOrPaperBack =hardCoverOrPaperBack;
setBookID();
bookNumberStuff++;
}
pointersbook::~pointersbook(void)
{
bookNumberStuff--;
//hotRodarr[0]->~pointersbook;
}
void pointersbook::setBookID()
{
srand(int(NULL));
bookID= rand() % 9001+ 1000;
}
int pointersbook::bookNumberStuff=0;
#include
#include
#include "pointersbook.h"
using namespace std;
string displayBookInfo();
pointersbook p1;
int main()
{
bool hardCoverOrPaperBack[6]={true,false,false,true,true,false};
string Auther[6]={"Paula Hawkins","Harlan Coben","Anthony Doerr","James Patterson and Marshall Karp","Helen Macdonald","Anne Tyler"};
int pagesArr[6] = {152,285,368,1024,288,36};
string bookarr[6] ={"The Girl on the Train","The Stranger","All the Light We Cannot See","NYPD Red Three", "H Is For Hawk","A Spool of Blue Thread"};
pointersbook* hotRodarr[7];
for(int i=0;i <6; i++)
{
hotRodarr[i]= new pointersbook(bookarr[i],pagesArr[i],Auther[i],hardCoverOrPaperBack[i]);
}
//string title,int page,string Auther,bool hardCoverOrPaperBack)
hotRodarr[6]=new pointersbook();
cout<< "Title:";
getline (cin,hotRodarr[6]->title);
cout<<" Page:";
cin>>hotRodarr[6]->page;
cout<< "Auther:";
getline (cin,hotRodarr[6]->Auther);
cout<< "Hard back book 1 or Paper back book 2: ";
cin>> hotRodarr[6]->hardCoverOrPaperBack;
if(int i=1)
{
true;
}
else
{
false;
}
//cout<< displayBookInfo(title,page,Auther,setBookID(),);
cout<
system("pause");
return 0;
}
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
#ifndef RESISTOR_H
#define RESISTOR_H
enum resistorValues{NOMINAL,TOLERANCE,MAX,MIN};
class Resistor {
private:
static int ResCounter;
double* ptrRes;
char* ResName;
public:
Resistor();//default constructor
//argument constructor
Resistor(const string&, double nominalResistance=1000, double Tolerance =.10);
//fuction getName
void getName();
//to display
void DisplayResistor(void);
//
void EnterResistance(void);
~Resistor();
};
int Resistor::ResCounter = 0;
Resistor::Resistor()
{
ptrRes=new double[4];
//default values
ptrRes[NOMINAL]=1000;
ptrRes[TOLERANCE]=.10;
//calculate min and max values
ptrRes[MIN]=ptrRes[NOMINAL]-(ptrRes[NOMINAL] * ptrRes[TOLERANCE]);
ptrRes[MAX]=ptrRes[NOMINAL]+(ptrRes[NOMINAL] * ptrRes[TOLERANCE]);
ResName = NULL;
Resistor::ResCounter++;
}
Resistor::Resistor(const string& name,double nominalResistance,double Tolerance)
{
ptrRes=new double[4];
//default values
ptrRes[NOMINAL]=nominalResistance;
ptrRes[TOLERANCE]=Tolerance;
//calculate min and max values
ptrRes[MIN]=ptrRes[NOMINAL]-(ptrRes[NOMINAL] * ptrRes[TOLERANCE]);
ptrRes[MAX]=ptrRes[NOMINAL]+(ptrRes[NOMINAL] * ptrRes[TOLERANCE]);
//Resistors Name
int length=(int)name.length();
ResName=new char[length+ 1];
strcpy_s(ResName,length+1,name.c_str());
//Increments number of resistor objects that exist
Resistor::ResCounter++;
}
Resistor::~Resistor()
{
delete[]ptrRes;
delete[]ResName;
ptrRes=NULL;
ResName=NULL;
Resistor::ResCounter--;
}
void Resistor::getName()
{
string Name;
cout<<"Enter resistors name: ";
cin >> Name;
int length=(int)Name.length();
if(ResName)
{
delete[] ResName;
ResName=NULL;
}
ResName=new char[length+ 1];
strcpy_s(ResName,length+1,Name.c_str());
}
void Resistor::DisplayResistor ()
{
//Displays all Resistor object data members
//sets the output parameters
cout << setprecision(2);
cout << fixed << showpoint;
//displays the output
cout << " ";
cout << setprecision(5) << fixed << showpoint << setfill(' ');
cout << "Values for " << *ResName << " are: ";
cout << left << setw(25) << "Resistor Nominal Value = "
<< right << setw(10) << ptrRes[NOMINAL] << " ";
cout << left << setw(25) << "ohms Resistor Tolerance = "
<< right << setw(10) << (100*ptrRes[TOLERANCE]) <<"%"<< " ";
cout << left << setw(25) << "Minimum Resistance = "
<< right << setw(10) << ptrRes[MIN] <<" ohms"<< " ";
cout << left << setw(25) << "Maximum Resistance = "
<< right << setw(10) << ptrRes[MAX] <<" ohms" <<" ";
}
#endif
//main .cpp
#include <iostream>
#include <iomanip>
#include <string>
#include "Resistor.h"
using namespace std;
int main()
{
//creating object for resistor
Resistor resistor;
//calling getname function
resistor.getName();
//displying resistor
resistor.DisplayResistor();
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.