Hello,
Can any of u help me with this assignment..
I tried to do it myself but cuz I'm new in Object Oriented my program was full of errors that I don't know how to fix :(
Please help me as soon as possible..
here is the question:
Write class definitions for Customer and for a derived class Premium_Customer. Customer should contain data members common to all customers, like the number of calls made in a month, the custom¬er's name, and so on. Also implement a virtual member function for computing a bill, Compute_Bill( ) which uses the appropriate algorithm and data members to compute a monthly charge for each type of customer. In all classes be sure to also include any constructors and other member functions that you think are needed especially if you use dynamic memory al¬location in your class. The derived class should contain data members and initializations spe¬cific to it's payment plan, and a specific implementation for the Compute_Bill() member func¬tion which overrides the default method. Demonstrate programmatically which plan is better for a customer who makes 30 calls per month averaging 3 minutes per call. What about 60 calls?
and my program:

#include <iostream>
#include <string>
using namespace std;

class customer{
     public:
             string name;
             int callNum;
     
            void set_values(string N ,int C){
                 name=N;
                 callNum=C;
                }
     virtual float compute_bill()
     { return (10+(.5 * callNum));}
     };
     
/*customer::customer(string N,int C)
{
      name=N;
      callNum=C;
      }
customer::~ customer()
{
            delete N;
            delete C;
            }
*/
class premium_customer: public customer{
     //public:
             //premium_customer(string N,int C): customer(N,C){
             int x;
             cout <<"Please enter the average number of min. per call"<<endl;
             cin >> x;
             float compute_bill()
             {return(20+(.5*callNum)+(.1*x));}
             };

int main()
{
    customer *list[6];
    list[0]=new customer("John Dough",20);
    list[1]=new customer("Bob Dough",50);
    list[2]=new customer("Jassim Al_Zaid",10);
    list[3]=premium_customer("Ghadeer Al_Zaid",50);
    list[4]=premium_customer("Anwar Al_Zaid",30);
    list[5]=premium_customer("Jane Doe",100);
    
    for (int i=0; i<6; i++){
        cout <<"customer "<<list[i]<<"owes "<< list[i].compute_bill()<<"Dollars. "<< endl;
        }
     /*delete list[0];
     delete list[1];
     delete list[2];
     delete list[3];
     delete list[4];
     delete list[5];*/
     delete [] list;
     
    return 0;
}

Thanks

Well first, you should put a meaningful topic. "need help" doesn't give us any kind of idea...... what your situation is, or if it is even relevant to something I know. If you had said "class behavior blah blah blah" I might be able to say "oh, he needs help with this" and then be prepared better for the topic of your issue.

In your class definition for the derived class "premium_customer" you can't just put code in it, such as the line to enter the average number of min. per call... that has to be in a method of some kind. You commented out public in the same derived class, which makes all the members by default private. You are passing data to a constructor that doesn't accept parameters (you commented it out). In your customer destructor, you are trying to call delete on variables that are not pointers (and from what I can tell, not pointers to memory on the heap). (sure you commented those out, but it's still a bit dangerous). You are creating instances of both classes, while one is a parent and one is derived, and the parent has parameters identical to the derived (which is clearly above your level at this point). Furthermore, you create some items in list on the heap, and others on the stack, and call delete on them all.....

This is still a mess, and requires some serious rethought, but it works:

#include <iostream>
#include <string>
using namespace std;

class customer
{
	public:
		customer(string N, int c);         // << -- Added
		~customer();			   // << -- Added

	    	string name;
	        int callNum;
     
        	void set_values(string N ,int C){
                	name=N;
                	callNum=C;
                }

	  	virtual float compute_bill() { return (10+(.5 * callNum));}
};
     
class premium_customer: public customer
{
     public:
	int x;
	premium_customer(string N, int C);         // << -- Added
	~premium_customer();	   		    // << -- Added

        int get_mins() {
			cout <<"Please enter the average number of min. per call"<<endl;
		        cin >> x;
                        return x;
	}

        float compute_bill() { return(20+(.5*callNum)+(.1*x)); }
};

customer::customer(string N,int C)
{
      name=N;
      callNum=C;
}

customer::~customer() {}

premium_customer::premium_customer(string N, int C) : customer(N, C) 
{
      name=N;
      callNum=C;
}

premium_customer::~premium_customer() {}


int main()
{
	customer *list[6];
	list[0] = new customer("John Dough",20);
	list[1] = new customer("Bob Dough",50);
	list[2] = new customer("Jassim Al_Zaid",10);
	list[3] = new premium_customer("Ghadeer Al_Zaid",50);
	list[4] = new premium_customer("Anwar Al_Zaid",30);
	list[5] = new premium_customer("Jane Doe",100);
    
	for (int i=0; i<6; i++){
        	cout << "customer " << list[i]->name << " owes " << list[i]->compute_bill() << " Dollars. " << endl;
	}

	for (int i=0; i < 6; i++) {
		delete list[i];
	}
    
	return 0;
}

EDIT: You're going to still need to figure out how to call the new method (get_mins), and use that in your code.

You dont need a destructor here since you are not dealing with pointers.

If I am correct compilers will automatically generate a default blank destructor for you anyway on compile time if non is specified. But I think it's good practise to make sure you include a destructor whilst learning about classes. If you don't you'll forget about them and when you come to mixing pointers and classes you'll completely forget and end up with memory leaks.

Chris

The worst error here is the lack of virtual destructor. The WRONG destructor is called. Regardless of you writing you own or letting the compiler write it. Doesn't matter.

So you should have

class customer
{
	public:
		customer(string N, int c);         // << -- Added
		virtual ~customer();			   // << -- Added

	    	string name;
	        int callNum;
     
        	void set_values(string N ,int C){
                	name=N;
                	callNum=C;
                }

	  	virtual float compute_bill() { return (10+(.5 * callNum));}
};

Technically you do not need a virtual destructor in premium_customer UNLESS you inherrit from it. BUT you have no idea if you / someone else will do so. So just add a virtual destructor as well.

Virtual functions:

Quick summay. Virtual function allow the following construct to work

class A
{ 
  public:
    void fncX() { std::cout<<"A:fncX"<<std::endl;}
    virtual void fncY() {std::cout<<"A:fncY"<<std::endl;}
};

class B : public A
{
 public:
    void fncX() { std::cout<<"B:fncX"<<std::endl;}
    virtual void fncY() {std::cout<<"B:fncY"<<std::endl;}
};


int main()
{
    B itemB;
    A* ptrA = new B;
    B* ptrB=new B;

   itemB.fncY();   // prints B
   itemB.fncX();   // prints B
   ptrA->fncX();   // prints A
   ptrA->fncY();   // prints B

   ptrB->fncX();    // prints B
}

Note the difference in the behaviour if you have a point that is being stored in a base class.

In your case you have a base pointer that stores a derived class.

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.