Need help with my base class

Hello all,

I have an odd compiling error. My base class is all delcared and read to go. But I could not figure out how to get my sub classes inherit from it. So I had to make everything public. Even inheriting using the protected inheritance thingy, still could not get them to inherit from that base class.

So I compiled the program, and no other class came back with an error. only my base class.

here is the base class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>

using namespace std;

class Bankaccount
{
public:
	int acctnum;
	double balance;
	string fName;
	string lName;

	void setacct(int accountnum);
	int getacct();

	void setbal(int ammount);
	double getbal();

	void setfirst(string firstname);
	string getfirst();
	void setlast(string lastname);
	string getlast();

	virtual void makedeposits() = 0;
	virtual void makewithdraw() = 0;
	virtual void statement() = 0;
}


The error I'm getting is saying:

error C2143: syntax error : missing ';' before 'using'
Where is there anything needing a semicolon before using?

'Bankaccount' : 'class' type redefinition
see declaration of 'Bankaccount'
Is is saying this because I don't have any private class members?

'Bankaccount' : base class undefined
But it is defined.

What is the issue with this class? And how can I get it to compile and run?
There should be a semicolon on line 28:
27
28
    virtual void statement() = 0;
}; // <-- here 

If that's just a copy/paste error, then I'll need to see a little more code.

In particular, if you're using multiple files, are your header files guarded, and did you remember to #include "bankaccount.h" (or whatever your header file is named) in each of your derived classes' header files?
ok, heres the part I currently have inheriting from this class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "BankAccountFunction.h"
#include <iostream>
#include <string>

using namespace std;

class checkingAcct : protected Bankaccount
{
public:
	virtual void writeCheck() = 0;
};

class serviceChargecheck : protected checkingAcct
{
	double minbalance = 0;
	double serviceCharge = 1.60;
	int checkcounter = 10;
public:

	void setacct(int accountnum);
	int getacct();

	void setbal(int ammount);
	double getbal();

	void setfirst(string firstname);
	string getfirst();
	void setlast(string lastname);
	string getlast();

	void makedeposits();
	void makewithdraw();
	void statement();
	void writeCheck();
};

class noSerivceChargeChecking : protected checkingAcct
{
	double minBalance = 100.00;
	double interest = .02;
public:

	void setacct(int accountnum);
	int getacct();

	void setbal(int ammount);
	double getbal();

	void setfirst(string firstname);
	string getfirst();
	void setlast(string lastname);
	string getlast();

	void makedeposits();
	void makewithdraw();
	void statement();
	void writeCheck();

};

class highInterestchecking : public noSerivceChargeChecking
{
	double minBalance = 200.00;
	double interest = .05;
public:

	void setacct(int accountnum);
	int getacct();

	void setbal(int ammount);
	double getbal();

	void setfirst(string firstname);
	string getfirst();
	void setlast(string lastname);
	string getlast();

	void makedeposits();
	void makewithdraw();
	void statement();
	void writeCheck();

};

Just out of curiosity, what are the #include s at the top of your .cpp files?
If I'm understanding what your asking, then here you go

http://www.cplusplus.com/forum/articles/10627/
Ah, This reminds me of your earlier post yesterday which I didn't have time to reply to.

http://www.cplusplus.com/forum/general/129668/


This will help out others who wish to reply as well.

I would recommend you go back to that code, there is no need to make everything public or use protected inheritance.

With lines 15 to 17 those variables are private because no access was specified, in other words they are private by default. That means that they are only available to member functions of that class only. The same thing is happening in your original code.

Private variables are a good thing, it is very tempting to make them protected, but that is bad practice, because if one needs to change something it can break existing code .

The thing to do, is make use of initialiser lists in your constructors, derived classes can call base class constructors from their initialiser list too.

http://www.cplusplus.com/doc/tutorial/classes/
in the Member initialization in constructors section

When you have public functions that are inherited, you shouldn't redefine them in derived classes. If you want different behaviour for them, make them virtual. Not necessarily pure virtual, that is for when you need your function to be different in every derived class. Ordinary virtual means you can define a function once, and it applies to every derived class underneath, unless you provide a new definition.

Btw don't have using namespace std; , it defeats the purpose of having namespaces by bringing in the entire std ns into the global namespace, causing naming conflicts. There are lots of words one might use for variables & functions that are already in std, such as std::count, std::left, std::right, std::next, std::distance etc. Rather than worry about whether a variable is already in std, just put std:: before each std thing as in std::cout, std::string etc.

Ideally, one should put their own stuff into their own namespace, like this:

In the hesder file:

1
2
3
4
5
6
7
namespace BANK {

class Bankaccount {

}

} // end of BANK ns 


In the .cpp file:

1
2
3
4
5
namespace BANK {

Bankaccount::Bankaccount() {}//ctor

} // end of BANK ns 


When you come to use it, you need to put the namespace and class in, like this:

BANK::Bankaccount MyBank(); // creates a Bankaccount object

Namespaces can be nested, and one can use a using statement to provide an alias for it to shorten up references to them:

1
2
using MyNameSpace = mns;
using MyNameSpace::MyNestedNameSpace = mnns;


If you look at third party libraries like boost , they do this all the time.

Hope all goes well :+)
Last edited on
Also, IMO you shouldn't have any setBalance function (public or otherwise), as in:

mattig89ch.setBalance(-1000000); // instant 1 million $ mortgage !!

The bank balance should be set initially with a constructor, after that use Deposit & Withdraw functions
ok, thanks all! got it working, and am looking at what a few of you suggested as a means to improve what I have there already.

but it compiles and runs! woo!
Ok, cool you have it working.

Just wondering, what have you done so far to make it work? Did you go back to your original code & use the initialiser lists? Thing is, people say I have it working now: solved. But that doesn't mean it is right - everything public?. I don't mean to be a hound dog, just would like to know that your code is written reasonably well. No need to post all the code again, if you don't want to.

Regards
I won't lie to you, I'm not exactly sure what I did to get it to run.

I got it to compile.

Then I tried to run it, and it gave me a whole list of errors. Forgot to put parenthesis in my function calls, and a few other things here and there. Fixed all them, got it to run, put some random stuff in there, for each checking account, and the numbers looked right. So I turned it in. I would have spent some more time on it, but it was due 45 min ago. So I turned it in as it was.

And as for re-posting my code, I am always worried about some idiot finding my threads on here, and copying them (getting us both in trouble). So I have no issues posting my code when it doesn't work. But when it does, I'm a little reluctant to post that code. That said I didn't have time to change everything about what you said. The variables in the classes are still public, still have using name space std everywhere. I still had the user enter the account information rather than relying on a constructor for that. And I'm not sure what your talking about with initializer lists.

Oh and you were right, that was me the other day. I did try to use the protected inheritance before making everything public. It would not recognize anything from the base class unless it was public. Tried getting protected to go through for like an hour before I gave up and made it public.
Ok fair enough.

ns std is not such a bad thing, but having everything public is.

I guess you should read through the tutorial at the top left of this page (it is all in there), to get a clearer understanding of what is going on.

protected inheritance is a very different thing, you shouldn't need it - always have public inheritance. Note this is different to private, public and protected inside the class.

Also the default access inside a class is private, so if you don't specify it, then it will be private. You could make sure you specify the access you want regardless.

The basic idea of classes is to have private member variables, initialise them in a constructor with an initialiser list, provide an interface of public functions to manipulate the data, and have private or protected functions that the public functions can call - to do stuff that shouldn't be exposed out side the class.

Hope all goes well, look forward to helping out in future.
Topic archived. No new replies allowed.