LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-20-2020, 04:01 PM   #1
tundrasnow
LQ Newbie
 
Registered: May 2020
Posts: 2

Rep: Reputation: Disabled
Abstract base class, and Pure Virtual function - C++


Greetings,

I'm having a problem with a pure virtual function setup in a derived class. It's not working and I'm getting an error but I can't figure out as to why the error is occuring.


main.cpp

//g++ -std=c++11 -o account -g main.cpp base.cpp account.cpp bankaccount.cpp -I /home/cplusplus

#include <iostream>
#include <cmath>
#include <cstdlib>
#include "account.h"
#include "bankaccount.h"
#include "derived.h"

using namespace std;
int main(int argc, char *argv[])
{

Account account(0.0);
char action;
double amount;

account.getBalance();//Increments the counter

return 0;
}

---------------------------------------------------------------------------------------
account.cpp


#include <iostream>
#include <cmath>
#include <cstdlib>
#include "account.h"
#include "bankaccount.h"
#include "derived.h"

using namespace std;

Account::Account()
{
}

Account::Account(double b)
{
balance = b;
counter=1;
}

Account::~Account()
{

}

void Account::deposit(double amt)
{
balance +=amt;
}

void Account::withdraw(double amt)
{
balance -= amt;
}

double Account::getBalance() const
{
counter++;
return balance;
}


int Account::deposits = 0;




-------------------------------------------------------------------------------------
#ifndef ACCOUNT_H
#define ACCOUNT_H


class Account
{

int x;

public:
Account();
Account(double b);
virtual ~Account();

void deposit(double amt);
virtual void withdraw(double amt);
double getBalance() const;

virtual void show() = 0 ; // Pure Virtual Function
int getX() { return x; }


private:
static int deposits;
const double minBalance = 21.2;

static const double minBalanceEveryone;


protected:
double balance;

mutable int counter;

};


#endif
----------------------------------------------------------------------------------
bankaccount.h

#ifndef BANKACCOUNT_H
#define BANKACCOUNT_H

#include <iostream>
#include <cstdlib>
using namespace std;
#include "account.h"

class BankAccount : public Account
{
int y;

public:
BankAccount();
BankAccount(double r);
virtual ~BankAccount();

void addInterest();
void chargeFee(double c);



virtual void withdraw(double amt)
{
if (balance -amt < 0.0)
{
//Do nothing
}
else
{
//Balance ok
balance -= amt;
}
}

void show()
{
cout << "Implementation of Virtual Function in Derived class\n";
}


/*double estimateReturn()
{
cout << "estimateReturn fun() called";
return 55.5;
}
*/

private:
double interestRate;
};
#endif

-----------------------------------------------------------------------------
bankaccount.cpp

#include <iostream>
#include <cmath>
#include <cstdlib>
#include "account.h"
#include "bankaccount.h"


BankAccount::BankAccount(double r)
{
balance = r;
// counter=1;
}

BankAccount::~BankAccount()
{

}
void BankAccount::addInterest()
{

}






==================================================================================
main.cpp: In function ‘int main(int, char**)’:
main.cpp:33:10: error: cannot declare variable ‘account’ to be of abstract type ‘Account’
Account account(0.0);
^
In file included from main.cpp:6:0:
account.h:5:7: note: because the following virtual functions are pure within ‘Account’:
class Account
^
In file included from main.cpp:6:0:
account.h:32:30: note: virtual void Account::show()
virtual void show() = 0 ; // Pure Virtual Function

Last edited by tundrasnow; 05-21-2020 at 08:36 AM.
 
Old 05-21-2020, 08:16 AM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 9,606

Rep: Reputation: 4279Reputation: 4279Reputation: 4279Reputation: 4279Reputation: 4279Reputation: 4279Reputation: 4279Reputation: 4279Reputation: 4279Reputation: 4279Reputation: 4279
The destructor should be virtual (I just checked). Apart from that, this is the way it's usually done:
Code:
Account *account = new BankAccount(0.0);
account->show();
delete account;

Last edited by dugan; 05-21-2020 at 09:34 AM.
 
1 members found this post helpful.
Old 05-21-2020, 08:34 AM   #3
tundrasnow
LQ Newbie
 
Registered: May 2020
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thank you. This works. :-)

Quote:
Originally Posted by dugan View Post
The destructor should be virtual. Apart from that, this is the way it's usually done:
Code:
Account *account = new BankAccount(0.0);
account->show();
delete account;
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] C++: Does it make sense to *define* the pure virtual functions in the *abstract base* class itself? TheIndependentAquarius Programming 14 11-21-2011 06:51 PM
[SOLVED] How can a friend function access a public function from a base class? lesca Programming 1 11-17-2010 01:56 PM
casting void* to an abstract class qwijibow Programming 3 12-23-2006 08:43 PM
c++ Abstract Base Classes exodist Programming 1 06-11-2005 06:05 PM
c++ : regarding (inheritence)base class and derived class edreddy Programming 6 07-31-2002 06:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration