#ifndef FILM_H
#define FILM_H
#include <QString>

class Film {
  protected:
    QString title;
    double dailyRate;
  public:
    Film(QString ti,double dr);
    QString getTitle() const;
    virtual double calculateRental(int num)const;
};

class Video: public Film {
public:
    Video(QString ti,double dr,QString vt,int d);
    QString getTitle() const;
    QString getVideoType() const;
    virtual double calculateRental(int num)const;
protected:
    QString videoType;
    int discount;
};

#endif // FILM_H

Film.cpp

#include "film.h"
#include <QString>

String ti,double dr){
  title=ti;
  dailyRate=dr;
}

 QString Film::getTitle() const {
   return title;
}

 double Film::calculateRental(int num)const {
   return dailyRate*num;
}

 Video::Video(QString ti,double dr,QString vt,int d)
     :Film(ti,dr){


        videoType = vt;
        discount = d;
 }

  QString Video::getTitle() const {
    return title;
 }

  QString Video::getVideoType() const {
    return videoType;
 }

  double Video::calculateRental(int num)const {
    return (dailyRate*num)-discount;
 }

main.cpp

 #include <QtCore/QCoreApplication>
 #include <QtCore/QTextStream>
 #include  "film.h"

 using namespace std;

 int main(int argc, char *argv[]) {
 QCoreApplication a(argc, argv);
 QTextStream cout(stdout, QIODevice::WriteOnly);
 Film f("Top Gun", 10.00);
 // Print out title and rental fee (based on 2 days rental).
   cout << "Title: " << f.getTitle() << endl;
   cout << "Rental Fee: " << f.calculateRental(2)<<  endl;

   cout <<" " <<  endl;//blank line

 Video v("Top Gun", 10.00,"DVD",5);
 // Print out title and rental fee (based on 2 days rental).
   cout << "Title: " << v.getTitle() << endl;
   cout << "Video Type: " << v.getVideoType() << endl;
   cout << "Rental Fee: " << v.calculateRental(2) << endl;

}

how do I count the number of Film instances created? I know is something like that:

static int numOfFilms;
numOfFilms++;

how do I use the code?

Dani AI

Generated

As suggested, put a static counter inside the Film class declaration, then update it from the constructors/destructor. Decide first whether you want a running total of created objects (increment only) or the current number of live objects (increment in constructors, decrement in destructor). Declare the static member in the header and define/initialize it exactly once in Film.cpp — not in main.cpp — to avoid multiple-definition linker errors. Also add a virtual destructor to Film so derived objects (Video) clean up correctly.

Example additions to the header (Film.h):

class Film {
protected:
    QString title;
    double dailyRate;
    static int instanceCount;   // declared here
public:
    Film(const QString &ti, double dr);
    virtual ~Film();
    QString getTitle() const;
    virtual double calculateRental(int num) const;
    static int getInstanceCount() { return instanceCount; }
};

Define and update the counter in Film.cpp:

#include "film.h"

int Film::instanceCount = 0;   // single definition / initialization

Film::Film(const QString &ti, double dr)
    : title(ti), dailyRate(dr)
{
    ++instanceCount;           // increment for each newly constructed object
}

Film::~Film()
{
    --instanceCount;           // decrement if you want "live" count
}

Notes and troubleshooting: if you only want a cumulative count (how many were ever created) remove the decrement in the destructor. Copy-constructed objects are new instances too — if you want to ignore copies, provide/override the copy constructor accordingly. For multithreaded programs use std::atomic<int> (or QAtomicInt) for the static counter. To read the value from anywhere use Film::getInstanceCount() (for Qt you can print it with qDebug() or your existing QTextStream).

yes my friend you need to create a static variable inside the class, then increment the variable when constructor is called and decrement when destructor is called. And don't forget to initialize to 0 ;

in the film.cpp or in the main.cpp?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.