-
February 9th, 2013, 10:49 AM
#1
Class/private help with this uml?
So I'm trying to do a homework assignment, where I read a uml about a bank program, and just create it. It's been about 2 years since I've done any c++. I used to be very quick and talented with it. But i have a horrible retention rate.
Here is the UML.
I'm completely oblivious with these UMLs, never learned much about them. But I am giving it my best shot. So while working on Bank, the top one, i've come up with this so far.
Code:
#include <iostream>
#include <string>
using namespace std;
class bank
{ string name;
int routingNum;
private:
int createaccount(string, int);
};
int bank::createaccount(string name, int routingNum)
{ cout <<"--------Registration--------\n";
cout << "Please enter your name\n";
cin >> name;
cout << "Please enter your Routing Number\n";
cin >> routingNum;
return (name,routingNum);
cin.get();
cin.get();
}
int main()
{ bank bankk;
bankk.createaccount(name,routingNum);
cout<<"Thank you"<<bank.name();
return 0; }
I'm assuming that's what the uml wants. However I can't seem to access that private class. Idk why. I declared it as an object in the main. Can anyone assist please? And if possible point out as many errors as possible, need tons of criticism to get back into my c++ mind.
-
February 9th, 2013, 11:34 AM
#2
Re: Class/private help with this uml?
It's not the class that's private - its the method. Members of a class are private by default which means they can only be accessed by other class members. If you need to access them outside of the class definition they need to be declared public. In your class just change private to public.
Also createaccount is defined to return an int - but you have
Code:
return (name,routingNum);
which is invalid. createaccount needs to update the class variables and then return some number so doesn't need any parameters.
Also main cannot access directly the private member name. You need a class method to return this data.
Code:
#include <iostream>
#include <string>
using namespace std;
class bank {
string name;
int routingNum;
public:
int createaccount();
string getName();
};
int bank::createaccount()
{
cout <<"--------Registration--------\n";
cout << "Please enter your name: ";
cin >> name;
cout << "Please enter your Routing Number: ";
cin >> routingNum;
return 0;
}
string bank::getName()
{
return name;
}
int main()
{
bank bankk;
bankk.createaccount();
cout << "Thank you " << bankk.getName();
return 0;
}
-
February 9th, 2013, 12:43 PM
#3
Re: Class/private help with this uml?
Originally Posted by 2kaud
It's not the class that's private - its the method. Members of a class are private by default which means they can only be accessed by other class members. If you need to access them outside of the class definition they need to be declared public. In your class just change private to public.
Also createaccount is defined to return an int - but you have
Code:
return (name,routingNum);
which is invalid. createaccount needs to update the class variables and then return some number so doesn't need any parameters.
Also main cannot access directly the private member name. You need a class method to return this data.
Code:
#include <iostream>
#include <string>
using namespace std;
class bank {
string name;
int routingNum;
public:
int createaccount();
string getName();
};
int bank::createaccount()
{
cout <<"--------Registration--------\n";
cout << "Please enter your name: ";
cin >> name;
cout << "Please enter your Routing Number: ";
cin >> routingNum;
return 0;
}
string bank::getName()
{
return name;
}
int main()
{
bank bankk;
bankk.createaccount();
cout << "Thank you " << bankk.getName();
return 0;
}
But doesn't that minus sign on the uml mean it has to be private?
-
February 9th, 2013, 03:19 PM
#4
Re: Class/private help with this uml?
I think in your UML diagram for Bank that createAccount is mis-represented and it should be public. It makes no sense to have createAccount private as that means you won't be able to create an account outside of the class and derived classes won't be able to use it either. If createAccount is meant to be used only by derived classes from bank then it could be made protected instead of public - but in terms of your program to test the class it still needs to be public. I would check with your tutor about this.
-
February 11th, 2013, 08:11 AM
#5
Re: Class/private help with this uml?
also:
if you get homework assignments it's a good idea to make sure you know what you're supposed to be doing.
since you don't know what UML is. why didn't you ask your tutor/teacher ?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|