Help, Object Oriented Programming C++

Pages: 12
1.In the test.cpp file my professor declared two things NAME_SIZE and ID_SIZE. Now she gave us a hint that they are both suppose to be declared in Customer Header File. Whenever I try to declare them inside the class. My constructors end up getting weird errors might be because I am declaring them wrong. I am declaring them as const. That might be it.

I don't actually see anywhere in the code you've posted showing us how you declare them, so there's no way for us to tell what's wrong.

2.Also in my private member variables, is it safe to declare them as "string" or does one of them have to be an array?.

I don't see any reason to use a C-style char array for either of them. It's almost always preferable to use std::string.

EDIT: I don't see any reason not to use std::string for your variables in main, either.

BTW, I would recommend not putting "using namespace std" in header files. It effectively forces any file that includes your header to pull the entirety of the std:: namespace into the global namespace. It's not good practice at all. You should explicitly namespace-qualify all your string declarations in the header.

In cpp files, it's not such a problem.
Last edited on
Ah, I erased it since it was an error and no way correct but I just simply named them
1
2
const int NAME_SIZE = 0;
const int ID_SIZE = 0;


but I honestly don't know what to declare them as. It ends up giving me over 100 errors haha.



Okay thank you, definitely will keep that in mind and I edited the header files. Also what do you mean explicitly namespace-quality all the string declarations?. I'm a little confused by that.






Ah, I erased it since it was an error and no way correct but I just simply named them

1
2
const int NAME_SIZE = 0;
const int ID_SIZE = 0;


but I honestly don't know what to declare them as. It ends up giving me over 100 errors haha.

Why are you defining them as zero? What use is an array with zero elements?

Also what do you mean explicitly namespace-quality all the string declarations?. I'm a little confused by that.

If you take out "using namespace std" then presumably you start getting compilation errors saying "string" is undefined. You'll need to use std::string instead in the header.

EDIT: It looks like I misspelled "qualify" in my earlier post - it should have read:

You should explicitly namespace-qualify all your string declarations in the header.
Last edited on
Ohhh, okay. So I declared them out of the 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
#ifndef CUSTOMER_H
#define CUSTOMER_H
#include <iostream>
#include <string>
using namespace std;
const int NAME_SIZE = 10;
const int ID_SIZE = 10;
class Customer
{
private:
	string name; 
	string accountID; //which is a c-string or a string (your choice) for the customer's account number which you may assume consists of 6 digits.

public:
	

	Customer()
		{
			name = " ";
			accountID = " ";
			//which initializes each of the member variables to the null string (an empty string)
		}
	
	Customer(string sname, string accID) //which sets the name and accountID to the user supplied values.
	{
		sname = name;
		accID = accountID;


	}
	virtual void view(); //displays the name of the customer and the account number
	Customer& Customer::operator= (const Customer& right)
	{
		name = right.name;
		return *this;

	}
/*an overloaded assignment operator to copy the Customer object instantiated by the test driver to the 
Customer member variable of the Account instance also created in the test driver as 
shown in the following excerpts of the test driver:*/
};

#endif 



Once I did that. Now I get these 3 errors.

Error 1 error LNK2001: unresolved external symbol "public: virtual bool __thiscall Savings::makeWithdrawal(double)" (?makeWithdrawal@Savings@@UAE_NN@Z) C:\Users\Ivan\Desktop\Assignment4\Assignment4\test.obj Assignment4

Error 2 error LNK2001: unresolved external symbol "public: virtual void __thiscall Savings::view(void)" (?view@Savings@@UAEXXZ) C:\Users\Ivan\Desktop\Assignment4\Assignment4\test.obj Assignment4

Error 3 error LNK1120: 2 unresolved externals C:\Users\Ivan\Desktop\Assignment4\Debug\Assignment4.exe Assignment4



EDIT: I should post the the Savings Header file and .cpp

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
#ifndef SAVINGS_H
#define SAVINGS_H
#include <iostream>
#include "Account.h"



class Savings: public Account
{
protected:
	double interestRate;
public:
	Savings()
	{

		interestRate = 0;
		//which invokes the Account base class no-argument constructor and then sets interestRate to zero.
	}
	Savings(Customer c,double bal, double rate/*three argument parameter*/)
	{
		interestRate = rate;
		//which invokes the Account base class two-argument constructor and then sets interestRate to the 
		//user-supplied interest rate (which the user supplies via the test driver).
	}

	virtual void view(); 
	void adjustBalance(double);
	void makeDeposit();
	virtual bool makeWithdrawal(double);





};


#endif 


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
#include "savings.h"




void makeDeposit()
{
	/*calls the base Account class’ makeDeposit function, passing the user-supplied deposit amount as a parameter.  
		This function has no return value*/
}

bool makeWithdrawal(double w)
{
	if (w < 0)
		return false;
	else
		return true;
	/*determines whether a withdrawal can be made, and if so, calls the base Account class’ makeWithdrawal function, 
	passing the user supplied withdrawal amount as a parameter.  A withdrawal can only be made if doing so will not 
	result in a negative balance.  Returns a boolean value which is true if the withdrawal can be and is made; false otherwise*/
}
void view()
{
	/*displays the heading “Savings Account” and then calls the base Account class’ 
	view function to display general customer / account information. This function has no parameters and no return value.*/
}
void Savings::adjustBalance(double b)
{
	b*=interestRate;
	/*updates the balance by applying the user-supplied monthly interest rate.  
		This function has no parameters and no return value*/
}
Last edited on
From the format of those link errors, it looks as though you're using MSVC. Are you sure your project is compiling and linking your Savings.cpp along with test.cpp ?
I found the issue and it was in .cpp. It was because my functions in Savings.cpp were just named bool makeWithdrawal(double w). I obviously needed to name them bool Savings::makeWithdrawal(double w)
I just want to know if this is the correct way to invoke a constructor with another?. It somehow compiled but I am not entirely sold if I am correct here.


This is a constructor inside Checking Header file. As the comment says it invokes the account base class constructor
1
2
3
4
5
6
Checking(Customer c, double b, bool oP)
		:Account (c,b), overdraftProtection(oP)
	{
		/*a three-argument constructor which invokes the Account base class two-argument constructor and then sets the overdraftProtection 
		to the user-supplied value (which the user supplies via the given test driver).*/
	}


1
2
3
4
5
6
7
8
9
Account(Customer custo, double b)
	{
			balance = b;
			cust = custo;
			/*a two-argument constructor which sets balance to the value of the first parameter, 
			a floating point value entered by the user in the test driver, and sets cust to the value of the second parameter, 
			the Customer variable c created and initialized in the test driver, using the overloaded assignment operator of the 
			Customer class*/
	}
Well, I finally got everything working and done but I only have one issue left. When it displays the persons information such as Name and Account# they come out blank. I think my problem consists in either three places.

1. when I try to overload the operator.
2. when I created the customer object in Account Header file. Might be wrong when I created it.
3.Either when I am invoking or calling Constructors inside Constructors.


This is how I made the object.
1
2
3
4
protected:
	double balance;
	Customer cust;
	//cust which is a Customer object. 


This is when I try overloading the operator
1
2
3
4
5
6
7
8
9
10
11
12
13
Customer& Customer::operator= (const Customer &right)
	{
    // check for self-assignment by comparing the address of the
    // implicit object and the parameter
    if (this == &right)
        return *this;
 
    // do the copy
    name = right.name;
	accountID = right.accountID;
    // return the existing object
    return *this;
	}



These are my two in customer.h constructors. Above this post is my Checking Constructor trying to call my Account Constructor. Which i'm not sure is correct.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Customer()
		{
			name = "";
			accountID = "";
			//which initializes each of the member variables to the null string (an empty string)
		}
	
	Customer(string sname, string accID) //which sets the name and accountID to the user supplied values.
	{
		name = sname;
		accountID = accID;


	}
Topic archived. No new replies allowed.
Pages: 12